Recommended Posts

Ok, well this is my first time post. And im getting to understand C++ a little better after some practice and i decided to try a simple TicTacToe game, but now i have some problems. I''ll ask for player1''s move, then ill draw the board, but it will assign that input to my Move[8] char variable for the board output along with assigning X to the number i selected. Ill show you the code, i''m a newb so and suggestions to what i should do to make the code more efficent would help too, thanks.

Share this post

Link to post

Share on other sites

Guest Anonymous Poster

Guest Anonymous Poster

I''m just a newbie myself too, but I think you don''t need to have seperate functions for the two players, you can combine them somehow so it will look a little smarter. Another thing, I think you could use the constructor for the game loop, I''m not sure about that, I haven''t studied program structure much yet. So, somebody please confirm this?

Share this post

Link to post

Share on other sites

Looks like your not doing any error checking to see if the spot was previously taken. Though I guess that will come later, along with checking for wins. I had to write a Tic-Tac-Toe program in May for a Computer Science course, so if you need any help, just ask and I can post some code.

Share this post

Link to post

Share on other sites

Ohh, now i see siaspete, yeah now it works now, thanks alot. Yes its very basic right now and later on ill make more enhancements to it, i just wanna get it to be a working game right now. CWizard ill try your code out, i kinda see what your doing ill work on it. Thanks to everyone who replied.

Share this post

Link to post

Share on other sites

quote:Original post by FiXXoR ... i understand how it works and all except this part..

Move[nMove] != (nMove + 0x30 + 1)

Hopefully it works like this. You initialize your Move array with the ASCII values of "1" through "9", which in hex are 0x31-0x39. The test above is to check if anything is on that spot, and should evaluate to TRUE, if the position where the user tried to move is not equal to its initialization value. In short, when the Move[] array is just initialize the following is true: Move[n] == (0x31 + n) where 0 <= n <= 8. If that equation isn''t TRUE then the spot is taken.

This was just something I added while typing, and you should use some other technique to check if the spot is free.

Share this post

Link to post

Share on other sites

Argh, ok i have some problems. Im working on a function that checks to see if there is a winner. But when i win it will ask for the other players input still.. i tryed a couple things to get it to catch that i''ve won and stop but it will not do it. And when i make a line with X O X it will say player one wins (ugh). Well of course any help is very appreciated, ive learned somethings from starting this project so it isnt a total loss, and i would like to finish it.

// Check if player owns upperleft corner if(Cell[0]==Players_Symbol) { // Check if player has won the first way (0,1,2) if((Cell[1]==Players_Symbol)&&(Cell[2]==Players_Symbol)) {return(true)} // Check if player has won the second way (0,3,6) if((Cell[3]==Players_Symbol)&&(Cell[6]==Players_Symbol)) {return(true)} // Check if player has won the third way (0,4,8) if((Cell[4]==Players_Symbol)&&(Cell[8]==Players_Symbol)) {return(true)} }

// Check if player owns Center if(Cell[4]==Players_Symbol) { // Check if player has won the forth way (4,1,7) if((Cell[1]==Players_Symbol)&&(Cell[7]==Players_Symbol)) {return(true)} // Check if player has won the fifth way (4,2,6) if((Cell[2]==Players_Symbol)&&(Cell[6]==Players_Symbol)) {return(true)} // Check if player has won the sixth way (4,3,5) if((Cell[3]==Players_Symbol)&&(Cell[5]==Players_Symbol)) {return(true)} }

// Finally, Check if player owns bottomright corner if(Cell[8]==Players_Symbol) { // Check if player has won the seventh way (8,2,5) if((Cell[2]==Players_Symbol)&&(Cell[5]==Players_Symbol)) {return(true)} // Check if player has won the eighth way (8,6,7) if((Cell[6]==Players_Symbol)&&(Cell[7]==Players_Symbol)) {return(true)} }

Share this post

Link to post

Share on other sites

Wow, alot of code i have to go over. Anonymous Poster thanks for reminding me, i totally forgot about including that == ''X'' after every variable check, ugh! But ill see if i can try to implement and understand your guy''s other code. Thanks alot!

0

Share this post

Link to post

Share on other sites

Ok, well ive been messing with it for a while now and i just cant understand why the loop does not end when a player wins. I tryed tracing the GameOver variable and it doesnt change from false to true for some reason. Can anyone figure this out?

Share this post

Link to post

Share on other sites

You only have to check two things to determine if tic-tac-toe is done being played.

First, has the current player''s move won that player the game. Second, after the move was made are there no more empty cells left to move into.

So, just keep track of how many empty cells there are left and when it is zero the loop should exit. And if that happens that means the game was a draw.

And check after each move to see if the current player has won.

Main Program Flow for Tic=Tac-Toe:.Initialize (Current Player, Next Player, and Empty Cell Count)

.While there is a Move (empty cell)...Current player Moves by choosing an empty cell....subtract 1 from emtpy cell count....if player has one of the 8 winning squences the player wins.....and the game ends...Swap Current Player with Next Player.End of Main Loop