Example #1
0
 public void ItemTest2()
 {
     Board target = new Board();
     int y = -1;
     int x = 9;
     target[y, x] = new Draught(9, -1, DraughtType.None, Players.PlayerOne);
 }
Example #2
0
        public static DraughtsGame Deserialize(string path)
        {
            if (string.IsNullOrWhiteSpace(path))
                throw new ArgumentException();

            Draught[,] draughts = new Draught[8, 8];
            Board board;
            DraughtsGame game;

            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                using (BinaryReader br = new BinaryReader(fs))
                {
                    Players currentTurn = (Players)br.ReadByte();
                    int leftOne = br.ReadByte();
                    int leftTwo = br.ReadByte();

                    for (int i = 0; i < leftOne + leftTwo; i++)
                    {
                        int drY = br.ReadByte();
                        int drX = br.ReadByte();
                        DraughtType type = (DraughtType)br.ReadByte();
                        Players player = (Players)br.ReadByte();

                        draughts[drY, drX] = new Draught(new BoardPoint(drY, drX), type, player);
                    }

                    board = new Board(draughts, leftOne, leftTwo);
                    game = new DraughtsGame() { CurrentTurn = currentTurn, GameBoard = board };
                }
            }

            return game;
        }
Example #3
0
 public void KillOwnDraught()
 {
     Draught[,] draughts = new Draught[8, 8];
     draughts[0, 1] = new Draught(1, 0, DraughtType.Queen, Players.PlayerOne);
     draughts[1, 2] = new Draught(2, 1, DraughtType.None, Players.PlayerOne);
     draughts[7, 0] = new Draught(0, 7, DraughtType.None, Players.PlayerOne);
     Board board = new Board(draughts, 2, 1);
     DraughtsGame game = new DraughtsGame() { GameBoard = board, CurrentTurn = Players.PlayerOne };
     game.Turn(1, 0, 3, 2);
 }
Example #4
0
        public void EndCellIsNotNullTurnTest()
        {
            Draught[,] draughts = new Draught[8, 8];
            draughts[0, 1] = new Draught(1, 0, DraughtType.None, Players.PlayerOne);
            draughts[1, 2] = new Draught(2, 1, DraughtType.None, Players.PlayerOne);
            draughts[7, 1] = new Draught(0, 7, DraughtType.None, Players.PlayerTwo);
            Board board = new Board(draughts, 2, 1);
            DraughtsGame game = new DraughtsGame() { GameBoard = board, CurrentTurn = Players.PlayerOne };

            game.Turn(1, 0, 2, 1);
        }
Example #5
0
        public void DraughtConstructorTest1()
        {
            int x = 3;
            int y = 2;
            Players player = Players.PlayerOne;
            Draught target = new Draught(x, y, player);
            target.Type = DraughtType.Queen;

            Assert.AreEqual(x, target.X);
            Assert.AreEqual(y, target.Y);
            Assert.AreEqual(DraughtType.Queen, target.Type);
            Assert.AreEqual(player, target.Player);
        }
Example #6
0
        public void DraughtConstructorTest()
        {
            int x = 4;
            int y = 2;
            DraughtType type = DraughtType.Queen;
            Players player = Players.PlayerTwo;
            Draught target = new Draught(x, y, type, player);

            Assert.AreEqual(x, target.X);
            Assert.AreEqual(y, target.Y);
            Assert.AreEqual(type, target.Type);
            Assert.AreEqual(player, target.Player);
        }
Example #7
0
        public void DraughtToQueen()
        {
            Draught[,] draughts = new Draught[8, 8];
            draughts[1, 2] = new Draught(2, 1, DraughtType.None, Players.PlayerTwo);
            Draught nonQueen = new Draught(3, 2, DraughtType.None, Players.PlayerOne);
            draughts[2, 3] = nonQueen;

            Board board = new Board(draughts, 1, 1);
            DraughtsGame game = new DraughtsGame() { GameBoard = board };
            game.Turn(3, 2, 1, 0);

            Assert.AreEqual(DraughtType.None, nonQueen.Type);
        }
Example #8
0
        private BoardPoint CheckRange(Draught queen, int fromX, int toX, int fromY, int toY)
        {
            int draughtsCount = 0;
            BoardPoint point = new BoardPoint(-1, -1);

            for (int y = fromY; y <= toY; y++)
            {
                for (int x = fromX; x <= toX; x++)
                {
                    if (board[y, x] != null)
                    {
                        if (board[y, x].Player == queen.Player)
                            throw new DraughtsGameException("Вы не можете побить свою шашку.");

                        draughtsCount++;
                        if (draughtsCount > 1)
                        {
                            throw new DraughtsGameException("Бить больше, чем одну шашку нельзя.");
                        }
                        point = new BoardPoint(y, x);
                    }
                }
            }

            return point;
        }
Example #9
0
        public void Move(Draught draught, int x, int y)
        {
            if (draught == null)
                throw new ArgumentNullException();

            Move(draught.X, draught.Y, x, y);
        }
