【问题标题】:Tic Tac Toe game with small looping error带有小循环错误的井字游戏
【发布时间】:2015-01-08 03:13:13
【问题描述】:

我的井字游戏代码在这里用于我的 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

【问题讨论】:

标签: java


【解决方案1】:

您的程序依靠机会找到最后可用的移动。 这有点奇怪——你会在随机抽出数字后把它们放回帽子里吗? 不,你会把它们排除在外。

Scott Hunter 建议您先列出随机顺序的动作列表。继续进行类比,这是事先将数字从帽子中拉出来。

您不必预先执行此操作。您也可以根据当前棋盘状态创建列表,然后从该列表中随机选择。

基本思想是……

  1. 生成可能的移动列表
  2. 从此列表中随机选择(如果列表为空,则无法移动)。

提示:您可以使用单个值来表示板单元...

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

【讨论】:

  • 我觉得这会对我的情况有所帮助,但不幸的是我不确定你在这里的意思。不过感谢您的回答。
【解决方案2】:

为什么不在开始时随机列出可能的移动列表,然后简单地遍历列表?这样你的循环就必须结束。您当前的循环不仅无法识别游戏何时结束,而且可以重复尝试相同的非法动作。

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

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

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

【讨论】:

  • 您能详细说明一下吗?我试图通过生成一个随机数使其随机化,然后将该数字分配给特定的移动,这非常好。只是这里有循环问题。
  • 感谢您的编辑。我刚刚在我的代码中尝试了这一点,它似乎仍然发生了一些奇怪的事情,例如每当我放置我的“X”时,你告诉我使用的计算机“O”会删除我的“X”并将自己放置在那里,并且仍然没有显示最后一步。有任何想法吗?我会尝试揭穿我自己并感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2022-12-04
  • 2015-07-18
相关资源
最近更新 更多