Implement Tic Tac Toe game in Java. I use Eclipse. Please make sure the program
ID: 3730140 • Letter: I
Question
Implement Tic Tac Toe game in Java. I use Eclipse. Please make sure the program works. Show screen shots.
The program requirements are as follows:
Initial State - indicates how the game is set up at start
Players - defines which player has a move in the state
Actions - returns the set of legal moves in a state
Results - determines the outcome of a move
Utility - gives the payoff with numeric values for each player
Reset Button - to be ablet to start new game
Exit Button - to quit the game
There should be a set limit when the player wins the game when certain number is reached.
It is not required to develop a GUI in the program. It is sufficient to show the execution of the game with respect to the program requirements above.
Indicate with comments which part of program does what.
The Game should be similar to the below picture:
0 0 Exit ResetExplanation / Answer
Following is the java Program:
Tic_Tac_Toe_Main.java
public class Tic_Tac_Toe_Main {
public static void main(String[] args) {
// Main CLass
Control C = new Control();
}
}
Game_Control.java
import java.awt.*;
import javax.swing.*;
public class Game_Control extends JFrame {
private Board GameBoard; //Board and Button
private Tools TButtons; // Exit and Reset
Game_Control() {
setLayout(new BorderLayout());
GameBoard = new Board();
TButtons = new Tools();
TButtons.SetObject(GameBoard);
add(GameBoard, BorderLayout.CENTER);
add(TButtons, BorderLayout.SOUTH);
setVisible(true);
setSize(350, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Game_Board.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Board extends JPanel implements ActionListener {
private JButton B1, B2, B3, B4, B5, B6, B7, B8, B9; // Buttons
private GameArray GArray; // Class with Array
private boolean Player = false;
private int PlayerMark = 1;
Board() {
// creates the panel
setLayout(new GridLayout(3, 3));
B1 = new JButton("");
B2 = new JButton("");
B3 = new JButton("");
B4 = new JButton("");
B5 = new JButton("");
B6 = new JButton("");
B7 = new JButton("");
B8 = new JButton("");
B9 = new JButton("");
SetGame();
add(B1);
add(B2);
add(B3);
add(B4);
add(B5);
add(B6);
add(B7);
add(B8);
add(B9);
B1.addActionListener(this);
B2.addActionListener(this);
B3.addActionListener(this);
B4.addActionListener(this);
B5.addActionListener(this);
B6.addActionListener(this);
B7.addActionListener(this);
B8.addActionListener(this);
B9.addActionListener(this);
}
public void SetGame() {
GArray = new GameArray(this);
DefaultText();
DisableAll(true);
Player = false; // default Value
PlayerMark = 1; // default Value
}
public void Reset() {
SetGame(); // To Reset the Game
}
public void actionPerformed(ActionEvent E) {
JButton Pressed = (JButton) E.getSource();
/*
if any button is pressed the value is sent to GameArray class
*/
if (Pressed == B1) {
GArray.ArrayInitialize(0, 0, PlayerMark);
SetText(Pressed, Player); // chaneg button text to "X" or "O" based on player turn
PlayerMark = SwithTurn(Player); // Swithch Turns
ButtonDisabler(B1); // Disable pressed Button
} else if (Pressed == B2) {
GArray.ArrayInitialize(0, 1, PlayerMark);
SetText(Pressed, Player);
SetText(Pressed, Player);
PlayerMark = SwithTurn(Player);
ButtonDisabler(B2);
} else if (Pressed == B3) {
GArray.ArrayInitialize(0, 2, PlayerMark);
SetText(Pressed, Player);
PlayerMark = SwithTurn(Player);
ButtonDisabler(B3);
} else if (Pressed == B4) {
GArray.ArrayInitialize(1, 0, PlayerMark);
SetText(Pressed, Player);
PlayerMark = SwithTurn(Player);
ButtonDisabler(B4);
} else if (Pressed == B5) {
GArray.ArrayInitialize(1, 1, PlayerMark);
SetText(Pressed, Player);
PlayerMark = SwithTurn(Player);
ButtonDisabler(B5);
} else if (Pressed == B6) {
GArray.ArrayInitialize(1, 2, PlayerMark);
SetText(Pressed, Player);
PlayerMark = SwithTurn(Player);
ButtonDisabler(B6);
} else if (Pressed == B7) {
GArray.ArrayInitialize(2, 0, PlayerMark);
SetText(Pressed, Player);
PlayerMark = SwithTurn(Player);
ButtonDisabler(B7);
} else if (Pressed == B8) {
GArray.ArrayInitialize(2, 1, PlayerMark);
SetText(Pressed, Player);
PlayerMark = SwithTurn(Player);
ButtonDisabler(B8);
} else if (Pressed == B9) {
GArray.ArrayInitialize(2, 2, PlayerMark);
SetText(Pressed, Player);
PlayerMark = SwithTurn(Player);
ButtonDisabler(B9);
}
}
public int SwithTurn(boolean last) {
// if the past player was false(player 1) then swith to true(player 2)
System.out.println();
if (last == true) {
Player = false;
return 1;
} else if (last == false) {
Player = true;
return 2;
} else {
return 3;
}
}
public void ButtonDisabler(JButton Btn) {
Btn.setEnabled(false); // Disable Button
}
public void DisableAll(boolean Opp) {
// Disable All Buttons
B1.setEnabled(Opp);
B2.setEnabled(Opp);
B3.setEnabled(Opp);
B4.setEnabled(Opp);
B5.setEnabled(Opp);
B6.setEnabled(Opp);
B7.setEnabled(Opp);
B8.setEnabled(Opp);
B9.setEnabled(Opp);
}
public void SetText(JButton Btn, boolean Play) {
if (Play == true) {
Btn.setText("O");
} else if (Play == false) {
Btn.setText("X");
}
}
public void DefaultText(){
B1.setText("");
B2.setText("");
B3.setText("");
B4.setText("");
B5.setText("");
B6.setText("");
B7.setText("");
B8.setText("");
B9.setText("");
}
}
GameArray.java
import javax.swing.*;
public class GameArray {
/*
Class for Array
*/
private Board Brd;
private int GameArr[][];
private boolean Turn;
private JButton Pressed;
GameArray(Board B) {
GameArr = new int[3][3];
Brd = B;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
GameArr[i][j] = 0;
}
}
}
public void ArrayInitialize(int i, int j, int Marker) {
//Set Data sent by Action listener in Board
GameArr[i][j] = Marker;
WinCheck(Marker);
}
public void WinCheck(int Marker) {
// if the specified array indexs contain a certain Maker (1,2) on winning pattrens then announce winner
if ((GameArr[0][0] == Marker && GameArr[0][1] == Marker && GameArr[0][2] == Marker) || (GameArr[1][0] == Marker && GameArr[1][1] == Marker && GameArr[1][2] == Marker) || (GameArr[2][0] == Marker && GameArr[2][1] == Marker && GameArr[2][2] == Marker)) {
if (Marker == 1) {
JOptionPane.showMessageDialog(Brd, "CONGRATULATIONS : Player 1 (Winner)");
} else if (Marker == 2) {
JOptionPane.showMessageDialog(Brd, "CONGRATULATIONS : Player 2 (Winner)");
}
Brd.DisableAll(false);
} else if ((GameArr[0][0] == Marker && GameArr[1][0] == Marker && GameArr[2][0] == Marker) || (GameArr[0][1] == Marker && GameArr[1][1] == Marker && GameArr[2][1] == Marker) || (GameArr[0][2] == Marker && GameArr[1][2] == Marker && GameArr[2][2] == Marker)) {
if (Marker == 1) {
JOptionPane.showMessageDialog(Brd, "CONGRATULATIONS : Player 1 (Winner)");
} else if (Marker == 2) {
JOptionPane.showMessageDialog(Brd, "CONGRATULATIONS : Player 2 (Winner)");
}
Brd.DisableAll(false);
} else if ((GameArr[0][0] == Marker && GameArr[1][1] == Marker && GameArr[2][2] == Marker) || (GameArr[2][0] == Marker && GameArr[1][1] == Marker && GameArr[0][2] == Marker)) {
if (Marker == 1) {
JOptionPane.showMessageDialog(Brd, "CONGRATULATIONS : Player 1 (Winner)");
} else if (Marker == 2) {
JOptionPane.showMessageDialog(Brd, "CONGRATULATIONS : Player 2 (Winner)");
}
Brd.DisableAll(false);
}
}
}
Tools.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Tools extends JPanel {
private JButton Exit, Reset;
private Board Brd;
Tools() {
setLayout(new FlowLayout());
Exit = new JButton("Exit");
Reset = new JButton("Reset");
Exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
Reset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Brd.Reset();
}
});
add(Exit);
add(Reset);
}
public void SetObject(Board B) {
Brd = B;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.