Example #10
0
        public void Move(Draught draught, BoardPoint point)
        {
            if (draught == null)
                throw new ArgumentNullException();

            Move(draught.X, draught.Y, point.X, point.Y);
        }
Example #11
0
 public Board(Draught[,] draughts, int leftDraughtsPlayerOne, int leftDraughtsPlayerTwo)
 {
     this.draughts = draughts;
     this.leftDraughtsPlayerOne = leftDraughtsPlayerOne;
     this.leftDraughtsPlayerTwo = leftDraughtsPlayerTwo;
 }
Example #12
0
        public void QueenKillTest()
        {
            Draught[,] draughts = new Draught[8, 8];
            Draught quuen = new Draught(1, 0, DraughtType.Queen, Players.PlayerOne); ;
            draughts[0, 1] = quuen;
            Draught dr = new Draught(3, 2, DraughtType.None, Players.PlayerTwo);
            draughts[2, 3] = dr;

            Board board = new Board(draughts, 1, 0);
            DraughtsGame game = new DraughtsGame { GameBoard = board };

            game.Turn(1, 0, 7, 6);
            Assert.AreEqual<Draught>(quuen, game.GameBoard[6, 7]);
        }
Example #13
0
        private void TurnKill(Draught draught, int endX, int endY, int centerX, int centerY)
        {
            DraughtIsQueen(draught, endY);
            board.Move(draught, endX, endY);
            board.Remove(centerX, centerY);
            canKill = false;

            if (!DraughtCanKill(draught, endX, endY))
                ChangeCurrentPlayer();
            OnBoardChanged();
            CheckWin();
        }
Example #14
0
        public void QueenWrongTurnTest()
        {
            Draught[,] draughts = new Draught[8, 8];
            Draught dr = new Draught(1, 0, DraughtType.Queen, Players.PlayerOne); ;
            draughts[0, 1] = dr;
            Board board = new Board(draughts, 1, 0);
            DraughtsGame game = new DraughtsGame { GameBoard = board };

            game.Turn(1, 0, 6, 7);
        }
Example #15
0
        public void QueenTurnTest()
        {
            Draught[,] draughts = new Draught[8, 8];
            Draught dr = new Draught(1, 0, DraughtType.Queen, Players.PlayerOne);
            draughts[0, 1] = dr;
            Board board = new Board(draughts, 1, 1);
            DraughtsGame game = new DraughtsGame { GameBoard = board };

            // вниз-вправо
            game.Turn(1, 0, 4, 3);
            Assert.AreEqual<Draught>(dr, game.GameBoard[3, 4]);
            game.CurrentTurn = Players.PlayerOne;

            // вверх-вправо
            game.Turn(4, 3, 6, 1);
            Assert.AreEqual<Draught>(dr, game.GameBoard[1, 6]);
            game.CurrentTurn = Players.PlayerOne;

            // вниз-влево
            game.Turn(6, 1, 2, 5);
            Assert.AreEqual<Draught>(dr, game.GameBoard[5, 2]);
            game.CurrentTurn = Players.PlayerOne;

            // вверх-влево
            game.Turn(2, 5, 0, 3);
            Assert.AreEqual<Draught>(dr, game.GameBoard[3, 0]);
            game.CurrentTurn = Players.PlayerOne;
        }
Example #16
0
        public void QueenTurnBackTest()
        {
            Draught[,] draughts = new Draught[8, 8];
            Draught dr = new Draught(4, 3, DraughtType.Queen, Players.PlayerOne); ;
            draughts[3, 4] = dr;
            Board board = new Board(draughts, 1, 0);
            DraughtsGame game = new DraughtsGame { GameBoard = board };

            game.Turn(4, 3, 1, 0);
            Assert.AreEqual<Draught>(dr, game.GameBoard[0, 1]);
        }
Example #17
0
        private bool DraughtCanKill(Draught draught, int startX, int startY)
        {
            if ((startY - 1 >= 0 && startX + 1 <= 7) &&
                (startY - 2 >= 0 && startX + 2 <= 7) &&
                board[startY - 1, startX + 1] != null &&
                board[startY - 1, startX + 1].Player != draught.Player &&
                board[startY - 2, startX + 2] == null)
            {
                canKill = true;
                canDraugtKill = draught;
            }
            else if ((startY - 1 >= 0 && startX - 1 >= 0) &&
                (startY - 2 >= 0 && startX - 2 >= 0) &&
                board[startY - 1, startX - 1] != null &&
                board[startY - 1, startX - 1].Player != draught.Player &&
                board[startY - 2, startX - 2] == null)
            {
                canKill = true;
                canDraugtKill = draught;
            }
            else if ((startY + 1 <= 7 && startX - 1 >= 0) &&
                (startY + 2 <= 7 && startX - 2 >= 0) &&
                board[startY + 1, startX - 1] != null &&
                board[startY + 1, startX - 1].Player != draught.Player &&
                board[startY + 2, startX - 2] == null)
            {
                canKill = true;
                canDraugtKill = draught;
            }
            else if ((startY + 1 <= 7 && startX + 1 <= 7) &&
                (startY + 2 <= 7 && startX + 2 <= 7) &&
                board[startY + 1, startX + 1] != null &&
                board[startY + 1, startX + 1].Player != draught.Player &&
                board[startY + 2, startX + 2] == null)
            {
                canKill = true;
                canDraugtKill = draught;
            }

            return canKill;
        }
