public void PlayDisc() { var expectedFirstLocation = new Location(GameBoard.MaxRow, 1); var result = board.PlayDisc(Disc.Red, 1); Assert.AreEqual(expectedFirstLocation, board.LastLocationPlayed); var expectedSecondLocation = new Location(GameBoard.MaxRow - 1, 1); board.PlayDisc(Disc.Black, 1); Assert.AreEqual(expectedSecondLocation, board.LastLocationPlayed); Assert.IsTrue(result); }
public bool PlayDisc(Disc disc, int column) { if (column > MaxColumn || column < 1) throw new ArgumentException(String.Format("Column must be between 1 and {0}", MaxColumn)); int row = MaxRow; while (row > 0 && board[new Location(row, column)] != Disc.Empty) { row--; } if (row > 0) { board[new Location(row, column)] = disc; lastLocationPlayed = new Location(row, column); return true; } return false; }
public static bool Exists(Location location) { return location.Row > 0 && location.Row <= MaxRow && location.Column > 0 && location.Column <= MaxColumn; }
bool VerticalWins(Location location, Disc disc) { var contiguousDiscs = 0; var row = location.Row + 1; //check down. while (row <= MaxRow) { var loc = new Location(row, location.Column); if (board[loc] == disc) { contiguousDiscs++; row++; } else { break; } } return contiguousDiscs >= 3; }
bool NortWestSouthEastDiagonalWins(Location lastLocationPlayed, Disc disc) { var contiguousDiscs = 0; var row = lastLocationPlayed.Row - 1; var column = lastLocationPlayed.Column - 1; //check north west. while (row >= 1 && column >= 1) { var loc = new Location(row, column); if (board[loc] == disc) { contiguousDiscs++; column--; row--; } else { break; } } row = lastLocationPlayed.Row + 1; column = lastLocationPlayed.Column + 1; //check south east while (row <= MaxRow && column <= MaxColumn) { var loc = new Location(row, column); if (board[loc] == disc) { contiguousDiscs++; column++; row++; } else { break; } } return contiguousDiscs >= 3; }
bool HorizontalWins(Location location, Disc disc) { var contiguousDiscs = 0; var column = location.Column + 1; //check right. while (column <= MaxColumn) { var loc = new Location(location.Row, column); if (board[loc] == disc) { contiguousDiscs++; column++; } else { break; } } //check left. column = location.Column - 1; while (column >= 1) { var loc = new Location(location.Row, column); if (board[loc] == disc) { contiguousDiscs++; column--; } else { break; } } return contiguousDiscs >= 3; }