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 tictactoe 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
setTitleTic Tac Toe"; sets title of the game
setSize;
getContentPanesetBackgroundnew Color;
setDefaultCloseOperationJFrameEXITONCLOSE;
mainFrame new JPanelnew BorderLayout;
gameBoard new JPanelnew GridLayout; sets the icon lay out to be rows of
gameBoard.setPreferredSizenew Dimension;
addmainFrame;
mainFrame.addtextField BorderLayout.NORTH;
mainFrame.addgameBoard BorderLayout.CENTER;
resetButton new JButtonReset;
mainFrame.addresetButton BorderLayout.SOUTH;
resetButton.addActionListenerthis;
buttons new JButton;
determineFirstPlayer;
iconX new ImageIconstarfishpng;
iconO new ImageIconfishpng;
makeButtons; calls a seperate method that makes all the buttons
setVisibletrue;
public void determineFirstPlayer
if Mathrandom
currentPlayer X;
else
currentPlayer O;
textField.setTextPlayer currentPlayer plays.";
private void makeButtons
nested for loops to make all buttons
for int i ; i ; i
for int j ; j ; j
buttonsij new JButton;
gameBoard.addbuttonsij;
buttonsijaddActionListenerthis;
private boolean checkWinner
checking rows
for int i ; i ; i
if
buttonsigetIcon null && buttonsigetIconequalsbuttonsigetIcon &&
buttonsigetIconequalsbuttonsigetIcon
return true;
columns
for int i ; i ; i
if
buttonsi null && buttonsigetIconequalsbuttonsigetIcon &&
buttonsigetIconequalsbuttonsigetIcon
return true;
checking diagonals
if
buttonsgetIcon null && buttonsgetIconequalsbuttonsgetIcon &&
buttonsgetIconequalsbuttonsgetIcon
return true;
else if
buttonsgetIcon null && buttonsgetIconequalsbuttonsgetIcon &&
buttonsgetIconequalsbuttonsgetIcon
return true;
return false;
private boolean isBoardFull
if the board gets full and theres no winner
for int i ; i ; i
for int j ; j ; j
if buttonsijgetIcon 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 ; i ; i
for int j ; j ; j
buttonsijsetIconnull;
@Override
public void actionPerformedActionEvent event
JButton clickedButton JButton event.getSource;
if clickedButton resetButton
resetGame;
else
if clickedButtongetIcon null
sets icon for the current player
if currentPlayerequalsX
clickedButton.setIconiconX;
currentPlayer O; switches to next player
else
clickedButton.setIconiconO;
currentPlayer X; switched to next player
textField.setTextPlayer currentPlayer plays.";
checking if there is a winner or draw
if checkWinner
String winner;
if currentPlayerequalsX
winner O;
else
winner X;
JOptionPane.showMessageDialogthis winner wins!";
resetGame
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
