Example #1
0
        /// <summary>
        /// Opens a game from disk using the specified path.
        /// </summary>
        /// <param name="path"></param>
        public void OpenGame(string path)
        {
            FileStream stream = null;

            try
            {
                BoardFormatter formatter = new BoardFormatter();
                stream = new FileStream(path, FileMode.Open);
                Board savedBoard = formatter.Deserialize(stream) as Board;

                if (savedBoard == null)
                {
                    throw new GameException(string.Format("Board could not be loaded from path {0}.", path));
                }

                this.Reset(savedBoard);
                this.savePath = path;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
Example #2
0
        public void Roundtrip()
        {
            var board = new Board();

            board.MovePiece(new Coordinate(0, 0, 0), new Coordinate(0, 4, 0));
            board.MovePiece(new Coordinate(0, 0, 3), new Coordinate(0, 4, 3));
            board.MovePiece(new Coordinate(1, 0, 0), new Coordinate(1, 4, 0));
            board.MovePiece(new Coordinate(1, 0, 3), new Coordinate(1, 4, 3));
            board.MovePiece(new Coordinate(2, 0, 0), new Coordinate(2, 4, 0));
            board.MovePiece(new Coordinate(2, 0, 3), new Coordinate(2, 4, 3));

            var   formatter = new BoardFormatter();
            Board newBoard  = null;

            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, board);
                stream.Position = 0;
                newBoard        = formatter.Deserialize(stream) as Board;
            }

            Assert.IsNotNull(newBoard);
            Assert.AreEqual(board.MoveHistory.Count, newBoard.MoveHistory.Count);
            Move move = board.MoveHistory[2];

            Assert.AreEqual(new Coordinate(1, 0, 0), move.Source);
            Assert.AreEqual(new Coordinate(1, 4, 0), move.Destination);
        }
        public static void SerializeInvalidType()
        {
            var board     = "0,0:0,4|4,0:4,4|0,0:0,4";
            var formatter = new BoardFormatter();

            Assert.Throws <ArgumentException>(() => formatter.Serialize(new MemoryStream(), board));
        }
        public static void SerializeWithNullStream()
        {
            var board     = new Board();
            var formatter = new BoardFormatter();

            Assert.Throws <ArgumentNullException>(() => formatter.Serialize(null, board));
        }
        public static void DeserializeWithInvalidMovePairDelimiter()
        {
            var boardData = "0,0?0,4|4,0:4,4|0,0:0,4";
            var formatter = new BoardFormatter();
            var stream    = new MemoryStream((new ASCIIEncoding()).GetBytes(boardData));

            Assert.Throws <SerializationException>(() => formatter.Deserialize(stream));
        }
Example #6
0
        public void PrintBoardMethodPrintsCorrectBoardAsGenerated()
        {
            var output = new TestOutput();
            var board  = new Board(3);

            output.OutputText(BoardFormatter.PrintBoard(board));

            Assert.Contains(". . . \n. . . \n. . . ", output.CalledText);
        }
