对Tic Tac Toe的建议 [英] Suggestion on Tic Tac Toe

查看:118
本文介绍了对Tic Tac Toe的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Tic-Tac-Toe游戏设计我的实施策略。由于这是我的第一个游戏实现,我有点困惑,需要一些一般性的指示。

I am designing my implementation strategy for Tic-Tac-Toe game. Since this is my 1st game implementation, I am a bit confused and need some general pointers.

现在,Tic-Tac-Toe的获胜组合总数是8.目前,我计划将这些获胜组合存储在一个数组中。一旦最终用户进行了至少3次移动,我将通过比较Player对阵此阵列的当前位置来开始检查玩家是否赢得了游戏。但是,我确信这不是检查玩家是否有获胜组合的有效方法。

Now, the total number of winning combinations in a Tic-Tac-Toe are 8. Currently, I plan to store these winning combinations in an array. Once the end user has made at least 3 moves, I would start checking if the Player has won the game by comparing the current positions used by a Player against this array. However, I am sure this is not an efficient way to check if the player has a winning combination.

有人可以建议我如何设计游戏的逻辑吗?

Can anyone please suggest me on how to go about with design the logic for the game?

推荐答案

不要担心效率。我写了一个回溯解决方案,只有549,945个可能的游戏状态。在笔记本电脑上运行这些操作只需不到0.25秒。这是我的逻辑,看看游戏是否结束 - 显然效率不高,但没关系:

Don't worry about efficiency. I wrote a backtracking solution and there are only 549,945 possible game states. It takes less than 0.25 seconds to run through these on my laptop. Here was my logic to see if the game was over - clearly not very efficient, but it doesn't matter:

private boolean isWinningMove(int row, int col) {
    int piece = board[row][col];

    // check current row
    boolean w = true;
    for (int x = 0; x < 3; x++) { w = w && (board[row][x] == piece); }
    if (w) { return true; }

    // check current column
    w = true;
    for (int x = 0; x < 3; x++) { w = w && (board[x][col] == piece); }
    if (w) { return true; }

    // check 0,0 diagonal
    w = true;
    for (int x = 0; x < 3; x++) { w = w && (board[x][x] == piece); }
    if (w) { return true; }

    // check 0,2 diagonal
    w = true;
    for (int x = 0; x < 3; x++) { w = w && (board[x][2 - x] == piece); }
    return w;
}

这是我的结果,与维基百科页面上的数据匹配tic-tac -toe:

Here were my results, which match data on the Wikipedia page for tic-tac-toe:

Moves Simulated: 549945
Draws=46080   Player1-Wins=131184   Player2-Wins=77904
Perfect Strategy Implies: Always a tie.

Games won in 0 moves? 0
Games won in 1 moves? 0
Games won in 2 moves? 0
Games won in 3 moves? 0
Games won in 4 moves? 0
Games won in 5 moves? 1440
Games won in 6 moves? 5328
Games won in 7 moves? 47952
Games won in 8 moves? 72576
Games won in 9 moves? 81792

这篇关于对Tic Tac Toe的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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