Advertisement
Guest User

Untitled

a guest
Mar 1st, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. #include <string>
  4. #include <sstream>
  5. #include <cstdlib>
  6. #include "BattleshipControl.h"
  7. #include "BattleshipSetup.h"
  8. #include "BattleshipAI.h"
  9.  
  10. //Used to tell if ships have been sunk
  11. struct shipSunk {
  12.   bool isAircraftSunk;
  13.   bool isBattleshipSunk;
  14.   bool isSubmarineSunk;
  15.   bool isDestroyerSunk;
  16.   bool isPatrolSunk;
  17. } sunkShips;
  18.  
  19. struct shipSunk *isSunk = &sunkShips;
  20.  
  21. // Main function, ask user to start the game, exit, or ask for help
  22. int main (){
  23.   BattleshipControl control;
  24.   string line = "";
  25.  
  26.   while (true) {
  27.     std::cout << control.getInstructions () << std::endl;
  28.     getline(std::cin,line);
  29.     // If user decides to start game, go to function basic_setup
  30.     if (line == "start") {
  31.       control.basicSetup ();
  32.     }    
  33.     // If user decides to exit, exit the program
  34.     else if (line == "exit") {
  35.       exit(0);
  36.     }
  37.     // If user asks for help, redisplay prompt message
  38.     else if (line == "help") {
  39.       std::cout << control.getInstructions () << std::endl;
  40.     }
  41.     // Otherwise its an incorrect command
  42.     else {
  43.       std::cout << std::endl << "Incorrect command" << std::endl << std::endl;
  44.     }
  45.   }
  46.   return 0;
  47. }
  48.  
  49. std::string BattleshipControl::getInstructions () {
  50.   std::string instruction_string =  "Battleship" ;
  51.   instruction_string += "Type \"start\" to begin the game \n";
  52.   instruction_string += "Type \"exit\" to exit the game \n";
  53.   instruction_string +=  "Type \"help\" to redisplay this message \n";
  54.   return instruction_string;
  55. }
  56. // Start the game
  57. void BattleshipControl::basicSetup () {
  58.   BattleshipSetup setup;
  59.  
  60.   // Setup a board consisting of 200 spots to serve as the game board
  61.   gameGrid.resize (20);
  62.   for (int i = 0; i < 20; i++)
  63.     gameGrid [i].resize (10);
  64.  
  65.   // Place on the board user and computer ships
  66.   gameGrid = setup.setupGame ();
  67.  
  68.   // Begin the game
  69.   beginGame ();
  70. }
  71.  
  72. // Begin the game
  73. void BattleshipControl::beginGame () {
  74.   BattleshipAI ai;
  75.   std:: string line = "";
  76.   int coin_flip;
  77.   const int HEADS = 1;
  78.   const int TAILS = 2;
  79.  
  80.   // Send game board to computer ai
  81.   ai.initiateAI (gameGrid);
  82.  
  83.   std::cout << std::endl << std::endl << "Flip to see who goes first" << std::endl;
  84.   std::cout << "1 for HEADS, 2 for tails." << std::endl;
  85.   while(true) {
  86.     std::cout << std::endl << "Enter: ";
  87.     getline(std::cin,line);
  88.     // Make sure user has selected HEADS or tails
  89.     if (line == "exit") {
  90.       exit (0);
  91.     }
  92.     else if (line == "1" || line == "2") {
  93.       srand (time(NULL));
  94.       coin_flip = rand ()% 2+1;
  95.       // Coin flip HEADS  
  96.       if(coin_flip == HEADS) {
  97.     std::cout << std::endl << "HEADS!" << std::endl;
  98.       }
  99.       // Coin flip tails
  100.       else if(coin_flip == TAILS) {
  101.     std::cout << std::endl << "TAILS!" << std::endl;
  102.       }
  103.       break;
  104.     }
  105.     else {
  106.       std::cout << std::endl << "Incorrect selection" << std::endl;
  107.     }
  108.   }
  109.   if ((line == "1" && coin_flip == HEADS) || (line == "2" && coin_flip == TAILS)) {
  110.     playerTurn();
  111.   }
  112.   else if ((line == "1" && coin_flip == TAILS) || (line == "2" && coin_flip == HEADS)) {
  113.    gameGrid = ai.computerTurn(gameGrid);
  114.   }
  115.   playerTurn ();
  116. }  
  117.  
  118. void BattleshipControl::playerTurn () {
  119.   BattleshipSetup setup;
  120.   BattleshipAI ai;
  121.   string line = "";
  122.   int column;
  123.   int row;
  124.   char *row_char;
  125.   bool hasPlayerWon = false;
  126.   bool firePositionGood = false;
  127.   // While there has been no victory keep going
  128.   while (!hasPlayerWon) {
  129.     firePositionGood = false;
  130.     // Have the user select the column they want to fire at
  131.     std::cout << std::endl << "Fire at which column? (A-J): ";
  132.     // While the position is no good for an attack
  133.     while (!firePositionGood) {
  134.       getline (std::cin, line);
  135.       // Allow user to exit out
  136.       if (line == "exit") {
  137.     exit(0);
  138.       }
  139.       // If the columnConverter doesn't return a 10 which constitutes an error
  140.       // then the position is good, and the location is noted, and loop ends.
  141.       else if (setup.columnConverter (line) != 10) {
  142.     column = setup.columnConverter (line);
  143.     firePositionGood = true;
  144.     // Else it was an invalid selection and it asks the user again for a column
  145.       } else {
  146.     std::cout << "Invalid selection" << std::endl;
  147.       }
  148.     }
  149.     firePositionGood = false;
  150.     // While the position is no good for an attack
  151.     while (!firePositionGood) {
  152.       // Have the user select the row they want to fire at
  153.       std::cout << std::endl << "Fire at which row? (1-10): ";
  154.       getline (std::cin, line);
  155.       // Allow user to exit out
  156.       if (line == "exit") {
  157.     exit(0);
  158.       }
  159.       else {
  160.     // Turn the string in to a useable integer
  161.     std::stringstream buffer(line);
  162.     buffer >> row;
  163.     row_char = &line [0];
  164.     // If what is entered is a digit between 1 and 10 the position is good and the loop ends
  165.     if (row == 10 || (isdigit(*row_char) && line.length () == 1 && row >= 1 && row <= 9)) {
  166.       firePositionGood = true;
  167.     }
  168.     // Else what was typed was an invalid selection and it asks the user again for a row
  169.     else {
  170.       std::cout << "Invalid selection" << std::endl;
  171.     }
  172.       }
  173.     }
  174.     // Use the column and row to attack a spot on the board with the playerFire function
  175.     playerFire (column, row);
  176.     // Assume player has won
  177.     hasPlayerWon = true;
  178.     // See if this is true
  179.     for (int i = 10; i < 20; i++) {
  180.       for (int j = 0; j < 10; j++) {
  181.     if (gameGrid [i][j] != 0) {
  182.       hasPlayerWon = false;
  183.       break;
  184.     }
  185.       }
  186.     }
  187.    
  188.     //Redisplay user's battleground every turn
  189.     for (int i = 0; i < 20; i++) {
  190.     std::cout << std::endl;
  191.     for (int j = 0; j < 10; j++) {
  192.       std::cout << gameGrid[i][j];
  193.     }
  194.     }
  195.    
  196.     // See if any player ships have been destroyed
  197.  
  198.     shipDestructionChecker ();
  199.  
  200.     // Let the computer take its turn
  201.     gameGrid = ai.computerTurn (gameGrid);
  202.   }
  203.   // If the player has won congratulate them
  204.   if (hasPlayerWon == true) {
  205.     std:: cout << std::endl << "CONGRATS, VICTORY IS YOURS" << std::endl << std::endl;
  206.   }
  207. }
  208.  
  209. // Player attacking, tell user if they hit or miss and make adjustment to board
  210. void BattleshipControl::playerFire (int column, int row) {
  211.   row = row +10;
  212.   if (gameGrid[row-1][column] != 8) {
  213.     std::cout << std::endl << "HIT!!" << std::endl;
  214.     gameGrid[row-1][column] = 8;
  215.   }
  216.   else {
  217.     std::cout << std::endl << "miss" << std::endl;
  218.   }
  219. }
  220. // Check what ships the player has destroyed
  221. void BattleshipControl::shipDestructionChecker () {
  222.  
  223.   int aircraft_carrier_parts_count = 0;
  224.   int battleship_parts_count = 0;
  225.   int submarine_parts_count = 0;
  226.   int destroyer_parts_count = 0;
  227.   int patrol_boat_parts_count = 0;
  228.  
  229.   // Count the pieces of ships left
  230.   for (int i = 10; i < 20; i++) {
  231.     for (int j = 0; j < 10; j++) {
  232.       if (gameGrid[i][j] == 1)
  233.     aircraft_carrier_parts_count++;
  234.       if (gameGrid[i][j] == 2)
  235.     battleship_parts_count++;
  236.       if (gameGrid[i][j] == 3)
  237.     submarine_parts_count++;
  238.       if (gameGrid[i][j] == 4)
  239.     destroyer_parts_count++;
  240.       if (gameGrid[i][j] == 5)
  241.     patrol_boat_parts_count++;
  242.     }
  243.   }
  244.   shipDestructionInformer (aircraft_carrier_parts_count, AIRCRAFT_MARK);
  245.   shipDestructionInformer (battleship_parts_count, BATTLESHIP_MARK);
  246.   shipDestructionInformer (submarine_parts_count, SUBMARINE_MARK);
  247.   shipDestructionInformer (destroyer_parts_count, DESTROYER_MARK);
  248.   shipDestructionInformer (patrol_boat_parts_count, PATROL_MARK);
  249. }
  250.  
  251. void BattleshipControl::shipDestructionInformer (int partsCount, int shipMark) {
  252.   /*
  253.    * If there are 0 remaining pieces of ships of a particular ship
  254.    * inform the player once they have sunk their opponents ship
  255.    */
  256.   if (partsCount == 0 && shipMark == AIRCRAFT_MARK && isSunk->isAircraftSunk == false) {
  257.     std::cout << std::endl << "You sunk their aircraft carrier!" << std::endl;
  258.     isSunk->isAircraftSunk = true;
  259.   }
  260.   else if (partsCount == 0 && shipMark == BATTLESHIP_MARK && isSunk->isBattleshipSunk == false) {
  261.     std::cout << std::endl << "You sunk their battleship!" << std::endl;
  262.     isSunk->isBattleshipSunk = true;
  263.   }
  264.   else if (partsCount == 0 && shipMark == SUBMARINE_MARK && isSunk->isSubmarineSunk == false) {
  265.     std::cout << std::endl << "You sunk their destroyer!" << std::endl;
  266.     isSunk->isSubmarineSunk = true;
  267.   }
  268.   else if (partsCount == 0 && shipMark == DESTROYER_MARK && isSunk->isDestroyerSunk == false) {
  269.     std::cout << std::endl << "You sunk their submarine!" << std::endl;
  270.     isSunk->isDestroyerSunk = true;
  271.   }
  272.   else if (partsCount == 0 && shipMark == PATROL_MARK && isSunk->isPatrolSunk == false) {
  273.     std::cout << std::endl << "You sunk their patrol boat!" << std::endl;
  274.     isSunk->isPatrolSunk = true;
  275.   }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement