0

I made a program that has to simulate a TicTacToe game. I get an error I cannot understand. If you know how to solve this please let me know. So this is the error:

java.lang.VerifyError: Constructor must call super() or this() before return Exception Details: Location: tictactoe/TicTacToe.()V @0: return Reason: Error exists in the bytecode Bytecode: 0000000: b1

at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
at java.lang.Class.getMethod0(Class.java:2764)
at java.lang.Class.getMethod(Class.java:1653)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

Exception in thread "main" Java Result: 1 BUILD SUCCESSFUL (total time: 2 seconds)

And this is the program(it has 2 classes):

package TicTacToeResit;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Alin
*/
public class TicTacToeResit {
 public static void main(String[] args){        
    Board tictactoegame=new Board();
    Board tictactoegame1=new Board(5);
    Board tictactoegame3=new Board('O');
    tictactoegame.Game();
    tictactoegame.restartGame(tictactoegame);


  }

  }

and

package TicTacToeResit;

import java.util.Scanner;

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
*
* @author Alin
*/
public class Board {
public char[][] board= new char[6][6]; 
public char player1;
public char player2;
public char ply;
public int size, move;
public Board(){
     for (int i=0; i<4; i++)
         for (int j=0; j<4; j++)
             board[i][j]='-';
}
public Board(int boardSize){
    for (int i=0; i<boardSize; i++)
         for (int j=0; j<boardSize; j++)
             board[i][j]='-';
}
public Board(char player){
    if (player=='X'){
        this.player1='X';
        this.player2='O';
    }
    else{   
        this.player1='O';
        this.player2='X';
    }
}
public void Game(){
    System.out.println("Choose game mode:");
    System.out.println("For 3x3 press 3");
    System.out.println("For 4x4 press 4");
    System.out.println("For 5x5 press 5");
    System.out.println("For 6x6 press 6");
    System.out.println("For 7x7 press 7");
    System.out.println("Any input different from the stated ones will be considered as 4");
    Scanner keyboard = new Scanner(System.in); 
    String mode = keyboard.next();
    switch(mode){
        case "3":
            initializeTheBoard(3);
            size=Integer.parseInt(mode);
            break;
        default:
            initializeTheBoard(4);
            size=4;
            break;
        case "5":
            initializeTheBoard(5);
            size=Integer.parseInt(mode);
            break;
        case "6":
            initializeTheBoard(6);
            size=Integer.parseInt(mode);
            break;
        case "7":
            initializeTheBoard(7);
            size=Integer.parseInt(mode);
            break;
     }
    whoStarts();
}
public void addAMove(int i, int j){
    int filled=0;
    while(filled<1){
        if (board[i][j]=='-'){
            board[i][j]=ply;
            filled=1;
        }
        else{
            System.out.println("Space taken already. Choose another");
            Scanner keyboard = new Scanner(System.in);
            i=keyboard.nextInt()-1;
            j=keyboard.nextInt()-1;
        }
    }
    move++;
}
public void initializeTheBoard(int size){
    for (int i=0;i<size;i++){
        for (int j=0;j<size;j++){
            board[i][j]='-';
        }
    }            
}
public void whoseTurn(){
    if (ply=='X')
        System.out.println("Player O's turn");
    else
        System.out.println("Player X's turn");        
}
public void changeTurn(){
    if (ply==player1)
        ply=player2;
    else
        ply=player1;
}
public void whoStarts(){
    System.out.println("Choose player1");
    System.out.println("X or O");
    Scanner keyboard = new Scanner(System.in); 
    String answer = keyboard.next();
    while(("X".equals(answer))||("O".equals(answer))){
        if ("X".equals(answer)){
            player1='X';
            player2='O';}
        if ("O".equals(answer)){
            player1='O';
            player2='X';   
        }
    }
    if ((!"X".equals(answer))&&(!"O".equals(answer))){
        System.out.println("Please answer with X or O");
        answer = keyboard.next();
    }
    move=1;
 }
/**
 * ToDo
 */    
public void writeBoard(int size){     
    writeBoardHeader();
    for(int i=0; i<size; i++)
    {
        writeBoardRow(i);
        writeRowBoundary();
    }
}

/**
 * ToDo
 */    
public void writeBoardRow(int i)
{
    System.out.print("| " + (i + 1) + " | ");
    for(int j = 0; j < size ; j++)
    {
        System.out.print( board[i][j] + " | ");
    }
    System.out.println();
}

/**
 * ToDo
 */
public void writeRowBoundary()
{
    System.out.print("-----");
    for(int j = 0; j < size ; j++)
    {
            System.out.print("----" );
    }
    System.out.println();
}


/**
 * ToDo
 */
public void writeBoardHeader()
{
    writeRowBoundary();

    System.out.print("|R\\C|"); 
    for (int j = 1; j <= size; j++)
    {
        System.out.print(" " + j + " |");
    }    

    System.out.println(); 

    writeRowBoundary();
}
public int checkWinner(){
    int checkPlayer1=0;
    int checkPlayer2=0;
    for (int i=0; i<size; i++){
        if (board[i][i]=='X')
            checkPlayer1=1;
        if (board[i][i]=='O')
            checkPlayer2=1;
        if (board[i][i]!='X')
            checkPlayer1=0;
        if (board[i][i]!='O')
            checkPlayer2=0;
    }
    for (int i=0;i<size;i++){
        if (board[i][size-i]=='X')
            checkPlayer1=1;
        if (board[i][size-i]=='O')
            checkPlayer2=1;
        if (board[i][size-i]!='X')
            checkPlayer1=0;
        if (board[i][size-i]!='O')
            checkPlayer2=0;
    }
    for (int i=0;i<size;i++){
        for (int j=0;j<size;j++){
            if (board[i][j]=='X')
            checkPlayer1=1;
            if (board[i][j]=='O')
            checkPlayer2=1;
            if (board[i][j]!='X')
            checkPlayer1=0;
            if (board[i][j]!='O')
            checkPlayer2=0;
        }
    }
    for (int j=0;j<size;j++){
        for (int i=0;i<size;i++){
            if (board[i][j]=='X')
            checkPlayer1=1;
            if (board[i][j]=='O')
            checkPlayer2=1;
            if (board[i][j]!='X')
            checkPlayer1=0;
            if (board[i][j]!='O')
            checkPlayer2=0;
        }
    }
    if (checkPlayer1==1)
        return 1;
    if (checkPlayer2==1)
        return 2;
    return 0;
}
public void winner(){
    if (checkWinner()==1)
        System.out.println("Player1 wins");
    if (checkWinner()==2)
        System.out.println("Player2 wins");
}

public void restartGame(Board board){
    while(board.checkWinner()==0){
        board.whoseTurn();
        board.changeTurn();
        Scanner keyboard = new Scanner(System.in);
        board.addAMove(keyboard.nextInt()-1,keyboard.nextInt()-1);
        board.writeBoard(size);
    }
    board.winner();
}
} 

Please help! I need to finish this before 8.30 EU West hour.

4

1 回答 1

0
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)

似乎问题是针对您mainTicTacToeResit. tictactoegame.Game()在初始化棋盘之前尝试初始化游戏 ( )。

希望这可以帮助!

于 2013-11-12T05:36:24.423 回答