示例#1
0
        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++;
                    }
                }
            });
        }
示例#2
0
        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;
            }
        }
示例#5
0
 public void SendTurnMesage(int index, CellSign sign)
 {
     Send((short)MessageType.MakeTurn,
          new MakeTurnMessage
     {
         index        = index,
         whoseTurnWas = sign
     });
 }
示例#6
0
        public void Clear()
        {
            for (int i = 0; i < cells.Length; ++i)
            {
                cells[i] = CellSign.None;
            }

            winner = CellSign.None;
        }
示例#7
0
        public void MakeTurn(int index, CellSign sign)
        {
            cells[index] = sign;
            CheckWinner();

            if (Winner != CellSign.None || IsFullMarked)
            {
                OnGameEnd(Winner);
            }
        }
示例#8
0
        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);
        }
示例#9
0
        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;
        }
示例#10
0
 private void GameEnd(CellSign winner)
 {
     if (firstPlayer.Sign == winner)
     {
         firstPlayer.Score++;
     }
     else if (secondPlayer.Sign == winner)
     {
         secondPlayer.Score++;
     }
 }
示例#11
0
        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");
        }
示例#12
0
        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})");
        }
示例#13
0
        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);
        }
示例#14
0
        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);
        }
示例#15
0
        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];
        }
示例#16
0
        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;
                }
            }
        }
示例#17
0
        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);
        }
示例#18
0
        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);
        }
示例#19
0
        public void LoadWonPicture(CellSign sign)
        {
            var name = sign == CellSign.O ? "OWon.png" : "XWon.png";

            SignImage.Source = ImageSourceCache[name];
        }
示例#20
0
 public void LoadPicture(CellSign sign)
 {
     SignImage.Source = sign == CellSign.O ? OImage : XImage;
     Logger.LogEntry("DRAW", LogLevel.Trace, $"{sign} loaded into cell ({_x}, {_y})");
 }
示例#21
0
 public void MarkCell(int n, CellSign sign)
 {
     cells[n].Icon = sign == CellSign.None ? null : sign == CellSign.Cross ? crossSprite : noughtSprite;
 }
示例#22
0
 public AiPlayer(CellSign sign)
 {
     Sign   = sign;
     random = new Random();
 }
示例#23
0
 public Player(CellSign sign, IPlayerTurnController turnController)
 {
     Sign = sign;
     this.turnController = turnController;
 }
示例#24
0
 public void LoadWonPicture(CellSign sign)
 {
     SignImage.Source = sign == CellSign.O ? OWonImage : XWonImage;
 }