Exemple #1
0
        private void finishGame()
        {
            int blackCount = 0;
            int whiteCount = 0;

            for (int i = 0; i <= gameGrid.GetUpperBound(0); i++)
            {
                for (int j = 0; j <= gameGrid.GetUpperBound(1); j++)
                {
                    if (gameGrid[i, j] == 0)
                    {
                        blackCount++;
                    }
                    else if (gameGrid[i, j] == 1)
                    {
                        whiteCount++;
                    }
                }
            }


            if (blackCount > whiteCount)
            {
                GameFinished?.Invoke(this, Turn.Black);
            }
            else if (whiteCount > blackCount)
            {
                GameFinished?.Invoke(this, Turn.White);
            }
            else
            {
                GameFinished?.Invoke(this, Turn.None);
            }
        }
Exemple #2
0
        private void RebindState()
        {
            var possibleMoves = PossibleMoves;

            foreach (var chessSquare in Squares)
            {
                chessSquare.Piece = _board.GetPieceAtPosition(chessSquare.Position);
                if (_board.IsCheck && chessSquare.Piece.Player == CurrentPlayer &&
                    chessSquare.Piece.PieceType == ChessPieceType.King)
                {
                    chessSquare.IsInCheck = true;
                }
                else if (!_board.IsCheck)
                {
                    chessSquare.IsInCheck = false;
                }
            }

            OnPropertyChanged(nameof(BoardValue));
            OnPropertyChanged(nameof(Squares));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));

            if (!_board.GetPossibleMoves().Any())
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
        }
Exemple #3
0
        private void raiseFinishGameEvent(string filePath)
        {
            string fileContent = null;

            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                StreamReader sr = new StreamReader(fs);
                fileContent = sr.ReadToEnd();
            }

            string[] lines = fileContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            int fieldHeight             = getHeightFromInitLine(lines.First());
            List <GameCommand> commands = new List <GameCommand>();

            for (int i = fieldHeight + 1; i < lines.Length; i++)
            {
                string      line    = lines[i];
                GameCommand command = BotJournalFileHelper.ParseGameCommand(line);

                if (command.BotId != null)
                {
                    commands.Add(command);
                }
            }

            // TODO: detect winner basing on last field state
            GameFinished?.Invoke(this, new GameFinishedEventArgs(null, commands));
        }
        public async Task ApplyMove(BoardPosition position)
        {
            var possMoves = mBoard.GetPossibleMoves() as IEnumerable <TicTacToeMove>;

            foreach (var move in possMoves)
            {
                if (move.Position.Equals(position))
                {
                    mBoard.ApplyMove(move);
                    break;
                }
            }

            RebindState();

            if (Players == NumberOfPlayers.One && !mBoard.IsFinished)
            {
                var bestMoveTask = Task.Run(() => mGameAi.FindBestMove(mBoard));

                var bestMove = await bestMoveTask;
                if (bestMove != null)
                {
                    mBoard.ApplyMove(bestMove as TicTacToeMove);
                }
                RebindState();
            }


            if (mBoard.IsFinished)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
        }
        /// <summary>
        /// Applies a move for the current player at the given position.
        /// </summary>
        public async Task ApplyMove(BoardPosition start, BoardPosition end, ChessPieceType cpt)
        {
            foreach (var move in mBoard.GetPossibleMoves())
            {
                if (move.StartPosition.Equals(start) & move.EndPosition.Equals(end))
                {
                    if(move.PromotionPiece != ChessPieceType.Empty & move.PromotionPiece == cpt | move.PromotionPiece == ChessPieceType.Empty)
                    {
                        mBoard.ApplyMove(move);
                        break;
                    }
                }
            }

            if (Players == NumberOfPlayers.One && !mBoard.IsFinished)
            {
                var bestMove = await Task.Run(() => mGameAi.FindBestMove(mBoard));
                if (bestMove != null)
                {
                    mBoard.ApplyMove(bestMove as ChessMove);
                }
            }

            RebindState();

            if (mBoard.IsFinished)
                GameFinished?.Invoke(this, new EventArgs());
        }
