Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Structured Programming
- // Quinn H.
- // BATTLESHIP!
- // Started May 13, 2019
- #include <iostream>
- #include <cstdlib>
- #include <thread>
- #include <chrono>
- using namespace std;
- void printBoard( char grid[6][6] );
- int main()
- {
- // intro
- cout << "Welcome to BATTLESHIP!\n\n";
- cout << "The coordinate grid is 6 by 6, and your boat is 3 spaces long,\n";
- cout << "so please keep that in mind when entering the coordinates\n\n";
- // initialize variables
- int xBow;
- int yBow;
- int xStern;
- int yStern;
- // create the boards
- char gridOne[6][6] =
- {
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' }
- };
- char gridTwo[6][6] =
- {
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' },
- { '/', '/', '/', '/', '/', '/' }
- };
- char gridOneRadar[6][6] =
- {
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' }
- };
- char gridTwoRadar[6][6] =
- {
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' },
- { '-', '-', '-', '-', '-', '-' }
- };
- // prompt player one for their ship coords
- cout << " - PLAYER 1 -\nPlease enter the coordinates of your bow (front).\n";
- cout << "x > ";
- cin >> xBow;
- cout << "y > ";
- cin >> yBow;
- cout << endl;
- cout << "Now the stern (back) of your boat.\n";
- cout << "x > ";
- cin >> xStern;
- cout << "y > ";
- cin >> yStern;
- // "place" player one's ship on the grid
- gridOne[yBow-1][xBow-1] = 'O';
- gridOne[yStern-1][xStern-1] = 'O';
- // experimental tests to try to fill in 3rd space
- // talk to Quinn before making changes to this
- if( xBow == xStern )
- {
- // ensure that the coords are 2 spaces apart
- if( abs(yBow-yStern) != 2 )
- {
- cout << "\n!! Something has gone wrong with your coordinates.\n\n";
- cout << "Please ensure you bow and stern are 2 spaces apart.\n\n";
- cout << "The program will now terminate.\n";
- exit(2);
- }
- if( yBow > yStern )
- {
- gridOne[yStern][xStern-1] = 'O';
- }
- else // yStern > yBow
- {
- gridOne[yBow][xBow-1] = 'O';
- }
- }
- else if( yBow == yStern )
- {
- // ensure that the coords are 2 spaces apart
- if( abs(xBow-xStern) != 2 )
- {
- cout << "\n!! Something has gone wrong with your coordinates.\n\n";
- cout << "Please ensure you bow and stern are 2 spaces apart.\n\n";
- cout << "The program will now terminate.\n";
- exit(2);
- }
- if( xBow > xStern )
- {
- gridOne[yStern-1][xStern] = 'O';
- }
- else // xStern > xBow
- {
- gridOne[yBow-1][xBow] = 'O';
- }
- }
- else
- {
- cout << "\n!! Something has gone wrong with your coordinates.\n\n";
- cout << "Please ensure that the bow and the stern are\non the same horizontal or vertical axis.\n\n";
- cout << "The program will now terminate.\n";
- exit(1);
- }
- // TEST print w/formatting
- cout << endl;
- printBoard(gridOne);
- cout << endl;
- cout << "Does this look right to you? (y/n) > ";
- char userChoice = 'n';
- cin >> userChoice;
- switch(userChoice)
- {
- case 'y':
- cout << "Got it!\n\n";
- break;
- case 'Y':
- cout << "Got it!\n\n";
- break;
- default:
- cout << "Understood. Please rerun the program to reenter coordinates.\n";
- exit(0);
- }
- cout << "\n\n\n\n\n\n\n\n"; // just to clean up the screen
- // prompt player 2 for their stuff
- cout << " - PLAYER 2 -\nPlease enter the coordinates of your bow (front).\n";
- cout << "x > ";
- cin >> xBow;
- cout << "y > ";
- cin >> yBow;
- cout << endl;
- cout << "Now the stern (back) of your boat.\n";
- cout << "x > ";
- cin >> xStern;
- cout << "y > ";
- cin >> yStern;
- // "place" player 2's ship on the grid
- gridTwo[yBow-1][xBow-1] = 'O';
- gridTwo[yStern-1][xStern-1] = 'O';
- // experimental tests to try to fill in 3rd space
- // talk to Quinn before making changes to this
- if( xBow == xStern )
- {
- // ensure that the coords are 2 spaces apart
- if( abs(yBow-yStern) != 2 )
- {
- cout << "\n!! Something has gone wrong with your coordinates.\n\n";
- cout << "Please ensure you bow and stern are 2 spaces apart.\n\n";
- cout << "The program will now terminate.\n";
- exit(2);
- }
- if( yBow > yStern )
- {
- gridTwo[yStern][xStern-1] = 'O';
- }
- else // yStern > yBow
- {
- gridTwo[yBow][xBow-1] = 'O';
- }
- }
- else if( yBow == yStern )
- {
- // ensure that the coords are 2 spaces apart
- if( abs(xBow-xStern) != 2 )
- {
- cout << "\n!! Something has gone wrong with your coordinates.\n\n";
- cout << "Please ensure you bow and stern are 2 spaces apart.\n\n";
- cout << "The program will now terminate.\n";
- exit(2);
- }
- if( xBow > xStern )
- {
- gridTwo[yStern-1][xStern] = 'O';
- }
- else // xStern > xBow
- {
- gridTwo[yBow-1][xBow] = 'O';
- }
- }
- else
- {
- cout << "\n!! Something has gone wrong with your coordinates.\n\n";
- cout << "Please ensure that the bow and the stern are\non the same horizontal or vertical axis.\n\n";
- cout << "The program will now terminate.\n";
- exit(1);
- }
- // TEST print w/formatting
- cout << endl;
- printBoard(gridTwo);
- cout << endl;
- cout << "Does this look right to you? (y/n) > ";
- cin >> userChoice;
- switch(userChoice)
- {
- case 'y':
- cout << "Got it!\n\n";
- break;
- case 'Y':
- cout << "Got it!\n\n";
- break;
- default:
- cout << "Understood. Please rerun the program to reenter coordinates.\n";
- exit(0);
- }
- // BEGINNING OF PLAY LOOP //////////////////////////////////////////////////
- // counter that will be used for the win condition
- // if either of these hits 3, the loop should stop and a win screen should appear
- int p1Score = 0;
- int p2Score = 0;
- // This counter will be used to determine who's turn it is.
- // ODD = P1, EVEN = P2
- int turn = 0;
- // reusable target coordinates
- int xTarget;
- int yTarget;
- cout << "\n\n\n\n\n\n\n\n"; // just to clean up the screen
- while( p1Score != 3 && p2Score != 3 )
- {
- turn++;
- cout << " TURN " << turn << endl;
- if( turn % 2 != 0 ) // P1's turn
- {
- cout << " PLAYER 1\n";
- }
- else
- {
- cout << " PLAYER 2\n";
- }
- cout << "ENEMY RADAR:\n";
- // if..else function based on turn that tests for whose turn it is
- if( turn % 2 != 0 ) // P1's turn
- {
- // display opponent's ghost board
- printBoard(gridTwoRadar);
- cout << "\nYOUR BOARD:\n";
- printBoard(gridOne);
- // prompt current player for their desired target
- // make it so that they can't attack the same place twice
- cout << "Where would you like to attack?\n";
- cout << "x > ";
- cin >> xTarget;
- cout << "y > ";
- cin >> yTarget;
- // check for if they hit an enemy boat
- if( gridTwo[yTarget-1][xTarget-1 ] == 'O' )
- {
- cout << "\n Nice shot!\nYou hit the enemy's ship at (" << xTarget << "," << yTarget << ").\n";
- this_thread::sleep_for (chrono::seconds(3));
- gridTwo[yTarget-1][xTarget-1] = 'X';
- gridTwoRadar[yTarget-1][xTarget-1] = 'X';
- p1Score++;
- }
- else if( gridTwoRadar[yTarget-1][xTarget-1 ] == 'X' || gridTwoRadar[yTarget-1][xTarget-1 ] == '#' )
- {
- cout << "\n Sorry, you've already attacked that space.\n";
- this_thread::sleep_for (chrono::seconds(3));
- }
- else
- {
- cout << "\nYou missed... better luck next turn.\n";
- gridTwoRadar[yTarget-1][xTarget-1] = '#';
- gridTwo[yTarget-1][xTarget-1] = '#';
- this_thread::sleep_for (chrono::seconds(3));
- }
- }
- else // P2's turn
- {
- // display opponent's ghost board
- printBoard(gridOneRadar);
- cout << "\nYOUR BOARD:\n";
- printBoard(gridTwo);
- // prompt current player for their desired target
- // make it so that they can't attack the same place twice
- cout << "Where would you like to attack?\n";
- cout << "x > ";
- cin >> xTarget;
- cout << "y > ";
- cin >> yTarget;
- // check for if they hit an enemy boat
- if( gridOne[yTarget-1][xTarget-1 ] == 'O' )
- {
- cout << "\n Nice shot!\nYou hit the enemy's ship at (" << xTarget << "," << yTarget << ").\n";
- this_thread::sleep_for (chrono::seconds(3));
- gridOne[yTarget-1][xTarget-1] = 'X';
- gridOneRadar[yTarget-1][xTarget-1] = 'X';
- p2Score++;
- }
- else if( gridOneRadar[yTarget-1][xTarget-1 ] == 'X' || gridOneRadar[yTarget-1][xTarget-1 ] == '#' )
- {
- cout << "\n Sorry, you've already attacked that space.\n";
- this_thread::sleep_for (chrono::seconds(3));
- }
- else
- {
- cout << "\nYou missed... better luck next turn.\n";
- gridOneRadar[yTarget-1][xTarget-1] = '#';
- gridOne[yTarget-1][xTarget-1] = '#';
- this_thread::sleep_for (chrono::seconds(3));
- }
- }
- cout << "\n\n\n\n\n\n\n\n"; // just to clean up the screen
- }
- // display win message
- cout << "Congratulations to Player ";
- if( turn % 2 != 0 )
- {
- cout << 1;
- }
- else
- {
- cout << 2;
- }
- cout << " for winning the game!\n\n";
- // end of program
- return 0;
- }
- void printBoard( char grid[6][6] )
- {
- cout << " ";
- for( int i = 0; i < 6; i++ )
- {
- cout << i+1 << " ";
- }
- cout << endl;
- for( int i = 0; i < 6; i++ )
- {
- cout << i+1 << " ";
- for( int j = 0; j < 6; j++ )
- {
- cout << grid[i][j] << " ";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement