private async void StartGameButton_Click(object sender, RoutedEventArgs e) { (sender as Button).Visibility = Visibility.Hidden; _playerId = await _repository.GetPlayerIdAsync(); if (_playerId == 1) { PlayerLabel.Content = "You are Red"; } else { PlayerLabel.Content = "You are black"; } _gameState = await _repository.GetGameStateAsync(); UpdateGrid(); CheckersGrid.IsEnabled = false; await WaitForPlayer(); _gameState = await _repository.GetGameStateAsync(); UpdateGrid(); CheckersGrid.IsEnabled = true; }
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(); } }