Example #18
0
        private void TurnNormalDraught(Draught draught, int endX, int endY)
        {
            AllDraughtsCanKill();
            if ((draught.Player == Players.PlayerOne &&
                ((draught.X - 1 == endX && draught.Y - 1 == endY) || (draught.X + 1 == endX && draught.Y - 1 == endY))) ||
               (draught.Player == Players.PlayerTwo &&
                ((draught.X - 1 == endX && draught.Y + 1 == endY) || (draught.X + 1 == endX && draught.Y + 1 == endY))))
            {
                TurnMove(draught, endX, endY);
            }
            else
            {
                int centerX = 0;
                int centerY = 0;

                if (draught.X + 2 == endX && draught.Y - 2 == endY) // вверх-вправо
                {
                    centerX = draught.X + 1;
                    centerY = draught.Y - 1;
                }
                else if (draught.X - 2 == endX && draught.Y - 2 == endY) // вверх-влево
                {
                    centerX = draught.X - 1;
                    centerY = draught.Y - 1;
                }
                else if (draught.X - 2 == endX && draught.Y + 2 == endY) // вниз-влево
                {
                    centerX = draught.X - 1;
                    centerY = draught.Y + 1;
                }
                else if (draught.X + 2 == endX && draught.Y + 2 == endY) // вниз-вправо
                {
                    centerX = draught.X + 1;
                    centerY = draught.Y + 1;
                }
                else
                {
                    throw new DraughtsGameException("Неверный ход.");
                }

                if (board[centerY, centerX] != null && board[centerY, centerX].Player != draught.Player)
                {
                    TurnKill(draught, endX, endY, centerX, centerY);
                }
            }
        }
Example #19
0
 private void DraughtIsQueen(Draught draught, int endY)
 {
     if (draught.Type != DraughtType.Queen &&
         (draught.Player == Players.PlayerOne && endY == 7) ||
         (draught.Player == Players.PlayerTwo && endY == 0))
     {
         draught.Type = DraughtType.Queen;
     }
 }
Example #20
0
        public void DraughtConstructorTest2()
        {
            BoardPoint point = new BoardPoint(2, 4);
            DraughtType type = DraughtType.Queen;
            Players player = Players.PlayerTwo;
            Draught target = new Draught(point, type, player);

            Assert.AreEqual(point, target.Point);
            Assert.AreEqual(type, target.Type);
            Assert.AreEqual(player, target.Player);
        }
Example #21
0
        private void TurnMove(Draught draught, int endX, int endY)
        {
            if (canKill)
            {
                board.Remove(canDraugtKill);
                canKill = false;
            }

            DraughtIsQueen(draught, endY);
            board.Move(draught, endX, endY);

            ChangeCurrentPlayer();
            OnBoardChanged();
            CheckWin();
        }
Example #22
0
 public void Remove(Draught draught)
 {
     Remove(draught.X, draught.Y);
 }
Example #23
0
        private void TurnQueenDraught(Draught draught, int endX, int endY)
        {
            if (Math.Abs(endX - draught.X) == Math.Abs(endY - draught.Y))
            {
                int count = Math.Abs(endX - draught.X) - 1;
                BoardPoint point = new BoardPoint();

                if (draught.X < endX && draught.Y > endY) // вверх-вправо
                {
                    point = CheckRange(draught, draught.X + 1, draught.X + count, endY + 1, endY + count);
                }
                else if (draught.X > endX && draught.Y > endY) // вверх-влево
                {
                    point = CheckRange(draught, endX + 1, endX + count, endY + 1, endY + count);
                }
                else if (draught.X > endX && draught.Y < endY) // вниз-влево
                {
                    point = CheckRange(draught, endX + 1, endX + count, draught.Y + 1, draught.Y + count);
                }
                else if (draught.X < endX && draught.Y < endY) // вниз-вправо
                {
                    point = CheckRange(draught, draught.X + 1, draught.X + count, draught.Y + 1, draught.Y + count);
                }

                if (point.X != -1 && point.Y != -1)
                {
                    TurnKill(draught, endX, endY, point.X, point.Y);
                }
                else
                {
                    TurnMove(draught, endX, endY);
                }
            }
            else
            {
                throw new DraughtsGameException("Неверный ход дамкой.");
            }
        }
Example #24
0
        private void CreatePlayerTwoDraught()
        {
            for (int y = 0; y < 3; y++)
            {
                for (int x = 0; x < 8; x++)
                {
                    if ((y + x) % 2 != 0)
                    {
                        draughts[y, x] = new Draught(x, y, Players.PlayerTwo);
                    }
                }
            }

            leftDraughtsPlayerOne = 12;
        }