Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
0
        public static void Serialize(DraughtsGame game, string path)
        {
            if (game == null)
                throw new ArgumentNullException("board");
            if (string.IsNullOrWhiteSpace(path))
                throw new ArgumentException();

            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    bw.Write((byte)game.CurrentTurn);
                    bw.Write((byte)game.GameBoard.LeftDraughtPlayerOne);
                    bw.Write((byte)game.GameBoard.LeftDraughtPlayerTwo);

                    foreach (Draught item in game.GameBoard.Draughts)
                    {
                        if (item != null)
                        {
                            bw.Write((byte)item.Y);
                            bw.Write((byte)item.X);
                            bw.Write((byte)item.Type);
                            bw.Write((byte)item.Player);
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public MainViewModel()
        {
            game = new DraughtsGame();
            game.BoardChanged += Game_BoardChanged;
            game.GameOver += GameOver;

            currentTurn = string.Format(Resource.CurrentTurnText, game.CurrentTurn == Players.PlayerOne ? Resource.PlayerOneText : Resource.PlayerTwoText);
            leftDraughts = string.Format(Resource.LeftText, game.GameBoard.LeftDraughtPlayerOne, game.GameBoard.LeftDraughtPlayerTwo);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
0
        public MainView(MainViewModel mainViewModel)
        {
            InitializeComponent();

            this.mainViewModel = mainViewModel;
            game = mainViewModel.Game;
            mainViewModel.PropertyChanged += PropertyChanged;

            InitGradients();
            ReDraw();
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public void WrongPlayerTurnTest()
        {
            DraughtsGame target = new DraughtsGame();

            target.Turn(3, 2, 4, 3);
            target.Turn(4, 3, 5, 4);
        }
Ejemplo n.º 8
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;
        }
Ejemplo n.º 9
0
        public void TurnTest()
        {
            DraughtsGame target = new DraughtsGame();
            int startX = 2;
            int startY = 5;
            Draught draught = target.GameBoard[startY, startX];

            int endX = 3;
            int endY = 4;
            target.Turn(startX, startY, endX, endY);

            Assert.AreEqual(draught, target.GameBoard[endY, endX]);
        }
Ejemplo n.º 10
0
        public void TurnTest1()
        {
            DraughtsGame target = new DraughtsGame();
            BoardPoint startPoint = new BoardPoint(5, 2);
            Draught draught = target.GameBoard[startPoint];

            BoardPoint endPoint = new BoardPoint(4, 3);
            target.Turn(startPoint, endPoint);

            Assert.AreEqual(draught, target.GameBoard[endPoint]);
        }
Ejemplo n.º 11
0
        public void StartEqualEndTurnTest()
        {
            DraughtsGame target = new DraughtsGame();
            int startX = 4;
            int startY = 5;
            Draught draught = target.GameBoard[startY, startX];

            int endX = 4;
            int endY = 5;
            target.Turn(startX, startY, endX, endY);
        }
Ejemplo n.º 12
0
        public void TurnBackTest()
        {
            DraughtsGame game = new DraughtsGame();

            game.Turn(3, 2, 4, 3);
            game.Turn(2, 5, 1, 4);
            game.Turn(4, 3, 3, 2);
        }
Ejemplo n.º 13
0
 private void PropertyChanged(object o, PropertyChangedEventArgs args)
 {
     if (args.PropertyName == "BoardChanged")
     {
         ReDraw();
     }
     else if (args.PropertyName == "Load")
     {
         game = ((MainViewModel)o).Game;
         ReDraw();
     }
 }
Ejemplo n.º 14
0
        public void KillTest()
        {
            DraughtsGame game = new DraughtsGame();
            Draught dr = game.GameBoard[5, 4];

            game.Turn(4, 5, 5, 4);
            game.Turn(3, 2, 4, 3);
            game.Turn(5, 4, 3, 2);

            Assert.AreEqual<Draught>(dr, game.GameBoard[2, 3]);
            Assert.AreEqual(11, game.GameBoard.LeftDraughtPlayerTwo);
        }
Ejemplo n.º 15
0
        private void LoadGameCommand_Execute(object o)
        {
            LoadView loadView = new LoadView { Owner = view };
            LoadViewModel loadViewModel = new LoadViewModel { View = loadView };
            loadView.DataContext = loadViewModel;

            if (loadView.ShowDialog() == true)
            {
                game = DraughtsGame.Load("Saves\\" + loadViewModel.SelectedSave + ".sav");
                game.BoardChanged += Game_BoardChanged;
                game.GameOver += GameOver;
                OnPropertyChanged("Load");

                CurrentTurn = string.Format(Resource.CurrentTurnText, game.CurrentTurn == Players.PlayerOne ? Resource.PlayerOneText : Resource.PlayerTwoText);
                LeftDraughts = string.Format(Resource.LeftText, game.GameBoard.LeftDraughtPlayerOne, game.GameBoard.LeftDraughtPlayerTwo);
            }
        }
Ejemplo n.º 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]);
        }
Ejemplo n.º 17
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]);
        }
Ejemplo n.º 18
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);
 }
Ejemplo n.º 19
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);
        }
Ejemplo n.º 20
0
 public static void Save(DraughtsGame game, string path)
 {
     BoardSerializer.Serialize(game, path);
 }
Ejemplo n.º 21
0
        public void StartCellIsNullTurnTest()
        {
            DraughtsGame target = new DraughtsGame();
            int startX = 4;
            int startY = 5;
            target.GameBoard[startY, startX] = null;

            int endX = 5;
            int endY = 4;
            target.Turn(startX, startY, endX, endY);
        }