我不知道我的 java tictactoe 有什么问题

问题描述 投票:0回答:1

它一直吐出“你可以选择它已经被占用的空间,它似乎忽略了所有其他 if 语句。我已经修补了几个小时,但我不知道如何修复它。

public class tictactoegame {

    public static void main(String[] args) {
        int j = 0;

        System.out.println("");
        char a = ('a');
        char b = ('b');
        char c = ('c');
        char d = ('d');
        char e = ('e');
        char f = ('f');
        char g = ('g');
        char h = ('h');
        char i = ('i');

        
        
        String gameboard = ("    1  2  3\n1   "+a+"| "+b+ " | "+c+"\n   - - - - -"+"\n2   "+d+"| "+e+ " | "+f+"\n   - - - - -"+"\n3   "+g+"| "+h+ " | "+i+"\n   - - - - -");
        System.out.println(gameboard);
        a = (' ');
        b = (' ');
        c = (' ');
        d = (' ');
        e = (' ');
        f = (' ');
        g = (' ');
        h = (' ');
        i = (' ');


        


        System.out.println();

        
        while (j < 9){
            int x = IO.inputInt("What is your x value");
            int y = IO.inputInt("What is your y value");
                
            //For X

            if (j%2 == 0){
                System.out.println("\n Invalid attempt, try again");
            }
            else if (x == 1 && y == 1 && a != 'X' && a != 'O' ){
                a = ('X');
                j++;
            }
            else if (x == 1 && y == 2 && b != 'X' && b != 'O' ){
                b = ('X');
                j++;
            } 
            else if (x == 1 && y == 3 && c != 'X' && c != 'O' ){
                c = ('X');
                j++;
            }
            else if (x == 2 && y == 1  && d != 'X' && d != 'O' ){
                d = ('X');
                j++;
            }
            else if (x == 2 && y == 2  && e != 'X' && e != 'O' ){
                e = ('X');
                j++;
            }
            else if (x == 2 && y == 3  && f != 'X' && f != 'O' ){
                f = ('X');
                j++;
            }
            else if (x == 3 && y == 1 && g != 'X' && g != 'O' ){
                g = ('X');
                j++;
            }
            else if (x == 3 && y == 2  && h != 'X' && h != 'O' ){
                h = ('X');
                j++;
            }
            else if (x == 3 && y == 3  && i != 'X' && i != 'O' ){
                i = ('X');
                j++;
            }
            else {
                System.out.println("you cant pick that space its already been taken");}

            //For O
            
            if (j%2 != 0){
                System.out.println("\n Invalid attempt, try again");
            }
            else if (x == 1 && y == 1  && a != 'X' && a != 'O' ){
                a = ('O');
                j++;
            }
            else if (x == 1 && y == 2  && b != 'X' && b != 'O' ){
                b = ('O');
                j++;
            } 
            else if (x == 1 && y == 3  && c != 'X' && c != 'O' ){
                c = ('O');
                j++;
            }
            else if (x == 2 && y == 1 && d != 'X' && d != 'O' ){
                d = ('O');
                j++;
            }
            else if (x == 2 && y == 2 && e != 'X' && e != 'O' ){
                e = ('O');
                j++;
            }
            else if (x == 2 && y == 3 && f != 'X' && f != 'O' ){
                f = ('O');
                j++;
            }
            else if (x == 3 && y == 1 && g != 'X' && g != 'O' ){
                g = ('O');
                j++;
            }
            else if (x == 3 && y == 2 && h != 'X' && h != 'O' ){
                h = ('O');
                j++;
            }
            else if (x == 3 && y == 3 && i != 'X' && i != 'O' ){
                i = ('O');
                j++;

            }
            else {
                System.out.println("you cant pick that space its already been taken");}
            
            if (a == 'X') {
                if (b == 'X') {
                    if (c == 'X') {
                        System.out.println("X wins!!!");
                        System.exit(1);
                    }
                }
            }
             if (d == 'X') {
                if (e == 'X') {
                    if (f == 'X') {
                        System.out.println("X wins!!!");
                        System.exit(1);
                    }
                }
            }
              if (g == 'X') {
                if (h == 'X') {
                    if (i == 'X') {
                        System.out.println("X wins!!!");
                        System.exit(1);
                    }
                }
            }
               if (a == 'X') {
                if (e == 'X') {
                    if (f == 'X') {
                        System.out.println("X wins!!!");
                        System.exit(1);
                    }
                }
            }
                if (a == 'X') {
                    if (d == 'X') {
                        if (g == 'X') {
                            System.out.println("X wins!!!");
                            System.exit(1);
                    }
                }
            }
                if (b == 'X') {
                    if (e == 'X') {
                        if (h == 'X') {
                            System.out.println("X wins!!!");
                            System.exit(1);
                    }
                }
            }
                if (c == 'X') {
                    if (f == 'X') {
                        if (i == 'X') {
                            System.out.println("X wins!!!");
                            System.exit(1);
                    }
                }
            }
                if (c == 'X') {
                    if (e == 'X') {
                        if (g == 'X') {
                            System.out.println("X wins!!!");
                            System.exit(1);
                    }
                }
            }

            //O

            if (a == 'O') {
                if (b == 'O') {
                    if (c == 'O') {
                        System.out.println("O wins!!!");
                        System.exit(1);
                    }
                }
            }
             if (d == 'O') {
                if (e == 'O') {
                    if (f == 'O') {
                        System.out.println("O wins!!!");
                        System.exit(1);
                    }
                }
            }
              if (g == 'O') {
                if (h == 'O') {
                    if (i == 'O') {
                        System.out.println("O wins!!!");
                        System.exit(1);
                    }
                }
            }
               if (a == 'O') {
                if (e == 'O') {
                    if (f == 'O') {
                        System.out.println("O wins!!!");
                        System.exit(1);
                    }
                }
            }
                if (a == 'O') {
                    if (d == 'O') {
                        if (g == 'O') {
                            System.out.println("O wins!!!");
                            System.exit(1);
                    }
                }
            }
                if (b == 'O') {
                    if (e == 'O') {
                        if (h == 'O') {
                            System.out.println("O wins!!!");
                            System.exit(1);
                    }
                }
            }
                if (c == 'O') {
                    if (f == 'O') {
                        if (i == 'O') {
                            System.out.println("O wins!!!");
                            System.exit(1);
                    }
                }
            }
                if (c == 'O') {
                    if (e == 'O') {
                        if (g == 'O') {
                            System.out.println("O wins!!!");
                            System.exit(1);
                    }
                }
            }            
            if (j == 9 && a != ' '&& b != ' '&& c != ' '&& d != ' '&& e != ' ' && f != ' '&& g != ' '&& h != ' '&& i != ' '){
                System.out.println("Tie Game");
                System.exit(1);

            }


            System.out.println(gameboard);
    
        }}




    }

