Ajp Microproject

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Bhartiya Vidyapeeth

Bhartiya Vidyapeeth
jawaharlal nehru
jawaharlal nehru of
of
technologyted
technology

BRANCH: COMPUTER TECHNOLOGY


AJP Microproject : Tic-toc-toe game
SUPERVISED BY: Mrs Sapate s.d
NAME: Vaibhav D. Hoke
ENROLLMENT NO:2101250116
Abstract:

The AJPmicroproject presents a simple


implementation of the classic Tic Tac Toe game
using Java and the Swing framework. Developed by
[Your Name], this project utilizes a graphical user
interface composed of a 3x3 grid of buttons.
Players take turns marking their moves as 'X' or 'O'
until a winner is determined or the game ends in a
draw.

Key Features:

Graphical User Interface: The game employs a


user-friendly interface created with Java's Swing
framework, providing an intuitive platform for
players to interact with.

Dynamic Player Turns: The application


dynamically switches between players,
displaying a label indicating the current player's
turn.
Winning and Draw Conditions: The game
checks for winning combinations after
each move, and if no winner is found, it
verifies if the game has ended in a draw.

Responsive UI Elements: Buttons


respond to user clicks, preventing players
from making invalid moves.

Game Reset Functionality: After a game


concludes, players have the option to
reset the board and start a new match.

This microproject serves as a demonstration


of basic Java programming, GUI
development, event handling, and game logic
implementation. It offers a foundation for
further enhancements and customization to
create more complex games or applications.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AJPmicroproject extends


JFrame implements ActionListener {
private JButton[][] buttons;
private JLabel label;
private char currentPlayer;

public AJPmicroproject() {
setTitle("Tic Tac Toe Game");
setSize(300, 300);

setDefaultCloseOperation(JFrame.EXIT_ON
_CLOSE);
setLayout(new GridLayout(3, 3));

buttons = new JButton[3][3];


currentPlayer = 'X';

label = new JLabel("Player " +


currentPlayer + "'s Turn");
add(label);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j] = new JButton("");
buttons[i][j].setFont(new Font("Arial", Font.PLAIN,
40));
buttons[i][j].addActionListener(this);
add(buttons[i][j]);
}
}
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


JButton clickedButton = (JButton) e.getSource();

if (!clickedButton.getText().equals("")) {
return;
}
clickedButton.setText(Character.toString(currentPlayer));
if (checkWinner()) {
JOptionPane.showMessageDialog(this, "Player " +
currentPlayer + " wins!");
resetGame();
} else if (checkDraw()) {
JOptionPane.showMessageDialog(this, "It's a draw!");
resetGame();
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
label.setText("Player " + currentPlayer + "'s Turn");
}
}
private boolean checkWinner() {
// Check rows
for (int i = 0; i < 3; i++) {
if (buttons[i]
[0].getText().equals(Character.toString(currentPlayer))
&& buttons[i]
[1].getText().equals(Character.toString(currentPlayer))
&& buttons[i]
[2].getText().equals(Character.toString(currentPlayer))) {
return true;
}
}

// Check columns
for (int i = 0; i < 3; i++) {
if (buttons[0]
[i].getText().equals(Character.toString(currentPlayer))
&& buttons[1]
[i].getText().equals(Character.toString(currentPlayer))
&& buttons[2]
[i].getText().equals(Character.toString(currentPlayer))) {
return true;
}
}

// Check diagonals
if (buttons[0]
[0].getText().equals(Character.toString(currentPlayer))
&& buttons[1]
[1].getText().equals(Character.toString(currentPlayer))
&& buttons[2]
[2].getText().equals(Character.toString(currentPlayer))) {
return true;
}

if (buttons[0]
[2].getText().equals(Character.toString(currentPlayer))
&& buttons[1]
[1].getText().equals(Character.toString(currentPlayer))
&& buttons[2]
[0].getText().equals(Character.toString(currentPlayer))) {
return true;
}

return false;
}

private boolean checkDraw() {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (buttons[i][j].getText().equals("")) {
return false;
}
}
}
return true;
}

private void resetGame() {


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setText("");
}
}
currentPlayer = 'X';
label.setText("Player " + currentPlayer + "'s Turn");
}

public static void main(String[] args) {


new AJPmicroproject();
}
}
Output:
Condition 1: Player x wins

Condition 2: Player O wins


Condition 3: Draw
Thank
You

You might also like