Answered step by step
Verified Expert Solution
Question
1 Approved Answer
My image if a 3 x 3 tictactoe board is showing up , but when i press on a square, an o nor x is
My image if a x tictactoe board is showing up but when i press on a square, an o nor x is showing. But if i press the buttons as if im playing the game still, it eventually says that x win, o wins, or that its a tie. What is wrong with this code that its not show the images of my xs and os
package games.boards;
import java.awt.event.ActionEvent;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import games.boards.Cell;
import java.awt.event.ActionListener;
public class TicTacToeGame extends JFrame
private Board gb;
private int turn;
private intgameboard;
static final int EMPTY ;
static final int NOUGHT ;
static final int CROSS ;
public static void mainString args
SwingUtilities.invokeLaternew Runnable
public void run
new TicTacToeGame;
;
public TicTacToeGameint rows, int cols
gb new Boardrows cols, new ActionListener
public void actionPerformedActionEvent e
Cell c Cell egetSource;
takeTurnc;
;
gameboard new introwscols;
turn ;
this.addgb;
this.setDefaultCloseOperationEXITONCLOSE;
this.setTitleTICTACTOE";
this.setSize;
this.setVisibletrue;
public void setint val, int row, int col throws IllegalArgumentException
if gameboardrowcol EMPTY
gameboardrowcol val;
else
throw new IllegalArgumentException player already there";
public int getOutcome
for int i ; i ; i
if gameboardi gameboardi gameboardi NOUGHT
gameboardi gameboardi gameboardi NOUGHT
return NOUGHT;
else if gameboardi gameboardi gameboardi CROSS
gameboardi gameboardi gameboardi CROSS
return CROSS;
Check diagonals
if gameboard gameboard gameboard NOUGHT
gameboard gameboard gameboard NOUGHT
return NOUGHT;
else if gameboard gameboard gameboard CROSS
gameboard gameboard gameboard CROSS
return CROSS;
Check tie
boolean TIE true;
for introw : gameboard
for int cell : row
if cell EMPTY
TIE false;
break;
ifTIE
return ;
return ;
protected void takeTurnCell c
int curMark turn NOUGHT :
CROSS;
setcurMarkcgetRow cgetColumn;
int outcome getOutcome;
if outcome
JOptionPane.showMessageDialogthiss win";
else if outcome
JOptionPane.showMessageDialogthisXs win";
else if outcome
JOptionPane.showMessageDialogthis "TIE" ;
System.exit;
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