Public Group

# C++ tic-tac-toe

This topic is 4738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

#include <iostream> // header-file for input/output
using namespace std; // standard namespace

int main()  // begin the main function
{
cout << " ___________"  << endl;
cout << "|   |   |   |" << endl;
cout << "|   |   |   |" << endl;
cout << "|___|___|___|" << endl;
cout << "|   |   |   |" << endl;
cout << "|   |   |   |" << endl;
cout << "|___|___|___|" << endl;
cout << "|   |   |   |" << endl;
cout << "|   |   |   |" << endl;
cout << "|___|___|___|" << endl;

system("pause"); // pauses the program and wait for the users input.

return 0;  // exit the progam
}


but how do i go further with this? i mean should i use arrays in the space in the board? and what else?

##### Share on other sites
isn't using OOP for tic-tac-toe a humungous overkill?

##### Share on other sites
i didn't even know i was using OOP:P hehe srry ill edit the code

##### Share on other sites
no, using OOP for for this is not an overkill. there is noting about OOP that makes it 'heavy weight' or anything. it also is a good solution for almost all problem spaces. of course there is nothing OO in the above code.

##### Share on other sites
? what are you guys talking about ?

##### Share on other sites
That isn't what he meant. I assume he meant your use of iostream, but I don't see a reason *not* to use it (except maybe personal preference).

##### Share on other sites
Quote:
 Original post by mgarrissno, using OOP for for this is not an overkill. there is noting about OOP that makes it 'heavy weight' or anything. it also is a good solution for almost all problem spaces. of course there is nothing OO in the above code.

same thing he said. there are a lot of tic tac toe games in the forums to look at to get an idea of were you can take it.

##### Share on other sites
You arn't using OOP and it isn't overkill, especially if this is a learning project which I'm guessing it is.

Your first step should be to replace your board drawng code with a function or method so you can draw the board to screen whenever you want. Then (as you suggested) use an array to hold the boards state. Allow your drawing function (or method) to have the board passed into it and draw the O, X and blanks accordingly.

Once you have that done you can go about actually coding the game :P

##### Share on other sites
Here are some directions :-

well you could use a [3][3] array to store the user input , per turn - may be initialize it to some constant value say 100.
Give each turn a different value say for user a = 10 , b = 20 ( so while
checking use these values , you could also use characters , just that i prefer intgegers ) Take the input in a while loop , after every input check if the user is winning , ie a conditional statement which if satisfied will break from the loop.
For outputting the values , there is a C function which i don't remember at the moment , but it takes 2 arguments , x and y . so you could jump there and print it. As for the selection , you could give each cell a number , so that based on the condition you pass some pre-defined co-ordinates and print the selection.

Just some ideas which came randomly [:)]

##### Share on other sites

Here are some directions :-

well you could use a [3][3] array to store the user input , per turn - may be initialize it to some constant value say 100.
Give each turn a different value say for user a = 10 , b = 20 ( so while
checking use these values , you could also use characters , just that i prefer intgegers ) Take the input in a while loop , after every input check if the user is winning , ie a conditional statement which if satisfied will break from the loop.
For outputting the values , there is a C function which i don't remember at the moment , but it takes 2 arguments , x and y . so you could jump there and print it. As for the selection , you could give each cell a number , so that based on the condition you pass some pre-defined co-ordinates and print the selection.

Just some ideas which came randomly [:)]

• 10
• 17
• 9
• 14
• 41
• ### Forum Statistics

• Total Topics
631067
• Total Posts
2997735
×