How to Create a Tic-Tat-Toe Game

Im currently a college student, Im majoring in Computer Science. My professor request us to make a program in C++. So my idea was to make a Tic-Tat-Toe game, but I want it to taunt the player. If its a tie with the computer it say etc, if the player wins the computer says etc, and so on. What do I need to learn to make a Tic-Tat-Toe game from scratch, without copying someone else code. I broke up what I want the Tic-Tat-Toe game to do.

1. You can play with the computer or player.
2. The computer will organize you, when you type your name into the program.
3.The game will reset if its a tie or player/computer/player 2 wins.

Thank You.

My first Tic-Tac-Toe game in C++, i made it using:

-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:

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.

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
Last edited on

AI is pretty advanced topic, it requires a lot of thinking

Well, it is far not that hard for 3x3 tit-tat-toe because simple recursive search for best move (minimax algorithm) could be evaluated completely in several milliseconds. So the algorithm has a simple cost function (win or lose) and could be written in 15-20 lines of code.

Cost function should only be able to check whether we have a winner in given position and return either 1 (crosses wins), -1 (noughts wins) or 0 (no one wins).

Then we should write the recursive function (call it assessPosition) in the following way:
- call costFunction on it, if 1 or -1 is returned - return this value immediately;
- otherwise try to perform each possible move and calling assessPosition recursively;
- so we'll have assessments for each possible move - and we should make one of the moves with best assessment (most positive if we are Xs or most negative if we are Os).

Hm... That's all - though I agree, it is not for complete beginner who could not even write the cost function itself... :)

At my website here is an exercise for writing cost function, by the way:
AI is pretty advanced topic, it requires a lot of thinking and it could be super overwhelming at times

I agree, but for simple games like tic tac toe it shouldn't be so hard, but it could be challenging a bit
Topic archived. No new replies allowed.