2

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. There are a possible 8 winning combinations. 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.

Patrick Kennedy
  • 177
  • 2
  • 2
  • 11

3 Answers3

2

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).

Oleksi
  • 12,947
  • 4
  • 56
  • 80
  • Absolutely! While this can get complicated, you can certainly do this. You can have arrays of arrays of arrays of arrays, if you really want to scare people reading your code. I wouldn't advise it. – Oleksi Feb 12 '12 at 21:17
  • so would there be a better option than making an array of arrays? – Patrick Kennedy Feb 12 '12 at 21:19
  • You should start wrapping your structures in your own defined objects. Of course you can just make your board an array of arrays. A set of possible boards would then be an array of arrays of arrays, which gets hard to manage. If you wrap your array of arrays in a board objects, your other array is just an array of boards. As a rule of thumb usually up to three layers is still manageable, after that it get's hard and you should add additional abstractions. – LiKao Feb 12 '12 at 21:25
  • Well if I were to write this program, I wouldn't store all winning moves at all. I would make a function like checkForWin(char grid [][], char player), which checks if player has won given the current configuration of board. The inside of this function would contain logic to determine all the possible ways to win – Oleksi Feb 12 '12 at 21:25
  • checkForWin(char grid [][], char player) would, in turn, contain three sub (helper) functions, like checkForDiagonalWin, checkForHorizontalWin, and checkForVerticalWin. If any one of those is true, we have a win. Writing these three functions is a good exercise if you are just learning to program. – Oleksi Feb 12 '12 at 21:27
  • In general, this is a useful strategy for solving problems: Try to break your problem up into simpler sub-problems that are easier to solve. – Oleksi Feb 12 '12 at 21:28
  • Oleksi i thought of doing that like incremeting the position of the of each element in the array and if for 3 moves it doesnt stop than its winning. Or something like like, but i thought it would be more complicated, but now i dont know – Patrick Kennedy Feb 12 '12 at 21:28
  • 1
    Well here's how the code for horizontal might look: http://pastebin.com/kkSsb6va From here, see if you can write the other two functions. checkVertical should be very similar. – Oleksi Feb 12 '12 at 21:35
  • wow thank you, ya that is exactly the kind of thing i need, i see how i could do a vertical.A diagonal would just use 2 for loops right. – Patrick Kennedy Feb 12 '12 at 21:42
  • For a diagonal, there are really only two ways to win. Considering that, I would just have two if statements to check those two ways to win. – Oleksi Feb 12 '12 at 21:43
1

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. 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). 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.

Uko
  • 13,134
  • 6
  • 58
  • 106
1

Using 3x3 array to store your current board is ok. 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.

p.s. There are 8, not 10 possible winning moves for each player: 3 horizontal, 3 vertical and 2 diagonals.

thomson
  • 61
  • 6