Answered step by step
Verified Expert Solution
Link Copied!

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 3x3 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 x's and o's?
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 int[][]gameboard;
static final int EMPTY =0;
static final int NOUGHT =-1;
static final int CROSS =1;
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable()
{
public void run(){
new TicTacToeGame(3,3);
}
});
}
public TicTacToeGame(int rows, int cols){
gb = new Board(rows, cols, new ActionListener(){
public void actionPerformed(ActionEvent e){
Cell c =(Cell) e.getSource();
takeTurn(c);
}
});
gameboard = new int[rows][cols];
turn =0;
this.add(gb);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("TIC-TAC-TOE");
this.setSize(300,300);
this.setVisible(true);
}
public void set(int val, int row, int col) throws IllegalArgumentException{
if (gameboard[row][col]== EMPTY)
gameboard[row][col]= val;
else
throw new IllegalArgumentException ("player already there");
}
public int getOutcome(){
for (int i =0; i <3; i++){
if ((gameboard[i][0]+ gameboard[i][1]+ gameboard[i][2]==3* NOUGHT)||
(gameboard[0][i]+ gameboard[1][i]+ gameboard[2][i]==3* NOUGHT)){
return NOUGHT;
} else if ((gameboard[i][0]+ gameboard[i][1]+ gameboard[i][2]==3* CROSS)||
(gameboard[0][i]+ gameboard[1][i]+ gameboard[2][i]==3* CROSS)){
return CROSS;
}
}
// Check diagonals
if ((gameboard[0][0]+ gameboard[1][1]+ gameboard[2][2]==3* NOUGHT)||
(gameboard[0][2]+ gameboard[1][1]+ gameboard[2][0]==3* NOUGHT)){
return NOUGHT;
} else if ((gameboard[0][0]+ gameboard[1][1]+ gameboard[2][2]==3* CROSS)||
(gameboard[0][2]+ gameboard[1][1]+ gameboard[2][0]==3* CROSS)){
return CROSS;
}
// Check tie
boolean TIE = true;
for (int[]row : gameboard){
for (int cell : row){
if (cell == EMPTY ){
TIE = false;
break;
}
}
}
if(TIE){
return 0;
}
return -2;
}
protected void takeTurn(Cell c){
int curMark =(turn++%2==0)? NOUGHT :
CROSS;
set(curMark,c.getRow(), c.getColumn());
int outcome = getOutcome();
if (outcome ==-1){
JOptionPane.showMessageDialog(this,"0's win");
}
else if (outcome ==1){
JOptionPane.showMessageDialog(this,"X's win");
}
else if (outcome ==0){
JOptionPane.showMessageDialog(this, "TIE" );
System.exit(0);
}
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Master The Art Of Data Storytelling With Visualizations

Authors: Alexander N Donovan

1st Edition

B0CNMD9QRD, 979-8867864248

More Books

Students also viewed these Databases questions