Exemple #6
0
        public void FinishGame(List <List <Point> > points)
        {
            int firstPlayerPointsCount  = BoardHandler.CountPoints(firstPlayerColor, points);
            int secondPlayerPointsCount = BoardHandler.CountPoints(secondPlayerColor, points);

            GameFinished?.Invoke(firstPlayerPointsCount, secondPlayerPointsCount);
        }
Exemple #7
0
        protected void InformManagerGameFinished()
        {
            GameFinishedArgs args = new GameFinishedArgs();

            args.newScore = _score;
            GameFinished?.Invoke(this, args);
        }
        public async Task ApplyMove(ChessMove move)
        {
            var possMoves = mBoard.GetPossibleMoves() as IEnumerable <ChessMove>;

            foreach (var m in possMoves)
            {
                if (m.StartPosition.Equals(move.StartPosition) && m.EndPosition.Equals(move.EndPosition))
                {
                    mBoard.ApplyMove(m);
                    //MessageBox.Show("Weight: " + mBoard.Weight.ToString());
                    break;
                }
            }

            RebindState();

            if (Players == NumberOfPlayers.One && CurrentPlayer == 2 && !mBoard.IsFinished)
            {
                var bestMove = await Task.Run(() => mGameAi.FindBestMove(mBoard));

                if (bestMove != null)
                {
                    mBoard.ApplyMove(bestMove);
                    RebindState();
                }
            }

            if (mBoard.IsCheckmate || mBoard.IsStalemate)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
        }
Exemple #9
0
        public void FinishGame()
        {
            int firstPlayerCellsCount  = field.CountCells(firstPlayerColor);
            int secondPlayerCellsCount = field.CountCells(secondPlayerColor);

            GameFinished?.Invoke(firstPlayerCellsCount, secondPlayerCellsCount);
        }
Exemple #10
0
 public void Handle(GameFinished e)
 {
     UsersHub.CurrentContext.Clients.Group(e.Id).gameFinished(new
     {
         Winners = e.Winners.Select(x => new WinnerViewModel(x))
     });
 }
Exemple #11
0
        internal void ApplyMove(BoardPosition start_position, BoardPosition end_position)
        {
            var possMoves = mChessBoard.GetPossibleMoves() as IEnumerable <ChessMove>;

            // Validate the move as possible.
            foreach (var move in possMoves)
            {
                if (move.EndPosition.Equals(end_position) && move.StartPosition.Equals(start_position))
                {
                    if (move.MoveType == ChessMoveType.PawnPromote)
                    {
                        //open new window
                        //in new window return chesspieceType
                        //apply in the new window and break
                        var pawnPromotionSelect = new ChessPromotionView(this, start_position, end_position);
                        pawnPromotionSelect.Show();
                        break;
                    }
                    else
                    {
                        mChessBoard.ApplyMove(move);

                        break;
                    }
                }
            }

            RebindState();
            if (mChessBoard.IsFinished)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
        }
Exemple #12
0
        public void SetWinner(PieceColor winnerColor)
        {
            Player winner = Player1.Color == winnerColor ? Player1 : Player2;
            Player looser = Player1.Color == winnerColor ? Player2 : Player1;

            GameFinished?.Invoke(winner, looser);
        }
        public void CreateLoopThread()
        {
            LoopThread = new Thread(new ThreadStart(() =>
            {
                while (!AbortLoop)
                {
                    if (Game.GameOngoing)
                    {
                        Stopwatch s = new Stopwatch();
                        s.Start();
                        Game.Solve_Tick();
                        s.Stop();
                        LastTickMs = s.ElapsedMilliseconds;
                        Console.WriteLine($"Process game tick end: {LastTickMs}ms");
                    }

                    if (Game.GameFinished)
                    {
                        Console.WriteLine($"Game finished. Stopping tick loop and firing events.");
                        GameFinished?.Invoke(this, new GameFinishedEventArgs(Game, Game.GameResult));
                        Stop();
                    }

                    Thread.Sleep(DelayMs);
                }
            }));
            LoopThread.IsBackground = true;
        }
