Java-Tictactoe字符覆盖

时间:2016-03-31 18:04:34

标签: java

我正在试图弄清楚如何阻止X覆盖O,而O来覆盖X,但它们仍然能够相互覆盖。不知何故,即使在那个空间中有一个角色,它仍然会出现S是真的。

import java.util.*;
public class TicTacToe {
public static char X = 'X';
public static char O = 'O';
public static char S = ' ';
public static boolean isFull = false;
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    char[][] board = new char[3][3];
    int c,r;
    for(r=0;r<board.length;r++){
        for(c=0;c<board[r].length;c++)
            board[r][c] = ' ';              
    }
    printBoard(board);
    for(int i = 0; i <9;i++){
        if(i%2==0){
            do {
                System.out.print("Enter your next move:  ");
                r = in.nextInt();
                c = in.nextInt();
                if(r>board.length || c>board.length || r<0 || c<0)
                    System.out.println("Error, try again ");
                if(board[r][c]==X || board[r][c]==O){
                    isFull=true;
                    System.out.print("This square is already taken, try again");
                }
                if(board[r][c]==S);{
                    System.out.print(board);
                    isFull=false;
                    board[r][c] = X;
                    printBoard(board);
                }
            }while(isFull==true);

        }
        else{
            do{ 
                System.out.print("Enter your next move:  ");
                r = in.nextInt();
                c = in.nextInt();
                if(r>board.length || c>board.length || r<0 || c<0)
                    System.out.println("Error, try again ");
                if(board[r][c]==X || board[r][c]==O){
                    isFull=true;
                    System.out.print("This square is already taken, try again");
                }
                if(board[r][c]==S);{
                    System.out.print(board[r][c]);
                    isFull=false;
                    board[r][c] = O;
                    printBoard(board);
                }
            }while(isFull==true);

        }
    }
}

public static void printBoard(char[][] b){
    int r = 0,c = 0;
    System.out.println();
    for(r=0;r<b.length;r++){
        for(c=0;c<b[r].length-1;c++)
            System.out.print(" " + b[r][c] + " |");
        System.out.println(" "+b[r][c]);
        if(r<b.length-1){
            for(c=0;c < b[r].length-1;c++)
                System.out.print("---+");
            System.out.println("---");
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

您在if声明

后忘记了分号(;)
if(board[r][c]==S);{        <----

所以它不会考虑这个测试并替换已经放置的角色。

答案 1 :(得分:0)

你还应该使用“else if”语句,以避免在你所有人参与的时候进入两个不同的陈述。

if(r>board.length || c>board.length || r<0 || c<0)
                System.out.println("Error, try again ");
else if(board[r][c]==X || board[r][c]==O){
     isFull=true;
     System.out.print("This square is already taken, try again");
}
else if(board[r][c]==S){
     System.out.print(board[r][c]);
     isFull=false;
     board[r][c] = O;
     printBoard(board);
}
else{
    //what ever 
}