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

            winner = CellSign.None;
        }
Ejemplo n.º 7
0
        public void MakeTurn(int index, CellSign sign)
        {
            cells[index] = sign;
            CheckWinner();

            if (Winner != CellSign.None || IsFullMarked)
            {
                OnGameEnd(Winner);
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 10
0
 private void GameEnd(CellSign winner)
 {
     if (firstPlayer.Sign == winner)
     {
         firstPlayer.Score++;
     }
     else if (secondPlayer.Sign == winner)
     {
         secondPlayer.Score++;
     }
 }
Ejemplo n.º 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");
        }
Ejemplo n.º 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})");
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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];
        }
Ejemplo n.º 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;
                }
            }
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 19
0
        public void LoadWonPicture(CellSign sign)
        {
            var name = sign == CellSign.O ? "OWon.png" : "XWon.png";

            SignImage.Source = ImageSourceCache[name];
        }
Ejemplo n.º 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})");
 }
Ejemplo n.º 21
0
 public void MarkCell(int n, CellSign sign)
 {
     cells[n].Icon = sign == CellSign.None ? null : sign == CellSign.Cross ? crossSprite : noughtSprite;
 }
Ejemplo n.º 22
0
 public AiPlayer(CellSign sign)
 {
     Sign   = sign;
     random = new Random();
 }
Ejemplo n.º 23
0
 public Player(CellSign sign, IPlayerTurnController turnController)
 {
     Sign = sign;
     this.turnController = turnController;
 }
Ejemplo n.º 24
0
 public void LoadWonPicture(CellSign sign)
 {
     SignImage.Source = sign == CellSign.O ? OWonImage : XWonImage;
 }