public void GetThreatsFuzzTest() { Random random = new Random(0); const int gameLimit = 200; for (int game = 1; game < gameLimit; game++) { Grid_Accessor grid = new Grid_Accessor(new PrivateObject(new Grid(7, 6, 0))); // Play a full random, game, and verify threats after each move. for (int i = 0; i < 7 * 6 && grid.IsGameOver() == -1; i++) { ValidateThreats(grid, 0); ValidateThreats(grid, 1); int move; do { move = random.Next(grid.Width); } while (!grid.IsValidMove(move)); int player = i & 1; grid.Move(move, player); } } }
private void ValidateThreats(Grid_Accessor grid, int player) { ulong threats = grid.GetThreats(player); for (int i = 0; i < (grid.Height + 1) * grid.Width; i++) { ulong mask = 1UL << i; bool isThreat = (threats & mask) == mask; bool isInBuffer = ((i + 1) % (grid.Height + 1)) == 0; if (isThreat) { Assert.AreEqual(0UL, grid.playerPositions[0] & mask, "Player cannot have a threat in a nonempty location"); Assert.AreEqual(0UL, grid.playerPositions[1] & mask, "Player cannot have a threat in a nonempty location"); Assert.IsTrue(!isInBuffer, "Cannot have a threat in the column buffer"); } if (!isInBuffer) { ulong originalPosition = grid.playerPositions[player]; grid.playerPositions[player] |= mask & ~grid.playerPositions[1 - player]; Assert.AreEqual(isThreat, grid.IsGameOver(player)); grid.playerPositions[player] = originalPosition; } } Assert.AreEqual(0UL, threats >> ((grid.Height + 1) * grid.Width), "Threats must be on the board"); }