/// <summary> /// Places the ball on game field based on an event callback. /// </summary> /// <param name="sender">The sender.</param> /// <param name="args">The <see cref="BallPlacedOnGameFieldEventArgs"/> instance containing the event data.</param> private void PlaceBallOnGameField(object sender, BallPlacedOnGameFieldEventArgs args) { var viewModel = this.gameWindowViewModels.FirstOrDefault(x => x.Window.Id == args.WindowId); if (viewModel != null) { var position = GameHelpers.GetBallPositionFromGridCoordinates(args.Position); this.CurrentGame.CurrentBallPosition = position; this.CurrentGame.CurrentWindow = viewModel.Window; this.CurrentGame.CurrentPlayer.CurrentPlayerState = Player.PlayerState.BallPlaced; viewModel.PlaceBall(position, this.CurrentGame.CurrentPlayer.PlayerColor); } }
/// <summary> /// Places the ball on game field. /// </summary> private void PlaceBallOnGameField() { var possibleStartWindows = this.gameWindowViewModels.Where(x => x.Window.IsOwnWindow).Select(x => x.Window).ToArray(); var randomWindow = GameHelpers.GetRandomWindow(possibleStartWindows); if (randomWindow != null) { Point?ballPosition = GameHelpers.GetRandomBallPosition(randomWindow); if (ballPosition != null) { this.gameController.PlaceBallOnGameField(randomWindow.Id, ballPosition.Value); } } }
/// <summary> /// Calculates all relevant data and starts the round /// </summary> /// <returns>The task</returns> private async Task ComputerStartNewRound() { // Choose a random window out of the possible windows var randomWindow = GameHelpers.GetRandomWindow(this.computerWindows); if (randomWindow != null) { // Set the ball on a free grid part var ballPosition = GameHelpers.GetRandomBallPosition(randomWindow); if (ballPosition != null) { // Place the ball on the selected window this.PlaceBallOnGameField(randomWindow.Id, ballPosition.Value); // Simulate "thinking" time :) await Task.Delay(this.computerThinkingSimulationTime); // Start the round in a direction which does not end up in a hole within the initial move this.StartRound(GameHelpers.GetRandomBallDirection(randomWindow, GameHelpers.GetBallPositionFromGridCoordinates(ballPosition.Value))); } } }