//Alpha Beta void Ai() { _bestMoveSet = null; _bestPiece = null; _nodeGeneration = 0; _maxPruning = 0; _minPruning = 0; _maxDepth = 0; AlphaBetaSearch(_checkerBoard, _currentTurn); //If it never found something b/c it was limited in depth if (_bestMoveSet == null && _bestPiece == null){ var randomPiece = Rand.Next(0, _checkerBoard.MovablePieces[_currentTurn].Count - 1); var randomMove = Rand.Next(0, _checkerBoard.MoveDict[_checkerBoard.MovablePieces[_currentTurn][randomPiece]].Count - 1); _bestMoveSet = _checkerBoard.MoveDict[_checkerBoard.MovablePieces[_currentTurn][randomPiece]][randomMove]; _bestPiece = _checkerBoard.MovablePieces[_currentTurn][randomPiece]; } _checkerBoard.SelectedPiece = _bestPiece; if (_bestPiece != null) Console.WriteLine("Moving Piece at Row: {0}, Col: {1}", _bestPiece.Row, _bestPiece.Col); if (_bestMoveSet != null) Console.WriteLine("{0}", _bestMoveSet.MoveList.ToList()); Console.WriteLine("Max Depth: {0}", _maxDepth); Console.WriteLine("# of Pruning in Max: {0}", _maxPruning); Console.WriteLine("# of Pruning in Min: {0}", _minPruning); Console.WriteLine("# of Nodes Generated: {0}", _nodeGeneration); Console.WriteLine("Time Elapsed: {0}", TimeElapsed); if (_selectedParticle != null) { _selectedParticle.status = ParticleStatus.Dead; _selectedParticle = null; } if (_bestPiece != null) _selectedParticle = _particleManager.Spawn(_particleManager.particleSystemInfoDict["fireSmall"], _checkerBoard.GetCenterOfTile(_bestPiece.Row, _bestPiece.Col)); Thread.Sleep(TimeDelay); //Handle the move to that location Debug.Assert(_bestMoveSet != null, "bestMove != null"); _checkerBoard.HandleMove(_bestMoveSet, TimeDelay); if (_selectedParticle != null) { _selectedParticle.status = ParticleStatus.Dead; _selectedParticle = null; } }
public void HandleInput(GameTime gameTime) { lastKeyboardState = currentKeyboardState; lastGamePadState = currentGamePadState; currentKeyboardState = Keyboard.GetState(); currentGamePadState = GamePad.GetState(PlayerIndex.One); // Check for exit. if (currentKeyboardState.IsKeyDown(Keys.Escape) || currentGamePadState.Buttons.Back == ButtonState.Pressed) { Exit(); } if (CurrentGameStatus == GameStatus.Setup) { } else if (CurrentGameStatus == GameStatus.InProgress) { if (Mouse.GetState().LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed) { if ((CurrentVsType == VsType.PlayerVsCpu && _currentTurn == PlayerColor) || CurrentVsType == VsType.PlayerVsPlayer) { Vector2 worldPosition = Vector2.Transform(new Vector2(Mouse.GetState().X, Mouse.GetState().Y), Matrix.Invert(_cam.get_transformation(GraphicsDevice))); CheckerTile tile = _checkerBoard.Board.GetCheckerTile(worldPosition); //Handle mouse input for the game if it is player vs player or (player vs cpu if it is the player's turn) if (CurrentVsType == VsType.PlayerVsPlayer || CurrentVsType == VsType.PlayerVsCpu && _currentTurn == PlayerColor) { //Check that the user clicked on a tile if (tile != null) { //User clicked on his own piece var checkerPiece = _checkerBoard.GetCheckerPiece(tile); if ((checkerPiece) != null && checkerPiece.Color == _currentTurn) { _checkerBoard.SelectedPiece = checkerPiece; if (_selectedParticle != null) { _selectedParticle.status = ParticleStatus.Dead; _selectedParticle = null; } _selectedParticle = _particleManager.Spawn(_particleManager.particleSystemInfoDict["fireSmall"], _checkerBoard.GetCenterOfTile(checkerPiece.Row, checkerPiece.Col)); } //If a user has selected a piece and the piece is a movable piece if (_checkerBoard.SelectedPiece != null && _checkerBoard.MovablePieces[_currentTurn].Contains(_checkerBoard.SelectedPiece) && _checkerBoard.MoveDict.ContainsKey(_checkerBoard.SelectedPiece) && _checkerBoard.MoveDict[_checkerBoard.SelectedPiece].Any(moveSet => moveSet.MoveList.First().Row == tile.Row && moveSet.MoveList.First().Col == tile.Col)) { //Handle the move to that location _checkerBoard.HandleMove(tile.Row, tile.Col); if (_selectedParticle != null) { _selectedParticle.status = ParticleStatus.Dead; _selectedParticle = null; } //Move to the next turn _currentTurn = _checkerBoard.NextTurn(_currentTurn); } } } } } } }