• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Problem with my project - Tic Tac Toe with recursive game tree search

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am building a program that will tic tac toe against a human or against another computer. (two humans can also work, but is just not interesting).

My classes are: 1. main class 2. game 3. gameBoard 4. player (with children ->) 5. human (and) 6. hardComputer 7. Move (with child ->) 8. bestMove

My problem seems to be with the method chooseMove for the computer (code below):



Any help would be greatly appreciated.

Thank you for looking it over.
 
author & internet detective
Posts: 39448
768
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth,
Welcome to CodeRanch! Can you elaborate on what the problem is? In particular, what is/isn't happening that you expect?
 
kenneth goodman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

The computer is not playing perfectly. The computer does pick a legal move, and sometimes even the winning move, but the computer never defends itself from a possible loss. My program should have a draw every time a computer play another computer, and should win or at least tie when playing a human, but that is not the case.

I've tried to debug the program, but I find recursive calls very hard to debug.

Anything else that would be helpful?

BTW I am using eclipse.
 
Jeanne Boyarsky
author & internet detective
Posts: 39448
768
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before delving into the recursion, the first step is to identify EXACTLY what is wrong. There isn't any randomness in the program so you should be able to reproduce the problem. Can you make a list of the sequence of moves that you (and the computer) makes and say what you think should happen at the computer's turn. For example,

1) Ken makes X in center
2) Computer makes O in upper right corner
3) Ken makes X in center row of left column
4) Computer makes O in bottom right corner. However this isn't correct because the computer should make the center row right column move.

The board now looks like:
_ _ O
X X _
_ _ O
 
kenneth goodman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right now I am playing the computer against another computer, but can change that easily from the game menu.
The game looks like this:
Game Board:
_0 1 2
0
1
2
Turn #: 1
_0 1 2
0 X
1
2
Turn #: 2
_0 1 2
0 X O
1
2
Turn #: 3
_0 1 2
0 X O
1 X
2
Turn #: 4
_0 1 2
0 X O O
1 X
2
Turn #: 5
_0 1 2
0 X O O
1 X X
2
Turn #: 6
_0 1 2
0 X O O
1 X X O
2
Turn #: 7
_0 1 2
0 X O O
1 X X O
2 X

On turn number 4 O does not block the X win on row 2, column 0, on turn 5, x doesn't go for the win on row 2, column o. The problem starts is on move 2 though, O should not go in row 0, column 1, because X can go on space (1,1) to create board:
_0 1 2
0 X O
1 __ X
2
This will enable a double trap, O goes to space(2,2) :
_0 1 2
0 X O
1 _ X
2 _ _ O
X goes to space (1,0):
_0 1 2
0 X O
1 X X
2 _ _ O
which is a double trap. I am hoping to create a method that plays tic tac toe perfectly that is independent of whose turn it is, and which side is playing. Thus computerX and computerO are both objects of the same class hardComputer and both use the method chooseMove for choosing the next move.
 
kenneth goodman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For anyone else in the future who is stuck on this problem, I have figured it out.

My problem was with the score I was assigning each board.

Thank you Jeanne Boyarsky.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!