Exemple #14
0
        public async Task ApplyMove(BoardPosition startPos, BoardPosition endPos, ChessPieceType pieceType)
        {
            var possMove = mBoard.GetPossibleMoves().Where(m => startPos == m.StartPosition && endPos == m.EndPosition);

            if (possMove.Count() == 1)
            {
                mBoard.ApplyMove(mBoard.GetPossibleMoves().Where(m => startPos == m.StartPosition && endPos == m.EndPosition).Single());
            }
            else//PROMOTION
            {
                mBoard.ApplyMove(mBoard.GetPossibleMoves().Where(m => startPos == m.StartPosition && endPos == m.EndPosition && pieceType == m.PromoteType).Single());
            }


            if (Players == NumberOfPlayers.One && !mBoard.IsFinished)
            {
                var bestMove     = Task.Run(() => mGameAi.FindBestMove(mBoard));
                var waitBestMove = await bestMove;
                if (waitBestMove != null)
                {
                    mBoard.ApplyMove(waitBestMove as ChessMove);
                }
            }
            RebindState();

            if (mBoard.IsFinished)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
        }
        /// <summary>
        /// Applies a move for the current player at the given position.
        /// </summary>
        public async Task ApplyMove(ChessMove cmove)
        {
            var possMoves = mBoard.GetPossibleMoves() as IEnumerable <ChessMove>;

            // Validate the move as possible.
            if (possMoves.Contains(cmove))
            {
                mBoard.ApplyMove(cmove);
            }
            RebindState();
            if (Players == NumberOfPlayers.One && !mBoard.IsFinished && this.CurrentPlayer == 2)
            {
                this.canundo = false;
                var bestMove = Task.Run(() => { return(mGameAi.FindBestMove(mBoard)); });
                var temp     = await bestMove;
                this.canundo = true;
                if (bestMove != null)
                {
                    mBoard.ApplyMove(temp as ChessMove);
                }
            }
            RebindState();
            if (mBoard.IsFinished)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
        }
Exemple #16
0
        /// <summary>
        /// Applies a move for the current player at the given position.
        /// </summary>
        public void ApplyMove(BoardPosition position)
        {
            var possMoves = mBoard.GetPossibleMoves() as IEnumerable <OthelloMove>;

            // Validate the move as possible.
            foreach (var move in possMoves)
            {
                if (move.Position.Equals(position))
                {
                    mBoard.ApplyMove(move);
                    break;
                }
            }

            if (Players == NumberOfPlayers.One && !mBoard.IsFinished)
            {
                var bestMove = mGameAi.FindBestMove(mBoard);
                if (bestMove != null)
                {
                    mBoard.ApplyMove(bestMove as OthelloMove);
                }
            }

            RebindState();

            if (mBoard.IsFinished)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
        }
Exemple #17
0
        private void RebindState()
        {
            PossibleMoves = new HashSet <BoardPosition>(
                from TicTacToeMove m in mBoard.GetPossibleMoves()
                select m.Position
                );
            var newSquares =
                from r in Enumerable.Range(0, 3)
                from c in Enumerable.Range(0, 3)
                select new BoardPosition(r, c);
            int i = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Player = mBoard.GetPieceAtPosition(pos);
                i++;
            }

            if (mBoard.IsFinished)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }

            OnPropertyChanged(nameof(BoardValue));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
        }
Exemple #18
0
        public override void Execute()
        {
            MoveObjectRequest moveObjectRequest = request as MoveObjectRequest;
            var room = gameService.FindGameByID(moveObjectRequest.GameID);

            bool?IsMoved = gameService.MoveObject(moveObjectRequest.GameID, moveObjectRequest.UserID, moveObjectRequest.Direction);

            if (room.Status != StatusGame.FINISHED)
            {
                response = new MoveObjectResponse(IsMoved);
                SendMessage(moveObjectRequest.UserID);

                Guid curUser = gameService.CurUser(moveObjectRequest.GameID);
                SendMessage(curUser, new YourStep()); // Шлём другому пользователю, что может ходить
            }
            else
            {
                // TODO: Можно сделать два разных пакета для победителя и проигравшего
                foreach (var us in room.GetUsersID)
                {
                    response = new GameFinished(moveObjectRequest.UserID, room.FindMazeByID(room.FindUserByID(us).MazeID).GetMazeStruct());
                    SendMessage(us, response);
                }
            }
        }
