Java中basic TICTATCOE程序的语义错误

Java中basic TICTATCOE程序的语义错误,java,semantics,tic-tac-toe,Java,Semantics,Tic Tac Toe,首先,我希望我可以在这里请求一些调试帮助。尽管如此, 我已经创建了一个简单的小tic-tac-toe程序,它基本上已经完成了,但是这个语义错误一直在折磨着我 显然,我花了一些时间试图自己解决这个问题,但你可以说我已经放弃了,所以我现在就在这里:) 对于一些简要概述,电路板由char ttt[3][3]类型的数组表示。玩家也是char类型的变量,因此他们是“X”或“O”,棋盘的坐标以字母形式输入: 示例运行如下所示: ******************************** ---a-

首先,我希望我可以在这里请求一些调试帮助。尽管如此, 我已经创建了一个简单的小tic-tac-toe程序,它基本上已经完成了,但是这个语义错误一直在折磨着我

显然,我花了一些时间试图自己解决这个问题,但你可以说我已经放弃了,所以我现在就在这里:)

对于一些简要概述,电路板由char ttt[3][3]类型的数组表示。玩家也是char类型的变量,因此他们是“X”或“O”,棋盘的坐标以字母形式输入:

示例运行如下所示:

********************************
  ---a------b------c---
  |      |      |     |
  ---d------e------f---
  |      |      |     |
  ---g------h------i---
  |      |      |     |
  ---------------------
player1: O, it is your turn.
Select a cell [a, b, c, ... i]
a
********************************
  ---a------b------c---
  |  O   |      |     |
  ---d------e------f---
  |      |      |     |
  ---g------h------i---
  |      |      |     |
  ---------------------
player2: X, it is your turn.
Select a cell [a, b, c, ... i]
数组ttt[3][3]已初始化,因此每个元素仅为“”

在大多数情况下,程序运行良好。为了给你们节省一些时间,我相信下面的方法运行得很好

  • 布尔赢家(字符播放器)
  • 布尔gameIsDraw()
  • 无效显示板()
  • 字符串播放器ID(字符播放器)
  • 主要方法是什么
我发现问题最有可能包含在我的 getPlayerInput(字符播放器)方法:

