Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication67
- {
- class Program
- {
- public static void generategame(ref int[,] grid, int ships)
- {
- Random a = new Random();
- for (int i = 0; i < ships; i++)
- {
- int row = a.Next(0, 9);
- int column = a.Next(0, 9);
- if (grid[row, column] == 0)
- {
- grid[row, column] = 1;
- }
- else
- {
- i--;
- }
- }
- }
- public static void showgame(ref int[,] grid)
- {
- int x;
- int y;
- String output = "Tips: \n [9] You have hit a target \n [5] You have missed the target\n\n |";
- for (y = 0; y < grid.GetLength(1); y++)
- {
- output += String.Format("[{0}]", y);
- }
- output += "\n";
- for (y = 0; y < grid.GetLength(0); y++)
- {
- // coordinates
- output += String.Format(" [{0}] ", y);
- for (x = 0; x < grid.GetLength(1); x++)
- {
- if (grid[x, y] == 1)
- output += " 0 ";
- else
- output += String.Format(" {0} ", grid[x, y]);
- }
- output += "\n";
- }
- Console.WriteLine(output);
- }
- public static bool checklist(ref int[,] grid, int row, int column)
- {
- if (grid[row, column] == 1)
- return true;
- else
- return false;
- }
- static void Main(string[] args)
- {
- int[,] grid = new int[10, 10];
- int ships = 0;
- int x;
- int y;
- bool valid = false;
- bool invalid = false;
- int hitcount = 0;
- int misscount = 0;
- while (!valid)
- {
- Console.WriteLine("Please enter the amount of ships(3-10):");
- ships = Convert.ToInt32(Console.ReadLine());
- if (ships > 2 && ships < 11)
- {
- valid = true;
- }
- }
- generategame(ref grid, ships);
- showgame(ref grid);
- valid = false;
- while (!valid)
- {
- Console.WriteLine("Please enter the coordinates on the grid");
- Console.WriteLine("Please Select [X] coordinates");
- x = int.Parse(Console.ReadLine());
- Console.WriteLine("Please Select [Y] coordinates");
- y = int.Parse(Console.ReadLine());
- if (y < 0 || x < 0 || y >= 10 || x >= 10)
- {
- Console.WriteLine("InvalidInput - Please try again");
- invalid = true;
- }
- else
- {
- invalid = false;
- }
- if (checklist(ref grid, x, y))
- {
- grid[x, y] = 9;
- hitcount++;
- Console.WriteLine("you have hit {0} ships", hitcount);
- if (hitcount == ships)
- {
- Console.WriteLine("|- - - - - - - - - - - - - - - - - - - -| ");
- Console.WriteLine("| - - - - you have won the game - - - - | ");
- Console.WriteLine("|- - - - - - - - - - - - - - - - - - - -| ");
- Console.WriteLine("| With a total of {0} hits and {1} misses |", hitcount, misscount);
- break;
- }
- }
- else
- {
- grid[x, y] = 5;
- Console.WriteLine("you have missed {0} times", misscount + 1);
- misscount++;
- }
- showgame(ref grid);
- }
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement