public void FireOnInvaders(Invader[] invaders) { for (int index = 0; index < invaders.Length; index++) { Invader invader = invaders[index]; // Do stuff with invader } foreach (Invader invader in invaders) { if (invader.IsActive && _location.InRangeOf(invader.Location, _range)) { if (IsSuccessfulShot()) { invader.DecreaseHealth(_power); if (invader.IsNeutralized) { Console.WriteLine("Neutralized an invader!"); } } else { Console.WriteLine("Shot at and missed an invader."); } break; } } }
public static void Main() { //This is setting the value of my 'map' varaiable to a new instance of my 'Map' class with the values 8 and 5 (i.e. newing up my class) var mymap = new Map(8, 5); try { var mypoint = new MapLocation(20, 20, mymap); } catch (Exception error) { Console.WriteLine(error); } //These are new instances of my Classes and 'mytower' is the object of my 'Tower' class. //By creating the 'mytower' object, it has created an 'instance' of the 'Tower' class. //The term for creating an object from a class is called 'Instantiation', I've just instantiated an object from the tower class var mytower = new Tower(2); var towerplace = mytower.Place(5, 2); var invader = new Invader(4, 3); var path = new Path(4, 3); }
public static void Main() { Tower tower = new Tower(); Map map = new Map(8, 5); try { Path path = new Path( new [] { new MapLocation(0, 2, map), new MapLocation(1, 2, map), new MapLocation(2, 2, map), new MapLocation(3, 2, map), new MapLocation(4, 2, map), new MapLocation(5, 2, map), new MapLocation(6, 2, map), new MapLocation(7, 2, map) } ); Invader invader = new Invader(); MapLocation location = new MapLocation(0, 0, map); // invader.SetLocation(location); // uses method setter // invader.Location = location; //uses property setter // location = invader.Location; // uses property getter } catch (OutOfBoundsException ex) { Console.WriteLine(ex.Message); } catch (DefenseException) { Console.WriteLine("Unhandled tree house defense exception"); } catch (Exception ex) { Console.WriteLine("Unhandled exception: " + ex); } Console.ReadLine(); }
public void PlayGame() { Map map = new Map(8, 5); try { Path path = new Path( new[] { new MapLocation(0, 2, map), new MapLocation(1, 2, map), new MapLocation(2, 2, map), new MapLocation(3, 2, map), new MapLocation(4, 2, map), new MapLocation(5, 2, map), new MapLocation(6, 2, map), new MapLocation(7, 2, map) } ); IInvader[] invaders = { new ShieldedInvader(path), new FastInvader(path), new StrongInvader(path), new BasicInvader(path), new ResurrectingInvader(path) }; Tower[] towers = { new Tower(new MapLocation(1, 3, map)), new Tower(new MapLocation(3, 3, map)), new Tower(new MapLocation(5, 3, map)) }; Level leve1 = new Level(invaders) { Towers = towers }; bool playerWon = leve1.Play(); int numberOfGameActions = leve1.getActionCount(); Console.WriteLine("****************Game Results*********************"); Console.WriteLine("Player " + (playerWon ? "won" : "lost")); Console.WriteLine("*************************************************"); Console.WriteLine("***************Meta Analysis*********************"); Console.WriteLine("The number of individual actions the game generated was " + numberOfGameActions); Console.WriteLine("Number of Invaders Created " + Invader.getNumberOfInvaders()); Console.WriteLine("*************************************************"); } catch (OutOfBoundsException ex) { Console.WriteLine(ex.Message); } catch (DefenseException) { Console.WriteLine("Unhandled DefenseException"); } catch (Exception ex) { Console.WriteLine("Unhandled Exception: " + ex); } Console.ReadKey(); }
static void Main() { Map map = new Map(8, 5); Random _random = new Random(); int numberOfInvaders = _random.Next(1, 4); try { Path path = new Path( new[] { new MapLocation(0, 2, map), new MapLocation(1, 2, map), new MapLocation(2, 2, map), new MapLocation(3, 2, map), new MapLocation(4, 2, map), new MapLocation(5, 2, map), new MapLocation(6, 2, map), new MapLocation(7, 2, map) } ); Invader[] invaders = new Invader[numberOfInvaders]; for (int i = 0; i < numberOfInvaders; i++) { if (i % 2 == 0) { invaders[i] = new FastInvader(path); } else { invaders[i] = new Invader(path); } } Tower[] towers = { new StrongTower(new MapLocation(1, 3, map)), new Tower(new MapLocation(3, 3, map)), new SniperTower(new MapLocation(5, 3, map)) }; Level level = new Level(invaders) { Towers = towers }; bool playerWon = level.Play(); Console.WriteLine("Player " + (playerWon ? "won" : "lost")); } catch (OutOfBoundsException ex) { Console.WriteLine(ex.Message); } catch (DefenseException) { Console.WriteLine("Unhandled DefenseException"); } catch (Exception ex) { Console.WriteLine("Unhandled Exception: " + ex); } }