Noughts和Crosses检查是否有人赢了

时间:2014-01-15 21:42:30

标签: java operators

import java.util.Scanner;
public class noughtscross {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        String[][] visBoard = new String[3][3];
        int[][] board = new int[3][3];
        boolean[][] check = new boolean[3][3];
        int total, diag, moves, mod, xcoord, ycoord;
        boolean draw = false, gamewon = false;
        String symbol, play1, play2;

        //Initialising Board
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                visBoard[i][j] = "[ ]";
                board[i][j] = 0;
                check[i][j] = false;
            }
        }for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                System.out.print(visBoard[i][j]);
            }System.out.print("\n");
        }

        //Getting Names
        System.out.println("Player 1 - Enter your name");
        play1 = sc.nextLine();
        System.out.println("Player 2 - Enter your name");
        play2 = sc.nextLine();
        //
        moves = 0;
        symbol = " X ";
        do{
            do{
                //Get Coords
                System.out.println("X Coordinate");
                xcoord = sc.nextInt() -1;
                System.out.println("Y Coordinate");
                ycoord = sc.nextInt() -1;

                if(check[xcoord][ycoord] == true){
                    System.out.println("Not a valid move!");
                }
            }while(check[xcoord][ycoord] == true);

            //Making move
            check[xcoord][ycoord] = true;
            visBoard[xcoord][ycoord] = symbol;
            if(symbol.equals(" X ")){
                board[xcoord][ycoord] = 1;
            }else if(symbol.equals(" O ")){
                board[xcoord][ycoord] = 5;
            }else{
                System.out.println("You've messed up James");
            }

            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    System.out.print(visBoard[i][j]);
                }System.out.print("\n");
            }

            //Check if game has won
            //columns
            total = 0;
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    total = total + board[j][i];
                }if(total == 15 || total == 3){
                    gamewon = true;
                }
            }total = 0;
            //rows
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    total = total + board[i][j];
                }if(total == 15 || total == 3){
                    gamewon = true;
                }
            }total = 0;
            //diagonals
            for(int i = 0; i < 3; i++){
                total = total + board[i][i];
            }if(total == 15 || total == 3){
                gamewon = true;
            }total = 0;
            diag = 2;
            for(int i = 0; i < 3; i++){
                total = total + board[i][diag];
                diag--;
            }if(total == 15 || total == 3){
                gamewon = true;
            }
            System.out.println(gamewon);
            moves++;
            if(gamewon == false){
                if(moves == 9){
                    System.out.println("Game has been drawn! No one wins!");
                }else{
                    mod = moves % 2;
                    if(mod == 0){
                        symbol = " X ";
                    }else{
                        symbol = " O ";
                    }
                }
            }
        }while(gamewon == false);

        if(gamewon == true){
            if(symbol.equals(" X ")){
                System.out.println("Winner is "+play1);
            }else{
                System.out.println("Winner is "+play2);
            }
        }else{
            System.out.println("Game is drawn");
        }

    }
}

一旦游戏运行并且有人赢了,即使布尔变量gamewon为真,它也不会停止循环,这应该会停止while循环。

如果这个运行正常并与你一起工作可能是我的日食,如果没有那么请告诉我什么是错的。 JimiiBee

1 个答案:

答案 0 :(得分:0)

if(moves == 9){
    System.out.println("Game has been drawn! No one wins!");
}

应该是:

if(moves == 9){
    System.out.println("Game has been drawn! No one wins!");
    break;
}