Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <Windows.h>
- using namespace std;
- void Direction(char BoardMe[10][10], char HMSMe[10][10], int ship, bool& Error, int cor1, int cor2, bool Computer); //0 is right, 1 is left, 2 is up, 3 is down
- void BoardCreate(char Initialize[10][10]);
- void OutputTables(char Display[10][10]);
- bool AlreadyFilled(char BoardMe[10][10], int ship, int x, int y, int direction);
- int RandomShip();
- int RandomCor();
- int RandomDirection();
- int main()
- {
- char BoardComputer[10][10];
- char BoardMe[10][10];
- char HMSComputer[10][10];
- char HMSMe[10][10];
- int ship_select, direction;
- int ship;
- int x = 0;
- int y = 0;
- int cor1 = 1;
- int cor2 = 1;
- string Carrier = " 1 for your carrier (5 length),";
- string Battleship = " 2 for your battleship (4 length)";
- string Cruiser = "\n3 for your cruiser (3 length),";
- string Sub = " 4 for your sub (3 length),";
- string Destroyer = " or 5 for your destroyer (2 length)";
- bool carrier = false; //This block of code is made to decipher what ship the user wants to manipulate and what direction
- bool battle = false;
- bool cruiser = false;
- bool destroyer = false;
- bool sub = false;
- bool Finished = false; //Stops setup
- bool check = false;
- bool Error = false;
- bool Computer = true;
- bool ComputerError = false;
- int CFin = 0; //Checks if computer setup is complete
- BoardCreate(HMSComputer);
- BoardCreate(HMSMe);
- BoardCreate(BoardMe);
- BoardCreate(BoardComputer);
- /*
- OutputTables(HMSComputer);
- OutputTables(HMSMe);
- OutputTables(BoardComputer);
- OutputTables(BoardMe);
- */
- srand((unsigned)time(0));
- while (Finished == false)
- {
- if (Computer == false)
- {
- cout << "Welcome to battleship, which ship would you like to set up?" << endl << endl;
- cout << "Enter" << Carrier << Battleship << Cruiser << Sub << Destroyer << endl;
- cin >> ship_select;
- }
- else
- ship_select = RandomShip();
- switch (ship_select) //Selects ship you use.
- {
- case 1:
- //Carrier ship setup
- if (carrier == false)
- {
- ship = 5;
- if (Computer == false)
- Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
- else
- Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
- }
- if (Error == false)
- {
- Carrier = "";
- carrier = true;
- CFin++;
- }
- Error = false;
- break;
- case 2:
- //battle ship setup
- if (battle == false)
- {
- ship = 4;
- if (Computer == false)
- Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
- else
- Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
- }
- if (Error == false)
- {
- Battleship = "";
- battle = true;
- CFin++;
- }
- Error = false;
- break;
- case 3:
- //cruiser ship setup
- if (cruiser == false)
- {
- ship = 3;
- if (Computer == false)
- Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
- else
- Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
- }
- if (Error == false)
- {
- Cruiser = "";
- cruiser = true;
- }
- if (Computer == true && Error == false)
- CFin++;
- Error = false;
- break;
- case 4:
- //submarine ship setup
- if (sub == false)
- {
- ship = 3;
- if (Computer == false)
- Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
- else
- Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
- }
- if (Error == false)
- {
- Sub = "";
- sub = true;
- }
- if (Computer == true && Error == false)
- CFin++;
- Error = false;
- break;
- case 5:
- //destroyer ship setup
- if (destroyer == false)
- {
- ship = 3;
- if (Computer == false)
- Direction(BoardMe, HMSMe, ship, Error, cor1, cor2, Computer);
- else
- Direction(BoardComputer, HMSComputer, ship, Error, x, y, Computer);
- }
- if (Error == false)
- {
- Destroyer = "";
- destroyer = true;
- }
- if (Computer == true && Error == false)
- CFin++;
- Error = false;
- break;
- default: cout << "You have entered in an incorrect number, Try again" << endl;
- break;
- }
- if (CFin == 5 && Computer == true)
- {
- if (CFin==5)
- cout << "Got here" << endl << flush;
- Carrier = " 1 for your carrier (5 length),";
- Battleship = " 2 for your battleship (4 length)";
- Cruiser = "\n3 for your cruiser (3 length),";
- Sub = " 4 for your sub (3 length),";
- Destroyer = " or 5 for your destroyer (2 length)";
- carrier = false; //This block of code resets for human setup
- battle = false;
- cruiser = false;
- destroyer = false;
- sub = false;
- Computer == false;
- }
- cout << CFin << endl;
- if (carrier == true && battle == true && cruiser == true && destroyer == true && sub == true && CFin == 5)
- Finished = true;
- }
- return 0;
- }
- void Direction(char BoardMe[10][10], char HMSMe[10][10], int ship, bool& Error, int cor1, int cor2, bool Computer)
- {
- int i = 1;
- int direction;
- string up = " 1) up,";
- string down = " 0) down,";
- string left = " 3) left";
- string right = " 2) right,";
- if (Computer == false)
- {
- cout << "Please enter your starting coordinates, they will be numbers between 1-10. For example, 1 3 is first row, third column." << endl;
- cin >> cor1 >> cor2;
- }
- else
- {
- cor1 = RandomCor();
- cor2 = RandomCor();
- }
- if ((cor2 - 1 - ship) < 1)
- left = "";
- if ((cor2 - 1 + ship) > 10)
- right = "";
- if ((cor1 - 1 + ship) > 10)
- down = "";
- if ((cor1 - 1 - ship) < 1)
- up = "";
- if (Computer == false)
- {
- cout << "Would you like to go" << down << up << right << left << " from your origin point, ";
- cout << cor1 << " " << cor2 << "?" << endl;
- cin >> direction;
- }
- else
- direction = RandomDirection();
- cout << direction << " " << cor1 << " " << cor2 << endl;
- switch (direction) //Sets placement of ships
- {
- case 0:
- if (down != "" && AlreadyFilled(BoardMe, ship, cor1, cor2, direction) == true)
- {
- for (int count = 0; count < ship; count++)
- {
- BoardMe[cor1 - i][cor2 - 1] = '#';
- HMSMe[cor1 - i][cor2 - 1] = '#';
- i--;
- }
- i = 1;
- }
- else
- {
- Error = true;
- if (Computer == false)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
- cout << "You are not allowed to do that. Try again." << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
- }
- }
- break;
- case 1:
- if (up != "" && AlreadyFilled(BoardMe, ship, cor1, cor2, direction) == true)
- {
- for (int count = 0; count < ship; count++)
- {
- BoardMe[cor1 - i][cor2 - 1] = '#';
- HMSMe[cor1 - i][cor2 - 1] = '#';
- i++;
- }
- i = 1;
- }
- else
- {
- Error = true;
- if (!Computer)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
- cout << "You are not allowed to do that. Try again." << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
- }
- }
- break;
- case 2:
- if (right != "" && AlreadyFilled(BoardMe, ship, cor1, cor2, direction) == true)
- {
- for (int count = 0; count < ship; count++)
- {
- BoardMe[cor1 - 1][cor2 - i] = '#';
- HMSMe[cor1 - 1][cor2 - i] = '#';
- i--;
- }
- i = 1;
- }
- else
- {
- Error = true;
- if (!Computer)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
- cout << "You are not allowed to do that. Try again." << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
- }
- }
- break;
- case 3:
- if (left != "" && AlreadyFilled(BoardMe, ship, cor1, cor2, direction) == true)
- {
- for (int count = 0; count < ship; count++)
- {
- BoardMe[cor1 - 1][cor2 - i] = '#';
- HMSMe[cor1 - 1][cor2 - i] = '#';
- i++;
- }
- i = 1;
- }
- else
- {
- Error = true;
- if (!Computer)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
- cout << "You are not allowed to do that. Try again." << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
- }
- }
- break;
- default:
- cout << "You have entered in a bad number. Try again." << endl;
- //worry about this later
- break;
- }
- OutputTables(BoardMe);
- }
- void BoardCreate(char Initialize[10][10])
- {
- for (int i = 0; i < 10; i++)
- for (int j = 0; j < 10; j++)
- Initialize[i][j] = '~';
- }
- void OutputTables(char Display[10][10])
- {
- cout << " 1 2 3 4 5 6 7 8 9 10" << endl;
- for (int i = 0; i < 10; i++)
- {
- if ((i + 1) < 10)
- cout << i + 1 << " ";
- else
- cout << i + 1 << " ";
- for (int j = 0; j < 10; j++)
- {
- if (Display[i][j] == '~')
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
- else
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 128);
- cout << Display[i][j] << " ";
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
- if (j == 9)
- cout << endl;
- }
- }
- cout << endl << endl;
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
- }
- bool AlreadyFilled(char BoardMe[10][10], int ship, int x, int y, int direction)
- {
- int i = 0;
- if (direction == 0) //down
- {
- while (i<ship)
- {
- if (BoardMe[x - 1 - i][y - 1] == '#')
- return false;
- i++;
- }
- return true;
- }
- if (direction == 1) //up
- {
- while (i<ship)
- {
- if (BoardMe[x - 1 + i][y - 1] == '#')
- return false;
- i++;
- }
- return true;
- }
- if (direction == 2) //right
- {
- while (i<ship)
- {
- if (BoardMe[x - 1][y - 1 + i] == '#')
- return false;
- i++;
- }
- return true;
- }
- if (direction == 3) //left
- {
- while (i<ship)
- {
- if (BoardMe[x - 1][y - 1 - i] == '#')
- return false;
- i++;
- }
- return true;
- }
- }
- int RandomCor()
- {
- return((rand() % 10) + 1);
- }
- int RandomDirection()
- {
- return((rand() % 3));
- }
- int RandomShip()
- {
- return((rand() % 5) + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement