public void MakeMove(ActionData action) { try { while (PauseX.Pause) { Thread.Sleep(100); } ExecuteAction(action); if (OnNewMove != null) { OnNewMove(action); } PlayerToMove = PlayerToMove == PlayerOne ? PlayerTwo : PlayerOne; if (!CheckFinishGame()) { Thread.Sleep(400); PlayerToMove.Move(Position); } else { FinishGame(); } } catch (Exception e) { Log.Error(e); } }
private void BoardView_AskForFinish(object sender, Tuple <Square, Square> e) { //Game is finished if (GamePosition.Status != Position.GameResult.InProcess) { BoardView.CancelMove(); MessageBox.Show("Error. Can't finish the move. Game is finished."); } int?Move = ConvertMove(e.Item1, e.Item2, GamePosition); if (Move is null) { BoardView.CancelMove(); } else { if (PlayerToMove is HumanPlayer) { PlayerToMove.MakeMove((int)Move); } else if (PlayerWaiting is HumanPlayer) { PlayerWaiting.PreMoves.Push((int)Move); } else { MessageBox.Show("Don't bother the comps! Let them play!"); } } }
/// <summary> /// Player wants to play a move /// </summary> /// <param name="sender">Player</param> /// <param name="Move">Move to play</param> private void Player_MakesMove(object sender, int Move) { if (GamePosition.Status != Position.GameResult.InProcess) { MessageBox.Show("Error. Game is finished. No moves allowed"); } //Engine tried to move but couldn't if (Move == Engine.NO_MOVES) { MessageBox.Show(PerfectChess.Move.Details(Move)); return; } //Check for legality if (!GamePosition.LegalMoves().Contains(Move)) { MessageBox.Show(PerfectChess.Move.Details(Move)); return; } //Make the move GamePosition.Make(Move); if (PlayerWaiting is HumanPlayer) { BoardView.FinishMove(Move); } else { BoardView.PerformComputerMove(Move); } //Add visual move effects ApplyMoveEffects(); if (GamePosition.Status != Position.GameResult.InProcess) { return; } //if (GamePosition.GameFinished) return; //Tell the other guy he can move PlayerToMove.YourMove(GamePosition.DeepCopy()); }
public void StartGame(PositionData startPosition, Player playerOne, Player playerTwo) { Actions.Clear(); PlayerOne = playerOne; PlayerOne.Logic.OnActionDecided = this.MakeMove; PlayerTwo = playerTwo; PlayerTwo.Logic.OnActionDecided = this.MakeMove; Position = startPosition; PlayerToMove = playerOne; if (!CheckFinishGame()) { PlayerToMove.Move(Position); } while (MatchInfo == null) { Thread.Sleep(20); } }
private void startGame() { Result = new Result(); // raise HumanEnabled event HumanEnabled(false); PlayersReady -= startGame; //disconnect(this, SIGNAL(playersReady()), this, SLOT(startGame())); if (IsFinished) { return; } m_gameInProgress = true; for (int i = 0; i < 2; i++) { Player player = m_player[i]; //Q_ASSERT(player != 0); //Q_ASSERT(player->isReady()); if (player.State == PlayerState.Disconnected) { return; } /* if (!player->supportsVariant(m_board->variant())) * { * qDebug("%s doesn't support variant %s", * qPrintable(player->name()), qPrintable(m_board->variant())); * m_result = Chess::Result(Chess::Result::ResultError); * stop(); * return; * }*/ } resetBoard(); initializePgn(); Started(this); // Raise a Started event FENChanged(Game.FENStart); // Raise a FENChanged event for (int side = 0; side < 2; side++) { // Q_ASSERT(m_timeControl[side].isValid()); m_player[side].TimeControl = m_timeControl[side]; m_player[side].NewGame(side, m_player[side ^ 1], Game); } // Play the forced opening moves first for (int i = 0; i < Moves.Count; i++) { List <Movement> move = new List <Movement>() { Moves[i] }; // Q_ASSERT(m_board->isLegalMove(move)); addPGNMove(move, "book"); PlayerToMove.MakeBookMove(move); PlayerToWait.MakeMove(move); //Game.PerformMove( move, true ); emitLastMove(); if (!Game.Result.IsNone) { // qDebug("Every move was played from the book"); Stop(); return; } } for (int i = 0; i < 2; i++) { m_player[i].MoveMade += OnMoveMade; // connect(m_player[i], SIGNAL(moveMade(Chess::Move)), this, SLOT(onMoveMade(Chess::Move))); if (m_player[i].IsHuman) { ((HumanPlayer)m_player[i]).WokeUp += Resume; //connect(m_player[i], SIGNAL(wokeUp()), this, SLOT(resume())); } } startTurn(); }