Aarya Sood - Tictactoe Code
Aarya Sood - Tictactoe Code
● Win method works correctly. Display a pop up window when a player wins, and the
window should say which player won.
● If the game ends in a tie, display a pop up window saying it’s a tie.
● Counting the number of wins (and ties)
● Option to display the number of wins and ties
● Resetting the board when appropriate
● Some changes to the look of the game (colors, pictures, etc.)
Optional:
● Computer player
● Make the computer player semi intelligent
TicTacToe2:
JFrame f = new JFrame("Tic Tac Toe"); //create a JFrame and title it
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //terminates the
program when the frame is closed
JPanel p = new TicTacToePanel(); //create a new panel using the
TicTacToePanel class
f.getContentPane().add(p); //add the panel to the frame
JMenu fileMenu = new JMenu("File"); //create a menu for the top of the
frame
JMenuItem newGame = new JMenuItem("New Game");
newGame.addActionListener(new ActionListener(){ //add a listener to the
menu item
public void actionPerformed(ActionEvent e){
TicTacToePanel.resetBoard();
}
});
fileMenu.add(newGame);
JMenuBar bar = new JMenuBar();
bar.add(fileMenu);
f.setJMenuBar(bar);
//f.pack();
f.setSize(600,600);
f.setVisible(true);
}
TicTacToePanel:
if(!board[0][0].getText().equals("") &&
board[0][0].getText().equals(board[0][1].getText()) &&
board[0][2].getText().equals(board[0][0].getText())) {
win++;
return true;
}
else if(board[1][0].getText().equals(board[1][1].getText()) &&
board[1][2].getText().equals(board[1][0].getText())) {
win++;
}
else if(board[2][0].getText().equals(board[2][1].getText()) &&
board[2][0].getText().equals(board[2][2].getText())) {
win++;
}
else if(board[0][0].getText().equals(board[1][0].getText()) &&
board[0][0].getText().equals(board[2][0].getText())) {
win++;
}
else if(board[0][1].getText().equals(board[1][1].getText()) &&
board[0][1].getText().equals(board[2][1].getText())) {
win++;
}
else if(board[0][2].getText().equals(board[1][2].getText()) &&
board[0][2].getText().equals(board[2][2].getText())) {
win++;
}
else if(board[0][0].getText().equals(board[1][1].getText()) &&
board[0][0].getText().equals(board[2][2].getText())) {
win++;
}
else if(board[0][2].getText().equals(board[1][1].getText()) &&
board[0][2].getText().equals(board[2][0].getText())) {
win++;
}
if(board[0][0].getText().equals(board[0][1].getText()) &&
board[0][2].getText().equals(board[0][0].getText())) {
if(board[0][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[1][0].getText().equals(board[1][1].getText()) &&
board[1][2].getText().equals(board[1][0].getText())) {
if(board[1][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[1][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[2][0].getText().equals(board[2][1].getText()) &&
board[2][0].getText().equals(board[2][2].getText())) {
if(board[2][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[2][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][0].getText().equals(board[1][0].getText()) &&
board[0][0].getText().equals(board[2][0].getText())) {
if(board[0][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][1].getText().equals(board[1][1].getText()) &&
board[0][1].getText().equals(board[2][1].getText())) {
if(board[0][1].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][1].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][2].getText().equals(board[1][2].getText()) &&
board[0][2].getText().equals(board[2][2].getText())) {
if(board[0][2].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][2].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][0].getText().equals(board[1][1].getText()) &&
board[0][0].getText().equals(board[2][2].getText())) {
if(board[0][0].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][0].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
if(board[0][2].getText().equals(board[1][1].getText()) &&
board[0][2].getText().equals(board[2][0].getText())) {
if(board[0][2].getText().equals("x")) {
winx++;
loseo++;
return true;
}
if(board[0][2].getText().equals("o")) {
wino++;
losex++;
return true;
}
}
return false;
//will execute when a button is clicked, as long as the listener was added to the
button
public class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
JButton current = (JButton)e.getSource(); //get the source of the
event and cast it to a JButton
int[][] matrix;
if (current.getText().equals("")){ //add an X or an O if the button is
blank
if(xTurn){
current.setText("x");
turnCount++;
xTurn = false;
}
else{
current.setText("o");
turnCount++;
xTurn = true;
}
if(win()) {
if(xTurn) { //if xTurn is true, then O just played
JOptionPane.showMessageDialog(null, "o
wins!!"); //display a pop up window when a player wins
JOptionPane.showMessageDialog(null, "Times
o has won: " + numofo++);
}
else
JOptionPane.showMessageDialog(null, "x
wins!!"); //display a pop up window when a player wins
JOptionPane.showMessageDialog(null, "Times x has
won: " + numofx++);
}
else if(turnCount == 9) {
JOptionPane.showMessageDialog(null, "It's a tie!!");
turnCount++;
JOptionPane.showMessageDialog(null, "Number of
ties: " + numofties++);
}
}
}