测试tic tac toe win condition

时间:2013-08-31 12:43:49

标签: java algorithm performance

我正在寻找最有效的 java 方式来测试是否有人在tic tac toe中获胜。数据是二维数组,如此...

char[][] ticTacToe = 
    {{'X',' ','O'},
     {'O','X','O'},
     {'X',' ','X'},};

我知道这不是初始化数组的专业方法,但我只是在这里测试。

我现在能做的最好的是一个详尽的if / else树。 这是其中一棵树......

if (ticTacToe[1][1] == 'X'){
        if (ticTacToe[0][0] == 'X'){
            if (ticTacToe[2][2] == 'X'){
                System.out.println("X wins");
            }
        }
        else if (ticTacToe[0][1] == 'X'){
             if (ticTacToe[2][1] == 'X'){
                System.out.println("X wins");
            }
        }
        else if (ticTacToe[1][0] == 'X'){
             if (ticTacToe[1][2] == 'X'){
                System.out.println("X wins");
            }
        }
        else if (ticTacToe[2][0] == 'X'){
             if (ticTacToe[0][2] == 'X'){
                System.out.println("X wins");
            }
        }
    }

这个只关心中间的内容

这是非常基础的,我想在最小化代码行的情况下对其进行改进。

5 个答案:

答案 0 :(得分:16)

只是为了好玩,保留两个数字,从零开始,一个用于X,一个用于O。通过or移动来更新它们。要检查获胜者,请先and,然后xor使用面具。

277 & 273 ^ 273
0  ==> we have a winner.

276 & 273 ^ 273
1  ==> not.

277 == parseInt(“100010101”,2)
 273 == parseInt(“100010001”,2)
276 == parseInt(“100010100”,2)

为了更有趣,这是一个在您喜欢的JavaScript控制台中播放O的示例:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
  </head>
  <body>
    <script>
    var x = 0, o = 0, count = 0, w = 0
        ws = [0007,0070,0700,0111,0222,0444,0124,0421]
    function t1(v){
        var w1 = 0
        for (var i in ws)
            w1 |= !(v & ws[i] ^ ws[i])
        return w1
    }
    function t(i){
        var ot = count % 2, m = 1 << (9 - i), bd = x | o
        if (!ot && (i > 9 || i < 1 || i != Math.floor(i)))
            return "Out of bounds."
        else if (m & bd)
            return "Position taken."
        if (ot){
            var n1 = 0, a1 = -2
            while (bd & (1 << n1))
                n1++
            var n = n1
            while (n1 < 9){
                var m1 = 1 << n1
                if (!(bd & m1)){
                    var bt = -mx(x,o | m1,count + 1)
                     if (bt > a1){
                         a1 = bt
                         n = n1
                     }
                }
                n1++
            }
            w = t1(o |= 1 << n)
        }
        else
            w = t1(x |= m)
        var b = "\n", p = 0400
        while (p > 0){
            if (p & x)
                b += "X"
            else if (p & o)
                b += "O"
            else b += "."
            if (p & 0110)
                b += "\n"
            p >>= 1
        }
        if (w)
            b += "\n\n" + (ot ? "O" : "X") + " wins!"
        else if (!(bd ^ 0777))
            b += "\n\nDraw."
        if (!ot){
            console.log(b + '\n\n"""')
            count++
            console.log(t(-1))
            count++
        }
        else
            return b + "\n"
        return '"'
    }
    function mx(x1,o1,c1){
        var ot1 = c1 % 2, w1 = ot1 ? t1(x1) : t1 (o1),
              b1 = x1 | o1, p = 0400
        if (w1)
            return -1
        if (!(b1 ^ 0777))
            return 0
        var a = -2
        while (p > 0){
            if (!(b1 & p))
                a = Math.max(a,-mx(ot1 ? x1 : x1 | p,ot1 ? o1 | p : o1,c1 + 1))
            p >>= 1
        }
        return a
    }
    console.log('              Plays O!'
            + '\nTo play, type t(MOVE); MOVE is from 1-9')
    </script>
  </body>
</html>

答案 1 :(得分:8)

将棋盘标记为3x3 magicSquare,当你的总和为15时,你就赢了。

enter image description here

答案 2 :(得分:2)

它有点冗长,但我认为这可能是最有效的方法(除非有人能想出一种聪明的方法来同时检查两个对角线)。

public class TicTacToe
{
    char[][] ticTacToe = 
    {{'X',' ','O'},
     {'O','X','O'},
     {'X',' ','X'},};

    private Character winner = null;

    public Character getWinner()
    {
        return this.winner;
    }

    public boolean isSolved()
    {
        this.checkSolved();
        return this.winner != null;
    }

    private void checkSolved()
    {
        for(int i = 0; i < ticTacToe.length; i++)
        {
            Character win = checkRow(i);
            if(win != null || (win = checkColumn(i)) != null)
            {
                this.winner = win;
                return;
            }
        }
        //Check diagonal top left to bottom right
        if(this.ticTacToe[0][0] != ' ')
        {
            if(this.ticTacToe[0][0] == this.ticTacToe[1][1] &&
               this.ticTacToe[1][1] == this.ticTacToe[2][2])
            {
                this.winner = this.ticTacToe[0][0];
            }
        }
        //Check diagonal top right to bottom left
        else if(this.ticTacToe[0][2] != ' ')
        {
            if(this.ticTacToe[0][2] == this.ticTacToe[1][1] &&
               this.ticTacToe[1][1] == this.ticTacToe[2][0])
            {
                this.winner = this.ticTacToe[0][2];
            }
        }
    }

    private Character checkRow(int row)
    {
        if(this.ticTacToe[row][0] == ' ')
        {
            return null;
        }
        if(this.ticTacToe[row][0] == this.ticTacToe[row][1] &&
           this.ticTacToe[row][1] == this.ticTacToe[row][2])
        {
            return this.ticTacToe[row][0];
        }
        return null;
    }

    private Character checkColumn(int column)
    {
        if(this.ticTacToe[0][column] == ' ')
        {
            return null;
        }
        if(this.ticTacToe[0][column] == this.ticTacToe[1][column] &&
           this.ticTacToe[1][column] == this.ticTacToe[2][column])
        {
            return this.ticTacToe[column][0];
        }
        return null;
    }

    public static void main(String[] args)
    {
        TicTacToe ttt = new TicTacToe();
        if(ttt.isSolved())
        {
            System.out.println(ttt.getWinner());  // X
        }
    }
}

答案 3 :(得分:1)

对于一个玩家,比如'x',有8种方法可以获胜,每种方式对应于一行/列/对角线中的3'x'。 因此,您可以创建一个长度为8的数组,每个项目对应于该行/列/对角线中的“x”数。 当玩家选择移动时,您更新阵列并检查阵列中是否存在3。 虽然它需要更多空间,但更容易推广到大型电路板。

答案 4 :(得分:0)

有四种不同的方法可以赢得嘀嗒声:

  1. 形成一条水平线
  2. 形成一条垂直线
  3. 从左上角到右下角形成对角线
  4. 从左下角到右上角形成对角线
  5. 所有这四个胜利条件都可以通过for循环解决。该解决方案的优点是它可以应用于任何矩阵大小。