private void RunAll(CellSign firstPlayerSign) { var winLossDistribution = firstPlayerSign == CellSign.X ? _firstPlayerXDistribution : _firstPlayerODistribution; Parallel.For(0, _runCount, new ParallelOptions { MaxDegreeOfParallelism = _concurrencyLevel }, i => { var sign = RunSingle(firstPlayerSign); lock (_statsLock) { if (sign == firstPlayerSign) { winLossDistribution.Wins++; } else if (sign == CellSign.Empty) { winLossDistribution.Draws++; } else { winLossDistribution.Losses++; } } }); }
private CellSign RunSingle(CellSign firstPlayerSign) { var firstPlayerKind = firstPlayerSign == CellSign.X ? _firstBotKind : _secondBotKind; var secondPlayerKind = firstPlayerSign == CellSign.X ? _secondBotKind : _firstBotKind; try { var config = new GameConfiguration { BotTurnLength = _botTurnLength, FirstPlayer = BotFactory.BuildBot(firstPlayerKind), SecondPlayer = BotFactory.BuildBot(secondPlayerKind), Height = _height, Width = _width }; var game = Game.CreateNewGame(config); if (game == null) { SetFailedToCreate(); return(CellSign.Empty); } var gameEndedEventArgs = game.RunSilently(); return(GameStateToSign(gameEndedEventArgs.State)); } catch (Exception ex) { LogException(LogLevel.Warning, ex); SetFailedToCreate(); return(CellSign.Empty); } }
private void SwitchPlayer() { _currentPlayer = _currentPlayer == _configuration.FirstPlayer ? _configuration.SecondPlayer : _configuration.FirstPlayer; _currentSign = _currentSign == CellSign.O ? CellSign.X : CellSign.O; }
private void LoadGame() { _game = Game.CreateNewGame(_configuration); if (_game == null) { return; } _currentPlayer = _configuration.FirstPlayer; _currentSign = CellSign.X; FirstPlayerNameLabel.Text = PlayerDescription(_configuration.FirstPlayer); SecondPlayerNameLabel.Text = PlayerDescription(_configuration.SecondPlayer); _game.GameStateChanged += ProcessGameStateChanged; _game.GameEnded += ProcessGameEnded; ReloadGrid(); LoadCellControls(); _game.Start(); if (!IsHumansTurn()) { return; } lock (_stateLock) { _waitingForTurn = true; } }
public void SendTurnMesage(int index, CellSign sign) { Send((short)MessageType.MakeTurn, new MakeTurnMessage { index = index, whoseTurnWas = sign }); }
public void Clear() { for (int i = 0; i < cells.Length; ++i) { cells[i] = CellSign.None; } winner = CellSign.None; }
public void MakeTurn(int index, CellSign sign) { cells[index] = sign; CheckWinner(); if (Winner != CellSign.None || IsFullMarked) { OnGameEnd(Winner); } }
public bool TrySetSign(int2 pos, CellSign playerSign) { if (field[pos.x, pos.y] != CellSign.Empty) { return(false); } field[pos.x, pos.y] = playerSign; return(true); }
public void NewRound() { board.Clear(); firstTurn = UnityEngine.Random.value > 0.5f ? CellSign.Cross : CellSign.Nought; bool rand = UnityEngine.Random.value > 0.5f; firstPlayer.Sign = rand ? CellSign.Cross : CellSign.Nought; secondPlayer.Sign = !rand ? CellSign.Cross : CellSign.Nought; }
private void GameEnd(CellSign winner) { if (firstPlayer.Sign == winner) { firstPlayer.Score++; } else if (secondPlayer.Sign == winner) { secondPlayer.Score++; } }
private void ClientNewRound(StartNewRoundMessage msg) { playerRoundSign = msg.yourSign; HideAll(); ShowGameElements(); ShowScoreText(msg.yourScore, msg.enemyScore); gameBoard.Lock = playerRoundSign != msg.whoseTurn; ShowInfoText(gameBoard.Lock ? "Enemy turn" : "Your turn"); }
private Game(GameConfiguration configuration) { _width = configuration.Width; _height = configuration.Height; _botTurnLength = configuration.BotTurnLength; _firstPlayer = configuration.FirstPlayer; _secondPlayer = configuration.SecondPlayer; _currentSign = CellSign.X; _currentPlayer = _firstPlayer; _field = new CellSign[_height, _width]; LogMessage(LogLevel.Info, $"Game {_height}x{_width} created ({_firstPlayer.Name} vs {_secondPlayer.Name})"); }
private CellSign[,] BuildFieldCopy() { var fieldCopy = new CellSign[_height, _width]; for (var i = 0; i < _height; i++) { for (var j = 0; j < _width; j++) { fieldCopy[i, j] = _field[i, j]; } } return(fieldCopy); }
private bool IsFillSideDiagonalLine(CellSign sign) { var size = Size; for (int i = 0; i < size.x; ++i) { if (field[i, size.x - 1 - i] != sign) { return(false); } } return(true); }
public async Task LoadPictureAsync(CellSign sign) { var justMovedName = sign == CellSign.O ? "OJustMoved.png" : "XJustMoved.png"; var currentSource = ImageSourceCache[justMovedName]; SignImage.Source = currentSource; await Task.Delay(300); // Different image is already loaded (for example, game ended) if (SignImage.Source != currentSource) { return; } var name = sign == CellSign.O ? "O.png" : "X.png"; SignImage.Source = ImageSourceCache[name]; }
void CheckWinner() { for (int i = 0; i < winCells.GetLength(0); ++i) { if (cells[winCells[i, 0]] == cells[winCells[i, 1]] && cells[winCells[i, 1]] == cells[winCells[i, 2]]) { if (cells[winCells[i, 0]] == CellSign.None) { continue; } winner = cells[winCells[i, 0]]; return; } } }
public bool IsSignFillAnyLine(CellSign sign) { if (IsFillAnyHorizontalLine(sign)) { return(true); } if (IsFillAnyVerticalLine(sign)) { return(true); } if (IsFillMainDiagonalLine(sign)) { return(true); } if (IsFillSideDiagonalLine(sign)) { return(true); } return(false); }
private bool IsFillAnyHorizontalLine(CellSign sign) { var size = Size; for (int y = 0; y < size.y; ++y) { for (int x = 0; x <= size.x; ++x) { if (x == size.x) { return(true); } if (field[x, y] != sign) { break; } } } return(false); }
public void LoadWonPicture(CellSign sign) { var name = sign == CellSign.O ? "OWon.png" : "XWon.png"; SignImage.Source = ImageSourceCache[name]; }
public void LoadPicture(CellSign sign) { SignImage.Source = sign == CellSign.O ? OImage : XImage; Logger.LogEntry("DRAW", LogLevel.Trace, $"{sign} loaded into cell ({_x}, {_y})"); }
public void MarkCell(int n, CellSign sign) { cells[n].Icon = sign == CellSign.None ? null : sign == CellSign.Cross ? crossSprite : noughtSprite; }
public AiPlayer(CellSign sign) { Sign = sign; random = new Random(); }
public Player(CellSign sign, IPlayerTurnController turnController) { Sign = sign; this.turnController = turnController; }
public void LoadWonPicture(CellSign sign) { SignImage.Source = sign == CellSign.O ? OWonImage : XWonImage; }