2名球员Tic-Tac-Toe比赛

时间:2013-11-16 22:53:44

标签: java

我正在尝试制作一个双人游戏,但是我无法得到它,所以当它的下一个玩家转身时它会打印出一个O 然后当它是下一个播放器时,它将打印出一个X.

import java.util.Scanner;

public class TicTacToeMain{
    public static void main(String[]args){
        Scanner scan = new Scanner(System.in);
        String[][] Board = new String[][] {
            {" ", " ", " "}, {" ", " ", " "," "}, 
            {" ", " ", " "," "},{" "," "," "," "},{" "," "," "}};
        grid1(Board,scan);
        System.out.println("Where do you wan't your x to be?");
        move1(Board,scan);
    }
    public static void grid1(String[][] Board, Scanner scan){
        System.out.println("A " + Board[0][1] +Board[0][0]+ " | " + Board[3][1] + " | "+                Board[0][2]);
        System.out.println(" -------------");
        System.out.println("B " + Board[1][1] +Board[1][0]+ " | " + Board[3][2] + " | "+   Board[1][2]);
        System.out.println(" -------------");
        System.out.println("C " + Board[3][3] +Board[2][0]+ " | " + Board[2][1] + " | "+ Board[2][2]);

    }
    public static void move1(String[][] Board, Scanner scan){
        int Input = scan.nextInt();
        for (int i = 0;i<100;i++){
            if (Input == 1){
                Board[0][1]="X";
                grid1(Board, scan);
                move2(Board,scan);
            } else if(Input == 2){
                Board[3][1]="X";
                grid1(Board,scan);
                move2(Board,scan);
            } else if(Input == 3){
                Board[0][2]="X";
                grid1(Board,scan);
                move2(Board,scan);
            } else if(Input == 4){
                Board[1][1]="X";
                grid1(Board,scan);
                move2(Board,scan);
            } else if(Input ==5){
                Board[3][2]="X";
                grid1(Board,scan);
                move2(Board,scan);
            } else if(Input ==6){
                Board[1][2]="X";
                grid1(Board,scan);
                move2(Board,scan);
            } else if(Input ==7){
                Board[3][3]="X";
                grid1(Board,scan);
                move2(Board,scan);
            } else if(Input == 8){
                Board[2][1]="X";
                grid1(Board,scan);
                move2(Board,scan);
            } else if(Input ==9){
                Board[2][2]="X";
                grid1(Board,scan);
                move2(Board,scan);
            }
        }
    }
    public static void move2(String[][] Board, Scanner scan) {
        System.out.println("Where do you wan't your O to be?");
        int Input = scan.nextInt();
        if (Input == 1){
            Board[0][1]="O";
            grid1(Board, scan);
            move3(Board, scan,Input);
        } else if(Input == 2){
            Board[3][1]="O";
            grid1(Board,scan);
            move3(Board, scan,Input);
        } else if(Input == 3){
            Board[0][2]="O";
            grid1(Board,scan);
            move3(Board, scan,Input);
        } else if(Input == 4){
            Board[1][1]="O";
            grid1(Board,scan);
        } else if(Input ==5){
            Board[3][2]="O";
            grid1(Board,scan);
            move3(Board, scan,Input);
        } else if(Input ==6){
            Board[1][2]="O";
            grid1(Board,scan);
            move3(Board, scan,Input);
        } else if(Input ==7){
            Board[3][3]="O";
            grid1(Board,scan);
            move3(Board, scan,Input);
        } else if(Input == 8){
            Board[2][1]="O";
            grid1(Board,scan);
            move3(Board, scan,Input);
        } else if(Input ==9){
            Board[2][2]="O";
            grid1(Board,scan);
            move3(Board, scan,Input);
        }
    }
    public static void move3(String[][] Board, Scanner scan, int Input){
        if (Input == 1){
            Board[0][1]="X";
            grid1(Board, scan);
        } else if(Input == 2){
            Board[3][1]="X";
            grid1(Board,scan);
        } else if(Input == 3){
            Board[0][2]="X";
            grid1(Board,scan);
        } else if(Input == 4){
            Board[1][1]="X";
            grid1(Board,scan);
        } else if(Input ==5){
            Board[3][2]="X";
            grid1(Board,scan);
        } else if(Input ==6){
            Board[1][2]="X";
            grid1(Board,scan);
        } else if(Input ==7){
            Board[3][3]="X";
            grid1(Board,scan);
        } else if(Input == 8){
            Board[2][1]="X";
            grid1(Board,scan);
        } else if(Input ==9){
            Board[2][2]="X";
            grid1(Board,scan);
        }
    }
} 

2 个答案:

答案 0 :(得分:1)

作为一般编码实践,您希望减少所有冗余。在你的代码中有很多冗余代码,在清理之后,可能会揭示你的问题。

首先,我会编写一个辅助方法,将符号X或O放在电路板上。

public void addMove(String letter, int input){}

这样你就不需要move1,move2方法了。 在你的主要部分,你可以使用布尔值跟踪它的转向。我还建议编写一种方法来检查游戏是否结束。

while(!isOver()){
   boolean xTurn = false;
   if(xTurn){
      addMove("X", /*Their input*/); 
   } else {
      addMove("O"m /*Their input*/);
   }
   xTurn = !xTurn; //Set xTurn to the opposite
}

希望这有帮助!

答案 1 :(得分:0)

只是一个提示:因为你有一个3x3矩阵

public static void move(char c){
    int row = (input-1) / 3;
    int col = (input-1) % 3;
    board[row][col]=c;
}