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); }
/// <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(); } } }
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)); }
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 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 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); }
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 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)); }