Beispiel #1
0
 public void Start()
 {
     if (!GameHistory.PeekLastMove().IsValid())
     {
         NextPlayersTurnEvent?.Invoke(this, NextPlayer);
     }
 }
Beispiel #2
0
        public void Capture(Move move)
        {
            if (move.Player != NextPlayer || move.From < 0)
            {
                throw new InvalidOperationException();
            }
            else
            {
                GameBoard.RemoveFrom(move.From);
                GameHistory.SaveMove(move);

                bool isDraw = GameHistory.SaveState(GameBoard.ToString());

                if (isDraw)
                {
                    GameEnded = true;
                    GameEndedEvent?.Invoke(this, FieldState.Empty);
                }
                else if (_whitesInHand == 0 && _blacksInHand == 0 && GameBoard.GetFieldsCount((FieldState)(-(int)NextPlayer)) < 3)
                {
                    GameEnded = true;
                    Winner    = NextPlayer;
                    GameEndedEvent?.Invoke(this, NextPlayer);
                }
                else
                {
                    NextPlayer = (FieldState)(-(int)NextPlayer);
                    NextPlayersTurnEvent?.Invoke(this, NextPlayer);
                }
            }
            UpdateStatsEvent?.Invoke(this, move);
        }
Beispiel #3
0
        private void PlacePawn(Move move)
        {
            if (move.Player != NextPlayer || move.To < 0 || !HasPawnsInHand(move.Player))
            {
                throw new InvalidOperationException();
            }
            else
            {
                GameBoard.PlaceAt(move.To, move.Player);
                GameHistory.SaveMove(move);

                if (move.Player == FieldState.White)
                {
                    _whitesInHand--;
                }
                else if (move.Player == FieldState.Black)
                {
                    _blacksInHand--;
                }

                if (GameBoard.GetField(move.To).IsInMill())
                {
                    CaptureEvent?.Invoke(this, move.Player);
                }
                else
                {
                    NextPlayer = (FieldState)(-(int)NextPlayer);
                    GameHistory.SaveState(GameBoard.ToString());
                    NextPlayersTurnEvent?.Invoke(this, NextPlayer);
                }
            }
        }
Beispiel #4
0
        public GameHistory Duplicate()
        {
            GameHistory duplicate = new GameHistory();

            duplicate._history    = new Dictionary <string, int>(_history);
            duplicate._moves      = new Stack <Move>(new Stack <Move>(_moves));
            duplicate._whiteMoves = new Stack <Move>(new Stack <Move>(_whiteMoves));
            duplicate._blackMoves = new Stack <Move>(new Stack <Move>(_blackMoves));
            return(duplicate);
        }
Beispiel #5
0
 public void Restart()
 {
     _whitesInHand = 9;
     _blacksInHand = 9;
     GameEnded     = false;
     NextPlayer    = FieldState.White;
     Winner        = FieldState.Empty;
     GameBoard.Clear();
     GameHistory.Clear();
 }
Beispiel #6
0
        public Game Duplicate()
        {
            Game duplicate = new Game(GameBoard.Duplicate(), GameHistory.Duplicate());

            duplicate._whitesInHand = _whitesInHand;
            duplicate._blacksInHand = _blacksInHand;
            duplicate.NextPlayer    = NextPlayer;
            duplicate.Winner        = Winner;
            duplicate.GameEnded     = GameEnded;
            return(duplicate);
        }
Beispiel #7
0
        private void MovePawn(Move move)
        {
            if (move.Player != NextPlayer)
            {
                throw new InvalidOperationException();
            }
            else if (move.From < 0 || move.To < 0)
            {
                GameEnded = true;
                Winner    = (FieldState)(-(int)NextPlayer);
                GameEndedEvent?.Invoke(this, (FieldState)(-(int)NextPlayer));
            }
            else
            {
                GameBoard.RemoveFrom(move.From);
                GameBoard.PlaceAt(move.To, move.Player);
                GameHistory.SaveMove(move);

                if (GameBoard.GetField(move.To).IsInMill())
                {
                    CaptureEvent?.Invoke(this, move.Player);
                }
                else
                {
                    bool isDraw = GameHistory.SaveState(GameBoard.ToString());
                    if (isDraw)
                    {
                        GameEnded = true;
                        GameEndedEvent?.Invoke(this, FieldState.Empty);
                    }
                    else
                    {
                        NextPlayer = (FieldState)(-(int)NextPlayer);
                        NextPlayersTurnEvent?.Invoke(this, NextPlayer);
                    }
                }
            }
        }
Beispiel #8
0
 public Game(GameBoard gameboard, GameHistory gameHistory)
 {
     GameBoard   = gameboard;
     GameHistory = gameHistory;
 }
Beispiel #9
0
 public Game()
 {
     GameBoard   = new GameBoard();
     GameHistory = new GameHistory();
 }
Beispiel #10
0
        public void Undo()
        {
            if (GameEnded)
            {
                GameEnded = false;
                Winner    = FieldState.Empty;
            }

            //LAST MOVE WAS GIVE-UP MOVE
            if (!GameHistory.PeekLastMove().IsValid())
            {
                GameHistory.PopLastMove();
            }

            GameHistory.RemoveState(GameBoard.ToString());

            Move lastMove = GameHistory.PopLastMove();

            //LAST MOVE WAS CAPTURE, UNDO ONE MORE MOVE
            if (lastMove.To < 0)
            {
                GameBoard.PlaceAt(lastMove.From, (FieldState)(-(int)lastMove.Player));

                Move oneMore = GameHistory.PopLastMove();

                if (oneMore.Player != lastMove.Player)
                {
                    throw new InvalidOperationException();
                }

                if (oneMore.To >= 0)
                {
                    GameBoard.RemoveFrom(oneMore.To);
                }
                if (oneMore.From >= 0)
                {
                    GameBoard.PlaceAt(oneMore.From, oneMore.Player);
                }

                // LAST MOVE WAS PLACEMENT, ADD PAWN TO HAND
                if (oneMore.From < 0)
                {
                    if (oneMore.Player == FieldState.White)
                    {
                        _whitesInHand++;
                    }
                    else if (oneMore.Player == FieldState.Black)
                    {
                        _blacksInHand++;
                    }
                }
            }
            // LAST MOVE WAS REGULAR MOVE
            else
            {
                if (lastMove.To >= 0)
                {
                    GameBoard.RemoveFrom(lastMove.To);
                }
                if (lastMove.From >= 0)
                {
                    GameBoard.PlaceAt(lastMove.From, lastMove.Player);
                }

                // IF LAST MOVE WAS PLACEMENT, ADD PAWN TO HAND
                if (lastMove.From < 0)
                {
                    if (lastMove.Player == FieldState.White)
                    {
                        _whitesInHand++;
                    }
                    else if (lastMove.Player == FieldState.Black)
                    {
                        _blacksInHand++;
                    }
                }
            }

            NextPlayer = lastMove.Player;
            NextPlayersTurnEvent?.Invoke(this, NextPlayer);
        }