gempir

battleships

Mar 16th, 2016
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.84 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. // field codes
  5.  
  6. // 0 water
  7. // 1 enemy ship
  8. // 2 destroyed enemy ship
  9. public class Game
  10. {
  11.     private Random rand;
  12.     private int[,] field;
  13.     private Regex reg;
  14.  
  15.     public Game()
  16.     {
  17.         this.rand = new Random();
  18.         this.reg  = new Regex(@"^\d,\d$", RegexOptions.IgnoreCase);
  19.         this.CreateField();
  20.         this.GenerateAIShips();
  21.         this.PrintBoard();
  22.         // guess in format x,y
  23.         Console.WriteLine("The enemy has placed his ships. Ready your weapons.");
  24.         Console.WriteLine("The Top-Left Field is 0,0 fire in format x,y");
  25.         this.GuessManager();
  26.         // reveal board
  27.        
  28.     }
  29.  
  30.     private void GuessManager()
  31.     {
  32.         int rowLength = this.field.GetLength(0);
  33.         int colLength = this.field.GetLength(1);
  34.         bool victory = false;
  35.         while (true)
  36.         {
  37.             for (int i = 0; i < rowLength; i++)
  38.             {
  39.                 for (int j = 0; j < colLength; j++)
  40.                 {
  41.                     if (this.field[i, j] == 1)
  42.                     {
  43.                         victory = false;
  44.                         break;
  45.                     }
  46.                 }
  47.             }
  48.             if (victory)
  49.             {
  50.                 Console.WriteLine("You won! All ships of the enemy are destroyed.");
  51.                 break;
  52.             }
  53.             this.TakeGuess();
  54.         }
  55.     }
  56.  
  57.     private void TakeGuess()
  58.     {
  59.         Console.WriteLine("Take a guess where to fire: ");
  60.         string guess = Console.ReadLine();
  61.         if (!this.reg.IsMatch(guess)) {
  62.             Console.WriteLine("That's an invalid format try again like this: x,y");
  63.             this.TakeGuess();
  64.             return;
  65.         }
  66.        
  67.         string[] guessArr = guess.Split(new[] { "," }, StringSplitOptions.None);
  68.         int x = Int32.Parse(guessArr[0]);
  69.         int y = Int32.Parse(guessArr[1]);
  70.  
  71.         if (this.field[y, x] == 1)
  72.         {
  73.             Console.WriteLine("That's a hit!");
  74.             this.field[y, x] = 2;
  75.         }
  76.         else
  77.         {
  78.             Console.WriteLine("That was a shot in water.");
  79.         }
  80.     }
  81.  
  82.     private void GenerateAIShips()
  83.     {
  84.         // Patrol boat
  85.         this.GenerateShip(2);
  86.         // Destroyer
  87.         this.GenerateShip(3);
  88.         // Submarine
  89.         this.GenerateShip(3);
  90.         // Battleship
  91.         this.GenerateShip(4);
  92.         // Aircraft Carrier
  93.         this.GenerateShip(5);
  94.     }
  95.  
  96.     private void GenerateShip(int size)
  97.     {
  98.         int num1 = this.rand.Next(0, 10);
  99.         int num2 = this.rand.Next(0, 10);
  100.         bool horizontal = this.GetRandomBoolean();
  101.         bool valid = false;
  102.  
  103.         while (true)
  104.         {
  105.             if (horizontal)
  106.             {
  107.                 for (var i = 0; i < size; i++)
  108.                 {
  109.                     bool right, left, up, down = false;
  110.                     try {
  111.                         right = this.field[num1 + i + 1, num2] == 1;
  112.                     } catch (IndexOutOfRangeException e) {
  113.                         right = false;
  114.                     }
  115.                     try {
  116.                         left  = this.field[num1 + i - 1, num2] == 1;
  117.                     } catch (IndexOutOfRangeException e) {
  118.                         left = false;
  119.                     }
  120.                     try {
  121.                          up = this.field[num1 + i, num2 + 1] == 1;
  122.                     } catch (IndexOutOfRangeException e) {
  123.                          up = false;
  124.                     }
  125.                     try {
  126.                         down  = this.field[num1 + i, num2 - 1] == 1;
  127.                     } catch (IndexOutOfRangeException e) {
  128.                         down = false;
  129.                     }
  130.                    
  131.                     if (num1 + i > 4)
  132.                     {
  133.                         valid = false;
  134.                         break;
  135.                     }
  136.                     else if (this.field[num1 + i, num2] == 1)
  137.                     {
  138.                         valid = false;
  139.                         break;
  140.                     }
  141.                     else if (up || down || right || down)
  142.                     {
  143.                         valid = false;
  144.                         break;
  145.                     }
  146.                     valid = true;
  147.                 }
  148.                 if (valid)
  149.                 {
  150.                     for (var i = 0; i < size; i++)
  151.                         this.field[num1 + i, num2] = 1;
  152.                     valid = false;
  153.                     break;
  154.                 }
  155.             }
  156.             else
  157.             {
  158.                 for (var i = 0; i < size; i++)
  159.                 {
  160.                     bool right, left, up, down = false;
  161.                     try
  162.                     {
  163.                         right = this.field[num1, num2 + i + 1] == 1;
  164.                     }
  165.                     catch (IndexOutOfRangeException e)
  166.                     {
  167.                         right = false;
  168.                     }
  169.                     try
  170.                     {
  171.                         left = this.field[num1, num2 + i - 1] == 1;
  172.                     }
  173.                     catch (IndexOutOfRangeException e)
  174.                     {
  175.                         left = false;
  176.                     }
  177.                     try
  178.                     {
  179.                         up = this.field[num1 + 1, num2 + i] == 1;
  180.                     }
  181.                     catch (IndexOutOfRangeException e)
  182.                     {
  183.                         up = false;
  184.                     }
  185.                     try
  186.                     {
  187.                         down = this.field[num1 - 1, num2 + i] == 1;
  188.                     }
  189.                     catch (IndexOutOfRangeException e)
  190.                     {
  191.                         down = false;
  192.                     }
  193.  
  194.                     if (num2 + i > 4)
  195.                     {
  196.                         valid = false;
  197.                         break;
  198.                     }
  199.                     else if (this.field[num1, num2 + i] == 1)
  200.                     {
  201.                         valid = false;
  202.                         break;
  203.                     }
  204.                     else if (up || down || right || down)
  205.                     {
  206.                         valid = false;
  207.                         break;
  208.                     }
  209.                     valid = true;
  210.                 }
  211.                 if (valid)
  212.                 {
  213.                     for (var i = 0; i < size; i++)
  214.                         this.field[num1, num2 + i] = 1;
  215.                     valid = false;
  216.                     break;
  217.                 }
  218.             }
  219.             num1 = this.rand.Next(0, 10);
  220.             num2 = this.rand.Next(0, 10);
  221.             horizontal = this.GetRandomBoolean();
  222.         }
  223.     }
  224.  
  225.     private void PrintBoard()
  226.     {
  227.         int rowLength = this.field.GetLength(0);
  228.         int colLength = this.field.GetLength(1);
  229.  
  230.         for (int i = 0; i < rowLength; i++)
  231.         {
  232.             for (int j = 0; j < colLength; j++)
  233.             {
  234.                 Console.Write(string.Format("{0} ", this.field[i, j]));
  235.             }
  236.             Console.Write(Environment.NewLine + Environment.NewLine);
  237.         }
  238.     }
  239.  
  240.     public bool GetRandomBoolean()
  241.     {
  242.         return this.rand.Next(0, 2) == 0;
  243.     }
  244.  
  245.     private void CreateField()
  246.     {
  247.         // setup empty field
  248.         this.field = new int[10, 10] {
  249.                 {0,0,0,0,0,0,0,0,0,0},
  250.                 {0,0,0,0,0,0,0,0,0,0},
  251.                 {0,0,0,0,0,0,0,0,0,0},
  252.                 {0,0,0,0,0,0,0,0,0,0},
  253.                 {0,0,0,0,0,0,0,0,0,0},
  254.                 {0,0,0,0,0,0,0,0,0,0},
  255.                 {0,0,0,0,0,0,0,0,0,0},
  256.                 {0,0,0,0,0,0,0,0,0,0},
  257.                 {0,0,0,0,0,0,0,0,0,0},
  258.                 {0,0,0,0,0,0,0,0,0,0}
  259.             };
  260.     }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment