Advertisement
mess0011

proj3_final

Nov 12th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication67
  8. {
  9.     class Program
  10.     {
  11.         public static void generategame(ref int[,] grid, int ships)
  12.         {
  13.             Random a = new Random();
  14.  
  15.             for (int i = 0; i < ships; i++)
  16.             {
  17.                 int row = a.Next(0, 9);
  18.                 int column = a.Next(0, 9);
  19.                 if (grid[row, column] == 0)
  20.                 {
  21.                     grid[row, column] = 1;
  22.                 }
  23.                 else
  24.                 {
  25.                     i--;
  26.                 }
  27.             }
  28.         }
  29.  
  30.         public static void showgame(ref int[,] grid)
  31.         {
  32.  
  33.             int x;
  34.             int y;
  35.             String output = "Tips: \n [9] You have hit a target \n [5] You have missed the target\n\n    |";
  36.  
  37.             for (y = 0; y < grid.GetLength(1); y++)
  38.             {
  39.                 output += String.Format("[{0}]", y);
  40.                
  41.             }
  42.             output += "\n";
  43.  
  44.  
  45.             for (y = 0; y < grid.GetLength(0); y++)
  46.             {
  47.                 // coordinates
  48.                 output += String.Format(" [{0}] ", y);
  49.                 for (x = 0; x < grid.GetLength(1); x++)
  50.                 {
  51.                     if (grid[x, y] == 1)
  52.                         output += " 0 ";
  53.                     else
  54.                         output += String.Format(" {0} ", grid[x, y]);
  55.                 }
  56.                 output += "\n";
  57.             }
  58.  
  59.             Console.WriteLine(output);
  60.  
  61.         }
  62.  
  63.  
  64.         public static bool checklist(ref int[,] grid, int row, int column)
  65.         {
  66.             if (grid[row, column] == 1)
  67.  
  68.                 return true;
  69.  
  70.             else
  71.                 return false;
  72.         }
  73.  
  74.  
  75.  
  76.         static void Main(string[] args)
  77.         {
  78.             int[,] grid = new int[10, 10];
  79.             int ships = 0;
  80.             int x;
  81.             int y;
  82.             bool valid = false;
  83.             bool invalid = false;
  84.             int hitcount = 0;
  85.             int misscount = 0;
  86.  
  87.             while (!valid)
  88.             {
  89.                 Console.WriteLine("Please enter the amount of ships(3-10):");
  90.                 ships = Convert.ToInt32(Console.ReadLine());
  91.  
  92.                 if (ships > 2 && ships < 11)
  93.                 {
  94.                     valid = true;
  95.                 }
  96.             }
  97.  
  98.             generategame(ref grid, ships);
  99.             showgame(ref grid);
  100.             valid = false;
  101.  
  102.             while (!valid)
  103.             {
  104.                 Console.WriteLine("Please enter the coordinates on the grid");
  105.                 Console.WriteLine("Please Select [X] coordinates");
  106.                 x = int.Parse(Console.ReadLine());
  107.                 Console.WriteLine("Please Select [Y] coordinates");
  108.                 y = int.Parse(Console.ReadLine());
  109.  
  110.                 if (y < 0 || x < 0 || y >= 10 || x >= 10)
  111.                 {
  112.                     Console.WriteLine("InvalidInput - Please try again");
  113.                     invalid = true;
  114.                 }
  115.                 else
  116.                 {
  117.                     invalid = false;
  118.                 }
  119.  
  120.                 if (checklist(ref grid, x, y))
  121.                 {
  122.                     grid[x, y] = 9;
  123.                     hitcount++;
  124.                     Console.WriteLine("you have hit {0} ships", hitcount);
  125.  
  126.                     if (hitcount == ships)
  127.                     {
  128.                         Console.WriteLine("|- - - - - - - - - - - - - - - - - - - -| ");
  129.                         Console.WriteLine("| - - - - you have won the game - - - - | ");
  130.                         Console.WriteLine("|- - - - - - - - - - - - - - - - - - - -| ");
  131.                         Console.WriteLine("|   With a total of {0} hits and {1} misses |", hitcount, misscount);
  132.                         break;
  133.                     }
  134.                 }
  135.                 else
  136.                 {
  137.                     grid[x, y] = 5;
  138.                     Console.WriteLine("you have missed {0} times", misscount + 1);
  139.                     misscount++;
  140.                 }
  141.                 showgame(ref grid);
  142.             }
  143.             Console.ReadLine();
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement