//a function to take in the input and process it private void RestartGame() { Console.Clear(); _cgame = null; LogicDriver pOne = null, pTwo = null; var playerWantsToGoFirst = false; var newForm = new SplashScreen(); newForm.ShowDialog(); var result = newForm.Result; //depending on the TypeOfMatch, the new CheckersGame must have appropriate logic drivers if(result.TypeOfMatch == MatchType.PvP) { pOne = new PlayerLogicDriver(this); pTwo = new PlayerLogicDriver(this); CurrentGameState = GameState.SelectingPiece; } else if (result.TypeOfMatch == MatchType.PvC) { pOne = new PlayerLogicDriver(this); pTwo = new ComputerLogicDriver(result.ComputerOneDifficulty); CurrentGameState = GameState.WaitingForComputer; const string humanPlayerText = "Player One"; var goFirstResult = WinForms.MessageBox.Show(String.Format("Would you ({0}) like to go first?", humanPlayerText), "Choice", WinForms.MessageBoxButtons.YesNo); if (goFirstResult == WinForms.DialogResult.Yes) { playerWantsToGoFirst = true; CurrentGameState = GameState.SelectingPiece; } } else if (result.TypeOfMatch == MatchType.CvC) { pOne = new ComputerLogicDriver(result.ComputerOneDifficulty); pTwo = new ComputerLogicDriver(result.ComputerTwoDifficulty); } _cgame = new CheckersGameDriver(); _cgame.Start(6, pOne, pTwo, new GuiDrawer(_spriteBatch, Content), playerWantsToGoFirst); }
public override TurnResult GetNextMove(CheckersGameDriver gameDriver) { if(_xnaCheckersDriver.CurrentGameState == GameState.WaitingForComputer) _xnaCheckersDriver.CurrentGameState = GameState.SelectingPiece; var result = TurnResult.NotDone; //let the player continue doing what hes doing until he makes a move // e.g. clicking a piece and then clicking a new place if (_xnaCheckersDriver.CurrentGameState == GameState.SelectingPiece || _xnaCheckersDriver.CurrentGameState == GameState.MovingPiece) { if (InputManager.LeftMouseClick()) { int mx = Mouse.GetState().X, my = Mouse.GetState().Y; int ix = mx/XnaCheckersDriver.TileSize, iy = my/XnaCheckersDriver.TileSize; if (_xnaCheckersDriver.IsWithinBoard(mx, my)) { //see if mouse clicked on a piece var piece = gameDriver.Board.Pieces.GetPieceAtPosition(ix, iy); //see if piece is for the right player if (piece != null && piece.Color == Color) { SetSelectedPiece(gameDriver, piece); } else { } //ignore the player trying to move a piece that is not his } else { } //ignore a click on the gameDriver that isnt on a piece } } if (_xnaCheckersDriver.CurrentGameState == GameState.MovingPiece) { Debug.Assert(CurrentSelectedPiece != null); int mx = Mouse.GetState().X, my = Mouse.GetState().Y; int ix = mx/XnaCheckersDriver.TileSize, iy = my/XnaCheckersDriver.TileSize; //user dragged it to a new slot if (ix != CurrentSelectedPiece.X || iy != CurrentSelectedPiece.Y) { if (InputManager.LeftMouseClick()) //user clicked a new slot { try { //check if the clicked location matches one of the available moves for the currently selected piece var clickedPiece = gameDriver.Board.Pieces.GetPieceAtPosition(ix, iy); var appropriateMove = _selectedAvailableMoves.FirstOrDefault(mr => mr.FinalPieceLocation.X == ix && mr.FinalPieceLocation.Y == iy); var anyJumpsAvailable = _allAvailableMoves.Any(mr => mr.Type == MoveType.Jump); if (clickedPiece != null && clickedPiece.Color == Color) SetSelectedPiece(gameDriver, clickedPiece); else if (appropriateMove != null) { if (!(anyJumpsAvailable && appropriateMove.Type == MoveType.Forward)) { result = gameDriver.Board.MovePiece(appropriateMove, Color); _xnaCheckersDriver.CurrentGameState = GameState.SelectingPiece; CurrentSelectedPiece = null; } else throw new InvalidMoveException(CurrentSelectedPiece, ix, iy); } } catch (InvalidMoveException ex) { WinForms.MessageBox.Show(String.Format("Error: Invalid move (cannot move {0} to {1}).", gameDriver.Board.TileBoard.GetNameForLocation( ex.MovingPiece.X, ex.MovingPiece.Y), gameDriver.Board.TileBoard.GetNameForLocation( ex.AttemptedLocation)), "Error", WinForms.MessageBoxButtons.OK); CurrentSelectedPiece = null; _xnaCheckersDriver.CurrentGameState = GameState.SelectingPiece; } } } } return result; }
//must be implemented by all logic driver's // return true if it is done finding its next move public override TurnResult GetNextMove(CheckersGameDriver gameDriver) { if (_logicThread == null) { //starts up a new thread if it isnt already running and tells it to run alpha-beta search _finalResult = TurnResult.NotDone; _logicThread = new Thread(() => { ResetCounts(); AlphaBetaStartTime = DateTime.Now; var maxDepth = 0; var move = AlphaBeta(gameDriver.Board, ref maxDepth); if (move != null) { var result = gameDriver.Board.MovePiece(move, Color); _finalResult = result; } }); _logicThread.Start(); } else { //if there is indeed a thread running, if it is done, then we are done and return the finalResult if(!_logicThread.IsAlive) { _logicThread.Join(); _logicThread = null; } } return _finalResult; }
private void SetSelectedPiece(CheckersGameDriver gameDriver, CheckersPiece piece) { _xnaCheckersDriver.CurrentGameState = GameState.MovingPiece; CurrentSelectedPiece = piece; _allAvailableMoves = gameDriver.Board.GetAllAvailableMoves(Color); _selectedAvailableMoves = gameDriver.Board.GetAvailableMovesForPiece(piece, false); }