Can someone help me with my project? I\'ve gotten everything, but for some reaso
ID: 3873825 • Letter: C
Question
Can someone help me with my project? I've gotten everything, but for some reason I keep getting invoke exceptions. Can someone analyze my code and explain the cause of this issue?
Thank you
package tictactoe;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class MainActivity extends Application {
public static final int SIDE = 3;
private int turn;
private int [][] game;
private TicTacToe tttgame;
Button[][] tile;
@Override
public void start(Stage primaryStage) throws Exception {
game = new int [SIDE][SIDE];
resetGame();
tile= new Button[2][2];
Button btPlay;
Pane pane = new Pane();
ButtonHandler bh = new ButtonHandler();
ButtonHandler2 bh2 = new ButtonHandler2();
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
tile[row][col] = new Button();
tile[row][col].setScaleX(200);
tile[row][col].setScaleY(200);
tile[row][col].setTranslateX(200*row);
tile[row][col].setTranslateY(200*col);
tile[row][col].setOnMouseClicked(bh);
pane.getChildren().add(tile[row][col]);
}
}
btPlay = new Button();
btPlay.setScaleX(500);
btPlay.setScaleY(150);
btPlay.setTranslateX(50);
btPlay.setTranslateY(650);
btPlay.setOnMouseClicked(bh2);
pane.getChildren().add(btPlay);
Scene scene = new Scene(pane, 600, 800);
primaryStage.setTitle("TicTacToe"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public void update(int row, int col) {
int play = play(row, col);
if (play == 1)tile[row][col].setText("X");
else if (play == 2)tile[row][col].setText("O");
if (isGameOver())disableButtons(true);
}
public void disableButtons(boolean enabled) {
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
tile[row][col].setDisable(enabled);
}
}
}
class ButtonHandler implements EventHandler<MouseEvent> {
public void handle(MouseEvent e) {
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
if (e.getSource()== tile[row][col])
update(row, col);
}
}
}
}
class ButtonHandler2 implements EventHandler<MouseEvent> {
public void handle(MouseEvent e) {
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++)
{
resetGame();
tile[row][col].setText(null);
}
}
}
}
public static void main(String[] args) {
launch(args);
}
public int play(int row, int col){
int currentTurn = turn;
if (row>=0 && col>=0 && row<SIDE && col<SIDE && game[row][col]==0){
game[row][col] = turn;
if (turn==1)turn=2;
else turn=1;
return currentTurn;
}
else
return 0;
}
public int whoWon(){
int rows = checkRows();
if (rows>0)return rows;
int cols = checkCols();
if (cols>0)return cols;
int dias = checkDias();
if(dias>0)return dias;
return 0;
}
protected int checkRows(){
for(int row=0;row<SIDE;row++)
if(game[row][0]!=0 && game[row][0]==game[row][1]&&game[row][1]==game[row][2])
return game[row][0];
return 0;
}
protected int checkCols(){
for(int col=0;col<SIDE;col++)
if(game[0][col]!=0 && game[0][col]==game[1][col]&&game[1][col]==game[2][col])
return game[0][col];
return 0;
}
protected int checkDias(){
for(int dia=0;dia<SIDE;dia++)
if(game[0][2]!=0 && game[0][2]==game[1][1]&&game[1][1]==game[2][0])
return game[2][0];
return 0;
}
public boolean canNotPlay(){
boolean result = true;
for (int x=0;x<SIDE;x++)
for(int y=0;y<SIDE;y++)
if(game[x][y] ==0) result = false;
return result;
}
public boolean isGameOver(){
return canNotPlay() || (whoWon()>0);
}
public void resetGame(){
for (int x=0;x<SIDE;x++)
for(int y=0;y<SIDE;y++)
game[x][y]=0;
turn =1;
}
}
final class TicTacToe {
}
Explanation / Answer
Here you got java.lang.ArrayIndexOutOfBoundsException in for loop in line 32.
Fix :
In Button[][] tile; Change "tile= new Button[2][2];" to tile= new Button[SIDE][SIDE];
Your exceptions will clear now.
complete Code:
package com.rama;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class MainActivity extends Application {
public static final int SIDE = 3;
private int turn;
private int [][] game;
private TicTacToe tttgame;
Button[][] tile;
@Override
public void start(Stage primaryStage) throws Exception {
game = new int [SIDE][SIDE];
resetGame();
tile= new Button[SIDE][SIDE];
Button btPlay;
Pane pane = new Pane();
ButtonHandler bh = new ButtonHandler();
ButtonHandler2 bh2 = new ButtonHandler2();
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
tile[row][col] = new Button();
tile[row][col].setScaleX(200);
tile[row][col].setScaleY(200);
tile[row][col].setTranslateX(200*row);
tile[row][col].setTranslateY(200*col);
tile[row][col].setOnMouseClicked(bh);
pane.getChildren().add(tile[row][col]);
}
}
btPlay = new Button();
btPlay.setScaleX(500);
btPlay.setScaleY(150);
btPlay.setTranslateX(50);
btPlay.setTranslateY(650);
btPlay.setOnMouseClicked(bh2);
pane.getChildren().add(btPlay);
Scene scene = new Scene(pane, 600, 800);
primaryStage.setTitle("TicTacToe"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public void update(int row, int col) {
int play = play(row, col);
if (play == 1)tile[row][col].setText("X");
else if (play == 2)tile[row][col].setText("O");
if (isGameOver())disableButtons(true);
}
public void disableButtons(boolean enabled) {
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
tile[row][col].setDisable(enabled);
}
}
}
class ButtonHandler implements EventHandler<MouseEvent> {
public void handle(MouseEvent e) {
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++) {
if (e.getSource()== tile[row][col])
update(row, col);
}
}
}
}
class ButtonHandler2 implements EventHandler<MouseEvent> {
public void handle(MouseEvent e) {
for (int row = 0; row < SIDE; row++) {
for (int col = 0; col < SIDE; col++)
{
resetGame();
tile[row][col].setText(null);
}
}
}
}
public static void main(String[] args) {
launch(args);
}
public int play(int row, int col){
int currentTurn = turn;
if (row>=0 && col>=0 && row<SIDE && col<SIDE && game[row][col]==0){
game[row][col] = turn;
if (turn==1)turn=2;
else turn=1;
return currentTurn;
}
else
return 0;
}
public int whoWon(){
int rows = checkRows();
if (rows>0)return rows;
int cols = checkCols();
if (cols>0)return cols;
int dias = checkDias();
if(dias>0)return dias;
return 0;
}
protected int checkRows(){
for(int row=0;row<SIDE;row++)
if(game[row][0]!=0 && game[row][0]==game[row][1]&&game[row][1]==game[row][2])
return game[row][0];
return 0;
}
protected int checkCols(){
for(int col=0;col<SIDE;col++)
if(game[0][col]!=0 && game[0][col]==game[1][col]&&game[1][col]==game[2][col])
return game[0][col];
return 0;
}
protected int checkDias(){
for(int dia=0;dia<SIDE;dia++)
if(game[0][2]!=0 && game[0][2]==game[1][1]&&game[1][1]==game[2][0])
return game[2][0];
return 0;
}
public boolean canNotPlay(){
boolean result = true;
for (int x=0;x<SIDE;x++)
for(int y=0;y<SIDE;y++)
if(game[x][y] ==0) result = false;
return result;
}
public boolean isGameOver(){
return canNotPlay() || (whoWon()>0);
}
public void resetGame(){
for (int x=0;x<SIDE;x++)
for(int y=0;y<SIDE;y++)
game[x][y]=0;
turn =1;
}
}
final class TicTacToe {
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.