Exemplo n.º 1
0
        private async Task PlayMove()
        {
            int row = _moves.Dequeue();
            int col = _moves.Dequeue();

            if (_gameState[row][col] != _playerId)
            {
                StatusLabel.Content = "Invalid Move";
                return;
            }
            Move move = await _repository.PlayMoveAsync(
                row,
                col,
                _moves.Dequeue(),
                _moves.Dequeue()
                );

            string text          = "Invlaid move";
            bool   waitForPlayer = false;

            if (move.ValidMove)
            {
                _gameState = await _repository.GetGameStateAsync();

                UpdateGrid();
                if (move.AvailableMoves.Any())
                {
                    text = "You can jump again";
                }
                else
                {
                    waitForPlayer = true;
                }
            }
            if (waitForPlayer)
            {
                await WaitForPlayer();

                _gameState = await _repository.GetGameStateAsync();

                UpdateGrid();
                await CheckForWinner();
            }
            else
            {
                StatusLabel.Content = text;
            }
        }
        public async Task StartAsync()
        {
            try
            {
                _gameState = await _repo.GetGameStateAsync();

                _playerId = await _repo.GetPlayerIdAsync();

                int winner = await _repo.CheckForWinnerAsync();

                int fromX, fromY, toX, toY;
                while (true)
                {
                    while (!await _repo.IsMyTurnAsync(_playerId))
                    {
                        Console.WriteLine("waiting...");
                        Thread.Sleep(1000);
                    }
                    winner = await _repo.CheckForWinnerAsync();

                    if (winner != 0)
                    {
                        Console.WriteLine("Player {0} won", winner);
                        break;
                    }
                    else
                    {
                        Move move;
                        do
                        {
                            _gameState = await _repo.GetGameStateAsync();

                            Console.WriteLine(GetGameString(_gameState));
                            Console.WriteLine("Enter in fromRow");
                            int.TryParse(Console.ReadLine(), out fromX);
                            Console.WriteLine("Enter in fromCol");
                            int.TryParse(Console.ReadLine(), out fromY);
                            Console.WriteLine("Enter in toRow");
                            int.TryParse(Console.ReadLine(), out toX);
                            Console.WriteLine("Enter in toCol");
                            int.TryParse(Console.ReadLine(), out toY);
                            move = await _repo.PlayMoveAsync(fromX - 1, fromY - 1, toX - 1, toY - 1);
                        }while (!move.ValidMove);
                        _gameState = await _repo.GetGameStateAsync();

                        Console.WriteLine(GetGameString(_gameState));
                        while (move.AvailableMoves.Any())
                        {
                            Console.WriteLine("You can jump again");
                            Console.WriteLine("Enter in fromRow");
                            int.TryParse(Console.ReadLine(), out fromX);
                            Console.WriteLine("Enter in fromCol");
                            int.TryParse(Console.ReadLine(), out fromY);
                            Console.WriteLine("Enter in toRow");
                            int.TryParse(Console.ReadLine(), out toX);
                            Console.WriteLine("Enter in toCol");
                            int.TryParse(Console.ReadLine(), out toY);
                            move = await _repo.PlayMoveAsync(fromX - 1, fromY - 1, toX - 1, toY - 1);
                        }
                        _gameState = await _repo.GetGameStateAsync();

                        Console.WriteLine(GetGameString(_gameState));
                    }
                }
                Console.Read();
            }
            finally
            {
                await _repo.ResetGameAsync();
            }
        }