Question
I'm creating a Tic Tac Toe program with Java using Swing. The user is 'X' and the computer is 'O'. The program works if the
I'm creating a Tic Tac Toe program with Java using Swing. The user is 'X' and the computer is 'O'. The program works if the user won or lost. However, if doesn't work if there is a draw. The last remaining cell/button can't be clicked. Any helps would be great. First time using Swing. Here is my code:
import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JOptionPane;
public class TicTacToeButtonListener implements ActionListener {
private TicTacToeBoard board; /*playing board variable*/ private int posX; /* x position variable */ private int posY; /* y posigion variable */ private JButton[][] btns; /* 2-dim array holding buttons */
public boolean gameOver = false; public boolean compTurn = false; public int boardFilled = 0; public Component frame;
public TicTacToeButtonListener(TicTacToeBoard board, int posX, int posY, JButton[][] btns) {
this.board = board; this.posX = posX; this.posY = posY; this.btns = btns;
}
@Override public void actionPerformed(ActionEvent e) {
Random rand = new Random();
if (boardFilled == 0) { board.placeMarker(posX, posY, TicTacToeBoard.PLAYER); board.updateBoard(btns); compTurn = true; ++boardFilled; }
if (compTurn) {
int x = 0; int y = 0;
while (true) { x = rand.nextInt(3); y = rand.nextInt(3);
if (btns[x][y].getText() == " " || btns[x][y].getText() == " ") {
break; }
}
board.placeMarker(x, y, TicTacToeBoard.COMPUTER); board.updateBoard(btns); compTurn = false; ++boardFilled;
gameOver = checkIfWon("O");
if (gameOver) {
JOptionPane.showMessageDialog(frame, "You lost, sorry!!!"); System.exit(0); }
}
if (!compTurn) {
board.placeMarker(posX, posY, TicTacToeBoard.PLAYER); board.updateBoard(btns); compTurn = true; ++boardFilled; gameOver = checkIfWon("X"); if (gameOver) { System.out.println("You won!");
JOptionPane.showMessageDialog(frame, "You won!!!"); System.exit(0);
} }
if (boardFilled == 9) {
JOptionPane.showMessageDialog(frame, "It's a draw!!!"); gameOver = true;
}
}
public boolean checkIfWon(String marker) {
/* checks if won horizontally */ if (checkHorizontal(marker)) { return true; }
/* checks if won vertically */ if (checkVertically(marker)) { return true; }
/* checks if won diagonally */ if (checkDiagonally(marker)) { return true; }
return false; }
public boolean checkDiagonally(String marker) {
int pos1 = 0; int count1 = 0;
/* for loop to check diagonally left to right */ for (int i = 0; i <= 2; i++) { if (btns[i][pos1].getText() == marker) { count1++; } pos1++; }
if (count1 == 3) { return true; }
int pos2 = 2; int count2 = 0;
/* For loop to check diagonally right to left */ for (int i = 0; i <= 2; i++) { if (btns[i][pos2].getText() == marker) { count2++; } pos2--; }
if (count2 == 3) { return true; }
/* Returns false if user didn't win */ return false; }
public boolean checkVertically(String marker) {
/* For loop to check the marker vertially */ for (int i = 0; i <= 2; i++) {
int count = 0; for (int j = 0; j <= 2; j++) { if (btns[j][i].getText() == marker) { count++; }
if (count == 3 && j == 2) { return true; }
} }
/* Returns false if user didn't win */ return false; }
public boolean checkHorizontal(String marker) {
int count = 0;
/* For loop to check marker horizontally */ for (int i = 0; i <= 2; i++) { count = 0; for (int j = 0; j <= 2; j++) { if (btns[i][j].getText() == marker) { count++; } if (count == 3 && j == 2) { return true; }
} }
/* Returns false if user didn't win */ return false; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started