944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8138
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 14th, 2007
0

HELP!!! Tic-tac-toe game!

Expand Post »
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)..
C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. int main ()
  5. {
  6. int line1;
  7. int line2;
  8. int line3;
  9. int line4;
  10. int line5;
  11. int line6;
  12. int line7;
  13. int line8;
  14. int line9;
  15. int line10;
  16. int line11;
  17. int line12;
  18. int line13;
  19. int line14;
  20. cout << "Tic-Tac-Toe, 3 in a row!\n";
  21. cout << "I go first...\n\n\n\n\n\n\n\n";
  22. cout << " | | \n"<<line1;
  23. cout << " | | \n"<<line2;
  24. cout << " | | \n"<<line3;
  25. cout << "_________________________________\n"<<line4;
  26. cout << " | | \n"<<line5;
  27. cout << " | | \n"<<line6;
  28. cout << " | | \n"<<line7;
  29. cout << " | | \n"<<line8;
  30. cout << " | | \n"<<line9;
  31. cout << "_________________________________\n"<<line10;
  32. cout << " | | \n"<<line11;
  33. cout << " | | \n"<<line12;
  34. cout << " | | \n"<<line13;
  35. cout << " | | \n"<<line14;
  36. cout << "\n\n\n\n\n\n\n\n\n\n\n";
  37. cout << " | | \n"<<line1;
  38. cout << " | | \n"<<line2;
  39. cout << " | | \n"<<line3;
  40. cout << "_________________________________\n"<<line4;
  41. cout << " | | \n"<<line5;
  42. cout << " | | \n"<<line6;
  43. cout << " | _\/_ | \n"<<line7;
  44. cout << " | /\ | \n"<<line8;
  45. cout << " | | \n"<<line9;
  46. cout << "_________________________________\n"<<line10;
  47. cout << " | | \n"<<line11;
  48. cout << " | | \n"<<line12;
  49. cout << " | | \n"<<line13;
  50. cout << " | | \n"<<line14;
  51. return 0;
  52. }

Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
lotsofsloths is offline Offline
44 posts
since Jan 2007
Feb 14th, 2007
0

Re: HELP!!! Tic-tac-toe game!

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:
C++ Syntax (Toggle Plain Text)
  1. 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...
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 14th, 2007
0

Re: HELP!!! Tic-tac-toe game!

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
C++ Syntax (Toggle Plain Text)
  1. bool Mygrid[3]x[3]
Last edited by lotsofsloths; Feb 14th, 2007 at 9:22 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
lotsofsloths is offline Offline
44 posts
since Jan 2007
Feb 14th, 2007
0

Re: HELP!!! Tic-tac-toe game!

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
C++ Syntax (Toggle Plain Text)
  1. 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...
C++ Syntax (Toggle Plain Text)
  1. [0] [1] [2]
  2.  
  3. [0]char char char
  4.  
  5. [1]char char char
  6.  
  7. [2]char char char
Does this not look familiar to this?
C++ Syntax (Toggle Plain Text)
  1. || || ||
  2. x
  3. _____________
  4. || || ||
  5. x
  6. _____________
  7. || || || 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:
C++ Syntax (Toggle Plain Text)
  1. 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.
Last edited by John A; Feb 14th, 2007 at 9:48 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 14th, 2007
0

Re: HELP!!! Tic-tac-toe game!

could ya help me how that would look in code?
Reputation Points: 10
Solved Threads: 0
Light Poster
lotsofsloths is offline Offline
44 posts
since Jan 2007
Feb 14th, 2007
0

Re: HELP!!! Tic-tac-toe game!

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Feb 15th, 2007
0

Re: HELP!!! Tic-tac-toe game!

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
Last edited by Lazaro Claiborn; Feb 15th, 2007 at 5:00 am.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 15th, 2007
0

Re: HELP!!! Tic-tac-toe game!

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...
Reputation Points: 10
Solved Threads: 0
Light Poster
lotsofsloths is offline Offline
44 posts
since Jan 2007
Feb 15th, 2007
0

Re: HELP!!! Tic-tac-toe game!

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
lotsofsloths is offline Offline
44 posts
since Jan 2007
Feb 15th, 2007
0

Re: HELP!!! Tic-tac-toe game!

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: replace characters
Next Thread in C++ Forum Timeline: problem inserting elements in a 2D vector





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC