public override string ToString() { var sbRes = new StringBuilder(); sbRes.Append($"[{Id}]{Verdict}{{"); foreach (var c in Indicdentals) { sbRes.Append($"{c.Id},"); } if (Indicdentals.Count > 0) { sbRes.Remove(sbRes.Length - 1, 1); } sbRes.AppendLine("}"); for (var i = 0; i < Stones.GetLength(0); i++) { for (var j = 0; j < Stones.GetLength(1); j++) { switch (Stones[i, j]) { case Stone.Black: sbRes.Append('X'); break; case Stone.White: sbRes.Append('O'); break; default: sbRes.Append(' '); break; } if (j < Stones.GetLength(0) - 1) { sbRes.Append('|'); } } if (i < Stones.GetLength(0) - 1) { sbRes.AppendLine(); sbRes.Append('-', Stones.GetLength(1) * 2 - 1); sbRes.AppendLine(); } } return(sbRes.ToString()); }
private bool CheckStones(int x, int y, byte piece) { int count(int xPos, int yPos, Func <int, int> xInc, Func <int, int> yInc, int result = 0) { if (x >= Stones.GetLength(0) || y >= Stones.GetLength(1) || x < 0 || y < 0 || Stones[x, y] != piece) { return(result); } else { return(count(xInc(x), yInc(y), xInc, yInc, result + 1)); } } int diag1 = count(x, y, c => c + 1, c => c + 1) + count(x, y, c => c - 1, c => c - 1); int diag2 = count(x, y, c => c + 1, c => c - 1) + count(x, y, c => c - 1, c => c + 1); int horz = count(x, y, c => c + 1, c => c) + count(x, y, c => c - 1, c => c); int vert = count(x, y, c => c, c => c + 1) + count(x, y, c => c, c => c - 1); return(diag1 > 5 || diag2 > 5 || horz > 5 || vert > 5); //must be > 5 instead of >= because adding two count() calls overcounts by 1 (the first piece is counted twice) }
private void StoneOnMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs) { if (sender is Stone stone) { switch (TheWindow.Logic.MoveCount) { case 2: { int rows = Stones.GetLength(0); int columns = Stones.GetLength(1); int centerRow = rows - (rows / 2) - 1; int centerColumn = columns - (columns / 2) - 1; bool valid = true; for (int i = centerRow - 2; i < centerRow + 3; i++) { for (int j = centerColumn - 2; j < centerColumn + 3; j++) { if (!stone.Equals(Stones[i, j])) { continue; } valid = false; break; } if (!valid) { break; } } if (valid) { stone.CurrentState = StoneState.Black; TheWindow.Logic.SwitchPlayerTurn(); } break; } default: if (stone.CurrentState != StoneState.Open) { return; } stone.CurrentState = TheWindow.Logic.CurrentPlayer == TheWindow.Logic.Player1 ? StoneState.Black : StoneState.White; bool found = false; int foundX = 0; int foundY = 0; for (var i = 0; i < Stones.GetLength(0); i++) { for (var j = 0; j < Stones.GetLength(1); j++) { if (!stone.Equals(Stones[i, j])) { continue; } foundX = j; foundY = i; found = true; break; } if (found) { break; } } TheWindow.Logic.CheckCapture(foundX, foundY); TheWindow.Logic.CheckFiveInRow(foundX, foundY); TheWindow.Logic.CheckTrias(foundX, foundY); TheWindow.Logic.CheckTessera(foundX, foundY); TheWindow.Logic.SwitchPlayerTurn(); break; } } }