Question: The code when ran does not catch whether x , 0 , or of its a tie. How to fix it ? package games.boards; import

The code when ran does not catch whether x,0, or of its a tie. How to fix it?
package games.boards;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import java.awt.event.ActionListener;
public class TicTacToeGame extends JFrame {
private Board gb;
private int turn;
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable()
{
public void run(){
new TicTacToeGame();
}
});}
static int[][]gameboard;
static final int EMPTY =0;
static final int NOUGHT =-1;
static final int CROSS =1;
/* Set a square on the board must be empty */
public static 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 -1;
} 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 1;
}
}
// 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 -1;
} 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 1;
}
// Check tie
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){
if (gameboard[i][j]== EMPTY){
return -2;
}
}
}
return 0;
}
public void takeTurn(Cell c){
Mark curMark =(turn++%2==0)?
Mark.NOUGHT: Mark.CROSS;
gb.setCell(curMark,c.getRow(),c.getColumn());
int outcome = getOutcome();
JOptionPane.showMessageDialog(this,"");
System.exit(0); }
private TicTacToeGame(){
gb = new Board(3,3, new ActionListener(){
public void actionPerformed(ActionEvent al){
Cell c =(Cell) al.getSource();
takeTurn(c);
}
});
this.add(gb);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setTitle("TIC-TAC-TOE");
this.setSize(300,300);
this.setVisible(true);
}
}

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 Databases Questions!