简体   繁体   English

井字游戏数组或列表?

[英]Tic-Tac-Toe arrays or lists?

I have read a book and am trying to make a game of tic-tac-toe as one of my first projects. 我已经读过一本书,并尝试制作井字游戏,这是我的第一个项目。 Im having a problem making a function that checks whether the move made is a winning move or not. 我在制作一个功能来检查所进行的举棋是否为获胜举牌时遇到问题。 I just need a push in the right direction. 我只需要向正确的方向推进即可。

For my tic-tac-toe board im using a 3x3 array. 对于我的井字游戏板,即时通讯使用3x3阵列。 There are a possible 8 winning combinations. 可能有8种获胜组合。 So im thinking of making an array of the combinations and having the program check if any of the moves on the board match those in the winning moves. 因此,我想制作一系列组合并让程序检查棋盘上的任何移动是否与获胜移动相匹配。

I would like some advice whether an array would work or if something else would work better like vectors or lists. 我想要一些建议,以判断数组是否可以工作,或者向量或列表是否可以更好地工作。

An array would work nicely in this case because you have a static, unchanging amount of things to store. 在这种情况下,数组可以很好地工作,因为您要存储的静态内容是不变的。 This is a perfect use case for them. 对于他们来说,这是一个完美的用例。

Lists and vectors are more appropriate when the things you are storing at dynamic, and you don't know the size upfront. 当您动态存储事物时,列表和向量更合适,并且您不知道预先的大小。 In your case, you know the size upfront, so you should use the simplest data structure that will get the job done (arrays). 在您的情况下,您预先知道大小,因此您应该使用最简单的数据结构来完成工作(数组)。

Uh… it's really hard to say. 嗯...真的很难说。 You can definitely use both. 您绝对可以同时使用两者。 I myself probably would to this with static array, but as you are learning I recommend you to start with static array as it's more natural, and then try to do it with Vector, so you'll see the difference. 我本人可能会使用静态数组来做到这一点,但是随着您的学习,我建议您先从静态数组开始,因为它更自然,然后尝试使用Vector进行处理,这样您会发现其中的区别。 Also maybe you'll want to make a class TicTacToeBoard that can compare an instances of itself (as you need to check if the strategy is winning). 另外,也许您可​​能想制作一个TicTacToeBoard类,可以比较其自身的实例(因为您需要检查策略是否获胜)。 And when you have a class with all the methods you can change the implementation between array and Vector, and you'll see also the idea of encapsulation : so from the outside the class will be the same and from the inside you can use different solutions. 并且当您拥有一个具有所有方法的类时,可以在array和Vector之间更改实现,并且还将看到封装的思想:因此,从外部看,该类将是相同的,从内部来看,您可以使用不同的解决方案。

Using 3x3 array to store your current board is ok. 使用3x3阵列存储当前的电路板是可以的。 Using array to store possible winning combinations is NOT ok. 使用数组存储可能的获胜组合不是很好。 Although this is your first program, it never hurts to start learning good habits. 尽管这是您的第一个程序,但开始学习良好习惯绝不会有任何伤害。 It is probably true that with this problem you can enumerate all possible combinations. 可能确实存在这个问题,您可以列举所有可能的组合。 However, what would you do if you wanted to code checkers? 但是,如果您想对检查程序进行编码,该怎么办? Number of possible winning scenarios are enormous. 可能的获胜方案数量巨大。 How about chess? 棋呢? There's not enough memory in all computers available today to store all possible winning moves for chess. 如今,所有可用的计算机都没有足够的内存来存储国际象棋的所有可能的获胜手法。

Much better approach is to write a function that will check those winning conditions for you. 更好的方法是编写一个函数来检查您的获胜条件。 To make it simple, I recommend it writing like this. 为简单起见,我建议这样编写。 To not make it too simple, I will write that as pseudo-code: 为了避免过于简单,我将其编写为伪代码:

// returns true if specified side won
// board - a pointer to your array
// player - specifies if you want to check if X or O is the winner
bool game_won(board, player)
{
    For each row, check if each field is equal to player. 
        if yes return true.
    For each column, check if each field is equal to player. 
        if yes return true.
    Check if each field on diagonal is equal to player. 
        if yes return true.

    // winning conditions not met, let's keep playing
    return false
}

After player makes each move, you should call game_won() and it will check for you if that move make the player a winner. 玩家做出每一个举动之后,您应该调用game_won(),它将检查您是否通过该举动使玩家成为赢家。

ps There are 8, not 10 possible winning moves for each player: 3 horizontal, 3 vertical and 2 diagonals. ps每个玩家有8个,而不是10个可能的获胜举动:3个水平,3个垂直和2个对角线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM