// Check that stacking pieces works properly. Additionally, it // should be possible to add a piece to an invalid position on the // board without error. Throws Exception if the determined stack // size is not equal to the size of the stack placed at the // specified position. private static void TestAdd() { // Create a new empty game board. TzaarBoard board = new TzaarBoard(true); // Create some game pieces of various types. TzaarPiece p1 = new TzaarPiece.Tzaar(TzaarColor.BLACK); TzaarPiece p2 = new TzaarPiece.Tzarra(TzaarColor.BLACK); TzaarPiece p3 = new TzaarPiece.Tott(TzaarColor.BLACK); board.Add(p1, 0, 0); board.Add(p2, 0, 0); board.Add(p3, 0, 0); Stack<TzaarPiece> S = board.Query(0, 0); if (S.Count != 3) throw new Exception(); if (S.Peek() != p3) throw new Exception(); // Try to add to a spot that doesn't exist. board.Add((TzaarPiece)null, 8, 8); }
// Parse a message string into a TzaarBoard object. private TzaarBoard GetBoardStateFromString(string msg) { TzaarBoard board = new TzaarBoard(true); // Strip packet wrapper. string boardString = TzaarMessage.GetMessageData(msg); boardString = boardString.Substring(1, boardString.Length - 2); // Split into individual stacks. string[] stacks = Regex.Split(boardString, "},{"); int[] offsets = new int[] { 0, 5, 11, 18, 26, 34, 42, 49, 55 }; string[] aStack; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { Stack<TzaarPiece> S = board.Query(i, j); if (S == null) break; // Split stack into color (aStack[0]) and individual // pieces. aStack = stacks[j + offsets[i]].Split(','); TzaarColor color = (aStack[0].Equals(TzaarColor.BLACK.ToString())) ? TzaarColor.BLACK : TzaarColor.WHITE; // Add each piece to the board. for (int k = aStack.Length - 1; k >= 0; k--) { String s = aStack[k]; if (s.Equals(typeof(TzaarPiece.Tzaar).Name.ToString())) board.Add(new TzaarPiece.Tzaar(color), i, j); else if (s.Equals(typeof(TzaarPiece.Tzarra).Name.ToString())) board.Add(new TzaarPiece.Tzarra(color), i, j); else if (s.Equals(typeof(TzaarPiece.Tott).Name.ToString())) board.Add(new TzaarPiece.Tott(color), i, j); } } } return board; }
// The goal is to test the board-querying functionality. Throws // Exception if the piece(s) returned from the specified position // do not match those placed at that position. private static void TestQuery() { // Create a new empty game board. TzaarBoard board = new TzaarBoard(true); // Create some game pieces of various types. TzaarPiece p1 = new TzaarPiece.Tzaar(TzaarColor.BLACK); TzaarPiece p2 = new TzaarPiece.Tzarra(TzaarColor.BLACK); TzaarPiece p3 = new TzaarPiece.Tott(TzaarColor.BLACK); // Add the pieces we created to the board at the target // position. board.Add(p1, 0, 0); board.Add(p2, 0, 0); board.Add(p3, 0, 0); // Check that there are 3 pieces on the target position. Stack<TzaarPiece> pieces = board.Query(0, 0); if (pieces.Count != 3) throw new Exception(); }
// Test the functionality related to 'taking' a piece, or stack of // pieces, from a position on the game board. Additionally, it // should be possible to attempt to 'take' a piece from an invalid // position on the game board without error. Throws Exception if // 'taking' the pieces from the board does not result in the // specified position being empty. private static void TestTake() { // Create a new empty game board. TzaarBoard board = new TzaarBoard(true); // Create some game pieces of various types. TzaarPiece p1 = new TzaarPiece.Tzaar(TzaarColor.BLACK); TzaarPiece p2 = new TzaarPiece.Tzarra(TzaarColor.BLACK); TzaarPiece p3 = new TzaarPiece.Tott(TzaarColor.BLACK); // Add the pieces we created to the board at the target // position. board.Add(p1, 0, 0); board.Add(p2, 0, 0); board.Add(p3, 0, 0); // Check that there are 3 pieces on the target position. if (board.Query(0, 0).Count != 3) throw new Exception(); // Take the pieces. if (board.Take(0, 0).Count != 3) throw new Exception(); // Check that there are now 0 pieces on the target position. if (board.Query(0, 0).Count != 0) throw new Exception(); // Try to Take from a spot that doesn't exist. board.Take(-1, 8); }
// Start at the specified sourceNode and travel in the specified // direction. Return true if there is a clear path between the two // specified board positions; otherwise return false. private static bool ExplorePath(TzaarBoard board, Node fromNode, Node toNode, string direction) { Node currentNode = fromNode; while (true) { // Move to the next node in the specified direction. FieldInfo myPropInfo = currentNode.GetType().GetField(direction); Node nextNode = (Node)myPropInfo.GetValue(currentNode); currentNode = nextNode; if (currentNode == null) // We ran off the board. break; if (currentNode == toNode) // We have successfully reached our destination! return true; Stack<TzaarPiece> currentPieceStack = board.Query(currentNode.col, currentNode.row); if (currentPieceStack.Count != 0) // There are pieces on this position, stop here. break; } // We can't get there. return false; }
// Test the components which control functionality to 'stack' pieces // on one another. Throws Exception if an attempt to 'stack' pieces // results in a stack size not equal to the total number of pieces // being 'stacked'. private static void TestStack() { TzaarBoard board = new TzaarBoard(true); board.Add(new TzaarPiece.Tzaar(TzaarColor.WHITE), 2, 2); board.Add(new TzaarPiece.Tzaar(TzaarColor.WHITE), 2, 3); TzaarGameState state = new TzaarGameState(board, 4); TzaarLogic game = new TzaarLogic(state); game.Move(2, 2, 2, 3); Stack<TzaarPiece> S = board.Query(2, 3); // Stacked a total of 2 pieces, so stack size should be 2. if (S.Count() != 2) throw new Exception(); // Stacked a Tzaar on top of another Tzaar, so the top piece // should be a Tzaar. if (S.Peek().GetType() != typeof(TzaarPiece.Tzaar)) throw new Exception(); // Stacked a white piece on top of another white piece, so the // top piece should be white. if (S.Peek().Color != TzaarColor.WHITE) throw new Exception(); }
// Test components controlling the logic for one player to 'capture' // the piece of another player. Throws Exception if a valid // 'capture' results in an inconsistent game state, if an invalid // capture is allowed, or if a player is allowed to move the // opposite player's game pieces. private static void TestCapture() { // Test a simple, valid capture. TzaarBoard board = new TzaarBoard(true); TzaarPiece whitePiece = new TzaarPiece.Tzaar(TzaarColor.WHITE); board.Add(whitePiece, 2, 2); board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3); TzaarGameState state = new TzaarGameState(board); TzaarLogic game = new TzaarLogic(state); game.Move(2, 2, 2, 3); // The destination position should now contain the white piece // which captured the black piece. if (board.Query(2, 3).Peek() != whitePiece) throw new Exception(); // The origin position should no longer contain any pieces. if (board.Query(2, 2).Count() != 0) throw new Exception(); // Test an illegal capture. The capture is illegal because the // stack in the target position is more powerful than the stack // at the source. board = new TzaarBoard(true); board.Add(new TzaarPiece.Tzaar(TzaarColor.WHITE), 2, 2); board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3); board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3); state = new TzaarGameState(board); game = new TzaarLogic(state); bool passedTest = false; try { game.Move(2, 2, 2, 3); } catch { passedTest = true; } if (!passedTest) { throw new Exception(); } // Now try controlling some of the opponent's pieces. The game // should not allow it! board = new TzaarBoard(true); board.Add(new TzaarPiece.Tzaar(TzaarColor.WHITE), 2, 2); board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3); board.Add(new TzaarPiece.Tzaar(TzaarColor.BLACK), 2, 3); state = new TzaarGameState(board); game = new TzaarLogic(state); passedTest = false; try { game.Move(2, 3, 2, 2); } catch { passedTest = true; } if (!passedTest) { throw new Exception(); } }