954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

HELP!!! Tic-tac-toe game!

ok, i am going to *attempt* to make a tic-tac-toe kinda game...
he is what i got so far.. PLEASE help it out!(i will update the code sometimes)..

#include <iostream>
using namespace std;
int main ()
{
int line1;
int line2;
int line3;
int line4;
int line5;
int line6;
int line7;
int line8;
int line9;
int line10;
int line11;
int line12;
int line13;
int line14;
cout << "Tic-Tac-Toe, 3 in a row!\n";
cout << "I go first...\n\n\n\n\n\n\n\n";
cout << " | | \n"<<line1;
cout << " | | \n"<<line2;
cout << " | | \n"<<line3;
cout << "_________________________________\n"<<line4;
cout << " | | \n"<<line5;
cout << " | | \n"<<line6;
cout << " | | \n"<<line7;
cout << " | | \n"<<line8;
cout << " | | \n"<<line9;
cout << "_________________________________\n"<<line10;
cout << " | | \n"<<line11;
cout << " | | \n"<<line12;
cout << " | | \n"<<line13;
cout << " | | \n"<<line14;
cout << "\n\n\n\n\n\n\n\n\n\n\n";
cout << " | | \n"<<line1;
cout << " | | \n"<<line2;
cout << " | | \n"<<line3;
cout << "_________________________________\n"<<line4;
cout << " | | \n"<<line5;
cout << " | | \n"<<line6;
cout << " | _\/_ | \n"<<line7;
cout << " | /\ | \n"<<line8;
cout << " | | \n"<<line9;
cout << "_________________________________\n"<<line10;
cout << " | | \n"<<line11;
cout << " | | \n"<<line12;
cout << " | | \n"<<line13;
cout << " | | \n"<<line14;
return 0;
}
lotsofsloths
Light Poster
44 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

You could greatly simplify your program if you used arrays instead of numbered variables. You'll want to use 2-dimensional arrays, as it will make your program easier to write. Since it's a 3x3 grid, declare it like that:

bool myGrid[3][3];



You'll want to create a seperate function for drawing the grid, seeing that you're going to need to call it multiple times.

You might also want to consider posting a question so we can have an idea what you're having problems with...

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

lol!!!
first off i don't know how to do any of that stuff, is there like a tutorial for that?
well, not really a tutorial just the part about arrays and and

bool Mygrid[3]x[3]
lotsofsloths
Light Poster
44 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

lol!!! first off i don't know how to do any of that stuff, is there like a tutorial for that? well, not really a tutorial just the part about arrays and and

bool Mygrid[3]x[3]


Actually scratch that. It should be char, not bool, since there's going to be 3 possible values:Empty
an 'X'
an 'O'
The 2 dimensional array is used because there's 2 dimensions: up/down, and left/right. Let me show you some visualization...

[0]     [1]     [2]

[0]char    char    char

[1]char    char    char

[2]char    char    char



Does this not look familiar to this?

||    ||    ||
    x
_____________
||    ||    ||
          x
_____________
||    ||    || x



(Perhaps a bad diagram, but hopefully it gets my point across.)

Then if the user wants to put an X in, say coordinates (2,3), you could write something like this:

myGrid[1][2] = 'X';


Notice that it's (1,2) instead of (2,3) because C++ arrays start at 0.

You'd also have to do error checking, because that space could already be occupied by a X/O. But it was just an example.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

could ya help me how that would look in code?

lotsofsloths
Light Poster
44 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

A algorithm for the game can be found here though not in C but still will serve its purpose of pointing you in the right direction.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

I'm going to try to keep this reply as short as possible so I will not quote your posting. I've been involved in writing game AIs using nerual networks and/or finite state machines and/or etc., so for future thought, you might want to consider learning and writing a simple game AI so you don't have to play yourself or move while someone else goes.

I an idea would be to write a function with a static 3 X 3 grid, as aforementioned, and have the function take two parameters representing the x and y coordiantes of where top place the X or O. Also, you could use a constant to define an X and O for player 1 and player 2. The function would simply clear the terminal window then rewrite adding an X or O to the corrdinates specified by the passed parameters.

Then you'd simply write a function algorithm that'd recognize all the possible winning situations and scan before return from above function.

Here is some simple psuedo code:
define constant player one to X and player two to O
do
get input that'd represent a specific section or coordinate (i.e A for the first square)
switch the input
case A 1 x 1
case B 1 x 2
case C 1 x 3....
put the case coordinate into two variables
while the return value of the write function passing the previous case coord variables and a flag that says what player has moved, keep looping

For the two functions, you'd just write them so that the write function takes three parameters (i.e x, y, flag) and make sure the 3 x 3 array withing the function block is declared as static. Then write a scanning function that takes a character array paremeter, scans it to check for winning conditions then if a win condition is encountered, call another function printing whose the winner, afterwhich, the write function would return 1 so to break the loop.

Just an idea and I'm sure there are other ways, as usual.

Good luck, LamaBot

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

hmm, i'm not really sure what you mean by "case"(i haven't learned that yet, but i will "google" it), but i hope you know that this will be computer versus player...

lotsofsloths
Light Poster
44 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

i know this will take alot of time to make(to make the computer input an x or 0 if someone goes somewhere...
but trust me, i have alot of free time.

lotsofsloths
Light Poster
44 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 
hmm, i'm not really sure what you mean by "case"(i haven't learned that yet, but i will "google" it), but i hope you know that this will be computer versus player...


Consider first writing the program for 2-player mode, as that will be far easier to code and debug.

Once you have the framework working, you can proceed to write the artifical intelligence that the computer needs in order to play. Remember, always work your program up in steps - all at once means that there's too many bugs to find and fix, taking far longer than if you had simply solved little bugs one at a time.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

ok, i'll try

lotsofsloths
Light Poster
44 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 
ok, i'll try



Indeed, structured programming is good. However, since you don't know what a case and therefore a switch conditional statement is, I suggest you learn the basics. Here is my website http://techspire.podzone.net , if you can't reach it then try any of the following:

http://www.cprogramming.com/tutorial.html
http://www.cplusplus.com/doc/tutorial/
http://intro2cpp.tripod.com/
http://www.4p8.com/eric.brasseur/cppcen.html
http://www.astahost.com/info.php/basic-c-language_t2064.html

Once you've learned the basics you'll be equipped with the knowledge that'll allow you to be creative and maybe write some amazing code. As for your tic-tac-toe program, you can either write it while you're learning or you can write it after, your choice :) Also, you might want to LATER learn about structured programming but in my opinion, learn the basics and write the tic-tac-toe program first.

Good luck, LamaBot

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

here was my attempt at writing tic-tac-toe maybe it will give ye' some ideas:

http://flashdaddee.com/forum/showthread.php?t=50

Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
 

Kudos Clinton.... Also, there is a posting titled "Tic-Tac-Toe - Need information or ideas" in the same forum this thread was started in. The re-written program functions correctly but has some disfunctionality relative to the previous posted program, which with you can simply touch up, andalso might be of some use to someone learning or etc....

Here is a link to it: http://www.daniweb.com/techtalkforums/thread70107.html

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You