当前位置: 首页 > 知识库问答 >
问题:

我如何在我的抽水马龙项目中确定获胜者

干京
2023-03-14

早期文章:如何使tictactoe程序可伸缩

我已经尝试使一个Tic Tac Toe程序(人机)可伸缩(板大小可以改变)。我之前有过一些大问题,但大部分都解决了。

游戏的规则是基本的Tictactoe,但有一个不同的规则是,无论棋盘有多大(当>=5)玩家或计算机只需要连续五个分数就能获胜。

现在我的程序唯一的破局问题是决定谁赢了这场比赛。这场比赛现在只可能以‘平局’结束。(我还没有实现“>=5”)。

具体的问题解释是,我需要为类似“计算机赢”和/或“玩家赢”的内容确定赢家和结束屏幕。

package tictactoe;

import java.util.Scanner;
import java.util.Random;

public class TicTacToe {

    public static int size;
    public static char[][] board;
    public static int score = 0;
    public static Scanner scan = new Scanner(System.in);

    /**
     * Creates base for the game.
     * 
     * @param args the command line parameters. Not used.
     */
    public static void main(String[] args) {

        System.out.println("Select board size");
        System.out.print("[int]: ");
        size = Integer.parseInt(scan.nextLine());

        board = new char[size][size];
        setupBoard();

        int i = 1;

        while (true) {
            if (i % 2 == 1) {
                displayBoard();
                getMove();
            } else {
                computerTurn();
            }

            // isWon()
            if (isDraw()) {
                System.err.println("Draw!");
                break;
            }

            i++;
        }

    }

    /**
     * Checks for draws.
     *
     * @return if this game is a draw
     */
    public static boolean isDraw() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (board[i][j] == ' ') {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * Displays the board.
     * 
     * 
     */
    public static void displayBoard() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                System.out.printf("[%s]", board[i][j]);
            }

            System.out.println();
        }
    }

    /**
     * Displays the board.
     * 
     * 
     */
    public static void setupBoard() {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                board[i][j] = ' ';
            }
        }
    }

    /*
     * Checks if the move is allowed. 
     *
     *
     */
    public static void getMove() {

        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.printf("ROW: [0-%d]: ", size - 1);
            int x = Integer.parseInt(sc.nextLine());
            System.out.printf("COL: [0-%d]: ", size - 1);
            int y = Integer.parseInt(sc.nextLine());

            if (isValidPlay(x, y)) {
                board[x][y] = 'X';
                break;
            }
        }
    }

    /*
     * Randomizes computer's turn - where it inputs the mark 'O'.
     *
     *
     */
    public static void computerTurn() {
        Random rgen = new Random();  // Random number generator                        

        while (true) {
            int x = (int) (Math.random() * size);
            int y = (int) (Math.random() * size);

            if (isValidPlay(x, y)) {
                board[x][y] = 'O';
                break;
            }
        }
    }

    /**
     * Checks if the move is possible.
     * 
     * @param inX
     * @param inY
     * @return 
     */
    public static boolean isValidPlay(int inX, int inY) {

        // Play is out of bounds and thus not valid.
        if ((inX >= size) || (inY >= size)) {
            return false;
        }

        // Checks if a play have already been made at the location,
        // and the location is thus invalid.  
        return (board[inX][inY] == ' ');
    }
}

共有1个答案

宰父志新
2023-03-14

您已经有了要玩的循环,因此,在每次迭代中,按照检查游戏是否为draw()的相同方式,也检查一些玩家是否赢了:

while (true) {
    if (i % 2 == 1) {
        displayBoard();
        getMove();
    } else {
        computerTurn();
    }

    // isWon()
    if (isDraw()) {
        System.err.println("Draw!");
        break;
    } else if (playerHasWon()){
        System.err.println("YOU WIN!");
        break;
    } else if (computerHasWon()) {
        System.err.println("Computer WINS!\nYOU LOOSE!!");
        break;
    }

    i++;
}

在创建所需的方法之后:

public static boolean playerHasWon() {
    boolean hasWon = false;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
              // check if 5 in a line
        }
    }

    return hasWon ;
}

public static boolean computerHasWon() {
    boolean hasWon = false;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
              // check if 5 in a line
        }
    }

    return hasWon ;
}

当然,下一个问题是如何创建这个方法??不知道你是否有这个问题,但做一个快速检查这里,这里和这里,你会发现一些想法。

添加:

为了澄清,我将使一个函数返回int而不是booleans,以使用一些常量检查游戏是否完成:

private final int DRAW = 0;
private final int COMPUTER = 1;
private final int PLAYER = 2;

private int isGameFinished() {
    if (isDraw()) return DRAW;
    else if (computerHasWon()) return COMPUTER;
    else if (playerHasWon()) return PLAYER;
}

然后简单地检查开关情况(在这里检查如何中断while insite while)

loop: while (true) {
    // other stufff
    switch (isGameFinished()) {
    case PLAYER:
        System.err.println("YOU WIN!");
        break loop;
    case COMPUTER:
        System.err.println("Computer WINS!\nYOU LOOSE!!");
        break loop;
    case DRW:       
        System.err.println("IT'S A DRAW");
        break loop;
}
 类似资料:
  • 所以我试图用Java编写一个tic tac toe游戏。大部分都完成了,但是,如果有人选择了一个已经被占用的空间,我不能归还无效的移动。 下面是我想弄清楚的代码。我认为既然空间是由数字0表示的(我的教授告诉我们的),有 在if语句中将阻止播放器重复该空格。

  • 我尝试在Android Studio中通过常用的refactor->rename方法更改项目名称,但由于它并没有真正更改项目名称,它只是在标题上添加了一个附加项,所以我尝试只更改目录名称本身。因为我也需要改变。但在我做了这件事之后,我尝试重新打开Android Studio,现在它冻结了,弹出了。然后我不得不使用活动监视器来强制退出Android Studio。 那么,有没有合适的方法来改变你现有

  • 问题内容: 我不明白如何在四连冠中找到获胜方式,请告诉我应该怎么想以及如何在四连冠中找到赢家。我应该如何使用for循环来找到他们。我应该如何使用方法寻找赢家? 问题答案: 在一个简短的Google之后,会弹出以下代码:https : //codereview.stackexchange.com/questions/100917/connect- four-game-in- java 或http:/

  • 我有一个pycharm项目,大概还有一个Django项目。也许它们是一回事,也许不是——我不确定区别。 总之,在我的settings.py文件中(该文件位于< code>project目录的根目录下,我认为这是我的pycharm项目),我有: 这是否意味着< code>dumpstown是我的项目名称?还是我的pycharm项目名?有什么区别?因为我还有一个名为< code>dumpstownap

  • 我目前有一个WAR项目和一个EJB项目,以及一个将它们捆绑在一起的EAR项目。 实际上,它正是NetBeans示例中的project Maven>Enterprise应用程序。 一开始,我的编译器抱怨它找不到这个NewSessionBean类,因为它们在不同的项目中,这个类是足够公平的。 通过Netbeans的“提示”,我看到Netbeans建议我将EJB项目添加为依赖项,所以我就这样做了。 我的