// AI move, now it just generates random shot private string[,] AiMove(GameProperties props, string[,] field) { bool moveIsDone = false; while (!moveIsDone) { int row = _rand.Next(0, field.GetLength(0)); int col = _rand.Next(0, field.GetLength(1)); switch (field[row, col]) { case "ship": field[row, col] = "collision"; AddToBattleHistory(props, row, col, DataUtils.BattleMessageHit); props.GetAttackedFlotilla().SetDamage(props, row, col); CheckPlayerFlotillas(props, props.CurrentPlayer); moveIsDone = true; break; case null: case "border": field[row, col] = "miss"; AddToBattleHistory(props, row, col, DataUtils.BattleMessageMiss); moveIsDone = true; break; } } return(field); }
// If one of the fleets is destroyed - name the winner and render game over screen private void CheckPlayerFlotillas(GameProperties props, string attackingSide) { if (!props.GetAttackedFlotilla().Destroyed) { return; } props.Winner = attackingSide; props.MenuOptions.Remove("Save"); props.MenuOptions.Remove("Step back"); props.BattleHistory.Add($"{Color.YellowText}{props.Winner} destroys all enemy ships!"); props.SelectableRowCount = 0; props.Renderer?.GameOverScreen(props, 0); }
// Register damage if the move made hit the ship, indicate a miss if not private void RegisterHit(GameProperties props, int row, int col) { if (props.GetDefenderField()[row, col] == "ship") { props.GetDefenderField()[row, col] = "collision"; AddToBattleHistory(props, row, col, DataUtils.BattleMessageHit); props.GetAttackedFlotilla().SetDamage(props, row, col); CheckPlayerFlotillas(props, props.CurrentPlayer); } else { props.GetDefenderField()[row, col] = "miss"; AddToBattleHistory(props, row, col, DataUtils.BattleMessageMiss); } }