Computer Science Canada Tic Tac Toe Game Problem |
| Author: | compstudent [ Sun Feb 06, 2011 4:39 pm ] | ||
| Post subject: | Tic Tac Toe Game Problem | ||
I am making a simple two player tic tac toe game. I am having problems with having the the game draw a X and O when they are supposed to. The game is not done yet - I still need to add syntax to check if players won. Just need someone to point our what I am doing wrong. Thanks
|
|||
| Author: | huskiesgoaler34 [ Sun Feb 06, 2011 5:05 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
I am assuming that, after trying to play your game, you are trying to not draw both an x or an o each time the box is clicked. Right now, both are drawn on top of eachother. Am I right? |
|
| Author: | TokenHerbz [ Sun Feb 06, 2011 5:13 pm ] |
| Post subject: | RE:Tic Tac Toe Game Problem |
if you add a delay(60) in the loop you'll see that both arn't drawn the same time, however you need to make sure that the grid is "clear" before you can "draw" on it, so that you cant draw on it again... you could use a boolean for that. |
|
| Author: | huskiesgoaler34 [ Sun Feb 06, 2011 5:15 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
What I would do is set an x or an o to each player. So player1 is an o and player 2 is an x. If player one clicks a box, it draws a o. If player two clicks a box, then an x is drawn. |
|
| Author: | huskiesgoaler34 [ Sun Feb 06, 2011 5:16 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
Maybe stick to token's idea. His is easier. |
|
| Author: | compstudent [ Sun Feb 06, 2011 5:23 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
I'm not quite sure I completely understand tokens idea. I have put in the delay and noticed the difference. Now I just need to make sure that another symbol isn't drawn if the box is already taken. Thanks for the replies. |
|
| Author: | compstudent [ Sun Feb 06, 2011 5:26 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
Do you mean like a multi-dimensional array of boolean, and assign the variable true for the corresponding gird spot when it is drawn on. |
|
| Author: | compstudent [ Sun Feb 06, 2011 6:05 pm ] | ||
| Post subject: | Re: Tic Tac Toe Game Problem | ||
I have changed it and added variables, just for two places on the board so far. Now for when the first player clicks a spot it draws the correct symbol then, when clicked again nothing happens which is good. However when you click the next box, both symbols show up. I'm not sure whats wrong.
|
|||
| Author: | huskiesgoaler34 [ Sun Feb 06, 2011 6:20 pm ] | ||
| Post subject: | Re: Tic Tac Toe Game Problem | ||
compstudent @ Sun Feb 06, 2011 6:05 pm wrote: I have changed it and added variables, just for two places on the board so far. Now for when the first player clicks a spot it draws the correct symbol then, when clicked again nothing happens which is good. However when you click the next box, both symbols show up.
I'm not sure whats wrong.
I think I found the problem. The commented part was the problem. After you click one box, it drew the correct symbol but when you clicked another box, it draws both symbols. Now, I made it so that when one of the boxes is clicked, the x appears. When you click another box, a circle appears. Note: It only works for the first two boxes on the first column. |
|||
| Author: | compstudent [ Sun Feb 06, 2011 6:27 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
I notice that after you click a box an x will first appear, then the next box an O. But if i click the lower left box an x will appear, then if i click the one directly above it, both appear. |
|
| Author: | compstudent [ Sun Feb 06, 2011 7:00 pm ] | ||
| Post subject: | Re: Tic Tac Toe Game Problem | ||
Nevermind - I have fixed that problem now. Just needed to add
to each of the embedded if statements. Thanks for the help, I'll probably be back with more problems. |
|||
| Author: | RandomLetters [ Sun Feb 06, 2011 7:44 pm ] |
| Post subject: | RE:Tic Tac Toe Game Problem |
I think it would help a lot if you looked into else and elsif statements for the inner conditional instead of adding more conditions. (edit for clarity) |
|
| Author: | compstudent [ Sun Feb 06, 2011 8:02 pm ] | ||
| Post subject: | Re: Tic Tac Toe Game Problem | ||
I have another problem. I have worked it out so that each symbol is drawn when it is supposed to be and so that there aren't two in the same square. Now my problem is that there seems to be a delay in drawing them. For example if I click on it will draw. Next one nothing. Next one I click both show up: the one before where I clicked and the new one. I noticed though, if I click pause after one doesn't show up, it suddenly appears. I am not sure what is the problem.
Note: Not complete. Yet to add prompts and checking to see if a player won. |
|||
| Author: | huskiesgoaler34 [ Sun Feb 06, 2011 8:20 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
It works perfectly for me. I don't see a problem. The correct symbol is drawn without any delays. No bugs. I have tested it out and it works fine. |
|
| Author: | compstudent [ Sun Feb 06, 2011 8:32 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
Thanks, I guess it is just my computer, since I just tried it on another and it worked fine. |
|
| Author: | huskiesgoaler34 [ Sun Feb 06, 2011 8:45 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
Your welcome. Post the code when your done with determining the winner and ending the game. |
|
| Author: | TokenHerbz [ Sun Feb 06, 2011 10:06 pm ] |
| Post subject: | RE:Tic Tac Toe Game Problem |
you could use a 2D array for your "grid" to, and types are good at storing "multiple data" though i'd use a class myself. |
|
| Author: | compstudent [ Mon Feb 07, 2011 7:15 pm ] | ||
| Post subject: | Re: Tic Tac Toe Game Problem | ||
Here is the game now almost complete. The problems I am having now are that I need it to stop when a player one - you will notice it doesn't, and then to allow the players to play again. If I make a play again procedure i have to put it above the game procedure so that the game can recognize it, but then I cannot call the game procedure in the play again one.
|
|||
| Author: | ProgrammingFun [ Mon Feb 07, 2011 9:47 pm ] | ||
| Post subject: | RE:Tic Tac Toe Game Problem | ||
Have you heard of forward procedure? It enables you to do the following:
It was something like the above...I do not completely remember... |
|||
| Author: | DemonWasp [ Tue Feb 08, 2011 8:08 am ] |
| Post subject: | RE:Tic Tac Toe Game Problem |
Even if you do resolve the circular reference as ProgrammingFun says, you'll eventually run into stack overflow problems that way. Better to have a replay loop that exits if the user doesn't want to replay the game, and loops if they do. |
|
| Author: | TokenHerbz [ Tue Feb 08, 2011 10:08 am ] |
| Post subject: | RE:Tic Tac Toe Game Problem |
I Think the Best way for you (least work) to fix this problem would be to add a boolean Value to check for when the game is won. So in your procedure Check ( I would rework this so its not so repetitive, And make this into a function. If a player has won it turns this and use that in your game loop to exit the look when this occurs.. After if you want, you could even reset your variables after the loop still inside the game loop. Create another loop to cycle threw your two procedures to keep the game going. |
|
| Author: | TokenHerbz [ Tue Feb 08, 2011 10:39 am ] | ||||||||||||
| Post subject: | RE:Tic Tac Toe Game Problem | ||||||||||||
I went ahead and Did some re-working of your code to give you an example of some things your doing wrong. This is to help you better understand what to look for and how to problem solve things for the rest of your game and/or other programs your making. your OLD CODE:
The purpose of this check is obviously to see if the player has won the game. It's called like 16 times or so in the main loop, Which is absolutely not required. As you can see in your game loop we go to a first "if" statement. This goes step by step down the list until the end. You have "check" in every sub if statement inside that main one. All that is required is the check is in before the end if on the main if statement, Hell It doesn't even need to be inside this if statement at all. But if you wanted it inside there, it would have a correct place. The thing about programming is even you retype a ton of your code, and the game still seems to function correctly, It's Bad to do it for several reasons, the main being it's hard to change around, and/or fix bugs. Very time consuming. So First example to solving your question, Is how do we make this "check" work? Well I went ahead and redid that procedure into a Function, (which returns a value) and renamed this "Win_Game". Here is the source:
How I used this is just as important, I don't call this more then "ONCE" in the main loop, Here lets take a look and understand it!
So what i did was create a function that returns a true/false depending on circumstances, this case being if a player has won or not. I put this check right at the very end of the game procedure so how it goes is "start game -> player moves -> check if win -> if win exit else continue -> other player moves -> rinse and repeat. Now when the Player does win, and we exit the game proc loop We are still in the procedure, so why not reset variables and clear the screen so we may play again!!
I should also mention i've changed your Bored array to string, init values of blank ""x9 times.
Now what have we fixed? We made it possible to check if the game has won or not, and leave the game according to that (FIXED function). We reset variables and clear the screen so may play again!! Awesome!!! Now for the final thing, we loop our procs!! and we are ready for endless tic tac toe!
********************* ********************* So Now that you have your game working, YOUR not DONE yet... Oh noes, no no no!! I show'd you something in your check proc, how to minimize all this "re-do-re-write codes"... What you need to do now, Is re-write your proc game to minimize all that repeat codes. Just check how its supposed to work and when you fix/clean that up, post it here for some BITS! Good luck !!! PS: After you touch your code up, you can add more features to your game very easily. Cheers! |
|||||||||||||
| Author: | compstudent [ Tue Feb 08, 2011 1:53 pm ] | ||
| Post subject: | Re: Tic Tac Toe Game Problem | ||
Thank you very much for you lengthy reply. It was VERY helpful. Here is the game and i think it is finished.
I am now looking into adding an AI. Thanks Again! |
|||
| Author: | TokenHerbz [ Tue Feb 08, 2011 3:59 pm ] |
| Post subject: | RE:Tic Tac Toe Game Problem |
No no no!! All wrong!! lol... Please Re-read my post and try to understand what Iv'e done and try to fix your code to respond the knowledge in that post! If you don't even try I won't be of assistance anymore!! Read it carefully! |
|
| Author: | TerranceN [ Tue Feb 08, 2011 5:49 pm ] | ||||
| Post subject: | Re: Tic Tac Toe Game Problem | ||||
@TokenHerbz: What did he do wrong? He is no longer using the check procedure (even though it is still in his code), and he can restart his game, the two main points of your post. Anyway, I would recommend breaking that large if statement into smaller functions such as HorizontalCheck, VerticalCheck, DiagonalCheck, to make that complex boolean statement MUCH easier to read and understand. The section of code where you handle the mouse clicking on a grid square could be refactored a lot. Instead of having the code to check the turn and whether the grid square is full in each block of code for each grid square, find the coordinates of the grid square clicked on and save them in variables, which you can use to check if the grid square is full in, like this
Also, the method you use to find the clicked tile could be improved. The way you are currently doing it is what I would call the "Button" method. You treat everything as a button, with defined bounds you check when you click. This will work of course, but it will mean a more complicated way of managing all those buttons, or a lot of typing. Fortunately, since this is a grid with no spaces in between grid squares, we can just use integer division (the 'div' command in Turing) to divide the x-value of some point by the x-size of the grid squares, to find the x index of the tile that contains the point. The same thing can be done with Y values. Here is an example:
Hope that helps. |
|||||
| Author: | compstudent [ Tue Feb 08, 2011 6:15 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
Token, I understand that I should recode my game procedure to make it much shorter, faster and simpler. I would if I had the time, but at the moment I am happy that it works. I also copied the wrong version - new one had taken the check out. Thank you for the reply Terrence - good to know for other projects (although will thankfully get to use Java from now on). As for making an AI - from what I have seen people have just made long if statements for all the possible situations. Is that the best way to go or is there a simpler way. Thanks |
|
| Author: | TokenHerbz [ Tue Feb 08, 2011 6:42 pm ] |
| Post subject: | RE:Tic Tac Toe Game Problem |
you could make 2 kinds of AI. A very basic one that chooses randomly of the left over locations which may or may not "block" your win. Or you could code a lengthier one, which will be impossible to beat, however you could add chance percent of hitting that spot or not etc. Lots you could do with that. |
|
| Author: | compstudent [ Tue Feb 08, 2011 11:14 pm ] |
| Post subject: | Re: Tic Tac Toe Game Problem |
Could you please elaborate on how to go about making a lengthier one. How I am currently thinking about it would be to loop through to find any two in a row or column or diagonal and block that. |
|
| Author: | compstudent [ Wed Feb 09, 2011 5:35 pm ] | ||
| Post subject: | Re: Tic Tac Toe Game Problem | ||
Hi, I am close to finished (I think). I have made an AI that just blocks the win. I am now having problems with the one player game play.
I am not sure what is wrong, however after x goes, it will not draw an o. |
|||