需要更好的逻辑TicTacToe [英] Need better logic for TicTacToe

查看:105
本文介绍了需要更好的逻辑TicTacToe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java Swing实现一个TicTacToe GUI应用程序。获胜的当前逻辑是:

I'm implementing a TicTacToe GUI app using Java Swing. The current logic for a win is:

JButton[] button = new JButton[9];
boolean win = false;
//logic for player's win
if (button[0].getText() == button[1].getText() && button[1].getText() == button[2].getText() && button[0].getText() != "") {
    Won = true;
} else if (button[3].getText() == button[4].getText() && button[4].getText() == button[5].getText() && button[3].getText() != "") {
    Won = true;
} else if (button[6].getText() == button[7].getText() && button[7].getText() == button[8].getText() && button[6].getText() != "") {
    Won = true;
} else if (button[0].getText() == button[3].getText() && button[3].getText() == button[6].getText() && button[0].getText() != "") {
    Won = true;
} else if (button[1].getText() == button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText() != "") {
    Won = true;
} else if (button[2].getText() == button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText() != "") {
    Won = true;
} else if (button[0].getText() == button[4].getText() && button[4].getText() == button[8].getText() && button[0].getText() != "") {
    Won = true;
} else if (button[2].getText() == button[4].getText() && button[4].getText() == button[6].getText() && button[2].getText() != "") {
    Won = true;
}

这看起来有点笨拙。玩家的胜利是否有更好的逻辑?

This looks a bit clumsy. Is there a better logic for a player's win?

推荐答案

那么,你所拥有的(基本上)是一个二维矩阵(好吧,它是一个虚拟的矩阵,但这使它更简单考虑... ...

So, what you have (essentially) is a 2D matrix (okay, it's a virtual one, but that makes it simpler to think about)...

您需要的是一种方法,您可以从给定的行和列开始搜索此矩阵的三个单元格。

What you need is a means by which you can search three cells of this matrix, starting at a given row and column.

因此,给定一个起点,您还需要提供有关您要搜索哪个方向的信息(有时候,您想要向后搜索),可能是......

So, given a starting point, you also need to supply information about which direction you want to search in (as sometimes, you want to search backwards), maybe something like...

public boolean matrixWin(int row, int col, int rowDelta, int colDelta) {

    boolean win = false;
    String value = button[(row * 3) + col].getText();
    if (!value.isEmpty()) {
        win = true;
        for (int count = 1; count < 3; count++) {
            row += rowDelta;
            col += colDelta;
            String test = button[(row * 3) + col].getText();
            if (test.isEmpty() || !test.equals(value)) {
                win = false;
                break;
            }
        }
    }
    return win;

}

这基本上得到第一个单元格的值,如果它是不是空的,它开始在矩阵中移动,基于delta值,并检查每个其他单元格以查看它是否为空或是否与第一个单元格的值匹配。

This basically gets the value of the first cell, if it's not empty, it begins to move through the matrix, based on the delta values, and checks each other cell to see if it's not empty or if it matches the value of the first cell.

好的,这很酷,但是我想懒得尝试设置我想检查的每个排列,所以相反,我会做一些辅助方法......

Okay that's cool, but I'm way to lazy to try and set up EVERY permutation I might like to check for, so instead, I'd make some helper methods...

基本上,您要检查三行是否为水平获胜,三列是否为垂直获胜以及左或右对角线...

Basically, you want to check the three rows for a horizontal win, the three columns for a vertical win and the left or right diagonals...

public boolean horizontalWin(int row) {
    return matrixWin(row, 0, 0, 1);
}

public boolean verticalWin(int col) {
    return matrixWin(0, col, 1, 0);
}

public boolean leftDiagonalWin() {
    return matrixWin(0, 0, 1, 1);
}

public boolean rightDiagonalWin() {
    return matrixWin(0, 2, 1, -1);
}

但即便如此,除非我想知道哪一行/列/对角线实际上赢了,我会让它变得更容易......

But even then, unless I want to know which row/col/diagonal ACTUALLY won, I'd make it even easier...

public boolean horizontalWins() {

    int row = 0;
    boolean win = false;
    do {
        win = horizontalWin(row);
        row++;
    } while (row < 3 && !win);

    return win;

}

public boolean verticalWins() {

    int col = 0;
    boolean win = false;
    do {
        win = verticalWin(col);
        col++;
    } while (col < 3 && !win);

    return win;

}

然后你可以开始......

Then you could get down to...

public boolean didWin() {

    return horizontalWins() || verticalWins() || leftDiagonalWin() || rightDiagonalWin();

}

是的,一个方法调用,但你还有力量确切地确定如何

Yeah, one method call, but you still have the power to determine exactly "how"

这篇关于需要更好的逻辑TicTacToe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