My Tic-Tac-Toe Problem

Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).
Locked
Don M
Posts: 6
Joined: Sun Nov 19, 2006 3:54 pm

My Tic-Tac-Toe Problem

Post by Don M »

I have been doing the Tic-Tac-Toe project and everything has been fine and dandy, up until i tried putting in the checkWin() function. As soon as i entered that function into my code, my entire Tic-Tac-Toe game stopped working.

I have revised my text a bajillion times and still have no clue why it isn't working. The board was working perfectly before i entered the function. Just can't seem to get the X's and O's to show their faces.
vick_panwar
Former Expert
Posts: 5
Joined: Tue Sep 26, 2006 7:12 pm

Post by vick_panwar »

There are many ways to 'troubleshoot' a program -

1. Use a debugger to step through you code and examine the values to the variables. It should give you a clue as to what's going wrong and where.

2. If you don't have a debugger, just put a lot of print statements with the values of the variables you are tracking.

3. Isolate the problem to a smaller and smaller scope. You already have it narrowed down to a function. Try to simplify this function. e.g. by removing some code. This will get you to the bottom of the erroneous statement/s.
Don M
Posts: 6
Joined: Sun Nov 19, 2006 3:54 pm

Post by Don M »

Ive tried simplifying the code and that doesn't seem to help. If you could possibly give me your e-mail, i could send you a copy of my text and you could guide me to where my error is, but not fix it for me. After all, it is MY science project. Thanks.
vick_panwar
Former Expert
Posts: 5
Joined: Tue Sep 26, 2006 7:12 pm

Post by vick_panwar »

So if you remove this 'CheckWin' function from your code everything works ok ?
In that case can you post the function's code here ?
Let's see if we can find something.
Don M
Posts: 6
Joined: Sun Nov 19, 2006 3:54 pm

Got it fixed, but another problem arises

Post by Don M »

Well, i got the code thing figured out. I had a few braces that were in incorrect spots but now im having another problem, which is how to program the computer opponent. So far, ive used the 'checkWin()' function as a guide to make the computer move but it doesn't seem to be working. If somebody could possibly point me in the right direction it would be greatly appreaciated.[/quote]
pgordon
Posts: 8
Joined: Tue Sep 26, 2006 6:28 pm

Post by pgordon »

The easiest way would be for you to check if any of the conditions required to win the game are true. These conditions would consist of the 8 different possible ways a player could get 3 in a row. So essentially your CheckWin function would have a 9 layed if-elseif-elseif-.... -else statement where the first 8 conditions would be a win and the 9th would not be a win. Does that make sense? It looks like you have already written your AI but if you need more help on the strategy you can look here.
Paul Gordon
Software Engineer
Symantec
Don M
Posts: 6
Joined: Sun Nov 19, 2006 3:54 pm

Post by Don M »

What you said makes sense, but i havnt written my AI yet. Im having trouble starting off. I cant get the computer to make a mark for itself and its driving me insane. :shock:
pgordon
Posts: 8
Joined: Tue Sep 26, 2006 6:28 pm

Post by pgordon »

Writing the AI that gets the computer to make a good move will be the hardest part. You will need to write code that will do 2 major things:

1) Create all possible board combinations that the computer can create on the next move. Since tic-tac-toe is a rather simple game you have the advantage of not having to create a perfect system. Computers are fast enough that you can compare all possible board combinations without bogging the computer down. The number of possible boards you would need to compare would be at most 9 (the very first move) and at least 1 (the very last move).

2) Rank these possible board combinations based on a value system to determine which move is the best move. The wikipedia page I linked has a good ranking system:

1. Complete three in a row (WIN).
2. Block their opponent from completing three in a row.
3. Threaten a win with two possible completions in two rows.
4. Avoid a configuration in which the opponent can force the win.
5. Threaten a win with a possible completion (two in a row).
6. Prevent the opponent from getting two in a row.
7. Play in the middle box.
8. Play in a corner box.
9. Play on a side box.

I would rank a board in which the next move wins as 10,000, a board in which the next move loses as -1, and rank the rest of the boards using values you give to the list above. After you have ranked all the boards and found the best move, you need to make the computer take that move.
Paul Gordon
Software Engineer
Symantec
Don M
Posts: 6
Joined: Sun Nov 19, 2006 3:54 pm

Post by Don M »

Thats the problem. Making the computer do it. I just cant seem to get it to work.
pgordon
Posts: 8
Joined: Tue Sep 26, 2006 6:28 pm

Post by pgordon »

If you are having trouble getting the X's and O's to show on the board I would try researching how to display graphics in the programming language you have chosen. I can only assume that you will need to write a function to draw an X that would consist of drawing two overlapping lines; x1 to x2 and y1 to y2. To draw a circle or an oval you would probably tell it where the center is and then what the width and height should be. These are general ideas as how to draw graphics are different with different programming languages. What language are you using?
Paul Gordon
Software Engineer
Symantec
Don M
Posts: 6
Joined: Sun Nov 19, 2006 3:54 pm

Post by Don M »

I am using Javascript that is interacting with HTML as the project says to use. I just cant seem to get the computer to make its move.
pgordon
Posts: 8
Joined: Tue Sep 26, 2006 6:28 pm

Post by pgordon »

Don M wrote:I just cant seem to get the computer to make its move.
You need to delve into your code to figure out why. Use print statements to figure out where it's going wrong. Did the computer figure out where it wants to move but the graphics are not drawing correct, you could figure this out by print statements. Maybe your code is taking a different path than you expect, again print statements would show you this. Keep at it, you'll figure it out.
Paul Gordon
Software Engineer
Symantec
Mago
Posts: 11
Joined: Sun Sep 23, 2007 5:52 pm

Post by Mago »

Hey? Did you ever figure it out? I am doing exactly what you are, and I am having the same problem!

___________________________________________________________
Mago
Locked

Return to “Math & Computer Science Sponsored by Hyperion Solutions Corp”