Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <windows.h>
- #include <string>
- #include <cmath>
- #include <cstdlib>
- #include <vector>
- #include <time.h>
- using namespace std;
- void gotoxy(int x, int y)
- {
- COORD p = { static_cast<int>(x), static_cast<int>(y) };
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
- }
- void resetInitText();
- void locationCalc(std::string a, int boats, std::string b, int calc[]);
- void printer(int player, int array[], vector<vector <int>>& board, int& tracker, int diff);
- void checker(int& player, int array[], vector<vector <int>> board, int playerDiff);
- void resetText(int playerDiff);
- void hitregplayer(int i, int j, vector<vector <int>> plocation, int& timeshit, int& pHit);
- void hitregcomputer(int i, int j, vector<vector <int>> clocation, int& timeshitc, int& cHit, int& alreadyHit, int cAlreadyHit[8][8]);
- void computerBrains(int& x, int& y, int playerDiff);
- void hitPrinter(int x, int y, int player, int hit, int diff);
- void gameLoop(vector<vector<int>> playerOne, vector<vector <int>> playerTwo, int playerDiff, int multiPlayer);
- int main()
- {
- resetInitText();
- int i;
- int j;
- int diff; //used for board array size. Easy = 6x6, Medium = 8x8, Hard = 10x10//
- int multiPlayer;
- cout << "Enter in 1-Player or 2-Player. (1-Player = 1, 2-Player = 2)";
- cin >> multiPlayer;
- resetInitText;
- cout << "What difficulty would you like to play on? (Easy = 1, Medium = 2, Hard = 3)" << endl;
- cin >> diff;
- int shipsSizes[6] = { 2,3,3,4,5,4 };
- vector<vector <int>> playerOne;
- vector<vector <int>> playerTwo;
- if (diff == 1) {
- playerOne.resize(6, vector<int>(6, 0));
- playerTwo.resize(6, vector<int>(6, 0));
- }
- else if (diff == 2) {
- playerOne.resize(8, vector<int>(8, 0));
- playerTwo.resize(8, vector<int>(8, 0));
- }
- else if (diff == 3) {
- playerOne.resize(10, vector<int>(10, 0));
- playerTwo.resize(10, vector<int>(10, 0));
- }
- char letters[10] = { 'A','B','C','D','E','F','G','H','I','J' };
- //initializes the sizes of the boats, the second one is the computer's boats//
- string temp[2];
- /*the calc[4] array holds ship placement and is accessed throughout the game
- calc[0] = x placement
- calc[0] = y placement
- calc[0] = direction
- calc[0] = size of ship
- */
- int calc[4];
- int game = 17;
- int loopState = 0;
- int tracker = 0;
- resetInitText();
- //vvv This for loop couts<< the original empty board vvv//
- if (diff == 1) {
- for (i = 0; i < 6; i++) {
- for (j = 0; j < 6; j++) {
- cout << playerOne[i][j] << " "; //<<< couts player's board
- }
- cout << (i + 1) << " | " << (i + 1) << " "; //<<< couts spacer and vertical number coords on board
- for (j = 0; j < 6; j++) {
- cout << playerOne[i][j] << " "; //<<< couts computer's board, but uses same array because its all zeros
- }
- if (i == 5) {
- cout << endl;
- }
- for (j = 0; j < 6; j++) {
- if (i == 5) {
- cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
- }
- if (j == 5 && i == 5) {
- cout << " | "; //spacer
- }
- }
- for (j = 0; j < 6; j++) {
- if (i == 5) {
- cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
- }
- }
- cout << endl;
- }
- }
- else if (diff == 2) {
- for (i = 0; i < 8; i++) {
- for (j = 0; j < 8; j++) {
- cout << playerOne[i][j] << " "; //<<< couts player's board
- }
- cout << (i + 1) << " | " << (i + 1) << " "; //<<< couts spacer and vertical number coords on board
- for (j = 0; j < 8; j++) {
- cout << playerOne[i][j] << " "; //<<< couts computer's board, but uses same array because its all zeros
- }
- if (i == 7) {
- cout << endl;
- }
- for (j = 0; j < 8; j++) {
- if (i == 7) {
- cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
- }
- }
- if (i == 7) {
- cout << " | "; //spacer
- }
- for (j = 0; j < 8; j++) {
- if (i == 7) {
- cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
- }
- }
- cout << endl;
- }
- }
- else if (diff == 3) {
- for (i = 0; i < 10; i++) {
- for (j = 0; j < 10; j++) {
- cout << playerOne[i][j] << " "; //<<< couts player's board
- }
- if (i == 9) {
- cout << (i + 1) << " | " << (i + 1) << " ";
- }
- else {
- cout << (i + 1) << " | " << (i + 1) << " "; //<<< couts spacer and vertical number coords on board
- }
- for (j = 0; j < 10; j++) {
- cout << playerOne[i][j] << " "; //<<< couts computer's board, but uses same array because its all zeros
- }
- if (i == 9) {
- cout << endl;
- }
- for (j = 0; j < 10; j++) {
- if (i == 9) {
- cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
- }
- }
- if (i == 9) {
- cout << " | "; //spacer
- }
- for (j = 0; j < 10; j++) {
- if (i == 9) {
- cout << letters[j] << " "; //<<< couts horizontal letter coords on player board
- }
- }
- cout << endl;
- }
- }
- //^^^ This for loop couts<< the original empty board ^^^//
- tracker = 0;
- //vvv This for loop places down the player's 1 ships vvv//
- for (i = 0; i < (diff + 3); i++) {
- cout << "Player 1. Where would you like to place a size [" << shipsSizes[i] << "] ship? (Ex. B7)" << endl;
- cin >> temp[0]; //inputs coords for boat placement as a string. Example is "B7", or "C5"
- cout << "Player 1. What direction would you like to place that ship? (N,S,E,W)" << endl;
- cin >> temp[1]; //inputs direction for boat placement as a string. Example is "N", or "E"
- locationCalc(temp[0], shipsSizes[i], temp[1], calc); //this function takes the user inputs, and puts them all into the calc[4] array as integer values
- checker(loopState, calc, playerOne, diff); //this 374-checker() function works with the calc[4] array to check if the boat placement is "allowed"
- if (loopState == -1) { //if the placement isn't "allowed", a loopState of -1 is returned and forces the player to choose another placement
- i--;
- cout << "ERROR, Player 1 cannot put a ship there";
- Sleep(3000);
- loopState = 0;
- }
- else { //if loopState doesnt come back as -1, then the printer() function is ran
- printer(loopState, calc, playerOne, tracker, diff); //the 274-printer() function uses the calc[4] array to "print" the boats onto the board
- } //this is only done if the boat placement checked "valid" (see above)
- resetText(diff); //this 355-resetText() function resets the text to it's original position, ready for another loop
- }
- //^^^ This for loop places down the player's 1 ships ^^^//
- loopState = 0;
- if (multiPlayer == 1) {
- for (i = 0; i < (diff + 3); i++) {
- srand(time(0));
- srand(time(0) * (rand() % 1000) * (rand() % 513) + (i * 6584));
- calc[0] = rand() % (4 + (2 * diff)); //randomly picks x-coord to place ship
- srand(time(0) * (rand() % 1000) + (i * 4584)); //randomizes seed again
- calc[1] = rand() % (4 + (2 * diff)); //randomly picks y-coord to place ship
- srand(time(NULL) * rand() % 467 + (i * 5584)); //randomizes seed again a second time
- calc[2] = rand() % 4; //randomly picks direction to place ship
- calc[3] = shipsSizes[2 - i + diff]; //chooses the next size ship to place
- //program places them down in opposite order to increase placement speed, hence the [4-i].
- checker(loopState, calc, playerTwo, diff);//checks that the boat placement
- if (loopState == -1) { //same process as above, if the placement comes back invalid, then the computer goes back through the randomizing loop
- i--; //i-- <<<<this causes the loop to run forever, or until the computer chooses a "valid" placement
- loopState = 0;
- }
- else if (loopState == 0) {
- printer(1, calc, playerTwo, tracker, diff); //"prints" the computer's placement after it has found a "valid" ship placement
- Sleep(1500);
- }
- }
- }
- else if (multiPlayer == 2) {
- //vvv This for loop places down the player's 2 ships vvv//
- for (i = 0; i < (diff + 3); i++) {
- cout << "Player 2. Where would you like to place a size [" << shipsSizes[i] << "] ship? (Ex. B7)" << endl;
- cin >> temp[0]; //inputs coords for boat placement as a string. Example is "B7", or "C5"
- cout << "Player 2. What direction would you like to place that ship? (N,S,E,W)" << endl;
- cin >> temp[1]; //inputs direction for boat placement as a string. Example is "N", or "E"
- locationCalc(temp[0], shipsSizes[i], temp[1], calc); //this function takes the user inputs, and puts them all into the calc[4] array as integer values
- checker(loopState, calc, playerTwo, diff); //this 374-checker() function works with the calc[4] array to check if the boat placement is "allowed"
- if (loopState == -1) { //if the placement isn't "allowed", a loopState of -1 is returned and forces the player to choose another placement
- i--;
- cout << "ERROR, Player 2 cannot put a ship there";
- Sleep(3000);
- loopState = 0;
- }
- else { //if loopState doesnt come back as -1, then the printer() function is ran
- printer(1, calc, playerTwo, tracker, diff); //the 274-printer() function uses the calc[4] array to "print" the boats onto the board
- } //this is only done if the boat placement checked "valid" (see above)
- resetText(diff); //this 355-resetText() function resets the text to it's original position, ready for another loop
- }
- //^^^ This for loop places down the player's 2 ships ^^^//
- }
- resetText(diff);
- gameLoop(playerOne, playerTwo, diff, multiPlayer); //this enter's the function "gameLoop()". see line 413
- return 0;
- }
- void locationCalc(std::string a, int boats, std::string b, int calc[]) {
- int x = 0;
- int y = 0;
- int size = boats;
- int direction;
- direction = 5;
- if (b == "N" || b == "n") {
- direction = 0;
- }
- else if (b == "E" || b == "e") {
- direction = 1;
- }
- else if (b == "S" || b == "s") {
- direction = 2;
- }
- else if (b == "W" || b == "w") {
- direction = 3;
- }
- if (a[0] == 'A' || a[0] == 'a') {
- x = 0;
- }
- else if (a[0] == 'B' || a[0] == 'b') {
- x = 1;
- }
- else if (a[0] == 'C' || a[0] == 'c') {
- x = 2;
- }
- else if (a[0] == 'D' || a[0] == 'd') {
- x = 3;
- }
- else if (a[0] == 'E' || a[0] == 'e') {
- x = 4;
- }
- else if (a[0] == 'F' || a[0] == 'f') {
- x = 5;
- }
- else if (a[0] == 'G' || a[0] == 'g') {
- x = 6;
- }
- else if (a[0] == 'H' || a[0] == 'h') {
- x = 7;
- }
- else if (a[0] == 'I' || a[0] == 'i') {
- x = 8;
- }
- else if (a[0] == 'J' || a[0] == 'j') {
- x = 9;
- }
- else if (a[0] == 'K' || a[0] == 'k') {
- x = 10;
- }
- else if (a[0] == 'L' || a[0] == 'l') {
- x = 11;
- }
- if (a[1] == '2') {
- y = 1;
- }
- else if (a[1] == '3') {
- y = 2;
- }
- else if (a[1] == '4') {
- y = 3;
- }
- else if (a[1] == '5') {
- y = 4;
- }
- else if (a[1] == '6') {
- y = 5;
- }
- else if (a[1] == '7') {
- y = 6;
- }
- else if (a[1] == '8') {
- y = 7;
- }
- else if (a[1] == '9') {
- y = 8;
- }
- else if (a[1] == '1' && a[2] == '0') {
- y = 9;
- }
- else if (a[1] == '1' && a[2] == '1') {
- y = 10;
- }
- else if (a[1] == '1' && a[2] == '2') {
- y = 11;
- }
- else if (a[1] == '1') {
- y = 0;
- }
- calc[0] = x;
- calc[1] = y;
- calc[2] = direction;
- calc[3] = size;
- }
- void printer(int player, int array[], vector<vector <int>>& board, int& tracker, int diff) {
- int x;
- int y;
- int i;
- char box = (char)219;
- char c;
- c = box;
- std::string ship(2, c);
- ship.push_back(c);
- if (player == 0 || player == 1) {
- if (array[2] == 0) {//this places ships if they chose the north direction
- for (i = 0; i < array[3]; i++) {//runs a loop that places down all parts of the ship
- x = (array[0] * 2) + (18 * player) + player * (diff * 4);
- y = (array[1]) - (i); //shifts the placement upwards in the y-direction
- gotoxy(x, y);
- cout << 1;
- board[array[1] - i][array[0]] = 1;
- }
- }
- else if (array[2] == 1) {//this places ships if they chose the east direction
- for (i = 0; i < array[3]; i++) {
- x = (array[0] * 2) + (i * 2) + (18 * player) + player * (diff * 4); //shifts the placement to the right in the x-direction
- y = (array[1]);
- gotoxy(x, y);
- cout << 1;
- board[array[1]][array[0] + i] = 1;
- }
- }
- else if (array[2] == 2) {//this places ships if they chose the south direction
- for (i = 0; i < array[3]; i++) {
- x = (array[0] * 2) + (18 * player) + player * (diff * 4);
- y = (array[1]) + (i); //shifts the placement to the down in the y-direction
- gotoxy(x, y);
- cout << 1;
- board[array[1] + i][array[0]] = 1;
- }
- }
- else if (array[2] == 3) {//this places ships if they chose the west direction
- for (i = 0; i < array[3]; i++) {
- x = (array[0] * 2) - (i * 2) + (18 * player) + player * (diff * 4); //shifts the placement to the left in the x-direction
- y = (array[1]);
- gotoxy(x, y);
- cout << 1;
- board[array[1]][array[0] - i] = 1;
- }
- }
- }
- }
- void resetText(int playerDiff) {
- int i;
- if (playerDiff == 1) {
- for (i = 7; i < 15; i++) {
- gotoxy(0, i);
- cout << " ";
- }
- gotoxy(0, 7);
- }
- else if (playerDiff == 2) {
- for (i = 9; i < 15; i++) {
- gotoxy(0, i);
- cout << " ";
- }
- gotoxy(0, 9);
- }
- else if (playerDiff == 3) {
- for (i = 11; i < 15; i++) {
- gotoxy(0, i);
- cout << " ";
- }
- gotoxy(0, 11);
- }
- }
- void resetInitText() {
- int i;
- gotoxy(0, 0);
- for (i = 0; i < 20; i++) {
- gotoxy(0, i);
- cout << " ";
- }
- gotoxy(0, 0);
- }
- void checker(int& player, int array[], vector<vector <int>> board, int playerDiff) {
- int i;
- if (array[2] == 0) {
- for (i = 0; i < array[3]; i++) {
- if (array[1] - i < 0) {
- player = -1;
- }
- else if (board[array[1] - i][array[0]] != 0) {
- player = -1;
- }
- }
- }
- else if (array[2] == 1) {
- for (i = 0; i < array[3]; i++) {
- if (array[0] + i > 3 + (playerDiff * 2)) {
- player = -1;
- }
- else if (board[array[1]][array[0] + i] != 0) {
- player = -1;
- }
- }
- }
- else if (array[2] == 2) {
- for (i = 0; i < array[3]; i++) {
- if (array[1] + i > 3 + (playerDiff *2)) {
- player = -1;
- }
- else if (board[array[1] + i][array[0]] != 0) {
- player = -1;
- }
- }
- }
- else if (array[2] == 3) {
- for (i = 0; i < array[3]; i++) {
- if (array[0] - i < 0) {
- player = -1;
- }
- else if (board[array[1]][array[0] - i] != 0) {
- player = -1;
- }
- }
- }
- }
- void gameLoop(vector<vector<int>> playerOne, vector<vector <int>> playerTwo, int playerDiff, int multiPlayer) {
- string attackCoords;
- int coords[4];
- int pHitTimes = 0;
- int pHit;
- int cHitTimes = 0;
- int cHit;
- int x, y;
- bool win = false;
- int alreadyHit;
- int cAlreadyHit[8][8];
- //the code below runs the loop which is the "game". where both players take turns inputting coords.
- //the program inputs the user coords, but the computer randomly chooses coords using the 452-computerBrains() function
- //the program uses the 460-hitregplayer() and the 483-hitregcomputer() functions to see if it was a hit/miss
- //then it uses the 503-hitPrinter() function to display to the board
- int goal = 0;
- if (playerDiff == 1) {
- goal = 12;
- }
- else if (playerDiff == 2) {
- goal = 17;
- }
- else if (playerDiff == 3) {
- goal = 21;
- }
- do {
- cout << "Player 1. Enter in the coords you wish to attack. (Ex. B7): ";
- cin >> attackCoords;
- locationCalc(attackCoords, 0, "W", coords);
- hitregplayer(coords[1], coords[0], playerTwo, pHitTimes, pHit);
- hitPrinter(coords[0], coords[1], 1, pHit, playerDiff);
- Sleep(200);
- resetText(playerDiff);
- alreadyHit = 1;
- if (multiPlayer == 1) {
- do {
- computerBrains(x, y, playerDiff);
- hitregcomputer(y, x, playerOne, cHitTimes, cHit, alreadyHit, cAlreadyHit);
- } while (alreadyHit == 1);
- hitPrinter(x, y, 0, cHit, playerDiff);
- }
- else if (multiPlayer == 2) {
- cout << "Player 2. Enter in the coords you wish to attack. (Ex. B7): ";
- cin >> attackCoords;
- locationCalc(attackCoords, 0, "W", coords);
- hitregcomputer(coords[1], coords[0], playerOne, cHitTimes, cHit, alreadyHit, cAlreadyHit);
- hitPrinter(coords[0], coords[1], 0, cHit, playerDiff);
- }
- Sleep(200);
- if (cHitTimes == goal || pHitTimes == goal) {
- win = true;
- }
- resetText(playerDiff);
- } while (win == false);
- if (pHitTimes == goal) {
- cout << "Player 1 won!";
- }
- else {
- cout << "Player 2 won!";
- }
- Sleep(30000000);
- }
- void computerBrains(int& x, int& y, int playerDiff) {
- if (playerDiff == 1) {
- srand(time(0));
- x = rand() % 6;
- srand(time(0) * rand() % 1000);
- y = rand() % 6;
- }
- else if (playerDiff == 2) {
- srand(time(0));
- x = rand() % 8;
- srand(time(0) * rand() % 1000);
- y = rand() % 8;
- }
- else if (playerDiff == 3) {
- srand(time(0));
- x = rand() % 10;
- srand(time(0) * rand() % 1000);
- y = rand() % 10;
- }
- }
- void hitregplayer(int i, int j, vector<vector <int>> plocation, int& timeshit, int& pHit) { /* where location refers to where the player aims, and computer location*/
- int u;
- int v;
- if (plocation[i][j] == 1) {//clocation refers == true where ship coordinate is
- cout << "Hit" << endl;
- //alreadyHit[i][j] = 1; //sets alreadyHit as true
- //location[i][j] = 0; //resets player target
- timeshit++;
- pHit = 1;
- cout << endl;
- cout << endl;
- }
- else {
- cout << "Miss" << endl;
- pHit = 0;
- }
- }
- void hitregcomputer(int i, int j, vector<vector <int>> clocation, int& timeshitc, int& cHit, int& alreadyHit, int cAlreadyHit[8][8]) { /* where colocation refers to where the computer aims, and player location*/
- if (clocation[i][j] == 1 && cAlreadyHit[i][j] != 1) {//plocation refers == true where ship coordinate is
- cout << "Computer attacked at (" << j << ", " << i << ") and hit!." << endl;
- //calreadyHit[i][j] = 1; //sets alreadyHit as true
- //colocation[i][j] = 0; //resets player target
- timeshitc++;
- cHit = 1;
- alreadyHit = 0;
- cAlreadyHit[i][j] = 1;
- }
- else if (cAlreadyHit[i][j] != 1) {
- cout << "Computer attacked at (" << j + 1 << ", " << i + 1 << ") and missed!." << endl;
- cHit = 0;
- alreadyHit = 0;
- cAlreadyHit[i][j] = 1;
- }
- }
- void hitPrinter(int x, int y, int player, int hit, int diff) {
- gotoxy(x * 2 + (player * 18) + player * (diff * 4), y);
- if (hit == 1) {
- cout << "X";
- }
- else {
- cout << "~";
- }
- gotoxy(0, 9);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement