public HitResult TryToHit(int x, int y) { var square = Squares.Where(s => s.X == x && s.Y == y).FirstOrDefault(); if (square != null && !square.IsHit) { square.IsHit = true; if (Squares.All(s => s.IsHit)) { return(HitResult.Destroyed); } else { return(HitResult.Wounded); } } return(HitResult.Missed); }
public (bool gameIsOver, string winner) IsGameOver() { bool xHasRow1 = Squares.Take(3).All(s => s == xGlyph); bool xHasRow2 = Squares.Skip(3).Take(3).All(s => s == xGlyph); bool xHasRow3 = Squares.Skip(6).Take(3).All(s => s == xGlyph); bool xHasColumn1 = Squares[0] == xGlyph && Squares[3] == xGlyph && Squares[6] == xGlyph; bool xHasColumn2 = Squares[1] == xGlyph && Squares[4] == xGlyph && Squares[7] == xGlyph; bool xHasColumn3 = Squares[2] == xGlyph && Squares[5] == xGlyph && Squares[8] == xGlyph; bool xHasDiagonal = Squares[2] == xGlyph && Squares[4] == xGlyph && Squares[6] == xGlyph; bool xHasOtherDiagonal = Squares[0] == xGlyph && Squares[4] == xGlyph && Squares[8] == xGlyph; bool yHasRow1 = Squares.Take(3).All(s => s == oGlyph); bool yHasRow2 = Squares.Skip(3).Take(3).All(s => s == oGlyph); bool yHasRow3 = Squares.Skip(6).Take(3).All(s => s == oGlyph); bool yHasColumn1 = Squares[0] == oGlyph && Squares[3] == oGlyph && Squares[6] == oGlyph; bool yHasColumn2 = Squares[1] == oGlyph && Squares[4] == oGlyph && Squares[7] == oGlyph; bool yHasColumn3 = Squares[2] == oGlyph && Squares[5] == oGlyph && Squares[8] == oGlyph; bool yHasDiagonal = Squares[2] == oGlyph && Squares[4] == oGlyph && Squares[6] == oGlyph; bool yHasOtherDiagonal = Squares[0] == oGlyph && Squares[4] == oGlyph && Squares[8] == oGlyph; bool xwins = xHasRow1 || xHasRow2 || xHasRow3 || xHasColumn1 || xHasColumn2 || xHasColumn3 || xHasDiagonal || xHasOtherDiagonal; bool ywins = yHasRow1 || yHasRow2 || yHasRow3 || yHasColumn1 || yHasColumn2 || yHasColumn3 || yHasDiagonal || yHasOtherDiagonal; if (xwins && ywins) { throw new InvalidOperationException( "Both X and Y occupy winning positions. Review game log to identify how to fix this bug and then write some covering tests."); } if (xwins) { Winner = xGlyph; return(true, xGlyph); } if (ywins) { Winner = oGlyph; return(true, oGlyph); } //Check for a draw string[] xsandos = new string[] { xGlyph, oGlyph }; if (Squares.All(s => xsandos.Contains(s))) { Winner = "DRAW"; return(true, Winner); } return(false, null); }
/// <summary> /// Determines whether this instance is solved. /// </summary> /// <returns></returns> public bool IsSolved() { return(Squares.All(row => !row.Any(c => c == BOX))); }