Commit 7e1abaaa authored by Stefano Cobelli's avatar Stefano Cobelli

Ai working. Ready for JUnit

Training through gui almost done
parent 871f7443
......@@ -12,7 +12,6 @@ import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import model.Board;
import model.Board.State;
import model.Game;
import model.Mark;
......@@ -105,17 +104,20 @@ public class TTTController implements ActionListener {
return;
}
Game aiGames = new Game(theGame.getSize());
Board aiBoard = new Board(theGame.getSize());
aiGuy = new AIPlayer(aiBoard, Mark.O);
RandomPlayer randGuy = new RandomPlayer(aiBoard, Mark.X);
aiGames.setPlayerX(aiGuy);
aiGames.setPlayerO(randGuy);
RandomPlayer randGuy = new RandomPlayer(aiGames.getBoard(), Mark.X);
aiGuy = new AIPlayer(aiGames.getBoard(), Mark.O);
aiGames.setPlayerX(randGuy);
aiGames.setPlayerO(aiGuy);
for (int i = 0; i < trainCount; i++) {
while (aiGames.isPlayable()) {
((AutoPlayable) aiGames.getCurPlayer()).decideMove();
aiGames.switchTurn();
System.out.println(aiGames.getBoard());
}
aiGames.newGame();
}
aiGuy = new AIPlayer(theGame.getBoard(), Mark.O);
// handles the statuses of the buttons availability
} else if (e.getSource() == playerPanel.getGameModeComboBox()
|| e.getSource() == playerPanel.getaIComboBox()) {
......
......@@ -8,7 +8,6 @@
package model.player;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
......@@ -35,7 +34,7 @@ public class AIPlayer extends Player implements AutoPlayable {
/** */
private static List<Board> loseBoards = new ArrayList<Board>();
/** the last board hashed */
private static Mark[][] prevBoard;
private static Board prevBoard;
/**
* constructor for the aiplayer, just calls the super
......@@ -54,59 +53,56 @@ public class AIPlayer extends Player implements AutoPlayable {
*/
@Override
public Integer[] decideMove() {
Board answerBoard = new Board(theBoard.getSize());
Board curBoard = new Board(theBoard.getSize());
curBoard = boardDeepCopy(curBoard, theBoard);
curBoard = boardDeepCopy(theBoard);
for (int row = 0; row < theBoard.getSize(); row++) {
for (int col = 0; col < theBoard.getSize(); col++) {
if (curBoard.isEmpty(row, col)) {
curBoard.setMark(row, col, getMark());
if (hashBoards.get(curBoard) == null) {
System.out.println("What does this do...");
prevBoard = twoDArrayDeepCopy(curBoard);
answerBoard = isBoardInList(curBoard, winBoards);
if (answerBoard != null) {
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(hash(curBoard), true);
curBoard[row][col] = Mark.NONE;
for (int row2 = 0; row < theBoard.getSize(); row++) {
for (int col2 = 0; col < theBoard.getSize(); col++) {
if (curBoard[row2][col2] == Mark.NONE
&& row2 != row && col2 != col) {
curBoard[row2][col2] = getMark();
hashBoards.put(hash(curBoard), false);
}
}
}
}
Integer[] answer = { row, col };
return answer;
} else if (hashBoards.get(curBoard) == true) {
System.out.println("I got this in the bag!");
move(row, col);
Integer[] answer = { row, col };
return answer;
}
// if the move is ineligible or leads to a loss
curBoard[row][col] = Mark.NONE;
curBoard = boardDeepCopy(theBoard);
}
}
}
curBoard = boardDeepCopy(theBoard);
for (int row = 0; row < theBoard.getSize(); row++) {
for (int col = 0; col < theBoard.getSize(); col++) {
if (curBoard.isEmpty(row, col)) {
curBoard.setMark(row, col, getMark());
answerBoard = isBoardInList(curBoard, loseBoards);
if (answerBoard != null) {
curBoard = boardDeepCopy(theBoard);
continue;
}
move(row, col);
if (theBoard.getState() == State.WIN) {
winBoards.add(curBoard);
}
prevBoard = boardDeepCopy(theBoard);
Integer[] answer = { row, col };
return answer;
}
}
}
// if all moves lead to a loss, forfeit
System.out.println("I lose...");
hashBoards.put(hash(prevBoard), false);
// if all moves lead to a loss, forfeit
for (int row = 0; row < theBoard.getSize(); row++) {
for (int col = 0; col < theBoard.getSize(); col++) {
if (curBoard[row][col] == Mark.NONE) {
if (curBoard.isEmpty(row, col)) {
if (isBoardInList(prevBoard, loseBoards) != null)
loseBoards.add(prevBoard);
move(row, col);
Integer[] answer = { row, col };
return answer;
}
}
}
// board is finished
return null;
}
......@@ -116,7 +112,8 @@ public class AIPlayer extends Player implements AutoPlayable {
* @param board
* @return
*/
public Board boardDeepCopy(Board newBoard, Board oldBoard) {
public Board boardDeepCopy(Board oldBoard) {
Board newBoard = new Board(oldBoard.getSize());
for (int row = 0; row < oldBoard.getSize(); row++) {
for (int col = 0; col < oldBoard.getSize(); col++) {
newBoard.setMark(row, col, oldBoard.getMark(row, col));
......@@ -126,13 +123,21 @@ public class AIPlayer extends Player implements AutoPlayable {
}
public void loss() {
System.out.println("I lose, writing this down");
System.out.println(prevBoard[0]);
hashBoards.put(hash(prevBoard), false);
if (isBoardInList(prevBoard, loseBoards) == null)
loseBoards.add(prevBoard);
}
public int hash(Mark[][] board) {
return Arrays.deepHashCode(board);
public Board isBoardInList(Board board1, List<Board> list) {
for (int i = 0; i < list.size(); i++) {
if (board1.isSameBoard(list.get(i)))
return list.get(i);
}
return null;
}
public void printList(List<Board> l) {
for (Board i : l) {
System.out.println(i);
}
}
}
\ 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