Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* CS 125 - Intro to Computer Science II
- * File Name: CS125_Project6.cpp
- * Project 6 - Due 4/15/2019
- * Instructor: Dr. Dan Grissom
- *
- * Name 1: Mason Welge
- * Name 2: FirstName LastName
- * Description: Insert your meaningful description for Project 6.
- */
- using namespace std;
- #include <iostream>
- #include <vector>
- #include <map>
- #include <string>
- //Prefixes: 'H' is hidden, 'R' is revealed
- //Suffixes: 'E' is empty, 'S' is a part of a ship
- enum cellType { HE, RE, HS, RS };
- //Create a 2D array containing a grid of cells, using cellType
- //Since we'll only have 7 rows, set the initial capacity to 7.
- cellType grid[7][7];
- //Create more readable references to the enum
- cellType hiddenEmpty = HE;
- cellType hiddenShip = HS;
- cellType revealedEmpty = RE;
- cellType revealedShip = RS;
- int gameBoard[7][7];
- string size [7][7];
- string guessPlace[7][7];
- map<string, int> guess;
- int co1;
- int co2;
- string coordinates;
- int ship;
- int hp;
- // Method headers
- void printGrid();
- void pChart();
- void guessPlace();
- void guessPlaceHint();
- int main() {
- // Your program should always output your name and the project number.
- // DO NOT DELETE OR COMMENT OUT. Replace with relevant info.
- cout << "Mason Welge" << endl;
- cout << "Project 6" << endl << endl;
- // Your code should go below this line
- do {
- cout << "Please enter coordinates of ship length 2" << endl;
- cout << "Starting Row (0-6): " << endl;
- cin >> co1;
- cout << "Starting Column (0-6): " << endl;
- cin >> co2;
- cout << "From the starting point, would you like to move right or down? (d/r)" << endl;
- //coordinates = scan.next().toLowerCase();
- cin >> coordinates;
- // controls where it can go
- if ((co2 < 6) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-") && (coordinates == ("r") || coordinates == ("R"))){
- size[co1][co2] = "S";
- size[co1][co2+1] = "S";
- ship = ship +1;
- } else if ((co1 < 6) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-") && (coordinates == ("d") || coordinates == ("D"))){
- size[co1][co2] = "S";
- size[co1+1][co2] = "S";
- ship = ship +1;
- } else {
- cout << "There has already been a ship placed here, try again!" << endl;
- }
- } while(ship < 1);
- pChart();
- // placing second ship
- do {
- cout << "Please enter coordinates of ship length 2" << endl;
- cout << "Starting Row (0-6): " << endl;
- cin >> co1;
- cout << "Starting Column (0-6): " << endl;
- cin >> co2;
- cout << "From the starting point, would you like to move right or down? (d/r)" << endl;
- cin >> coordinates;
- // controls where it can go
- if ((co2 < 5) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-" && size[co1][co2+1] == "-" && size[co1][co2 +2] == "-") && (coordinates == ("r") || coordinates == ("R"))){
- size[co1][co2] = "S";
- size[co1][co2+1] = "S";
- size[co1][co2+2] = "S";
- ship = ship +1;
- } else if ((co1 < 5) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-" && size[co1+1][co2]=="-" && size[co1+2][co2]=="-") && (coordinates == ("d") || coordinates == ("D"))){
- size[co1][co2] = "S";
- size[co1+1][co2] = "S";
- size[co1+2][co2] = "S";
- ship = ship +1;
- } else {
- cout << "There has already been a ship placed here, try again!" << endl;
- }
- } while(ship < 2);
- pChart();
- // placing third ship
- do {
- cout << "Please enter coordinates of ship length 2" << endl;
- cout << "Starting Row (0-6): " << endl;
- cin >> co1;
- cout << "Starting Column (0-6): " << endl;
- cin >> co2;
- cout << "From the starting point, would you like to move right or down? (d/r)" << endl;
- cin >> coordinates;
- // controls where it can go
- if ((co2 < 4) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-" && size[co1][co2+1] == "-" && size[co1][co2 +2] == "-" && size[co1][co2 +3] == "-") && (coordinates == ("r") || coordinates == ("R"))){
- size[co1][co2] = "S";
- size[co1][co2+1] = "S";
- size[co1][co2+2] = "S";
- size[co1][co2+3] = "S";
- ship = ship +1;
- } else if ((co1 < 4) && (co1 >= 0 && co1 < 6) && (co2 >= 0 && co2 < 6) && (size[co1][co2]=="-" && size[co1+1][co2]=="-" && size[co1+2][co2]=="-" && size[co1+3][co2]=="-") && (coordinates == ("d") || coordinates == ("D"))){
- size[co1][co2] = "S";
- size[co1+1][co2] = "S";
- size[co1+2][co2] = "S";
- size[co1+3][co2] = "S";
- ship = ship +1;
- } else {
- cout << "There has already been a ship placed here, try again!" << endl;
- }
- } while(ship < 3);
- pChart();
- // ship placement over, guessing begins
- cout << "Here's the final grid... press ENTER to begin playing!" << endl;
- for(int i=0; i<20;i++) {
- cout << "" << endl;
- }
- //void guessPlace();
- int numTurns = 0;
- string key;
- vector<int> nameRow;
- vector<int> nameColumn;
- // counts amount of guesses
- do {
- cout << "Guess #" << numTurns << "!" << endl;
- cout << "Guess a Row (0-6): " << endl;
- cin >> co1;
- cout << "Guess a Column (0-6): " << endl;
- cin >> co2;
- key = co1 + "" + co2;
- if (!guess.find(key)->first) {
- // replaces - with m or h depending on guess
- if (size[co1][co2]=="-") {
- guessPlace[co1][co2]="m";
- cout << "Miss!" << endl;
- } else {
- guess.insert({key, 1});
- guessPlace[co1][co2]="H";
- cout << "Hit!" << endl;
- hp++;
- }
- // counts # of turns taken
- numTurns++;
- guess.insert({key, 1});
- } else {
- cout << "r\\c = " << co1 + "\\" << co2 << "has already been guessed " << guess.find(key)-> second << " times" << endl;
- guess.insert({key, guess.find(key)-> second + 1});
- }
- nameRow.push_back(co1);
- nameColumn.push_back(co2);
- guessPlaceHint();
- } while (hp < 9);
- cout << "Guesses\t |\t rows \t columns" << endl;
- cout << "---------------------------------" << endl;
- for (int i = 0; i < nameRow.size(); i++) {
- cout << (i+1) << "\t|" + nameRow.at(i) << "\t" << nameColumn.at(i) << endl;
- }
- }
- ///////////////////////////////////////////////////////////////////////
- // Prints the game grid
- // NOTE: You can change the parameters and return type of this method
- // as you see fit.
- void printGrid() {
- cout << "--------------- Welcome to Battleship! ---------------"<< endl;
- cout << "r\\c" << endl;
- //
- for(int m =0; m<7;m++) {
- cout <<"\t"+m << endl;
- }
- cout <<""<< endl;
- for(int i=0;i<7;i++) {
- cout << i << endl;
- cout <<"\t"<< endl;
- for(int j=0;j<7;j++) {
- // making every value of size a -
- size[i][j]="-";
- cout << size[i][j]+"\t"<< endl;
- }
- cout << "" << endl;
- }
- }
- void pChart () {
- cout << "r\\c" << endl;
- for(int m =0; m<7;m++) {
- cout << "\t"+m << endl;
- }
- cout << "" << endl;
- for(int i=0;i<7;i++) {
- cout << i << endl;
- cout << "\t" << endl;
- for(int j=0;j<7;j++) {
- cout << size[i][j]+"\t" << endl;
- }
- cout << "" << endl;
- }
- }
- void guessPlace() {
- cout << "--------------- Welcome to Battleship! ---------------" << endl;
- cout << "r\\c" << endl;
- for(int m =0; m<7;m++) {
- cout << "\t"+m << endl;
- }
- cout << "" << endl;
- for(int i=0;i<7;i++) {
- cout << i << endl;
- cout << "\t" << endl;
- for(int j=0;j<7;j++) {
- size[i][j]="-";
- cout << size[i][j]+"\t" << endl;
- }
- cout << "" << endl;
- }
- }
- void guessPlaceHint () {
- cout << "r\\c" << endl;
- //
- for(int m =0; m<7;m++) {
- cout << "\t"+m << endl;
- }
- cout << "" << endl;
- for(int i=0;i<7;i++) {
- cout << i << endl;
- cout << "\t" << endl;
- for(int j=0;j<7;j++) {
- cout << size[i][j]+"\t" << endl;
- }
- cout << "" << endl;
- }
- }
- /******************************************************************************
- Insert 2 test cases, which represent program input/output for two different
- combinations of inputs. You may literally copy and paste your console contents,
- but your two test cases should be DIFFERENT from any test cases given in the
- Project description itself.
- ------------
- Test Case 1:
- ------------
- (Your test case I/O here.)
- ------------
- Test Case 2:
- ------------
- (Your test case I/O here.)
- ******************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement