public ArrayList getSuccessorBoards(string whoMoves) { ArrayList retVal = new ArrayList(); if (whoMoves.Equals(W)) { ArrayList whitePieces = this.getPieces(W); foreach (XYLocation fromloc in whitePieces) { ArrayList moves = this.getMoves(fromloc); foreach (XYLocation toloc in moves) { OctapawnBoard board = this.cloneBoard(); board.movePiece(fromloc, toloc); retVal.Add(board); } } } else { ArrayList blackPieces = this.getPieces(B); foreach (XYLocation fromloc in blackPieces) { ArrayList moves = this.getMoves(fromloc); foreach (XYLocation toloc in moves) { OctapawnBoard board = this.cloneBoard(); board.movePiece(fromloc, toloc); retVal.Add(board); } } } return(retVal); }
public override bool terminalTest(GameState state) { OctapawnBoard board = (OctapawnBoard)state.get("board"); // bool line = board.lineThroughBoard(); // bool filled = board.getNumberOfMarkedPositions() == 9; // return (line || filled); return(board.gameLost("B") || board.gameLost("W")); }
public Object clone() { OctapawnBoard newBoard = new OctapawnBoard(); for (int i = 0; i < boardSize; i++) { for (int j = 0; j < boardSize; j++) { String s = getValue(i, j); newBoard.setValue(i, j, s); } } return(newBoard); }
private int computeUtility(OctapawnBoard aBoard, string playerToMove) { //int retVal = 0; if (aBoard.gameLost("W")) { if (playerToMove.Equals("W")) { return(-1); } } else if (aBoard.gameLost("B")) { if (playerToMove.Equals("B")) { return(1); } } return(0); }
//get all possible successors from the current state public override ArrayList getSuccessorStates(GameState state) { GameState temp = presentState; ArrayList retVal = new ArrayList(); int parentLevel = getLevel(state); for (int i = 0; i < getMoves(state).Count; i++) { //XYLocation loc = (XYLocation) getMoves(state)[i]; //for each move, actually make the move OctapawnBoard move = (OctapawnBoard)getMoves(state)[i]; GameState aState = makeMove(state, move); aState.put("moveMade", move); aState.put("level", parentLevel + 1); retVal.Add(aState); } presentState = temp; return(retVal); }
private void btnAlphaBetaOctapawn_Click(object sender, System.EventArgs e) { this.textBox1.Text = "ALPHA BETA Octapawn "; this.textBox1.Text += System.Environment.NewLine; Octapawn t4 = new Octapawn(); while (!(t4.hasEnded())) { this.textBox1.Text += (System.Environment.NewLine + t4.getPlayerToMove(t4.getState()) + " playing ... "); t4.makeAlphaBetaMove(); GameState presentState = t4.getState(); OctapawnBoard board = t4.getBoard(presentState); this.textBox1.Text += System.Environment.NewLine; this.textBox1.Text += board.ToString(); this.textBox1.Refresh(); this.Refresh(); } this.textBox1.Text += "ALPHA BETA Octapawn DEMO done"; }
private void btnMiniMaxOctapawn_Click(object sender, System.EventArgs e) { this.textBox1.Text = "MINI MAX Octapawn"; this.textBox1.Text += System.Environment.NewLine; Octapawn t3 = new Octapawn(); while (!(t3.hasEnded())) { this.textBox1.Text += (System.Environment.NewLine + t3.getPlayerToMove(t3.getState()) + " playing"); this.textBox1.Text += System.Environment.NewLine; t3.makeMiniMaxMove(); GameState presentState = t3.getState(); OctapawnBoard board = t3.getBoard(presentState); this.textBox1.Text += System.Environment.NewLine; this.textBox1.Text += board.ToString(); this.textBox1.Refresh(); this.Refresh(); } this.textBox1.Text += "Mini MAX Octapawn DEMO done"; }
public Octapawn() { ArrayList moves = new ArrayList(); //first, add all possible moves from the initial state //assume white goes first //store moves as gameboards OctapawnBoard board = new OctapawnBoard(); moves = board.getSuccessorBoards("W"); //all the moves from the current state initialState.put("moves", moves); //the player whose move it is initialState.put("player", "W"); initialState.put("utility", 0); //the board initialState.put("board", board); //the number of moves that have been made so far initialState.put("level", 0); presentState = initialState; }
public override GameState makeMove(GameState state, Object o) { GameState retVal = new GameState(); //a move is the board itself OctapawnBoard move = ((OctapawnBoard)o).cloneBoard(); //we need to: //find out whose move it is string playerToMove = getPlayerToMove(state); ArrayList newMoves; //set the player whose move is next if (playerToMove.Equals("W")) { retVal.put("player", "B"); //add the new moves newMoves = move.getSuccessorBoards("B"); } else { retVal.put("player", "W"); //add the new moves newMoves = move.getSuccessorBoards("W"); } retVal.put("moves", newMoves); retVal.put("board", move); retVal.put("utility", computeUtility(move, getPlayerToMove(getState()))); retVal.put("level", getLevel(state) + 1); //if (newMoves.Count != 0) presentState = retVal; return(retVal); }
public override bool Equals(Object anObj) { bool retVal = true; OctapawnBoard anotherBoard = (OctapawnBoard)anObj; bool secondBreak = false; for (int i = 0; i < boardSize; i++) { for (int j = 0; j < boardSize; j++) { if (anotherBoard.getValue(i, j) != getValue(i, j)) { retVal = false; secondBreak = true; break; } } if (secondBreak == false) { break; } } return(retVal); }
public Object clone() { OctapawnBoard newBoard = new OctapawnBoard(); for (int i = 0; i < boardSize; i++) { for (int j = 0; j < boardSize; j++) { String s = getValue(i, j); newBoard.setValue(i, j, s); } } return newBoard; }
private int computeUtility(OctapawnBoard aBoard, string playerToMove) { //int retVal = 0; if (aBoard.gameLost("W")) { if (playerToMove.Equals("W")) return -1; } else if (aBoard.gameLost("B")) { if (playerToMove.Equals("B")) return 1; } return 0; }