public void Copy(Board board) { this.board = new Piece[8, 8]; this.turn = board.GetTurn(); this.playerColour = board.GetPlayerColour(); this.ai = board.GetAIState(); for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) { if (board.GetPiece(i, j) != null) { if (board.GetPiece(i, j).IsDux()) this.board[i, j] = new Dux(board.GetPiece(i, j).GetColour()); else this.board[i, j] = new Pawn(board.GetPiece(i, j).GetColour()); } else this.board[i, j] = null; } }
// ClickHandler for gameButton. private void GameButton_Click(object sender, EventArgs e) { Button b = (Button)sender; ai = aiCheck.Checked; playerColour = whiteBullet.Checked ? 1 : -1; this.cpuImg.Location = playerColour == 1 ? new Point(515, 30) : new Point(515, 540); if (ai) this.cpuImg.Show(); else this.cpuImg.Hide(); if (b.Text == "Play") { b.Text = "Restart"; game = new Board(); PrintBoard(); } else { RestartBoard(); this.msgBox.Text = ""; msg = ""; } }
public static int[] MakeTurn(Board game, int level) { int[] bestMove = new int[4]; int mostValuable = -999999999; int tempVal = 0; for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) { for (int k = 0; k < 8; k++) if (game.GetPiece(i, j) != null && game.GetPiece(k, j) == null) { if (level < MAXLEVEL - 1) tempVal = GoDeeper(game, i, j, k, j, level, mostValuable); else tempVal = EvaluateMove(game, i, j, k, j, level); if (tempVal != -DONE && tempVal >= mostValuable) { mostValuable = tempVal; bestMove[0] = i; bestMove[1] = j; bestMove[2] = k; bestMove[3] = j; } } for (int l = 0; l < 8; l++) if (game.GetPiece(i, j) != null && game.GetPiece(i, l) == null) { if (level < MAXLEVEL - 1) tempVal = GoDeeper(game, i, j, i, l, level, mostValuable); else tempVal = EvaluateMove(game, i, j, i, l, level); if (tempVal != -DONE && tempVal >= mostValuable) { mostValuable = tempVal; bestMove[0] = i; bestMove[1] = j; bestMove[2] = i; bestMove[3] = l; } } } /* for (int l = 0; l < 8; l++) { if ((i == k || j == l) && (game.GetPiece(i, j) != null && game.GetPiece(k, l) == null)) { if (level < MAXLEVEL - 1) tempVal = GoDeeper(game, i, j, k, l, level, mostValuable); else tempVal = EvaluateMove(game, i, j, k, l, level); if (tempVal != -DONE && tempVal >= mostValuable) { mostValuable = tempVal; bestMove[0] = i; bestMove[1] = j; bestMove[2] = k; bestMove[3] = l; } } }*/ // Debug //if(level == 0) // System.Windows.Forms.MessageBox.Show(mostValuable + ""); return bestMove; }
private static int GoDeeper(Board currentBoard, int srcX, int srcY, int dstX, int dstY, int level, int highestValue) { int[] move = new int[]{srcX, srcY, dstX, dstY}; int[] arr; Board tempBoard = new Board(); tempBoard.Copy(currentBoard); if (tempBoard.UpdateBoard(srcX, srcY, dstX, dstY)) { if (EvaluateBoard(tempBoard) < highestValue) return DONE; else if (EvaluateBoard(tempBoard) < 9000 || EvaluateBoard(tempBoard) > -1000) { arr = MakeTurn(tempBoard, level + 1); tempBoard.UpdateBoard(arr[0], arr[1], arr[2], arr[3]); return EvaluateBoard(tempBoard); } else return EvaluateBoard(tempBoard); } return DONE; }
private static int EvaluateMove(Board currentBoard, int srcX, int srcY, int dstX, int dstY, int level) { Board tempBoard = new Board(); tempBoard.Copy(currentBoard); if (tempBoard.UpdateBoard(srcX, srcY, dstX, dstY)) return EvaluateBoard(tempBoard); return DONE; }
// Gets a board an evaluates it. private static int EvaluateBoard(Board turn) { int value = 0; int playerColour = turn.GetPlayerColour(); for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) if (turn.GetPiece(i, j) != null) { if (turn.GetPiece(i, j).GetColour() == playerColour) value--; else value++; if (turn.GetPiece(i, j).IsDux()) { if (turn.GetPiece(i, j).GetColour() == playerColour) if (turn.IsDuxStuck(i, j)) value += 9001; if (turn.GetPiece(i, j).GetColour() == playerColour * -1) if (turn.IsDuxStuck(i, j)) value -= 1337; } } return value; }