My Tic-Tac-Toe Problem
My Tic-Tac-Toe Problem
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.
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.
-
- Former Expert
- Posts: 5
- Joined: Tue Sep 26, 2006 7:12 pm
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.
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.
-
- Former Expert
- Posts: 5
- Joined: Tue Sep 26, 2006 7:12 pm
Got it fixed, but another problem arises
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]
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
Software Engineer
Symantec
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.
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
Software Engineer
Symantec
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
Software Engineer
Symantec
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.Don M wrote:I just cant seem to get the computer to make its move.
Paul Gordon
Software Engineer
Symantec
Software Engineer
Symantec