Exemple #19
0
        private void FinishGame(List <List <Cell> > cells)
        {
            var firstPlayerCellsCount  = Game.CalculatePlayersScore(_firstPlayerColor, cells);
            var secondPlayerCellsCount = Game.CalculatePlayersScore(_secondPlayerColor, cells);

            GameFinished?.Invoke(firstPlayerCellsCount, secondPlayerCellsCount);
        }
        public async Task ApplyMove(BoardPosition position)
        {
            try
            {
                String path = @"C:\Users\Nick\Documents\TimeWithAlphaBeta.txt";

                var possMoves = mBoard.GetPossibleMoves() as IEnumerable <OthelloMove>;
                foreach (var move in possMoves)
                {
                    if (move.Position.Equals(position))
                    {
                        mBoard.ApplyMove(move);
                        break;
                    }
                }
                RebindState();

                if (Players == NumberOfPlayers.One && !mBoard.IsFinished)
                {
                    //Initialize Time
                    DateTime start = DateTime.Now;
                    DateTime end;

                    var bestMove = await Task.Run(() => mGameAi.FindBestMove(mBoard));

                    //Record finish time
                    end = DateTime.Now;

                    System.IO.File.AppendAllText(path, "Move " + (++mCount) + ": " + (end - start).ToString() + Environment.NewLine);
                    //file.WriteLine((end - start));

                    if (bestMove != null)
                    {
                        mBoard.ApplyMove(bestMove);
                        RebindState();
                    }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            if (mBoard.PassCount == 2)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }

            if (PossibleMoves.Count == 1 && PossibleMoves.First().Row == -1)
            {
                CurrentPlayerMustPass?.Invoke(this, new EventArgs());
            }

            if (PossibleMoves.Count == 0 || mBoard.IsFinished)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
        }
        public void GameStatusIsPlayer2WonFrom3Moves()
        {
            var History = new List <wonDrawLost> {
                wonDrawLost.lost, wonDrawLost.won, wonDrawLost.lost
            };

            Assert.AreEqual(gameResult.player2Won, GameFinished.GameResult(History));
        }
Exemple #22
0
 /// <summary>
 /// Handles the event when the game is finished.
 /// </summary>
 private void OnUnlock(object sender, EventArgs args)
 {
     lockPickGame.gameObject.SetActive(false);
     Destroy(lockPickGame.gameObject);
     previousCamera.gameObject.SetActive(true);
     GameActive = false;
     GameFinished?.Invoke(this, EventArgs.Empty);
 }
        public void GameStatusIsNotFinished()
        {
            var History = new List <wonDrawLost> {
                wonDrawLost.lost
            };

            Assert.AreEqual(gameResult.notFinished, GameFinished.GameResult(History));
        }
        public void GameStatusIsDrawFrom2Moves()
        {
            var History = new List <wonDrawLost> {
                wonDrawLost.draw, wonDrawLost.draw
            };

            Assert.AreEqual(gameResult.draw, GameFinished.GameResult(History));
        }
        /// <summary>
        /// Applies a move for the current player at the given position.
        /// </summary>
        public async Task ApplyMove(BoardPosition position)
        {
            var possMoves = mBoard.GetPossibleMoves() as IEnumerable <ChessMove>;

            // Validate the move as possible.
            foreach (var move in possMoves)
            {
                if (SelectedSquare.Position.Equals(move.StartPosition) && move.EndPosition.Equals(position))
                {
                    if (move.MoveType == ChessMoveType.PawnPromote)
                    {
                        var window = new PawnPromotionWindow(this, move.StartPosition, move.EndPosition);
                        window.ShowDialog();
                        Promote = window.promotePicked;
                        ChessMove m = new ChessMove(move.StartPosition, move.EndPosition, ChessMove.StringToPromoType(Promote), ChessMoveType.PawnPromote);
                        mBoard.ApplyMove(m);
                        foreach (var s in mSquares)
                        {
                            s.KingInCheck = false;
                        }
                        SelectedState = false;
                        break;
                    }

                    else
                    {
                        mBoard.ApplyMove(move);
                        foreach (var s in mSquares)
                        {
                            s.KingInCheck = false;
                        }
                        SelectedState = false;
                        break;
                    }
                }
            }
            RebindState();

            if (Players == NumberOfPlayers.One && !mBoard.IsFinished)
            {
                var bestMove = await Task.Run(() => mGameAi.FindBestMove(mBoard));

                if (bestMove != null)
                {
                    mBoard.ApplyMove(bestMove as ChessMove);
                }
            }

            RebindState();

            if (mBoard.IsFinished)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
            //Show Boardweight in a message box when a move is applied
            //MessageBox.Show("Board Weight: " + mBoard.BoardWeight);
        }
 internal void ApplyPromotionMove(BoardPosition start_position, BoardPosition end_position, ChessPieceType pieceType)
 {
     mChessBoard.ApplyMove(new ChessMove(start_position, end_position, pieceType));
     RebindState();
     if (mChessBoard.IsFinished)
     {
         GameFinished?.Invoke(this, new EventArgs());
     }
 }
Exemple #27
0
        public void OnGameFinished(GameFinished e)
        {
            if (ActiveGame.State != GameState.InProgress)
                throw new DXGameException("no_game_in_progress");
            if (ActiveGame.Id != e.Game)
                throw new DXGameException("another_game_is_currently_active");

            ApplyEvent(new GameFinishReceived(Id, e.Game, Version));
        }
Exemple #28
0
        public void ApplyMove(BoardPosition position)
        {
            var possMoves = mBoard.GetPossibleMoves() as IEnumerable <OthelloMove>;

            foreach (var move in possMoves)
            {
                if (move.Position.Equals(position))
                {
                    mBoard.ApplyMove(move);
                    break;
                }
            }

            if (Players == NumberOfPlayers.One && !mBoard.IsFinished)
            {
                var bestMove = mGameAi.FindBestMove(mBoard);
                if (bestMove != null)
                {
                    mBoard.ApplyMove(bestMove);
                }
            }

            PossibleMoves = new HashSet <BoardPosition>(
                from OthelloMove m in mBoard.GetPossibleMoves()
                select m.Position
                );
            var newSquares =
                from r in Enumerable.Range(0, 8)
                from c in Enumerable.Range(0, 8)
                select new BoardPosition(r, c);
            int i = 0;

            foreach (var pos in newSquares)
            {
                mSquares[i].Player = mBoard.GetPieceAtPosition(pos);
                i++;
            }

            OnPropertyChanged(nameof(BoardValue));
            OnPropertyChanged(nameof(CurrentPlayer));
            OnPropertyChanged(nameof(CanUndo));
            if (mBoard.PassCount == 2)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }

            if (PossibleMoves.Count == 1 && PossibleMoves.First().Row == -1)
            {
                CurrentPlayerMustPass?.Invoke(this, new EventArgs());
            }

            if (PossibleMoves.Count == 0 || mBoard.IsFinished)
            {
                GameFinished?.Invoke(this, new EventArgs());
            }
        }
Exemple #29
0
 private void OnGameFinished()
 {
     GameFinished?.Invoke(
         this,
         new GameFinishedEventArgs
     {
         GameId        = _gameId,
         NumberOfTurns = _turns,
         WinnerTankId  = _winner.TankId
     });
 }
Exemple #30
0
 void CheckGame()
 {
     for (int i = 0; i < blockCount; i++)
     {
         if (towers[2][i] == null)
         {
             return;
         }
     }
     GameFinished?.Invoke();
 }
Exemple #31
0
 public GamePlay()
 {
     gameFinished = null;
     winningFinish = null;
     losingFinish = null;
 }