public static bool IsValidCell(int r, int c, CellStatus[,] cells) { if (r < 0 || r >= cells.GetLength(0)) return false; if (c < 0 || c >= cells.GetLength(1)) return false; return true; }
internal static bool IsPlayerFirstMove(CellStatus[,] cells, Color color) { CellStatus status = GetStatusFromPlayer(color); for (int row = 0; row < cells.GetLength(0); row++) { for (int col = 0; col < cells.GetLength(1); col++) { if (cells[row, col] == status) return false; } } return true; }
public static bool BoardIsEmpty(CellStatus[,] status) { for (int i = 0; i < status.GetLength(1); i++) { if (status[0, i] != CellStatus.Empty) return false; } return true; }
private string RepresentWorkingSet(CellStatus[,] workingSet) { var sb = new StringBuilder(); for (var i = 0; i < workingSet.GetLength(0); i++) { for (var j = 0; j < workingSet.GetLength(1); j++) { switch (workingSet[i, j]) { case CellStatus.Free: sb.Append("_"); break; case CellStatus.NotAvailableForPlacement: sb.Append("0"); break; case CellStatus.ShipPart: sb.Append("X"); break; } } sb.Append(Environment.NewLine); } return sb.ToString(); }
public override int Move(CellStatus[,] cells) { CellStatus target = GetTargetColor(); int nRows = cells.GetLength(0); int nCols = cells.GetLength(1); // if board is empty start in the middle if (PlayInTheMiddle(cells)) return (int)Math.Ceiling(nCols / 2.0); Combination best = new Combination() { Lenght = 0 }; for (int r = 0; r < nRows; r++) { int counter = 0; for (int c = 0; c < nCols; c++) { if (cells[r, c] != target) { if (counter > best.Lenght) { List<int> colToStop = new List<int>(); // check if the sequence can be stopped here! if (Utils.CanPlaceDiskAt(r, c, cells)) { colToStop.Add(c); } // check if the sequence can be stopped on the other end if (Utils.CanPlaceDiskAt(r, c - counter - 1, cells)) { colToStop.Add(c - counter - 1); } if (colToStop.Count > 0) { best = new Combination() { Lenght = counter, CanBeStopped = colToStop.Count > 0, ColumnsToStop = colToStop, }; } } counter = 0; } else { counter++; } } } for (int c = 0; c < nCols; c++) { int counter = 0; for (int r = 0; r < nRows; r++) { if (cells[r, c] != target) { if (counter > best.Lenght) { List<int> colToStop = new List<int>(); // check if the sequence can be stopped here! if (Utils.CanPlaceDiskAt(r, c, cells)) { colToStop.Add(c); } if (colToStop.Count > 0) { best = new Combination() { Lenght = counter, CanBeStopped = colToStop.Count > 0, ColumnsToStop = colToStop, }; } } counter = 0; } else { counter++; } } } // check diagonal backward for (int c = 3; c < nCols + 2; c++) { int counter = 0; for (int r = 0; r < nRows; r++) { int col = c - r; int row = r; if (col >= 0 && col < nCols) { if (cells[row, col] != target) { if (counter > best.Lenght) { List<int> colToStop = new List<int>(); // check if the sequence can be stopped here! if (Utils.CanPlaceDiskAt(row, col, cells)) { colToStop.Add(col); } // check if the sequence can be stopped on the other side! if (Utils.CanPlaceDiskAt(row - counter - 1, col + counter + 1, cells)) { colToStop.Add(col + counter + 1); } if (colToStop.Count > 0) { best = new Combination() { Lenght = counter, CanBeStopped = colToStop.Count > 0, ColumnsToStop = colToStop, }; } } counter = 0; } else { counter++; } } } } // check diagonal forward for (int c = -2; c < nCols -3; c++) { int counter = 0; for (int r = 0; r < nRows; r++) { int col = c + r; int row = r; if (col >= 0 && col < nCols) { if (cells[row, col] != target) { if (counter > best.Lenght) { List<int> colToStop = new List<int>(); // check if the sequence can be stopped here! if (Utils.CanPlaceDiskAt(row, col, cells)) { colToStop.Add(col); } // check if the sequence can be stopped on the other side! if (Utils.CanPlaceDiskAt(row - counter - 1, col - counter - 1, cells)) { colToStop.Add(col - counter - 1); } if (colToStop.Count > 0) { best = new Combination() { Lenght = counter, CanBeStopped = colToStop.Count > 0, ColumnsToStop = colToStop, }; } } counter = 0; } else { counter++; } } } } if (best.ColumnsToStop != null) { if (best.ColumnsToStop.Count == 1) { return best.ColumnsToStop[0]; } else { double center = nCols / 2.0; int closest = -1; double closestDistance = double.MaxValue; foreach (var col in best.ColumnsToStop) { double distance = col - center; if (distance < closestDistance) { closest = col; closestDistance = distance; } } return closest; } } else { // why does it end up here? return new Random().Next(0, nCols); } }
public override int Move(CellStatus[,] status) { return _rand.Next(0, status.GetLength(1)); }
public static string ToFriendlyString(CellStatus[,] cells) { StringBuilder sb = new StringBuilder(); for (int i = cells.GetLength(0) - 1; i >= 0; i--) { StringBuilder row = new StringBuilder(); for (int j = 0; j < cells.GetLength(1); j++) { row.Append(cells[i, j]); row.Append(","); } sb.AppendLine(row.ToString()); } return sb.ToString(); }