程序应该让我更改 a-i 的值并将其显示在游戏板上,而不是它只是吐出 else 语句。

java tic-tac-toe
1个回答
0
投票

你真的需要学习如何桌上检查你的代码(坐下来,如果一张纸和一支笔,并通过你的代码写下状态)

例如,让我们看看...

if (j % 2 == 0) {
    System.out.println("\n Invalid attempt, try again");
} else if (x == 1 && y == 1 && a != 'X' && a != 'O') {
    a = ('X');
    j++;
} else if (x == 1 && y == 2 && b != 'X' && b != 'O') {
    b = ('X');
    j++;
} else if (x == 1 && y == 3 && c != 'X' && c != 'O') {
    c = ('X');
    j++;
} else if (x == 2 && y == 1 && d != 'X' && d != 'O') {
    d = ('X');
    j++;
} else if (x == 2 && y == 2 && e != 'X' && e != 'O') {
    e = ('X');
    j++;
} else if (x == 2 && y == 3 && f != 'X' && f != 'O') {
    f = ('X');
    j++;
} else if (x == 3 && y == 1 && g != 'X' && g != 'O') {
    g = ('X');
    j++;
} else if (x == 3 && y == 2 && h != 'X' && h != 'O') {
    h = ('X');
    j++;
} else if (x == 3 && y == 3 && i != 'X' && i != 'O') {
    i = ('X');
    j++;
} else {
    System.out.println("you cant pick that space its already been taken");
}

//For O
if (j % 2 != 0) {
    System.out.println("\n Invalid attempt, try again");
} else if (x == 1 && y == 1 && a != 'X' && a != 'O') {
    a = ('O');
    j++;
} else if (x == 1 && y == 2 && b != 'X' && b != 'O') {
    b = ('O');
    j++;
} else if (x == 1 && y == 3 && c != 'X' && c != 'O') {
    c = ('O');
    j++;
} else if (x == 2 && y == 1 && d != 'X' && d != 'O') {
    d = ('O');
    j++;
} else if (x == 2 && y == 2 && e != 'X' && e != 'O') {
    e = ('O');
    j++;
} else if (x == 2 && y == 3 && f != 'X' && f != 'O') {
    f = ('O');
    j++;
} else if (x == 3 && y == 1 && g != 'X' && g != 'O') {
    g = ('O');
    j++;
} else if (x == 3 && y == 2 && h != 'X' && h != 'O') {
    h = ('O');
    j++;
} else if (x == 3 && y == 3 && i != 'X' && i != 'O') {
    i = ('O');
    j++;

} else {
    System.out.println("you cant pick that space its already been taken");
}

这看起来没问题,但是,让我们逐步完成它,首先循环...

  • j == 0
  • x
    y
    1
  • (j % 2 == 0)
    true
    ,打印
    "Invalid attempt, try again"
  • (j % 2 != 0)
    false
  • (x == 1 && y == 1 && a != 'X' && a != 'O')
    true
    ,将
    a
    设置为
    'O'
    ,将
    j
    加一(
    j
    ==
    1

下一个循环...

  • j == 1
  • x
    1
    y
    2
  • (j % 2 == 0)
    false
  • (x == 1 && y == 2 && b != 'X' && b != 'O')
    true
    ,将
    b
    设置为
    'X'
    ,将
    j
    加一(
    j
    ==
    2
    !!)
  • (j % 2 != 0)
    false
  • (x == 1 && y == 2 && b != 'X' && b != 'O')
    false
    !!
  • 打印
    "you cant pick that space its already been taken"

现在这是你的核心问题。在每个

j
语句中递增
if
将导致下一个块认为轮到它了。

你可以做的是使用单个

if-elseif-else
块,但是,基于
j
的值,设置一个
player
字符,例如...

char player = 'X';
while (j < 9) {
    if (j % 2 == 0) {
        player = 'X';
    } else {
        player = 'O';
    }

    System.out.println("Current player: " + player);
    System.out.println("What is your x value");
    int x = scanner.nextInt();
    scanner.nextLine();
    System.out.println("What is your y value");
    int y = scanner.nextInt();
    scanner.nextLine();

    if (x == 1 && y == 1 && a != 'X' && a != 'O') {
        a = player;
        j++;
    } else if (x == 1 && y == 2 && b != 'X' && b != 'O') {
        b = player;
        j++;
    } else if (x == 1 && y == 3 && c != 'X' && c != 'O') {
        c = player;
        j++;
    } else if (x == 2 && y == 1 && d != 'X' && d != 'O') {
        d = player;
        j++;
    } else if (x == 2 && y == 2 && e != 'X' && e != 'O') {
        e = player;
        j++;
    } else if (x == 2 && y == 3 && f != 'X' && f != 'O') {
        f = player;
        j++;
    } else if (x == 3 && y == 1 && g != 'X' && g != 'O') {
        g = player;
        j++;
    } else if (x == 3 && y == 2 && h != 'X' && h != 'O') {
        h = player;
        j++;
    } else if (x == 3 && y == 3 && i != 'X' && i != 'O') {
        i = player;
        j++;
    } else {
        System.out.println("A you cant pick that space its already been taken");
    }

(请注意,我用

IO
替换了您的
Scanner
类,因为
IO
不是标准的 Java 库类)

哦最后,你需要更新

gameboard

gameboard = ("    1  2  3\n1   " + a + "| " + b + " | " + c + "\n   - - - - -" + "\n2   " + d + "| " + e + " | " + f + "\n   - - - - -" + "\n3   " + g + "| " + h + " | " + i + "\n   - - - - -");

否则永远不会改变

© www.soinside.com 2019 - 2024. All rights reserved.