My first Tic-Tac-Toe game in C++, i made it using:
-
arrays.
-
references or
pointers(to pass certain crap through a function for efficiency, such as the Tic-Tac-Toe board, so that the compiler wouldn't have to copy it every time someone make a move)
-annnd lastly i used
vectors from the
STL.
so basically i used a 2 dimensional array of
ints which represents all eight ways to get three in a row and win the game, looked something like this:
1 2 3 4 5 6 7 8
|
const int WINNING_ROWS[8][3] = { {0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6} };
|
and i used a for loop to loop through each possible ways a player can win, if the player has 3 in a row, then we have a winner.
if there's no more empty squares on the board, then it's a tie.
if neither player has won and the game isn't a tie, then there's no winner.
You know what would be really really cool? you could write a small piece of AI into your small Tic-Tac-Toe game, yes! make the computer put up a decent fight, writing Tic-Tac-Toe games is very srandard and boring... your professor would probably not be impressed at all, but if you implement just a dash of AI, that would make the game somewhat challenging and funner to play, your professor will be very impressed trust me ;)
NOTE: AI is pretty advanced topic, it requires a lot of thinking and it could be super overwhelming at times, i don't expect you to know how to write any form of AI, even if it's just a small piece, but you could always give it a shot it's pretty worth it, though, it's okay if you forget about it. for now at least.
hints:
1) If the computer can win on this move, make that move.
2) otherwise, if the human can win on his next move, block him right away.
i'll leave the rest for you, goodluck!
Uk Marine