Exemple #1
0
        public bool Equals(PlacePosition p)
        {
            if (p == null)
                return false;

            return (X == p.X) && (Y == p.Y);
        }
        public bool Equals(PlacePosition p)
        {
            if (p == null)
            {
                return(false);
            }

            return((X == p.X) && (Y == p.Y));
        }
Exemple #3
0
        void IGamePlayerSync.MakeMove(PlacePosition pos, long playerUserId)
        {
            var playerId = GetPlayerId(playerUserId);
            if (playerId != _currentPlayerId)
                throw new ResultException(ResultCodeType.NotYourTurn);

            if (pos.X < 0 || pos.X >= Rule.BoardSize ||
                pos.Y < 0 || pos.Y >= Rule.BoardSize ||
                _boardGridMarks[pos.X, pos.Y] != 0)
            {
                throw new ResultException(ResultCodeType.BadPosition);
            }

            MakeMove(pos);
        }
Exemple #4
0
        private void MakeMove(PlacePosition pos)
        {
            _boardGridMarks[pos.X, pos.Y] = _currentPlayerId;
            _movePositions.Add(pos);

            var matchedRow = Logic.FindMatchedRow(_boardGridMarks);
            var drawed = _movePositions.Count >= Rule.BoardSize * Rule.BoardSize;
            var nextTurnPlayerId = (matchedRow == null && drawed == false) ? 3 - _currentPlayerId : 0;

            NotifyToAllObservers((id, o) => o.MakeMove(_currentPlayerId, pos, nextTurnPlayerId));

            if (matchedRow != null)
            {
                EndGame(_currentPlayerId);
            }
            else if (drawed)
            {
                EndGame(0);
            }
            else
            {
                ScheduleTurnTimeout(_movePositions.Count);
                _currentPlayerId = nextTurnPlayerId;
            }
        }
Exemple #5
0
 void IGameObserver.MakeMove(int playerId, PlacePosition pos, int nextTurnPlayerId)
 {
     _boardGridMarks[pos.X, pos.Y] = playerId;
     if (_isPlaying && nextTurnPlayerId == _playerId)
         RunTask(() => ThinkAndMakeMoveAsync());
 }
Exemple #6
0
        // simple AI
        public static PlacePosition DetermineMove(int[,] board, int playerId)
        {
            // If the player has two in a row, they can place a third to get three in a row.
            foreach (var rps in RowPositions)
            {
                var           x             = 0;
                PlacePosition emptyPosition = null;
                foreach (var pos in rps)
                {
                    var v = board[pos.X, pos.Y];
                    if (v == 0)
                    {
                        emptyPosition = pos;
                    }
                    else if (v == playerId)
                    {
                        x += 1;
                    }
                }
                if (x == Rule.BoardSize - 1 && emptyPosition != null)
                {
                    return(emptyPosition);
                }
            }

            // If the opponent has two in a row, the player must play the third themselves to block the opponent.
            foreach (var rps in RowPositions)
            {
                var           x             = 0;
                PlacePosition emptyPosition = null;
                foreach (var pos in rps)
                {
                    var v = board[pos.X, pos.Y];
                    if (v == 0)
                    {
                        emptyPosition = pos;
                    }
                    else if (v != playerId)
                    {
                        x += 1;
                    }
                }
                if (x == Rule.BoardSize - 1 && emptyPosition != null)
                {
                    return(emptyPosition);
                }
            }

            // Random pick
            var positions = new List <PlacePosition>();

            for (int x = 0; x < Rule.BoardSize; x++)
            {
                for (int y = 0; y < Rule.BoardSize; y++)
                {
                    if (board[x, y] == 0)
                    {
                        positions.Add(new PlacePosition(x, y));
                    }
                }
            }
            if (positions.Count > 0)
            {
                return(positions[new Random().Next(positions.Count)]);
            }

            return(null);
        }