/// <summary> /// /// </summary> /// <param name="e"></param> public void ReceiveShot(AimAndFireEventArgs e) { //erhält eine Kordinate, wo hingeschossen wurde //und fragt jedes seine Schiffe, ob es an dieser Stelle getroffen wurde Impact result = new Impact(); for (int i = 0; i < fleet.Ships.Length; i++) { result = fleet.Ships[i].TryToGetHit(e.Coord); if (result.HitDetection != HitDetection.Miss) //wenn ein Schiff getroffen wurde, { // müssen wir nicht mehr prüfen, break; //ob ein anderes Schiff getroffen wurde } } if (result.HitDetection == HitDetection.Sink) //wenn das getroffene schiff versenkt wurde, { //dann prüfen ob dieses Versenken das Spiel entschieden hat bool defeat = false; for (int i = 0; i < fleet.Ships.Length; i++) { defeat = fleet.Ships[i].Destroyed; if (!defeat) break; } if (defeat) { Program.Winner = Program.DetermineWinner(this._PLAYERTYPE); } } if (result.HitDetection == HitDetection.Miss) //wenn kein Schiff getroffen wurde, { // kann auch kein schuff sagen wo es getroffen wurde Coord[] coords = new Coord[1] { e.Coord }; //also müssen wir die impact-Koordinate selbst angeben result.Coords = coords; } this.RefreshTiles(result.Coords, result.HitDetection); this.Redraw(); e.Impact = result; }
/// <summary> /// controls an interface where the player may choose where to attempt a shot or forfeit the game to quit early /// </summary> /// <returns>returns true if the player forfeited the game</returns> public bool AimAndFire() { //gibt einen Schuss auf das übergebene Spielfeld ab. Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Gray; this.Redraw(); ConsoleKeyInfo myKey; do { //let the player choose a coordinate for taking a shot Console.SetCursorPosition(cursorPosition.Col * 4 + 3, cursorPosition.Row * 2 + 2); myKey = Console.ReadKey(true); switch (myKey.Key) { case ConsoleKey.UpArrow: { if (cursorPosition.Row - 1 >= 0) cursorPosition.Row--; break; } case ConsoleKey.DownArrow: { if (cursorPosition.Row + 1 < 10) cursorPosition.Row++; break; } case ConsoleKey.LeftArrow: { if (cursorPosition.Col - 1 >= 0) cursorPosition.Col--; } break; case ConsoleKey.RightArrow: { if (cursorPosition.Col + 1 <= 9) cursorPosition.Col++; } break; default: break; } } while (myKey.Key != ConsoleKey.Spacebar && myKey.Key != ConsoleKey.X); switch (myKey.Key) { case ConsoleKey.Spacebar: { AimAndFireEventArgs e = new AimAndFireEventArgs(); e.Coord = cursorPosition; AimFireEvent(e); Impact impactResult = e.Impact; this.RefreshTiles(impactResult.Coords, this.HitDetectionAsRadarTile(impactResult.HitDetection)); break; } case ConsoleKey.X: return true; default: break; } return false; }
/// <summary> /// Function for the AI taking a shot /// </summary> /// <param name="coord">coords where the AI wants to take a shot</param> public void AimAndFire(Coord coord) { //Feuerfunktion für die KI. AimAndFireEventArgs e = new AimAndFireEventArgs(); e.Coord = coord; //targetGrid.ReceiveShot(e); //deprecated due to transition to events AimFireEvent(e); Impact impactResult = e.Impact; if (impactResult.HitDetection == HitDetection.Miss) { Coord[] coords = new Coord[1]; impactResult.Coords = coords; impactResult.Coords[0] = new Coord(coord); } this.RefreshTiles(impactResult.Coords, this.HitDetectionAsRadarTile(impactResult.HitDetection)); }