public HitResult IsHit(Square square) { if (!Squares.Contains(square)) { return(HitResult.Missed); } var sq = Squares.First(s => s == square); sq.Hit = true; bool sunk = Squares.Count(s => s.Hit == true) == Squares.Count(); return(sunk ? HitResult.Sunk : HitResult.Hit); }
public void MoveRight() { int checksum = Squares.Count(s => s.CanMoveRight()); if (checksum != Squares.Length) { return; } foreach (Square s in Squares) { s.MoveRight(); } }
/// <summary> /// returns true if the drop was without collision, returns false if the block collided, and was put on the gameboard /// </summary> /// <returns>bool</returns> public bool MoveDown() { int checksum = Squares.Count(s => s.CanMoveDown()); if (checksum != Squares.Length) { GameBoard.Instance.PutBlock(this); return(false); } foreach (Square s in Squares) { s.MoveDown(); } return(true); }
public HitResult Hit(Square square) { if (!Squares.Contains(square)) { return(HitResult.Missed); } Squares.First(s => s == square).Hit = true; if (Squares.Count(s => s.Hit) == Squares.Count()) { foreach (var s in Squares) { s.SetState(HitResult.Sunken); } return(HitResult.Sunken); } return(HitResult.Hit); }
/// <summary> /// Logic for determining if a move is valid or not. /// </summary> /// <param name="XorO">the X or O</param> /// <param name="square">The number of the square on the gameboard to place the X or O.</param> /// <returns>Tuple with bool for if the move is a valid move and a string message describing why.</returns> public (bool isvalid, string msg) MoveIsValid(string XorO, int square) { string resultMessage = null; bool isvalid = false; if (square < 0 || square > 9) { return(isvalid, $"Chosen square must be between 0 and 9. You entered: {square}. Turn aborted, try again."); } if (Squares[square] == xGlyph || Squares[square] == oGlyph) { resultMessage = $"The square is already occupied by {Squares[square]}. Turn aborted, try again."; return(false, resultMessage); } int xCount = Squares.Count(s => s == xGlyph); int oCount = Squares.Count(s => s == oGlyph); switch (XorO) { case "X": isvalid = xCount == oCount; if (!isvalid) { resultMessage = $"It is O's turn to move. Play fair."; } break; case "O": isvalid = oCount == (xCount - 1); if (!isvalid) { resultMessage = $"It is X's turn to move. Play fair."; } break; default: resultMessage = $"Received {XorO} instead of an X or an O. Turn aborted, try again."; break; } return(isvalid, resultMessage); }
public (string whosTurn, string gameBoard) WhosTurnIsIt() { string board = RenderBoard(); var gameStatus = IsGameOver(); if (gameStatus.gameIsOver) { return("GAME OVER", board); } int xCount = Squares.Count(s => s == xGlyph); int oCount = Squares.Count(s => s == oGlyph); if (xCount == 0 || xCount == oCount) { return(xGlyph, board); } else { return(oGlyph, board); } }