Commit e7942050 authored by Stefano Cobelli's avatar Stefano Cobelli

Ai is not working. Added debug messages.

Added loss method so it can be called when it loses to learn.
There was an array out of bounds exception at some point too
parent 24c57f54
......@@ -92,6 +92,9 @@ public class TTTController implements ActionListener {
try {
trainCount = Integer.parseInt(controlPanel
.getTrainNumberField().getText());
if (trainCount <= 0) {
throw new Exception();
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,
"Please enter a valid number to train");
......@@ -163,6 +166,9 @@ public class TTTController implements ActionListener {
if (!theGame.isPlayable()) {
// performs relative actions if game is over
if (theGame.getState() == State.WIN) {
if (theGame.getCurPlayer() instanceof AIPlayer) {
((AIPlayer) theGame.getCurPlayer()).loss();
}
JOptionPane.showMessageDialog(null,
theGame.getPlayerTurn() + " wins!");
if (theGame.getPlayerTurn() == "X")
......
......@@ -52,13 +52,14 @@ public class AIPlayer extends Player implements AutoPlayable {
for (int col = 0; col < theBoard.getSize(); col++) {
if (curBoard[row][col] == Mark.NONE) {
curBoard[row][col] = getMark();
// if the board is a win
if (hashBoards.get(curBoard) == null) {
System.out.println("What does this do...");
prevBoard = twoDArrayDeepCopy(curBoard);
move(row, col);
// if the boards a win, set all the other spots to
// false, so it knows to win
if (theBoard.getState() == State.WIN) {
System.out.println("I win! Writing this down...");
hashBoards.put(curBoard, true);
curBoard[row][col] = Mark.NONE;
for (int row2 = 0; row < theBoard.getSize(); row++) {
......@@ -73,9 +74,8 @@ public class AIPlayer extends Player implements AutoPlayable {
}
Integer[] answer = { row, col };
return answer;
}
// if the board might lead to a win
else if (hashBoards.get(curBoard) == true) {
} else if (hashBoards.get(curBoard) == true) {
System.out.println("I got this in the bag!");
move(row, col);
Integer[] answer = { row, col };
return answer;
......@@ -87,7 +87,9 @@ public class AIPlayer extends Player implements AutoPlayable {
}
}
// if all moves lead to a loss, forfeit
System.out.println("I lose...");
hashBoards.put(prevBoard, false);
for (int row = 0; row < theBoard.getSize(); row++) {
for (int col = 0; col < theBoard.getSize(); col++) {
if (curBoard[row][col] == Mark.NONE) {
......@@ -114,4 +116,9 @@ public class AIPlayer extends Player implements AutoPlayable {
}
return newArray;
}
public void loss() {
System.out.println("I lose, writing this down");
hashBoards.put(prevBoard, false);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment