Commit f33f2e8a authored by Stefano Cobelli's avatar Stefano Cobelli

Redoing ai player because hashing 2d arrays doesn't work well

Blocker player needs to add priorities to corners and middle
GUI needs greying out when options arent available
parent e7942050
......@@ -125,6 +125,10 @@ public class TTTController implements ActionListener {
if (!theGame.isPlayable()) {
// performs relative actions if game is over
if (theGame.getState() == State.WIN) {
if (theGame.getOtherPlayer() instanceof AIPlayer) {
((AIPlayer) theGame.getOtherPlayer())
.loss();
}
JOptionPane.showMessageDialog(null,
theGame.getPlayerTurn() + " wins!");
if (theGame.getPlayerTurn() == "X")
......@@ -166,9 +170,7 @@ 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")
......@@ -244,4 +246,5 @@ public class TTTController implements ActionListener {
public String incrementString(String num) {
return String.valueOf(Integer.parseInt(num) + 1);
}
}
......@@ -282,11 +282,23 @@ public class Board {
public boolean isWinFor(Player player) {
return (updateBoardState() == player.getMark());
}
/**\
/**
* \
*
* @return board as a Mark[][]
*/
public Mark[][] getMarkArr() {
return board;
}
public boolean isSameBoard(Board otherBoard) {
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
if (this.getMarkArr()[row][col] != otherBoard.getMarkArr()[row][col])
return false;
}
}
return true;
}
}
......@@ -193,4 +193,13 @@ public class Game {
public void setPlayerO(Player player) {
players[1] = player;
}
/**
* @return the player who's turn it isn't
*/
public Player getOtherPlayer() {
if (currentPlayer == 0)
return players[1];
return players[0];
}
}
......@@ -7,7 +7,10 @@
*/
package model.player;
import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import model.Board;
import model.Board.State;
......@@ -26,7 +29,11 @@ public class AIPlayer extends Player implements AutoPlayable {
* move by the ai, after the other player has gone, so the last mvoe done is
* always AIPlayer
*/
private static Hashtable<Mark[][], Boolean> hashBoards = new Hashtable<Mark[][], Boolean>();
private static HashMap<Integer, Boolean> hashBoards = new HashMap<Integer, Boolean>();
/** */
private static List<Board> winBoards = new ArrayList<Board>();
/** */
private static List<Board> loseBoards = new ArrayList<Board>();
/** the last board hashed */
private static Mark[][] prevBoard;
......@@ -47,11 +54,12 @@ public class AIPlayer extends Player implements AutoPlayable {
*/
@Override
public Integer[] decideMove() {
Mark[][] curBoard = twoDArrayDeepCopy(theBoard.getMarkArr());
Board curBoard = new Board(theBoard.getSize());
curBoard = boardDeepCopy(curBoard, theBoard);
for (int row = 0; row < theBoard.getSize(); row++) {
for (int col = 0; col < theBoard.getSize(); col++) {
if (curBoard[row][col] == Mark.NONE) {
curBoard[row][col] = getMark();
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);
......@@ -60,14 +68,14 @@ public class AIPlayer extends Player implements AutoPlayable {
// false, so it knows to win
if (theBoard.getState() == State.WIN) {
System.out.println("I win! Writing this down...");
hashBoards.put(curBoard, true);
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(curBoard, false);
hashBoards.put(hash(curBoard), false);
}
}
}
......@@ -88,7 +96,7 @@ public class AIPlayer extends Player implements AutoPlayable {
}
// if all moves lead to a loss, forfeit
System.out.println("I lose...");
hashBoards.put(prevBoard, false);
hashBoards.put(hash(prevBoard), false);
for (int row = 0; row < theBoard.getSize(); row++) {
for (int col = 0; col < theBoard.getSize(); col++) {
......@@ -108,17 +116,23 @@ public class AIPlayer extends Player implements AutoPlayable {
* @param board
* @return
*/
public static Mark[][] twoDArrayDeepCopy(Mark[][] board) {
Mark[][] newArray = board.clone();
// you have to clone all the
for (int row = 0; row < board.length; row++) {
newArray[row] = board[row].clone();
public Board boardDeepCopy(Board newBoard, Board oldBoard) {
for (int row = 0; row < oldBoard.getSize(); row++) {
for (int col = 0; col < oldBoard.getSize(); col++) {
newBoard.setMark(row, col, oldBoard.getMark(row, col));
}
}
return newArray;
return newBoard;
}
public void loss() {
System.out.println("I lose, writing this down");
hashBoards.put(prevBoard, false);
System.out.println(prevBoard[0]);
hashBoards.put(hash(prevBoard), false);
}
public int hash(Mark[][] board) {
return Arrays.deepHashCode(board);
}
}
\ No newline at end of file
......@@ -87,6 +87,9 @@ public class BlockerPlayer extends Player implements AutoPlayable {
return answer;
}
// prioritizing middle
// if (theBoard.isEmpty(theBoard.getSize() / 2, theBoard.getSize() / 2))
System.out.println("Getting random");
Integer[][] coors = new Integer[theBoard.getSize() * theBoard.getSize()][2];
......
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