// this is the wrapper function for the recursive alpha-beta bestmove function. // this is an iterative deepening going 2 ply deeper if there's still time to search public GameNode BestMove(EvalSettings EvalSettings) { Console.WriteLine("BestMove Start"); GameNode best = null; MyEvalSettings = EvalSettings; this.MyHitTimeCutoff = false; for (int Depth = 2; Depth <= this.Depth; Depth++) { Console.WriteLine("BestMove Depth: " + Depth.ToString()); GameNode temp; temp = this.BestMove(this.MyBoard, Depth, EvalSettings.WorstScore, EvalSettings.BestScore, true); if (temp != null && temp.Move != null) { if (best != null) { best.Dispose(); } best = temp; } else if (temp == null) { break; } } Console.WriteLine("BestMove End"); return(best); }
private int EvalThree(EvalSettings Evals) { Player opponent = (MyPlayerTurn == Player.White) ? Player.Black : Player.White; int ret = 0; // a board in which I lose is the worst! we can stop right here. if (this.HasWon(opponent)) { return(Evals.WorstScore); } // give me points for each piece I have captured for (int i = 9; i > (this.MyPlaced[(int)opponent] + MyUnplaced[(int)opponent]); i--) { ret += Evals.CapturedPiece; } // a board that gives me an opportunity to form a mill is good ret += (Evals.MillFormable * this.CountMills(Player.Neutral, MyPlayerTurn)); // give me points for each mill I blocked ret += (Evals.MillBlocked * this.CountMills(this.MyPlayerTurn, opponent)); return(ret); }
// function to perform a computer move on the board public Move ComputerMove(EvalSettings EvalSettings, EvaluationBoardDelegate EvalBoardDelegate) { Move ret = null; MyEvalBoardDelegate = EvalBoardDelegate; Console.WriteLine("Determining computer's move..."); Move.OurMovesGenerated = 0; GameNode gameNode = BestMove(EvalSettings); if (gameNode == null) { if (this.MyBoard.HasWon(Player.Black)) { Console.WriteLine("Black has won!"); } else if (this.MyBoard.HasWon(Player.White)) { Console.WriteLine("White has won!"); } // this next bit is because we didn't find a move. I'm guessing it's because we're only a move or two away // from losing. So get the list of moves and just toss out the first one. else { Console.WriteLine("No best move found. You may be about to win!"); Move[] moveList = this.MyBoard.GetMoves(); if (moveList[0] != null) { this.MyBoard.Move(moveList[0]); ret = moveList[0]; for (int i = 0; moveList[i] != null && i < Board.MAX_MOVES; i++) { moveList[i].Dispose(); } } else { ret = null; } // Delete moveList = null; } } else { Console.WriteLine("Score: {0}", gameNode.Score); this.MyBoard.Move(gameNode.Move); ret = gameNode.Move; } return(ret); }
/// <summary> /// Evaluation test by evaluation settings. /// </summary> /// <param name="Evals">Evaluation settings</param> /// <returns>Stage depth</returns> private int EvalOne(EvalSettings Evals) { Player opponent = (MyPlayerTurn == Player.White) ? Player.Black : Player.White; int ret = 0; //// a board that gives me an opportunity to form a mill is good //ret+= (Evals.MillFormable*this.CountMills(GameController::NEUTRAL, MyPlayerTurn)); //// give me points for each mill I have //ret+= (Evals.mill_formed*this.CountMills(MyPlayerTurn, MyPlayerTurn)); // give me points for each mill I blocked ret += (Evals.MillBlocked * this.CountMills(MyPlayerTurn, opponent)); // give me points for each piece I have on the board. I get points for range of movement // this is specific for stage one for (int i = 0; i < 24; i++) { if (MyPositions[i].Player == MyPlayerTurn) { if (this.MyPositions[i].Up != null) { ret += Evals.AdjacentSpot; } if (this.MyPositions[i].Down != null) { ret += Evals.AdjacentSpot; } if (this.MyPositions[i].Left != null) { ret += Evals.AdjacentSpot; } if (this.MyPositions[i].Right != null) { ret += Evals.AdjacentSpot; } } } // give me points for each piece I have captured for (int i = 9; i > (MyPlaced[(int)opponent] + MyUnplaced[(int)opponent]); i--) { ret += Evals.CapturedPiece; } // take away points for each piece my opponent has captured for (int i = 9; i > (MyPlaced[(int)this.MyPlayerTurn] + MyUnplaced[(int)this.MyPlayerTurn]); i--) { ret += Evals.LostPiece; } // take away points for each mill my opponent has formed ret += (Evals.MillOpponent * this.CountMills(opponent, opponent)); return(ret); }
/// <summary> /// Evaluation test /// </summary> /// <param name="Evals">By evaluation settings</param> /// <returns>Stage depth</returns> private int EvalTest(EvalSettings Evals) { if (this.GetStage() == GameState.One) { return(this.EvalOneTest(Evals)); } else if (this.GetStage() == GameState.Two) { return(this.EvalTwoTest(Evals)); } else { return(this.EvalThreeTest(Evals)); } }
/// <summary> /// Evaluate the move by the evaluation settings. /// </summary> /// <param name="Evals"></param> /// <returns></returns> public int Evaluate(EvalSettings Evals) { if (this.GetStage() == GameState.One) { return(this.EvalOne(Evals)); } else if (this.GetStage() == GameState.Two) { return(this.EvalTwo(Evals)); } else { return(this.EvalThree(Evals)); } }
/// <summary> /// /// </summary> /// <param name="Evals">Evaluation settings.</param> /// <returns></returns> private int EvalOneTest(EvalSettings Evals) { Player opponent = (MyPlayerTurn == Player.White) ? Player.Black : Player.White; int ret = 0; // give me points for each piece I have on the board. I get points for range of movement // this is specific for stage one for (int i = 0; i < 24; i++) { if (MyPositions[i].Player == MyPlayerTurn) { if (MyPositions[i].Up != null) { ret += Evals.AdjacentSpot; } if (MyPositions[i].Down != null) { ret += Evals.AdjacentSpot; } if (MyPositions[i].Left != null) { ret += Evals.AdjacentSpot; } if (MyPositions[i].Right != null) { ret += Evals.AdjacentSpot; } } } // give me points for each piece I have captured for (int i = 9; i > (MyPlaced[(int)opponent] + MyUnplaced[(int)opponent]); i--) { ret += Evals.CapturedPiece; } // take away points for each piece my opponent has captured for (int i = 9; i > (MyPlaced[(int)this.MyPlayerTurn] + MyUnplaced[(int)this.MyPlayerTurn]); i--) { ret += Evals.LostPiece; } return(ret); }
private int EvalThreeTest(EvalSettings Evals) { //GameController::Player opponent = (MyPlayerTurn == GameController::WHITE) ? GameController::BLACK : GameController::WHITE; //int ret = 0; //// a board in which I lose is the worst! we can stop right here. //if (hasWon(opponent)) // return Evals.worst_score; //// a board in which I win is great! //if (hasWon(MyPlayerTurn)) // return Evals.best_score; //// give me points for each piece I have captured //for (int i = 9; i > (MyPlaced[opponent] + MyUnplaced[opponent]); i--) // ret+= Evals.CapturedPiece; //// take away points for each piece my opponent has captured //for (int i = 9; i > (MyPlaced[MyPlayerTurn] + MyUnplaced[MyPlayerTurn]); i--) // ret+= Evals.LostPiece; //return ret; return(this.EvalOne(Evals)); }
/// <summary> /// Constructor /// </summary> /// <param name="timeLimit">Tieme limit for work.</param> public GameController(int TimeLimit, byte Depth) { this.MyTimeLimit = TimeLimit; this.MyEvalSettings = new EvalSettings(); this.Depth = Depth; }
/// <summary> /// /// </summary> /// <param name="Evals"></param> /// <returns></returns> private int EvalTwo(EvalSettings Evals) { Player opponent = (MyPlayerTurn == Player.White) ? Player.Black : Player.White; int ret = 0; // a board in which I lose is the worst! we can stop right here. if (this.HasWon(opponent)) { return(Evals.WorstScore); } // a board in which I win is the best! if (this.HasWon(this.MyPlayerTurn)) { return(Evals.BestScore); } // give me points for each piece I have captured for (int i = 9; i > (this.MyPlaced[(int)opponent] + MyUnplaced[(int)opponent]); i--) { ret += Evals.CapturedPiece; } // take away points for each piece my opponent has captured for (int i = 9; i > (MyPlaced[(int)this.MyPlayerTurn] + MyUnplaced[(int)this.MyPlayerTurn]); i--) { ret += Evals.LostPiece; } // a board that gives me an opportunity to form a mill is good ret += (Evals.MillFormable * this.CountMills(Player.Neutral, this.MyPlayerTurn)); // give me points for each mill I have ret += (Evals.MillFormed * this.CountMills(this.MyPlayerTurn, this.MyPlayerTurn)); // take away points for each mill my opponent has formed ret += (Evals.MillOpponent * this.CountMills(opponent, opponent)); // give me points for each spot of my opponent that is blocked for (int i = 0; i < 24; i++) { if (MyPositions[i].Player == opponent) { bool blocked = true; if (MyPositions[i].Up != null && MyPositions[i].Up.Player == Player.Neutral) { blocked = false; } else if (MyPositions[i].Down != null && MyPositions[i].Down.Player == Player.Neutral) { blocked = false; } else if (MyPositions[i].Left != null && MyPositions[i].Left.Player == Player.Neutral) { blocked = false; } else if (MyPositions[i].Right != null && MyPositions[i].Right.Player == Player.Neutral) { blocked = false; } if (blocked) { ret += Evals.BlockedOpponentSpot; } } } return(ret); }