对我来说,它看起来很好,但我的输出显示了另一种情况。该方法包括两个故障保护

  • 如果用户输入的内容超出电路板范围(字符超出“a”到“i”)

  • 或者,如果用户在电路板上选择的字母/位置已被另一个“X”或“O”占用

  • 在这两种情况下,该方法都会打印出输入了错误的输入,然后再次调用getPlayerInput()

    通过调试我注意到,如果只输入有效的输入,程序似乎运行良好。但是,如果输入了错误的输入(任何一种类型),然后输入了有效的输入,有时该方法将打印仍然输入了错误的输入

    比如说,

    ********************************
      ---a------b------c---
      |      |      |     |
      ---d------e------f---
      |      |      |     |
      ---g------h------i---
      |      |      |     |
      ---------------------
    player1: O, it is your turn.
    Select a cell [a, b, c, ... i]
    a
    ********************************
      ---a------b------c---
      |  O   |      |     |
      ---d------e------f---
      |      |      |     |
      ---g------h------i---
      |      |      |     |
      ---------------------
    player2: X, it is your turn.
    Select a cell [a, b, c, ... i]
    z
    Invalid location, try again.
    player2: X, it is your turn.
    Select a cell [a, b, c, ... i]
    e
    This square is taken. Try again.
    player2: X, it is your turn.
    Select a cell [a, b, c, ... i]
    f
    ********************************
      ---a------b------c---
      |  O   |      |     |
      ---d------e------f---
      |      |  X   |  X  |
      ---g------h------i---
      |      |      |     |
      ---------------------
    player1: O, it is your turn.
    Select a cell [a, b, c, ... i]
    
    注意,我输入了字符的a-z-e-f.'z'显然是一个无效字符,因此该方法按预期工作(到目前为止),打印为无效输入,然后该方法再次运行,请求输入然后输入“e”,显然是一个有效的位置,但该方法显示“正方形已被取下”,而它显然不是。但是,输入一个不同的字符“f”允许我退出它

    最终的结果是,玩家“X”有两个回合,并填充了两个方块“e”和“f”

    需要注意的是,如果用户持续输入错误的输入,他应该一直使用该方法,直到输入了有效的输入,但很明显,在这种情况下,好的输入会被误解为错误的输入,除非输入了不同的好输入实例,否则无法退出循环

    所以,说了这么多,帮帮我??不管怎样,我非常感谢所有有耐心读到这篇文章的人

    如果您想自己运行代码,以下是源代码:

    import java.util.*;
    
    class TicTacToe
    {
        char ttt[][] = new char[3][3];
        static final char player1 = 'O';
        static final char player2 = 'X';
        Scanner scan  =new Scanner(System.in);
    
    
        String playerID(char player)
        {    
            if (player == player1)
                return "player1: "+player;
            else
                return "player2: "+ player;
        }
    
        void getPlayerInput(char player)
        {       
            int row = 0;
            int col = 0;
    
            System.out.println(playerID(player) + ", it is your turn.");
            System.out.println("Select a cell [a, b, c, ... i]");
    
            char answer;
            answer = scan.next().charAt(0);
    
            switch(answer)
            {
            case 'a':
                row = 0;
                col = 0;
                break;          
            case 'b':
                row = 0;
                col = 1;
                break;          
            case 'c':
                row = 0;
                col = 2;
                break;          
            case 'd':
                row = 1;
                col = 0;
                break;          
            case 'e':
                row = 1;
                col = 1;
                break;          
            case 'f':
                row = 1;
                col = 2;
                break;
            case 'g':
                row = 2;
                col = 0;
                break;
            case 'h':
                row = 2;
                col = 1;
                break;
            case 'i':
                row = 2;
                col = 2;
                break;
    
            default:
                System.out.println("Invalid location, try again.");
                getPlayerInput(player);             
            }
    
            if(ttt[row][col] != ' ')
            {
                System.out.println("This square is taken. Try again.");
                getPlayerInput(player);
            }
            else
            {
                ttt[row][col] = player;
            }            
        }
    
        boolean gameIsDraw()
        {       
            boolean isDraw = true;
            for(int i = 0; i < 3; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(ttt[i][j] == ' ')
                    {
                        isDraw = false;
                    }
                }
            }
    
            return isDraw;
        }
    
        boolean winner(char player)
        {
            boolean hasWon = false;
    
            // possible horizontal wins
            for(int i = 0; i < 3; i++)
            {
                if(ttt[i][0] == player && ttt[i][1] == player && ttt[i][2] == player)
                {
                    hasWon = true;
                }
            }
    
            // possible vertical wins
            for(int i = 0; i < 3; i++)
            {
                if(ttt[0][i] == player && ttt[1][i] == player && ttt[2][i] == player)
                {
                    hasWon = true;
                }
            }
    
            // one diagonal win    
            if(ttt[0][0] == player && ttt[1][1] == player && ttt[2][2] == player)
            {
                hasWon = true;
            }
    
            // other diagonal win
            if(ttt[0][2] == player && ttt[1][1] == player && ttt[2][0] == player)
            {
                hasWon = true;
            }
    
            return hasWon;
        }
    
    
    void displayBoard()
        {
            System.out.println("********************************");        
            System.out.println("      ---a------b------c---");
    
            for (int i=0; i<3; i++)
            {
                for (int j=0; j< 3; j++)
                {
                  if (j == 0) System.out.print("      |  "); 
                  System.out.print(ttt[i][j]);
                  if (j < 2) System.out.print( "   |  ");
                  if (j==2)  System.out.print("  |");
                }
                System.out.println();
                switch (i)
                {
                case 0:
                    System.out.println("      ---d------e------f---");
                    break;
                case 1:
                    System.out.println("      ---g------h------i---");
                    break;
                case 2:
                    System.out.println("      ---------------------");
                    break;
                }
            }
        }
    
    
    void newgame()
    {
        char currPlayer = player1;
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                ttt[i][j] =' ';
    
        boolean continueFlag = true;        
        while (continueFlag)
        {
            displayBoard();
            if (gameIsDraw())
            {
                System.out.println("Game Ends in Draw");
                continueFlag = false;
            }
            else
            {
                getPlayerInput(currPlayer);
                if (winner(currPlayer))
                {
                    System.out.println("We have a winner: " + playerID(currPlayer));
                    displayBoard();
                    continueFlag = false;
                }
                else
                { 
                    if (currPlayer == player1) currPlayer = player2;
                        else currPlayer = player1;
                }
             }
        }
    
    }
    
    
    public static void main(String[] args)
    {
        TicTacToe game = new TicTacToe();
        String str;
        do
        {
            game.newgame();
    
            System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
            str= game.scan.next();
        } while ("y".equals(str));
    
        System.out.println("Bye");
    }    
    } 
    
    import java.util.*;
    提克塔克类
    {
    字符ttt[][]=新字符[3][3];
    静态最终字符播放器1='O';
    静态最终字符播放器2='X';
    扫描仪扫描=新扫描仪(System.in);
    字符串播放器ID(字符播放器)
    {    
    如果(玩家==玩家1)
    返回“player1:”+玩家;
    其他的
    返回“player2:”+玩家;
    }
    无效getPlayerInput(字符播放器)
    {       
    int行=0;
    int col=0;
    System.out.println(playerID(player)+“,该你了。”);
    System.out.println(“选择一个单元格[a,b,c,…i]”;
    答案;
    answer=scan.next().charAt(0);
    开关(应答)
    {
    案例“a”:
    行=0;
    col=0;
    打破
    案例“b”:
    行=0;
    col=1;
    打破
    案例“c”:
    行=0;
    col=2;
    打破
    案例“d”:
    行=1;
    col=0;
    打破
    案例“e”:
    行=1;
    col=1;
    打破
    案例“f”:
    行=1;
    col=2;
    打破
    案例“g”:
    行=2;
    col=0;
    打破
    案例“h”:
    行=2;
    col=1;
    打破
    案例“i”:
    行=2;
    col=2;
    打破
    违约:
    System.out.println(“位置无效,请重试。”);
    getPlayerInput(玩家);
    }
    如果(ttt[行][列]!=“”)
    {
    System.out.println(“此方块已取得。请重试”);
    getPlayerInput(玩家);
    }
    其他的
    {
    ttt[行][列]=播放器;
    }            
    }
    布尔gameIsDraw()
    {       
    布尔isDraw=真;
    对于(int i=0;i<3;i++)
    {
    对于(int j=0;j<3;j++)
    {
    如果(ttt[i][j]='')
    {
    isDraw=假;
    }
    }
    }
    返回isDraw;
    }
    布尔赢家(字符播放器)
    {
    布尔hasWon=false;
    //可能的横向胜利
    对于(int i=0;i<3;i++)
    {
    如果(ttt[i][0]==玩家和ttt[i][1]==玩家和ttt[i][2]==玩家)
    {
    哈斯旺=真;
    }
    }
    //可能的纵向胜利
    对于(int i=0;i<3;i++)
    {
    如果(ttt[0][i]==玩家和ttt[1][i]==玩家和ttt[2][i]==玩家)
    {
    哈斯旺=真;
    }
    }
    //一个对角线的胜利
    如果(ttt[0][0]==玩家和ttt[1][1]==玩家和ttt[2][2]==玩家)
    {
    哈斯旺=真;
    }
    //其他对角线胜利
    如果(ttt[0][2]==玩家和ttt[1][1]==玩家和ttt[2][0]==玩家)
    {
    哈斯旺=真;
    }
    返回哈斯旺;
    }
    无效显示板()
    
    import java.util.*;
    
    class TicTacToe
    {
        char ttt[][] = new char[3][3];
        static final char player1 = 'O';
        static final char player2 = 'X';
        Scanner scan  =new Scanner(System.in);
    
    
        String playerID(char player)
        {    
            if (player == player1)
                return "player1: "+player;
            else
                return "player2: "+ player;
        }
    
        void getPlayerInput(char player)
        {       
            int row = 0;
            int col = 0;
    
            System.out.println(playerID(player) + ", it is your turn.");
            System.out.println("Select a cell [a, b, c, ... i]");
    
            char answer;
            answer = scan.next().charAt(0);
    
            switch(answer)
            {
            case 'a':
                row = 0;
                col = 0;
                break;          
            case 'b':
                row = 0;
                col = 1;
                break;          
            case 'c':
                row = 0;
                col = 2;
                break;          
            case 'd':
                row = 1;
                col = 0;
                break;          
            case 'e':
                row = 1;
                col = 1;
                break;          
            case 'f':
                row = 1;
                col = 2;
                break;
            case 'g':
                row = 2;
                col = 0;
                break;
            case 'h':
                row = 2;
                col = 1;
                break;
            case 'i':
                row = 2;
                col = 2;
                break;
    
            default:
                System.out.println("Invalid location, try again.");
                getPlayerInput(player);             
            }
    
            if(ttt[row][col] != ' ')
            {
                System.out.println("This square is taken. Try again.");
                getPlayerInput(player);
            }
            else
            {
                ttt[row][col] = player;
            }            
        }
    
        boolean gameIsDraw()
        {       
            boolean isDraw = true;
            for(int i = 0; i < 3; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(ttt[i][j] == ' ')
                    {
                        isDraw = false;
                    }
                }
            }
    
            return isDraw;
        }
    
        boolean winner(char player)
        {
            boolean hasWon = false;
    
            // possible horizontal wins
            for(int i = 0; i < 3; i++)
            {
                if(ttt[i][0] == player && ttt[i][1] == player && ttt[i][2] == player)
                {
                    hasWon = true;
                }
            }
    
            // possible vertical wins
            for(int i = 0; i < 3; i++)
            {
                if(ttt[0][i] == player && ttt[1][i] == player && ttt[2][i] == player)
                {
                    hasWon = true;
                }
            }
    
            // one diagonal win    
            if(ttt[0][0] == player && ttt[1][1] == player && ttt[2][2] == player)
            {
                hasWon = true;
            }
    
            // other diagonal win
            if(ttt[0][2] == player && ttt[1][1] == player && ttt[2][0] == player)
            {
                hasWon = true;
            }
    
            return hasWon;
        }
    
    
    void displayBoard()
        {
            System.out.println("********************************");        
            System.out.println("      ---a------b------c---");
    
            for (int i=0; i<3; i++)
            {
                for (int j=0; j< 3; j++)
                {
                  if (j == 0) System.out.print("      |  "); 
                  System.out.print(ttt[i][j]);
                  if (j < 2) System.out.print( "   |  ");
                  if (j==2)  System.out.print("  |");
                }
                System.out.println();
                switch (i)
                {
                case 0:
                    System.out.println("      ---d------e------f---");
                    break;
                case 1:
                    System.out.println("      ---g------h------i---");
                    break;
                case 2:
                    System.out.println("      ---------------------");
                    break;
                }
            }
        }
    
    
    void newgame()
    {
        char currPlayer = player1;
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
                ttt[i][j] =' ';
    
        boolean continueFlag = true;        
        while (continueFlag)
        {
            displayBoard();
            if (gameIsDraw())
            {
                System.out.println("Game Ends in Draw");
                continueFlag = false;
            }
            else
            {
                getPlayerInput(currPlayer);
                if (winner(currPlayer))
                {
                    System.out.println("We have a winner: " + playerID(currPlayer));
                    displayBoard();
                    continueFlag = false;
                }
                else
                { 
                    if (currPlayer == player1) currPlayer = player2;
                        else currPlayer = player1;
                }
             }
        }
    
    }
    
    
    public static void main(String[] args)
    {
        TicTacToe game = new TicTacToe();
        String str;
        do
        {
            game.newgame();
    
            System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
            str= game.scan.next();
        } while ("y".equals(str));
    
        System.out.println("Bye");
    }    
    } 
    
    void getPlayerInput(char player) {       
        int row = -1;
        int col = -1;
    
        System.out.println(playerID(player) + ", it is your turn.");
    
        while(row==-1) {
            System.out.println("Select a cell [a, b, c, ... i]");
    
            char answer;
            answer = scan.next().charAt(0);
    
            switch(answer) {
            case 'a':
                row = 0;
                col = 0;
                break;          
            // <snip>
            default:
                System.out.println("Invalid location, try again.");
            }
            if(row !- -1 && ttt[row][col] != ' ') {
                System.out.println("This square is taken. Try again.");
                row = -1;
            }
        }
        ttt[row][col] = player;
    }