Esempio n. 1
0
 public bool IsEqual(Position other)
 {
     bool equal = true;
     equal &= X == other.X;
     equal &= Y == other.Y;
     return equal;
 }
Esempio n. 2
0
 public AIBoard Clone()
 {
     //clone the positions array, and make new position objects. otherwise we would be sending the reference. (oops)
     Position[] clonedPositionsArr = new Position[10];
     for (int i = 1; i <= 9; i++)
     {
         clonedPositionsArr[i] = new Position() {Mark = this.Positions[i].Mark, PositionNum = this.Positions[i].PositionNum };
     }
     return new AIBoard(clonedPositionsArr);
 }
Esempio n. 3
0
        /// <summary>
        /// Check a winner.
        /// </summary>
        public List<Position> CheckWinner(bool player, Position last)
        {
            var chain = Enumerable.Repeat(new List<Position>(), 4).ToList();

            for (var i = 0; i < Math.Max(Size.X, Size.Y); i++)
            {
                // Directions
                var checks = new List<Position>
                {
                    new Position(last.x, i), new Position(i, last.y),
                    new Position(i, i - last.x + last.y), new Position(i, last.x + last.y - i)
                };

                // Check vertical, horizontal, diagonal, anti-diagonal
                for (var j = 0; j < checks.Count; j++)
                {
                    Piece piece;
                    if (_board.TryGetValue(checks[j], out piece) && player == piece.Player)
                    {
                        chain[j].Add(checks[j]);
                    }
                    else
                    {
                        chain[j].Clear();
                    }
                }

                // Got winner?
                var path = chain.Find(x => x.Count == WinSequence);
                if (path != null)
                {
                    return path;
                }
            }

            return null;
        }
Esempio n. 4
0
        public AIBoard(string[] inBoard)
        {
            //make the board
            for (int i = 0; i < inBoard.Length; i++)
            {
                Mark mark = Mark.Unmarked;
                if (inBoard[i] == "X")
                    mark = Mark.X;
                else if (inBoard[i] == "O")
                    mark = Mark.O;

                Positions[i + 1] = new Position()
                {
                    PositionNum = i + 1,
                    Mark = mark,
                };
            }
            //make the unmarked positions array
            for (int i = 1; i < Positions.Length; i++)
            {
                if (Positions[i].Mark == Mark.Unmarked)
                    UnmarkedPositions.Add(Positions[i].PositionNum);
            }
        }
Esempio n. 5
0
        private Position CheckTripletForNearWin(Position loc1, Position loc2, Position loc3, Mark markToCheck)
        {
            if (loc1.Mark == Mark.Unmarked && loc2.Mark == markToCheck && loc3.Mark == markToCheck)
                return new Position() {Mark = loc1.Mark, PositionNum = loc1.PositionNum};

            if (loc1.Mark == markToCheck && loc2.Mark == Mark.Unmarked && loc3.Mark == markToCheck)
                return new Position() { Mark = loc2.Mark, PositionNum = loc2.PositionNum };

            if (loc1.Mark == markToCheck && loc2.Mark == markToCheck && loc3.Mark == Mark.Unmarked)
                return new Position() { Mark = loc3.Mark, PositionNum = loc3.PositionNum };

            return null;
        }
Esempio n. 6
0
 public AIBoard(Position[] inArray)
 {
     this.Positions = inArray;
 }
Esempio n. 7
0
 Move(Position position)
 {
     _position = position;
 }
Esempio n. 8
0
 /// <summary>
 /// Get piece.
 /// </summary>
 public Piece GetPiece(Position position)
 {
     Piece piece;
     _board.TryGetValue(position, out piece);
     return piece;
 }
Esempio n. 9
0
        /// <summary>
        /// Update.
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            _mouseCurrent = Mouse.GetState();
            if (_mouseCurrent.LeftButton == ButtonState.Pressed &&
                _mousePrevious.LeftButton == ButtonState.Released &&
                _rectangle.Contains(new Point(_mouseCurrent.X, _mouseCurrent.Y)))
            {
                // Convert coordinates
                var translate = new Vector2(_mouseCurrent.X - _position.X,
                    _mouseCurrent.Y - _position.Y) / _cellSize;

                var position = new Position((int)translate.X, (int)translate.Y);
                // _board.Turn(position);
                // if (!_board.ContainsKey(position))
                // {
                //    _board.Add(position, new Piece(_playerCurrent));
                //    var path = _board.CheckWinner(_playerCurrent, position);
                //    if (path != null)
                //    {
                //        var center = new Vector2(_cellSize / 2, _cellSize / 2);
                //        Vector2 dir = new Vector2(path[1].x - path[0].x, path[1].y - path[0].y);
                //        _from = new Vector2(path.First().x, path.First().y) * _cellSize + (dir * _cellSize / 2) + _position + center;
                //        _to = new Vector2(path.Last().x, path.Last().y) * _cellSize - (dir * _cellSize / 2) + _position + center;
                //    }
                // }
            }
            _mousePrevious = _mouseCurrent;
        }