Example #7
0
        protected static void SaveBoard(Board board, string file)
        {
            BoardFormatter formatter = new BoardFormatter();

            using (Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
            {
                formatter.Serialize(stream, board);
            }
        }
        public static void DeserializeWithValidMoves()
        {
            var boardData = "0,0:0,4|4,0:4,4|0,0:0,4";
            var formatter = new BoardFormatter();
            var stream    = new MemoryStream((new ASCIIEncoding()).GetBytes(boardData));

            var board = formatter.Deserialize(stream) as Board;

            Assert.AreEqual(3, board.Moves.Count);
        }
        public void FormatNineDotsGivenEmptyBoard()
        {
            // Arrange
            var board = new Board(3, 3);
            // Act
            var actual = BoardFormatter.Format(board);
            // Assert
            const string expected = ". . . \n. . . \n. . . ";

            Assert.Equal(expected, actual);
        }
Example #10
0
        public void DeserializeWithInvalidFormat()
        {
            var boardData = "<Board><Move><Source<X>0</X><Y>0</Y><Z>0</Z></Source><Destination><X>0</X><Y>4</Y><Z>0</Z></Destination></Move></Board>";

            var formatter = new BoardFormatter();

            using (var stream = new MemoryStream((new ASCIIEncoding()).GetBytes(boardData)))
            {
                formatter.Deserialize(stream);
            }
        }
        public void FormatOInBottomRightCorner()
        {
            // Arrange
            var board = new Board(3, 3);
            var cell  = board.Cells.FirstOrDefault(c => c.X == 2 && c.Y == 2);

            if (cell != null)
            {
                cell.Piece = BoardPiece.O;
            }
            // Act
            var actual = BoardFormatter.Format(board);
            // Assert
            const string expected = ". . . \n. . . \n. . O ";

            Assert.Equal(expected, actual);
        }
        public void FormatXInTopLeftCorner()
        {
            // Arrange
            var board = new Board(3, 3);
            var cell  = board.Cells.FirstOrDefault(c => c.X == 0 && c.Y == 0);

            if (cell != null)
            {
                cell.Piece = BoardPiece.X;
            }
            // Act
            var actual = BoardFormatter.Format(board);
            // Assert
            const string expected = "X . . \n. . . \n. . . ";

            Assert.Equal(expected, actual);
        }
Example #13
0
        public void Serialize()
        {
            var board = new Board();

            board.MovePiece(new Coordinate(0, 0, 0), new Coordinate(0, 4, 0));
            board.MovePiece(new Coordinate(0, 0, 3), new Coordinate(0, 4, 3));
            board.MovePiece(new Coordinate(1, 0, 0), new Coordinate(1, 4, 0));
            board.MovePiece(new Coordinate(1, 0, 3), new Coordinate(1, 4, 3));
            board.MovePiece(new Coordinate(2, 0, 0), new Coordinate(2, 4, 0));
            board.MovePiece(new Coordinate(2, 0, 3), new Coordinate(2, 4, 3));

            var formatter = new BoardFormatter();

            using (var stream = new MemoryStream())
            {
                formatter.Serialize(stream, board);
            }
        }
Example #14
0
        public void Deserialize()
        {
            var boardData = "<Board><Move><Source><X>0</X><Y>0</Y><Z>0</Z></Source><Destination><X>0</X><Y>4</Y><Z>0</Z></Destination></Move></Board>";

            var   formatter = new BoardFormatter();
            Board board     = null;

            using (var stream = new MemoryStream((new ASCIIEncoding()).GetBytes(boardData)))
            {
                board = formatter.Deserialize(stream) as Board;
            }

            Assert.IsNotNull(board);
            Assert.AreEqual(1, board.MoveHistory.Count);
            Move move = board.MoveHistory[0];

            Assert.AreEqual(new Coordinate(0, 0, 0), move.Source);
            Assert.AreEqual(new Coordinate(0, 4, 0), move.Destination);
        }
Example #15
0
        /// <summary>
        /// Persists the game to disk at the specified path.
        /// </summary>
        /// <param name="path"></param>
        public void SaveGame(string path)
        {
            FileStream stream = null;

            try
            {
                this.savePath = path;
                BoardFormatter formatter = new BoardFormatter();
                stream = new FileStream(this.savePath, FileMode.Create);
                formatter.Serialize(stream, this.board);
                this.isDirty = false;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
        public static void RoundTripWithCustomFormatter()
        {
            MemoryStream outStream = null, inStream = null;
            Board        board = new Board(), newBoard = null;

            board.MovePiece(new Point(0, 0), new Point(0, 4));
            board.MovePiece(new Point(4, 0), new Point(4, 4));
            board.MovePiece(new Point(0, 0), new Point(0, 4));
            board.MovePiece(new Point(4, 0), new Point(4, 4));
            board.MovePiece(new Point(0, 0), new Point(0, 4));

            var boardFormatter = new BoardFormatter();

            outStream = new MemoryStream();
            boardFormatter.Serialize(outStream, board);
            inStream = new MemoryStream(outStream.ToArray());
            newBoard = (Board)boardFormatter.Deserialize(inStream);

            Assert.IsNotNull(newBoard, "The new board is null.");
            Assert.AreEqual(5, newBoard.Moves.Count, "The move history is incorrect.");
            Assert.AreEqual(Player.O, newBoard.CurrentPlayer, "The current player is incorrect.");
            Assert.AreEqual(Player.None, newBoard.WinningPlayer, "The winning player is incorrect.");

            board.MovePiece(new Point(4, 0), new Point(4, 4));
            board.MovePiece(new Point(0, 0), new Point(0, 4));
            board.MovePiece(new Point(4, 0), new Point(4, 4));
            board.MovePiece(new Point(0, 0), new Point(0, 4));

            var binaryFormatter = new BinaryFormatter();

            outStream = new MemoryStream();
            binaryFormatter.Serialize(outStream, board);
            inStream = new MemoryStream(outStream.ToArray());
            newBoard = (Board)binaryFormatter.Deserialize(inStream);

            Assert.AreEqual(9, newBoard.Moves.Count, "The move history (after the win) is incorrect.");
            Assert.AreEqual(Player.None, newBoard.CurrentPlayer, "The current player (after the win) is incorrect.");
            Assert.AreEqual(Player.X, newBoard.WinningPlayer, "The winning player (after the win) is incorrect.");
        }
		public void RoundTripWithCustomFormatter()
		{
			IFormatter formatter = null;
			MemoryStream outStream = null, inStream = null;
			Board board = new Board(), newBoard = null;

			board.MovePiece(new Point(0, 0), new Point(0, 4));
			board.MovePiece(new Point(4, 0), new Point(4, 4));
			board.MovePiece(new Point(0, 0), new Point(0, 4));
			board.MovePiece(new Point(4, 0), new Point(4, 4));
			board.MovePiece(new Point(0, 0), new Point(0, 4));

			formatter = new BoardFormatter();
			outStream = new MemoryStream();
			formatter.Serialize(outStream, board);
			inStream = new MemoryStream(outStream.ToArray());
			newBoard = (Board)formatter.Deserialize(inStream);

			Assert.IsNotNull(newBoard, "The new board is null.");
			Assert.AreEqual(5, newBoard.Moves.Count, "The move history is incorrect.");
			Assert.AreEqual(Player.O, newBoard.CurrentPlayer, "The current player is incorrect.");
			Assert.AreEqual(Player.None, newBoard.WinningPlayer, "The winning player is incorrect.");

			board.MovePiece(new Point(4, 0), new Point(4, 4));
			board.MovePiece(new Point(0, 0), new Point(0, 4));
			board.MovePiece(new Point(4, 0), new Point(4, 4));
			board.MovePiece(new Point(0, 0), new Point(0, 4));

			formatter = new BinaryFormatter();
			outStream = new MemoryStream();
			formatter.Serialize(outStream, board);
			inStream = new MemoryStream(outStream.ToArray());
			newBoard = (Board)formatter.Deserialize(inStream);

			Assert.AreEqual(9, newBoard.Moves.Count, "The move history (after the win) is incorrect.");
			Assert.AreEqual(Player.None, newBoard.CurrentPlayer, "The current player (after the win) is incorrect.");
			Assert.AreEqual(Player.X, newBoard.WinningPlayer, "The winning player (after the win) is incorrect.");
		}
		public void SerializeInvalidType()
		{
			string board = "0,0:0,4|4,0:4,4|0,0:0,4";
			IFormatter formatter = new BoardFormatter();
			Stream outStream = new MemoryStream();
			Assert.Throws<ArgumentException>(() => formatter.Serialize(outStream, board));
		}
		public void DeserializeWithValidMoves()
		{
			string boardData = "0,0:0,4|4,0:4,4|0,0:0,4";
			IFormatter formatter = new BoardFormatter();
			Stream stream = new MemoryStream((new ASCIIEncoding()).GetBytes(boardData));

			Board board = formatter.Deserialize(stream) as Board;
			Assert.AreEqual(3, board.Moves.Count);
		}
		public void DeserializeWithInvalidMovePairDelimiter()
		{
			string boardData = "0,0?0,4|4,0:4,4|0,0:0,4";
			IFormatter formatter = new BoardFormatter();
			Stream stream = new MemoryStream((new ASCIIEncoding()).GetBytes(boardData));

			Assert.Throws<SerializationException>(() => formatter.Deserialize(stream));
		}
		public void SerializeWithNullStream()
		{
			Board board = new Board();
			IFormatter formatter = new BoardFormatter();
			Stream outStream = null;
			Assert.Throws<ArgumentNullException>(() => formatter.Serialize(outStream, board));
		}