Question: I am trying to use Java GUI swing to make a tic - tac - toe game. It looks like there is something wrong with

I am trying to use Java GUI swing to make a tic-tac-toe game. It looks like there is something wrong with my checkWinner() method. It checks the rows just fine, but besides the first column, other column wins and diagonal wins aren't being detected and I can't figure out why. I've attached my entire code for context and would appreciate any help to solve my issue.
public toe(){
setTitle("Tic Tac Toe"); //sets title of the game
setSize(500,500);
getContentPane().setBackground(new Color(50,50,50));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame = new JPanel(new BorderLayout());
gameBoard = new JPanel(new GridLayout(3,3)); //sets the icon lay out to be 3 rows of 3
gameBoard.setPreferredSize(new Dimension(500,500));
add(mainFrame);
mainFrame.add(textField, BorderLayout.NORTH);
mainFrame.add(gameBoard, BorderLayout.CENTER);
resetButton = new JButton("Reset");
mainFrame.add(resetButton, BorderLayout.SOUTH);
resetButton.addActionListener(this);
buttons = new JButton[3][3];
determineFirstPlayer();
iconX = new ImageIcon("starfish.png");
iconO = new ImageIcon("fish.png");
makeButtons(); //calls a seperate method that makes all the buttons
setVisible(true);
}
public void determineFirstPlayer(){
if (Math.random()<0.5){
currentPlayer ="X";
} else {
currentPlayer ="O";
}
textField.setText("Player "+ currentPlayer +" plays.");
}
private void makeButtons(){
//nested for loops to make all 9 buttons
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
buttons[i][j]= new JButton();
gameBoard.add(buttons[i][j]);
buttons[i][j].addActionListener(this);
}
}
}
private boolean checkWinner(){
//checking rows
for (int i =0; i <3; i++){
if (
(buttons[i][0].getIcon()!= null) && (buttons[i][0].getIcon().equals(buttons[i][1].getIcon())) &&
(buttons[i][0].getIcon().equals(buttons[i][2].getIcon()))){
return true;
}
}
//columns
for (int i =0; i <3; i++){
if (
(buttons[0][i]!= null) && (buttons[0][i].getIcon().equals(buttons[1][i].getIcon())) &&
(buttons[0][i].getIcon().equals(buttons[2][i].getIcon()))){
return true;
}
}
//checking diagonals
if (
(buttons[0][0].getIcon()!= null) && (buttons[0][0].getIcon().equals(buttons[1][1].getIcon())) &&
(buttons[0][0].getIcon().equals(buttons[2][2].getIcon()))){
return true;
}
else if (
(buttons[0][2].getIcon()!= null) && (buttons[0][2].getIcon().equals(buttons[1][1].getIcon())) &&
(buttons[0][2].getIcon().equals(buttons[2][0].getIcon()))){
return true;
}
return false;
}
private boolean isBoardFull(){
//if the board gets full and theres no winner
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
if (buttons[i][j].getIcon()== null){
return false;
}
}
}
//if all buttons are full, then the board is full and the return is true.
return true;
}
private void resetGame(){
determineFirstPlayer();
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
buttons[i][j].setIcon(null);
}
}
}
@Override
public void actionPerformed(ActionEvent event){
JButton clickedButton =(JButton) event.getSource();
if (clickedButton == resetButton){
resetGame();
} else {
if (clickedButton.getIcon()== null){
//sets icon for the current player
if (currentPlayer.equals("X")){
clickedButton.setIcon(iconX);
currentPlayer ="O"; //switches to next player
} else {
clickedButton.setIcon(iconO);
currentPlayer ="X"; //switched to next player
}
textField.setText("Player "+ currentPlayer +" plays.");
//checking if there is a winner or draw
if (checkWinner()){
String winner;
if (currentPlayer.equals("X")){
winner ="O";
} else {
winner ="X";
}
JOptionPane.showMessageDialog(this, winner +" wins!");
resetGame()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!