C++ tic-tac-toe

Started by
20 comments, last by MikeWulf 18 years, 10 months ago
hey, ive made this code:

#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?
Advertisement
isn't using OOP for tic-tac-toe a humungous overkill?
i didn't even know i was using OOP:P hehe srry ill edit the code
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.
? what are you guys talking about ?
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).
Quote:Original post by mgarriss
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.



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.
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
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 [:)]
"I think there is a world market for maybe five computers." -- Thomas Watson, Chairman of IBM, 1943

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 [:)]
"I think there is a world market for maybe five computers." -- Thomas Watson, Chairman of IBM, 1943

This topic is closed to new replies.

Advertisement