Example #1
0
        static void Main(string[] args)
        {
            //Prepare Operating-system independent environment and initialize some variables
            _winner = Winner.undetermined;
            if (PlatformID.Win32NT == Environment.OSVersion.Platform) 
            {
                Console.WindowWidth = 100;
            } 
            else
            {
                Console.WriteLine("Please make sure that your Terminal Window is at least 100 characters wide");
                Console.WriteLine("Press any key to cotinue when ready...");
                Console.ReadKey();
            }
	        Console.ForegroundColor = ConsoleColor.Gray;
            
            //Create and initialize playing grids in which each player can see their own ships
            HomeGrid playerHomeGrid = new HomeGrid(Playertype.Human);
            HomeGrid aiHomeGrid = new HomeGrid(Playertype.AI);

            //Create and initialize radar Grids in which each player can 
            EnemyGrid playerEnemyGrid = new EnemyGrid(Playertype.Human);
            EnemyGrid AIEnemyGrid = new EnemyGrid(Playertype.AI);

            //fleet deployment (currently automatic)
			Fleet playerFleet = new Fleet();
            Fleet aiFleet = new Fleet();
            playerHomeGrid.DeployFleet(playerFleet);
            aiHomeGrid.DeployFleet(aiFleet);
            bool forfeit=false;

            // subscribe to fire-exchange events
            playerEnemyGrid.AimFireEvent += aiHomeGrid.ReceiveShot;
            AIEnemyGrid.AimFireEvent += playerHomeGrid.ReceiveShot;

            //Main game phase
            do
            {
                forfeit = playerEnemyGrid.AimAndFire();  
                if (Winner == Winner.undetermined && !forfeit) AI.Turn(AIEnemyGrid);
  
            } while (Winner == Winner.undetermined && !forfeit);

            //End of the game
            Console.SetCursorPosition(0, 23);

            if (forfeit)
            {
                
                Console.WriteLine("AI has won by player forfeiture");
            }
            else
            {
               
                Console.WriteLine("Game ends. {0} victorious!", _winner.ToString());
            }
            Console.ReadLine();
			
        }
Example #2
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="aiEnemyGrid"></param>
 public static void Turn(EnemyGrid aiEnemyGrid)
 {
     //führt den spielzug des Computergegners durch
     Coord coord = new Coord();
     Random rnd = new Random(DateTime.Now.GetHashCode());
     coord.Col = rnd.Next(10);
     coord.Row = rnd.Next(10);
     aiEnemyGrid.AimAndFire(coord);
 }