Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <string>
- #include <sstream>
- #include <cstdlib>
- #include "BattleshipControl.h"
- #include "BattleshipSetup.h"
- #include "BattleshipAI.h"
- //Used to tell if ships have been sunk
- struct shipSunk {
- bool isAircraftSunk;
- bool isBattleshipSunk;
- bool isSubmarineSunk;
- bool isDestroyerSunk;
- bool isPatrolSunk;
- } sunkShips;
- struct shipSunk *isSunk = &sunkShips;
- // Main function, ask user to start the game, exit, or ask for help
- int main (){
- BattleshipControl control;
- string line = "";
- while (true) {
- std::cout << control.getInstructions () << std::endl;
- getline(std::cin,line);
- // If user decides to start game, go to function basic_setup
- if (line == "start") {
- control.basicSetup ();
- }
- // If user decides to exit, exit the program
- else if (line == "exit") {
- exit(0);
- }
- // If user asks for help, redisplay prompt message
- else if (line == "help") {
- std::cout << control.getInstructions () << std::endl;
- }
- // Otherwise its an incorrect command
- else {
- std::cout << std::endl << "Incorrect command" << std::endl << std::endl;
- }
- }
- return 0;
- }
- std::string BattleshipControl::getInstructions () {
- std::string instruction_string = "Battleship" ;
- instruction_string += "Type \"start\" to begin the game \n";
- instruction_string += "Type \"exit\" to exit the game \n";
- instruction_string += "Type \"help\" to redisplay this message \n";
- return instruction_string;
- }
- // Start the game
- void BattleshipControl::basicSetup () {
- BattleshipSetup setup;
- // Setup a board consisting of 200 spots to serve as the game board
- gameGrid.resize (20);
- for (int i = 0; i < 20; i++)
- gameGrid [i].resize (10);
- // Place on the board user and computer ships
- gameGrid = setup.setupGame ();
- // Begin the game
- beginGame ();
- }
- // Begin the game
- void BattleshipControl::beginGame () {
- BattleshipAI ai;
- std:: string line = "";
- int coin_flip;
- const int HEADS = 1;
- const int TAILS = 2;
- // Send game board to computer ai
- ai.initiateAI (gameGrid);
- std::cout << std::endl << std::endl << "Flip to see who goes first" << std::endl;
- std::cout << "1 for HEADS, 2 for tails." << std::endl;
- while(true) {
- std::cout << std::endl << "Enter: ";
- getline(std::cin,line);
- // Make sure user has selected HEADS or tails
- if (line == "exit") {
- exit (0);
- }
- else if (line == "1" || line == "2") {
- srand (time(NULL));
- coin_flip = rand ()% 2+1;
- // Coin flip HEADS
- if(coin_flip == HEADS) {
- std::cout << std::endl << "HEADS!" << std::endl;
- }
- // Coin flip tails
- else if(coin_flip == TAILS) {
- std::cout << std::endl << "TAILS!" << std::endl;
- }
- break;
- }
- else {
- std::cout << std::endl << "Incorrect selection" << std::endl;
- }
- }
- if ((line == "1" && coin_flip == HEADS) || (line == "2" && coin_flip == TAILS)) {
- playerTurn();
- }
- else if ((line == "1" && coin_flip == TAILS) || (line == "2" && coin_flip == HEADS)) {
- gameGrid = ai.computerTurn(gameGrid);
- }
- playerTurn ();
- }
- void BattleshipControl::playerTurn () {
- BattleshipSetup setup;
- BattleshipAI ai;
- string line = "";
- int column;
- int row;
- char *row_char;
- bool hasPlayerWon = false;
- bool firePositionGood = false;
- // While there has been no victory keep going
- while (!hasPlayerWon) {
- firePositionGood = false;
- // Have the user select the column they want to fire at
- std::cout << std::endl << "Fire at which column? (A-J): ";
- // While the position is no good for an attack
- while (!firePositionGood) {
- getline (std::cin, line);
- // Allow user to exit out
- if (line == "exit") {
- exit(0);
- }
- // If the columnConverter doesn't return a 10 which constitutes an error
- // then the position is good, and the location is noted, and loop ends.
- else if (setup.columnConverter (line) != 10) {
- column = setup.columnConverter (line);
- firePositionGood = true;
- // Else it was an invalid selection and it asks the user again for a column
- } else {
- std::cout << "Invalid selection" << std::endl;
- }
- }
- firePositionGood = false;
- // While the position is no good for an attack
- while (!firePositionGood) {
- // Have the user select the row they want to fire at
- std::cout << std::endl << "Fire at which row? (1-10): ";
- getline (std::cin, line);
- // Allow user to exit out
- if (line == "exit") {
- exit(0);
- }
- else {
- // Turn the string in to a useable integer
- std::stringstream buffer(line);
- buffer >> row;
- row_char = &line [0];
- // If what is entered is a digit between 1 and 10 the position is good and the loop ends
- if (row == 10 || (isdigit(*row_char) && line.length () == 1 && row >= 1 && row <= 9)) {
- firePositionGood = true;
- }
- // Else what was typed was an invalid selection and it asks the user again for a row
- else {
- std::cout << "Invalid selection" << std::endl;
- }
- }
- }
- // Use the column and row to attack a spot on the board with the playerFire function
- playerFire (column, row);
- // Assume player has won
- hasPlayerWon = true;
- // See if this is true
- for (int i = 10; i < 20; i++) {
- for (int j = 0; j < 10; j++) {
- if (gameGrid [i][j] != 0) {
- hasPlayerWon = false;
- break;
- }
- }
- }
- //Redisplay user's battleground every turn
- for (int i = 0; i < 20; i++) {
- std::cout << std::endl;
- for (int j = 0; j < 10; j++) {
- std::cout << gameGrid[i][j];
- }
- }
- // See if any player ships have been destroyed
- shipDestructionChecker ();
- // Let the computer take its turn
- gameGrid = ai.computerTurn (gameGrid);
- }
- // If the player has won congratulate them
- if (hasPlayerWon == true) {
- std:: cout << std::endl << "CONGRATS, VICTORY IS YOURS" << std::endl << std::endl;
- }
- }
- // Player attacking, tell user if they hit or miss and make adjustment to board
- void BattleshipControl::playerFire (int column, int row) {
- row = row +10;
- if (gameGrid[row-1][column] != 8) {
- std::cout << std::endl << "HIT!!" << std::endl;
- gameGrid[row-1][column] = 8;
- }
- else {
- std::cout << std::endl << "miss" << std::endl;
- }
- }
- // Check what ships the player has destroyed
- void BattleshipControl::shipDestructionChecker () {
- int aircraft_carrier_parts_count = 0;
- int battleship_parts_count = 0;
- int submarine_parts_count = 0;
- int destroyer_parts_count = 0;
- int patrol_boat_parts_count = 0;
- // Count the pieces of ships left
- for (int i = 10; i < 20; i++) {
- for (int j = 0; j < 10; j++) {
- if (gameGrid[i][j] == 1)
- aircraft_carrier_parts_count++;
- if (gameGrid[i][j] == 2)
- battleship_parts_count++;
- if (gameGrid[i][j] == 3)
- submarine_parts_count++;
- if (gameGrid[i][j] == 4)
- destroyer_parts_count++;
- if (gameGrid[i][j] == 5)
- patrol_boat_parts_count++;
- }
- }
- shipDestructionInformer (aircraft_carrier_parts_count, AIRCRAFT_MARK);
- shipDestructionInformer (battleship_parts_count, BATTLESHIP_MARK);
- shipDestructionInformer (submarine_parts_count, SUBMARINE_MARK);
- shipDestructionInformer (destroyer_parts_count, DESTROYER_MARK);
- shipDestructionInformer (patrol_boat_parts_count, PATROL_MARK);
- }
- void BattleshipControl::shipDestructionInformer (int partsCount, int shipMark) {
- /*
- * If there are 0 remaining pieces of ships of a particular ship
- * inform the player once they have sunk their opponents ship
- */
- if (partsCount == 0 && shipMark == AIRCRAFT_MARK && isSunk->isAircraftSunk == false) {
- std::cout << std::endl << "You sunk their aircraft carrier!" << std::endl;
- isSunk->isAircraftSunk = true;
- }
- else if (partsCount == 0 && shipMark == BATTLESHIP_MARK && isSunk->isBattleshipSunk == false) {
- std::cout << std::endl << "You sunk their battleship!" << std::endl;
- isSunk->isBattleshipSunk = true;
- }
- else if (partsCount == 0 && shipMark == SUBMARINE_MARK && isSunk->isSubmarineSunk == false) {
- std::cout << std::endl << "You sunk their destroyer!" << std::endl;
- isSunk->isSubmarineSunk = true;
- }
- else if (partsCount == 0 && shipMark == DESTROYER_MARK && isSunk->isDestroyerSunk == false) {
- std::cout << std::endl << "You sunk their submarine!" << std::endl;
- isSunk->isDestroyerSunk = true;
- }
- else if (partsCount == 0 && shipMark == PATROL_MARK && isSunk->isPatrolSunk == false) {
- std::cout << std::endl << "You sunk their patrol boat!" << std::endl;
- isSunk->isPatrolSunk = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement