Tic Tac Toe游戏带有小循环错误

时间:2015-01-08 03:13:14

标签: java

我的tic tac toe游戏在这里编写了我的java任务,一切都很好,除了一个小问题,当你进入最后一步(第九回合)时,最后一个'X'没有出现。这不仅令人讨厌,因为没有显示获胜的部分,但它导致了一些问题,领带方法没有得到正确解决,因此它没有显示任何内容。

我明确知道为什么不这样做,只需要帮助修复它。

do
 {
     ComputerSpotChoice = (int) (Math.random() * 9 ) + 1 ; 
 }
 while (Board [(ComputerSpotChoice - 1) / 3] [(ComputerSpotChoice - 1) % 3] != '-') ;

当它进入第九回合时,这个条件总是为真,因为它找不到' - '。继续无限循环,我不太清楚如何解决这个问题,但保持计算机点选择半。

这是完整的代码。

/*I have methods for drawing the board, determining a winner, and a loser. This is just the 'main' method containing the bulk of the program */

    public static void main (String[] args)
        {
        //Variable declaration
        Scanner kbReader = new Scanner(System.in);
        char [] [] Board = new char [3] [3] ;
        String MenuInput ;
        int BoardOutput ;
        int UserSpotChoice ;
        int ComputerSpotChoice = 0;
        int UserTurn = 1 ;
        int Winner = 0 ;
        Board [0] [0] = '-' ;
        Board [0] [1] = '-' ;
        Board [0] [2] = '-' ;
        Board [1] [0] = '-' ;
        Board [1] [1] = '-' ;
        Board [1] [2] = '-' ;
        Board [2] [0] = '-' ;
        Board [2] [1] = '-' ;
        Board [2] [2] = '-' ;

        //Welcome
        System.out.println ("Welcome to Alex Montague's Tic Tac Toe game!") ;
        System.out.println ("") ;
        System.out.println ("If you wish to play, type 'Play'") ; 
        System.out.println ("If you wish to read the instructions, type 'Instructions'") ;
        System.out.println ("If you wish to exit, type 'Exit'") ;
        MenuInput = kbReader.next () ;

        do
        {
        if (MenuInput.equals ("Play") || MenuInput.equals ("play")) 
        {
            while (!GameOver) 
            {
            System.out.println ("\f") ;
            System.out.println (" Tic Tac Toe") ;
            BoardOutput = DrawBoard (Board) ;
            System.out.println (" 1 2 3") ;
            System.out.println (" 4 5 6") ;
            System.out.println (" 7 8 9") ;
            System.out.println ("Please enter the number you would like to move your spot to") ;
            UserSpotChoice = kbReader.nextInt () ;

            if (UserSpotChoice == 1) Board [0] [0] = 'X' ;
            if (UserSpotChoice == 2) Board [0] [1] = 'X' ;
            if (UserSpotChoice == 3) Board [0] [2] = 'X' ;
            if (UserSpotChoice == 4) Board [1] [0] = 'X' ;
            if (UserSpotChoice == 5) Board [1] [1] = 'X' ;
            if (UserSpotChoice == 6) Board [1] [2] = 'X' ;
            if (UserSpotChoice == 7) Board [2] [0] = 'X' ;
            if (UserSpotChoice == 8) Board [2] [1] = 'X' ;
            if (UserSpotChoice == 9) Board [2] [2] = 'X' ;

            do
            {
                ComputerSpotChoice = (int) (Math.random() * 9 ) + 1 ; 
            }
            while
            (Board [(ComputerSpotChoice - 1) / 3] [(ComputerSpotChoice - 1) % 3] != '-') ;

            if (ComputerSpotChoice == 1) Board [0] [0] = 'O' ;
            if (ComputerSpotChoice == 2) Board [0] [1] = 'O' ;
            if (ComputerSpotChoice == 3) Board [0] [2] = 'O' ;
            if (ComputerSpotChoice == 4) Board [1] [0] = 'O' ;
            if (ComputerSpotChoice == 5) Board [1] [1] = 'O' ;
            if (ComputerSpotChoice == 6) Board [1] [2] = 'O' ;
            if (ComputerSpotChoice == 7) Board [2] [0] = 'O' ;
            if (ComputerSpotChoice == 8) Board [2] [1] = 'O' ;
            if (ComputerSpotChoice == 9) Board [2] [2] = 'O' ;

            Winner (Board) ;
            Loser (Board) ;
            Tie (Board) ;

            } //While loop
            if (GameOver) System.exit (0) ;
     } //If play

        else if (MenuInput.equals ("Instructions") || MenuInput.equals ("instructions")) 
        {
            System.out.println ("\f") ;
            System.out.println ("You will be playing the game of Tic Tac Toe against the computer.") ;
            System.out.println ("The object of this game is to get three of your own x's or o's in a line.") ;    
            System.out.println ("You take turns placing the x's and o's and whoever gets three in a row first wins.") ;
            System.out.println ("Good Luck!") ;
            System.out.println ("") ;
            System.out.println ("If you wish to play, type 'Play'") ;
            System.out.println ("If you wish to exit, type 'Exit'") ;
            MenuInput = kbReader.next () ;
        }

        else if (MenuInput.equals ("Exit") || MenuInput.equals ("exit")) 
        {
            System.out.println ("Thank you for using Alex Montague's Tic Tac Toe game!") ;
            System.exit (0) ;
        }

        else 
        {
            System.out.println ("Sorry, that is not a valid choice.") ;
            System.out.println ("If you wish to play, type 'Play'") ; 
            System.out.println ("If you wish to read the instructions, type 'Instructions'") ;
            System.out.println ("If you wish to exit, type 'Exit'") ;
            MenuInput = kbReader.next () ;
        }

        } //do while
        while (!MenuInput.equals ("Instructions") || !MenuInput.equals ("instructions") || !MenuInput.equals ("Play") || !MenuInput.equals ("play") || !MenuInput.equals ("Exit") || !MenuInput.equals ("exit")) ;

        } // main method

2 个答案:

答案 0 :(得分:0)

为什么不在开始时随机化一系列可能的动作,只需查看列表?那样你的循环必须结束。当你的当前循环不仅不能识别游戏结束时间,而且可以反复尝试相同的非法移动。

这是一种不涉及排序的方法:添加这些定义:

char free[9] = {1,2,3,4,5,6,7,8,9}; // choices not taken yet
int numFree = 9;                    // number of choices available
int moveIndex;

然后选择这样的举动:

moveIndex = (int) (Math.random() * numFree);   // pick index of new move from free ones
ComputerSpotChoice = free[moveIndex];          // save that choice
numFree--;                                     // there is now one less free move
free[moveIndex] = free[numFree];               // move the "last" free move to where you just took a move from

您需要搜索“免费”以供用户选择,以便将其删除。

答案 1 :(得分:0)

您的计划依靠机会找到最后一次移动。 这有点奇怪 - 你是否会在将数字随机拉出后将数字放回帽子中? 不,你会让他们脱离帽子。

斯科特·亨特建议您首先列出随机排序的动作。继续这个比喻,就是先把数字拉出帽子。

您无需事先做到这一点。您还可以在给定当前板状态的情况下创建列表,然后从该列表中随机选择。

基本理念是......

  1. 生成可能的移动列表
  2. 从此列表中随机选择(如果列表为空,则无法移动)。
  3. 提示:您可以使用单个值代表电路板单元......

    Given a board[x][y] then...
    cell = x * 3 + y
    x = cell / 3
    y = cell - x * 3