Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text.RegularExpressions;
- // field codes
- // 0 water
- // 1 enemy ship
- // 2 destroyed enemy ship
- public class Game
- {
- private Random rand;
- private int[,] field;
- private Regex reg;
- public Game()
- {
- this.rand = new Random();
- this.reg = new Regex(@"^\d,\d$", RegexOptions.IgnoreCase);
- this.CreateField();
- this.GenerateAIShips();
- this.PrintBoard();
- // guess in format x,y
- Console.WriteLine("The enemy has placed his ships. Ready your weapons.");
- Console.WriteLine("The Top-Left Field is 0,0 fire in format x,y");
- this.GuessManager();
- // reveal board
- }
- private void GuessManager()
- {
- int rowLength = this.field.GetLength(0);
- int colLength = this.field.GetLength(1);
- bool victory = false;
- while (true)
- {
- for (int i = 0; i < rowLength; i++)
- {
- for (int j = 0; j < colLength; j++)
- {
- if (this.field[i, j] == 1)
- {
- victory = false;
- break;
- }
- }
- }
- if (victory)
- {
- Console.WriteLine("You won! All ships of the enemy are destroyed.");
- break;
- }
- this.TakeGuess();
- }
- }
- private void TakeGuess()
- {
- Console.WriteLine("Take a guess where to fire: ");
- string guess = Console.ReadLine();
- if (!this.reg.IsMatch(guess)) {
- Console.WriteLine("That's an invalid format try again like this: x,y");
- this.TakeGuess();
- return;
- }
- string[] guessArr = guess.Split(new[] { "," }, StringSplitOptions.None);
- int x = Int32.Parse(guessArr[0]);
- int y = Int32.Parse(guessArr[1]);
- if (this.field[y, x] == 1)
- {
- Console.WriteLine("That's a hit!");
- this.field[y, x] = 2;
- }
- else
- {
- Console.WriteLine("That was a shot in water.");
- }
- }
- private void GenerateAIShips()
- {
- // Patrol boat
- this.GenerateShip(2);
- // Destroyer
- this.GenerateShip(3);
- // Submarine
- this.GenerateShip(3);
- // Battleship
- this.GenerateShip(4);
- // Aircraft Carrier
- this.GenerateShip(5);
- }
- private void GenerateShip(int size)
- {
- int num1 = this.rand.Next(0, 10);
- int num2 = this.rand.Next(0, 10);
- bool horizontal = this.GetRandomBoolean();
- bool valid = false;
- while (true)
- {
- if (horizontal)
- {
- for (var i = 0; i < size; i++)
- {
- bool right, left, up, down = false;
- try {
- right = this.field[num1 + i + 1, num2] == 1;
- } catch (IndexOutOfRangeException e) {
- right = false;
- }
- try {
- left = this.field[num1 + i - 1, num2] == 1;
- } catch (IndexOutOfRangeException e) {
- left = false;
- }
- try {
- up = this.field[num1 + i, num2 + 1] == 1;
- } catch (IndexOutOfRangeException e) {
- up = false;
- }
- try {
- down = this.field[num1 + i, num2 - 1] == 1;
- } catch (IndexOutOfRangeException e) {
- down = false;
- }
- if (num1 + i > 4)
- {
- valid = false;
- break;
- }
- else if (this.field[num1 + i, num2] == 1)
- {
- valid = false;
- break;
- }
- else if (up || down || right || down)
- {
- valid = false;
- break;
- }
- valid = true;
- }
- if (valid)
- {
- for (var i = 0; i < size; i++)
- this.field[num1 + i, num2] = 1;
- valid = false;
- break;
- }
- }
- else
- {
- for (var i = 0; i < size; i++)
- {
- bool right, left, up, down = false;
- try
- {
- right = this.field[num1, num2 + i + 1] == 1;
- }
- catch (IndexOutOfRangeException e)
- {
- right = false;
- }
- try
- {
- left = this.field[num1, num2 + i - 1] == 1;
- }
- catch (IndexOutOfRangeException e)
- {
- left = false;
- }
- try
- {
- up = this.field[num1 + 1, num2 + i] == 1;
- }
- catch (IndexOutOfRangeException e)
- {
- up = false;
- }
- try
- {
- down = this.field[num1 - 1, num2 + i] == 1;
- }
- catch (IndexOutOfRangeException e)
- {
- down = false;
- }
- if (num2 + i > 4)
- {
- valid = false;
- break;
- }
- else if (this.field[num1, num2 + i] == 1)
- {
- valid = false;
- break;
- }
- else if (up || down || right || down)
- {
- valid = false;
- break;
- }
- valid = true;
- }
- if (valid)
- {
- for (var i = 0; i < size; i++)
- this.field[num1, num2 + i] = 1;
- valid = false;
- break;
- }
- }
- num1 = this.rand.Next(0, 10);
- num2 = this.rand.Next(0, 10);
- horizontal = this.GetRandomBoolean();
- }
- }
- private void PrintBoard()
- {
- int rowLength = this.field.GetLength(0);
- int colLength = this.field.GetLength(1);
- for (int i = 0; i < rowLength; i++)
- {
- for (int j = 0; j < colLength; j++)
- {
- Console.Write(string.Format("{0} ", this.field[i, j]));
- }
- Console.Write(Environment.NewLine + Environment.NewLine);
- }
- }
- public bool GetRandomBoolean()
- {
- return this.rand.Next(0, 2) == 0;
- }
- private void CreateField()
- {
- // setup empty field
- this.field = new int[10, 10] {
- {0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0}
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment