Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,480 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,724 people online right now. Registration is fast and FREE... Join Now!




Game TicTacToe

 
Reply to this topicStart new topic

Game TicTacToe, algorithm - function(s)

kaantexas
27 Jan, 2008 - 01:03 PM
Post #1

New D.I.C Head
*

Joined: 12 Jan, 2008
Posts: 14

I am not sure where should I start? I was wondering what is an algorithm for tictactoe? and how many function should I use?
Thanks

This post has been edited by kaantexas: 27 Jan, 2008 - 01:32 PM
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Game TicTacToe
27 Jan, 2008 - 01:39 PM
Post #2

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,459



Thanked: 10 times
Dream Kudos: 325
My Contributions
Write as many functions as you think are necessary.

You should have a main method, just like any program. You'll also want to use a two dimensional array for the board (ie. int board[3][3];.

Inside your main method, you'll want a "game loop". Something like this.
CODE

int game_running = 1;

...

while (game_running)
{
    /* Do game stuff. */
}
.

Inside your game loop, you'll want to do several things.

First, you'll want to print out the board (writing a function to do this would be good). Then you'll want to ask for user input (ie. which space to move to).

At the end of every loop you'll want to check if there's a winner (perhaps another function?). If there is no winner, you'll want to increment the turn.

Turns can be kept with an integer, like int turn = 0;. Incrementing the turn then would be as simple as turn++;. To determine which player needs to move, you could use modulus division.

For example, if turn % 2 equals 0, it is player one's turn (because turn divided by two has a remainder of zero, therefore turn is an even number, and player one started on an even number). If it equals 1, then it's player two's turn.

Lot's of people make tic-tac-toe as their first game. If you want more help, I suggest you look back at old threads (use the search function). smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 06:52PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month