Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Battleship_Try_and_Catch
- {
- class Program
- {
- //array declaration having farray = computer ships(pcboats) and zarray = user ships (userboats)
- public static int[,] farray = new int[12, 12]; //2D array
- public static int[,] zarray = new int[12, 12];
- public static int pc_score, user_score; //variable for the computer's and the player's score
- static void Main(string[] args) //main program
- {
- pcboats();
- userboats(); //goes to the function userboats()
- display(); //displays the userboats()
- pc_shoot(); //goes to the function pc_shoot()
- display(); //displayes userboats() and pc_shoot()
- for (int x = 0; x < 144; x++) //the grid is 12 x 12 giving a total of 144 hits, also the the max. no of hits
- {
- //how the program is displayed
- user_shoot(); //goes to the user_shoot() function
- display(); //shows the refreshed grid
- pc_shoot(); //goes to the pc_shoot() function
- display(); //shows the grid again
- if (pc_score == 15) //20 is the max. no. of hits since the ships add up to 20 squares
- {
- Console.WriteLine(" You LOSE the game ='[ "); //if the player loses the game it displays this message
- break; //stops the program
- }
- else if (user_score == 15) //if computer reaches a score of 20
- {
- Console.WriteLine(" You WIN =]"); //if the player wins the game is message is displayed
- break; //stops the program
- }
- }
- Console.ReadLine(); //waits for an input
- }
- static void display() //display program which will display the grids
- {
- Console.WriteLine("---------------------------------------------------------------------------"); //line above the grid
- Console.WriteLine(" A B C D E F G H I J K L" + " " + "A B C D E F G H I J K L"); //grid upper letters
- //how the arrays are set up
- for (int x = 0; x < 12; x++) //horizontal axes
- {
- Console.Write(" " + x + "\t"); //fills it with array values
- for (int y = 0; y < 12; y++) //with each input in the x-axis, a y-axis column is generated
- {
- if (farray[y, x] != 0) //to display an empty grid
- {
- Console.Write("|" + farray[y, x]); //array values fillup
- }
- else
- {
- Console.Write("| "); //else draw the array with an empty space
- }
- }
- Console.Write("|" + "\t" + x + "\t"); //repetitive process for player's array
- for (int z = 0; z < 12; z++)
- {
- if (zarray[z, x] != 0)
- {
- Console.Write("|" + zarray[z, x]);
- }
- else
- {
- Console.Write("| ");
- }
- }
- Console.Write("|" + " ");
- Console.WriteLine();
- }
- Console.WriteLine("\n\n\n\n\n\n"); //6 new lines
- Console.WriteLine("---------------------------------------------------------------------------"); //this line is displayed under the grids
- Console.ReadLine();
- }
- static void pcboats() //placing pc ships
- {
- int position1, position2; //the x-axes and y-axes
- //1 x 2 square ship
- for (int i = 0; i < 2; i++) //i < 2 since the ship is made up of 1 x 2 squares
- {
- position1 = random_number(12); //generate number from 0 to 11 both for x and for y
- position2 = random_number(12);
- if (farray[position1, position2] == 0) //if the generated position is empty, fill it with 1
- { farray[position1, position2] = 1; }
- else i--; //else make another position until an empty one is found
- }
- for (int boat = 3; boat < 7; boat++)
- {
- position1 = random_number(12 - boat);
- position2 = random_number(12 - boat);
- int x1 = boat - 6;
- int x2 = boat - 5;
- int x3 = boat - 4;
- int x4 = boat - 3;
- int x5 = boat - 2;
- int x6 = boat - 1;
- if (x1 < 0)
- {
- x1 = 0;
- }
- if (x2 < 0)
- {
- x2 = 0;
- }
- if (x3 < 0)
- {
- x3 = 0;
- }
- if (x4 < 0)
- {
- x4 = 0;
- }
- if (x5 < 0)
- {
- x5 = 0;
- }
- if (x6 < 0)
- {
- x6 = 0;
- }
- int direction = random_number(2);
- bool ready = false;
- //horizontal position since direction is 0
- if (direction == 0)
- {
- do
- {
- if ((farray[position1 + x1, position2] == 0) && (farray[position1 + x2, position2] == 0) &&
- (farray[position1 + x3, position2] == 0)&&(farray[position1 + x4, position2] == 0)&&
- (farray[position1 + x5, position2] == 0)&&(farray[position1 + x6, position2] == 0))
- //position1 (horizontal) +1 each time up to 3 squares
- {
- for (int i = 0; i < boat; i++) //i < 3 since the ship is made up of 3 squares
- {
- farray[position1 + i, position2] = 1; //display the ships on the gris as the number 1
- }
- ready = true; //ship is complete mark
- }
- else
- {
- position1 = random_number(12 - boat); //9+3=12
- position2 = random_number(12 - boat); //new position generator
- ready = false;
- }
- } while (ready == false);
- }
- if (direction == 1)
- {
- do
- {
- if ((farray[position1, position2 + x1] == 0) && (farray[position1, position2 + x2] == 0) &&
- (farray[position1, position2 + x3] == 0)&&(farray[position1, position2 + x4] == 0) &&
- (farray[position1, position2 + x5] == 0)&&(farray[position1, position2 + x6] == 0))
- {
- for (int i = 0; i < boat; i++)
- {
- farray[position1, position2 + i] = 1;
- }
- ready = true;
- }
- else
- {
- position1 = random_number(12 - boat);
- position2 = random_number(12 - boat);
- ready = false;
- }
- } while (ready == false);
- }
- }
- }
- static void userboats() //placing user ships
- {
- int direction = 0, position1 = 0, position2 = 0; //variables which will be inputted by the player using a keyboard
- bool flag1 = false, ready = false; //flag declatarion
- //filling up 1 boat
- do
- {
- Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
- try
- {
- position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
- }
- catch
- {
- }
- Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
- try
- {
- position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
- }
- catch
- {
- }
- if ((position1 > -1) && (position1 < 12) //range of the input
- && (position2 > -1) && (position2 < 12)
- && (zarray[position1, position2] == 0))
- {
- flag1 = true;
- zarray[position1, position2] = 1; //approves ship and displayes it
- }
- else
- Console.WriteLine("invalid inputs please enter data again"); //display if coordinates are not right
- }
- while (flag1 == false); //all the above happens
- flag1 = false;
- display(); //shows the grid with the ships on it
- //filling up the second 1 square boat
- {
- Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
- try
- {
- position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
- }
- catch
- {
- }
- Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
- try
- {
- position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
- }
- catch
- {
- }
- if ((position1 > -1) && (position1 < 12)
- && (position2 > -1) && (position2 < 12)
- && (zarray[position1, position2] == 0))
- {
- flag1 = true;
- zarray[position1, position2] = 1;
- }
- else
- Console.WriteLine("invalid inputs please enter data again");
- }
- while (flag1 == false) ;
- flag1 = false;
- display();
- //fill up boat with 3
- ready = false;
- do
- {
- do
- {
- Console.WriteLine("Enter Direction 1 or 0");
- try
- {
- direction = Convert.ToInt16(Console.ReadLine());
- }
- catch
- {
- }
- Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
- try
- {
- position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
- }
- catch
- {
- }
- Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
- try
- {
- position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
- }
- catch
- {
- }
- if (((direction == 1)
- && (position1 > -1) && (position1 < 12)
- && (position2 > -1) && (position2 < 9))
- || ((direction == 0)
- && (position1 > -1) && (position1 < 9)
- && (position2 > -1) && (position2 < 12)))
- flag1 = true;
- else
- Console.WriteLine("invalid inputs please enter data again");
- }
- while (flag1 == false);
- if (direction == 0)
- {
- if ((zarray[position1, position2] == 0) && (zarray[position1 + 1, position2] == 0) &&
- (zarray[position1 + 2, position2] == 0)) //checking if the grid already contains a ship
- {
- for (int ii = 0; ii < 3; ii++)
- {
- zarray[position1 + ii, position2] = 1;
- }
- ready = true;
- }
- else Console.WriteLine("Position contains a ship"); //displays if there is another ship
- }
- if (direction == 1)
- {
- if ((zarray[position1, position2] == 0) && (zarray[position1, position2 + 1] == 0) &&
- (zarray[position1, position2 + 2] == 0))
- {
- for (int ii = 0; ii < 3; ii++)
- {
- zarray[position1, position2 + ii] = 1;
- }
- ready = true;
- }
- else Console.WriteLine("Position contains a ship");
- }
- }
- while (ready == false);
- display();
- //fill up boat with 4
- ready = false;
- do
- {
- do
- {
- Console.WriteLine("Enter Direction 1 or 0");
- try
- {
- direction = Convert.ToInt16(Console.ReadLine());
- }
- catch
- {
- }
- Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
- try
- {
- position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
- }
- catch
- {
- }
- Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
- try
- {
- position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
- }
- catch
- {
- }
- if (((direction == 1)
- && (position1 > -1) && (position1 < 12)
- && (position2 > -1) && (position2 < 9))
- || ((direction == 0)
- && (position1 > -1) && (position1 < 9)
- && (position2 > -1) && (position2 < 12)))
- flag1 = true;
- else
- Console.WriteLine("invalid inputs please enter data again");
- }
- while (flag1 == false);
- if (direction == 0)
- {
- if ((zarray[position1, position2] == 0) && (zarray[position1 + 1, position2] == 0) &&
- (zarray[position1 + 2, position2] == 0) && (zarray[position1 + 3, position2] == 0))
- {
- for (int ii = 0; ii < 4; ii++)
- {
- zarray[position1 + ii, position2] = 1;
- }
- ready = true;
- }
- else Console.WriteLine("Position contains a ship");
- }
- if (direction == 1)
- {
- if ((zarray[position1, position2] == 0) && (zarray[position1, position2 + 1] == 0) &&
- (zarray[position1, position2 + 2] == 0) && (zarray[position1, position2 + 3] == 0))
- {
- for (int ii = 0; ii < 4; ii++)
- {
- zarray[position1, position2 + ii] = 1;
- }
- ready = true;
- }
- else Console.WriteLine("Position contains a ship");
- }
- } while (ready == false);
- display();
- //fill up boat with 5
- //fill up boat with 6
- ready = false;
- do
- {
- do
- {
- Console.WriteLine("Enter Direction 1 or 0");
- try
- {
- direction = Convert.ToInt16(Console.ReadLine());
- }
- catch
- {
- }
- Console.WriteLine("Enter vertical position"); //asks plater vertical postition (y-axes)
- try
- {
- position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
- }
- catch
- {
- }
- Console.WriteLine("Enter horizontal position"); //asks player horiontal position (x-axes)
- try
- {
- position2 = Convert.ToInt16(Console.ReadLine());//reads horizontal and converts string input to intiger
- }
- catch
- {
- }
- if (((direction == 1)
- && (position1 > -1) && (position1 < 12)
- && (position2 > -1) && (position2 < 9))
- || ((direction == 0)
- && (position1 > -1) && (position1 < 9)
- && (position2 > -1) && (position2 < 12)))
- flag1 = true;
- else
- Console.WriteLine("invalid inputs please enter data again");
- }
- while (flag1 == false);
- if (direction == 0)
- {
- if ((zarray[position1, position2] == 0) && (zarray[position1 + 1, position2] == 0) &&
- (zarray[position1 + 2, position2] == 0) && (zarray[position1 + 3, position2] == 0) &&
- (zarray[position1 + 4, position2] == 0) && (zarray[position1 + 5, position2] == 0))
- {
- for (int ii = 0; ii < 6; ii++)
- {
- zarray[position1 + ii, position2] = 1;
- }
- ready = true;
- }
- else Console.WriteLine("Position contains a ship");
- }
- if (direction == 1)
- {
- if ((zarray[position1, position2] == 0) && (zarray[position1, position2 + 1] == 0) &&
- (zarray[position1, position2 + 2] == 0) && (zarray[position1, position2 + 3] == 0) && (
- zarray[position1, position2 + 4] == 0) && (zarray[position1, position2 + 5] == 0))
- {
- for (int ii = 0; ii < 6; ii++)
- {
- zarray[position1, position2 + ii] = 1;
- }
- ready = true;
- }
- else Console.WriteLine("Position contains a ship");
- }
- } while (ready == false);
- }
- static void pc_shoot() //computer shoot
- {
- int position1, position2;
- bool flag = false;
- do
- {
- position1 = random_number(12);
- position2 = random_number(12);
- if ((zarray[position1, position2] == 0) || (zarray[position1, position2] == 1))
- {
- if (zarray[position1, position2] == 0)
- {
- //9 is a miss
- zarray[position1, position2] = 9;
- }
- else
- {
- //8 is a hit
- zarray[position1, position2] = 8;
- pc_score++; //increase the score everytime there is a hit
- }
- flag = true;
- }
- }
- while (flag == false);
- }
- static void user_shoot() //player shoot
- {
- int position1 = 0, position2 = 0;
- bool flag = false;
- do
- {
- Console.WriteLine(" Enter your horizontal coordinate ");
- try
- {
- position1 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
- }
- catch
- {
- }
- Console.WriteLine(" Enter your vertical coordinate");
- try
- {
- position2 = Convert.ToInt16(Console.ReadLine());//reads vertical and converts string input to intiger
- }
- catch
- {
- }
- if ((farray[position1, position2] == 0) || (farray[position1, position2] == 1))
- {
- if (farray[position1, position2] == 0)
- {
- farray[position1, position2] = 9;
- }
- else
- {
- farray[position1, position2] = 8;
- user_score++;
- //increases the user's score by 1 if it is a hit
- }
- flag = true;
- }
- }
- while (flag == false);
- }
- static int random_number(int produce) //random number generator
- {
- int die;
- Random generator = new Random();
- die = (int)(generator.NextDouble() * produce);
- return (die);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement