【问题标题】:Tic Tac Toe game wont loop+ or execute rules correctly井字游戏不会循环+或正确执行规则
【发布时间】:2020-05-09 13:57:25
【问题描述】:

基本上,我正在尝试在 2 名人类玩家之间制作井字游戏作为初学者任务。在控制台中运行程序时遇到了 2 个问题。第一个问题是我已经设置的循环让玩家可以一个接一个地进行多次,只让每个玩家只有一次。另一个问题是我设置了一套规则,如果你连续获得 3 个,游戏就会结束。问题是游戏每次都说“游戏”结束。

import java.util.Scanner;

public class Game {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);

        int SIDE = 3;
        char[][] grid = new char[SIDE][SIDE];
        for (int row = 0; row < SIDE; row++) {
            for (int col = 0; col < SIDE; col++) {
                grid[row][col] = '_';
            }
        }
        for (int row = 0; row < SIDE; row++) {
            for (int col = 0; col < SIDE; col++) {
                System.out.printf("%3c", grid[row][col]);
            }
            System.out.printf("%n");
        }
        boolean playing = true;
        while (playing) {
            System.out.println("Player1: Please enter coordinates for your move");

【问题讨论】:

  • 您的两种方法都是从创建一个新网格开始的,因此GameOver() 在所有方向上都正确地看到了相同的(空)值。尝试 1) 将游戏网格作为参数传递或 2) 将游戏网格提高一级,以便两种方法都可以访问它。 (请缩进你的代码)

标签: java arrays loops


【解决方案1】:

GameOver 方法在调用时正在创建一个新网格。因此,当检查行、列和对角线中的值时,会发现这些值是相同的,因为它们都是空的。给 GameOver 方法一个新参数,它接受网格并使用它。不过要小心这一点,因为一些行和列现在都有一个“_”作为它的值。因此,正如您在对角线条件中检查“_”一样,您应该在行和列 if 语句中检查它。

这显示了空网格数组的内容:

此外,您应该缩进代码以使其更易于阅读和调试。我还建议制作一种打印网格的方法,而不是使用重复的嵌套 for 循环来打印它。

【讨论】:

    【解决方案2】:

    一些变化

    • 我修改了gameOver() 方法,这显然是问题所在: 实际上您没有通过当前网格,但您创建了 总是一个新的,因此,控制很容易被扭曲。
    • 我通过替换布尔变量 playing 修复了 while 循环 使用简单的“break”方法:这意味着如果玩家 1 获胜,那么 它立即退出循环;
    • 我改进了插入;
    • 我创建了一个非常方便的方法,可以节省您每次 写一个用于显示网格:showGrid()

    代码

    public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner input = new Scanner(System.in);
    
            // GRID set up
            int SIDE = 3;
            char[][] grid = new char[SIDE][SIDE];
            for (int row = 0; row < SIDE; row++) {
                for (int col = 0; col < SIDE; col++) {
                    grid[row][col] = '_';
                }
            }
    
            while (true) {
                showGrid(grid, SIDE);
                System.out.println("Player1: Please enter row coordinate for your move");
                int row1 = input.nextInt();
                System.out.println("Player1: Please enter column coordinate *for-loop* your move");
                int col1 = input.nextInt();
                grid[row1 - 1][col1 - 1] = 'x';
                if (gameOver(grid, row1 - 1, col1 - 1)) {
                    showGrid(grid, SIDE);
                    System.out.println("Game Over");
                    break;
                }
                showGrid(grid, SIDE);
                System.out.println("Player2: Please enter row coordinate for your move");
                int row2 = input.nextInt();
                System.out.println("Player2: Please enter column coordinate for your move");
                int col2 = input.nextInt();
                grid[row2 - 1][col2 - 1] = 'O';
                if (gameOver(grid, row1 - 1, col1 - 1)) {
                    showGrid(grid, SIDE);
                    System.out.println("Game Over");
                    break;
                }
            }
    
        }
    
        public static boolean gameOver(char[][] grid, int rMove, int cMove) {
            if (grid[0][cMove] == grid[1][cMove]
                    && grid[0][cMove] == grid[2][cMove])
                return true;
            if (grid[rMove][0] == grid[rMove][1]
                    && grid[rMove][0] == grid[rMove][2])
                return true;
            //Check diagonal victory
            if (grid[0][0] == grid[1][1] && grid[0][0] == grid[2][2]
                    && grid[1][1] != '_')
                return true;
            if (grid[0][2] == grid[1][1] && grid[0][2] == grid[2][0]
                    && grid[1][1] != '_')
                return true;
            return false;
        }
    
        public static void showGrid(char[][] grid, int side) {
            for (int row = 0; row < side; row++) {
                for (int col = 0; col < side; col++) {
                    System.out.printf("%3c", grid[row][col]);
                }
                System.out.printf("%n");
            }
        }
    

    请记住,方法名称始终使用 驼峰式

    【讨论】:

      【解决方案3】:
      while(playing){
                  System.out.println ("Player1: Please enter coordinates for your move");
                  int row1 = input.nextInt();
                  int col1 = input.nextInt();
                  grid[row1-1][col1-1] = 'x';
                  if (!GameOver(row1-1, col1-1)) {
                      playing = false;
                      System.out.println("Game Over");
                  }
      
      
                  for (int row = 0; row < SIDE; row++) {
                      for (int col = 0; col < SIDE; col++) {
                          System.out.printf("%3c", grid [row][col]);
                      }
                      System.out.printf("%n");
                  }
                  System.out.println ("Player 2: Please enter coordinates for your move");
                  int row2 = input.nextInt();
                  int col2 = input.nextInt();
                  grid[row2-1][col2-1] = 'O';
                  if (!GameOver(row1-1, col1-1)) {
                      playing = false;
                      System.out.println("Game Over");
                  }
                  for (int row = 0; row < SIDE; row++) {
                      for (int col = 0; col < SIDE; col++) {
                          System.out.printf("%3c", grid [row][col]);
                      }
                      System.out.printf("%n");
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多