Hey everyone. So I need to make a battleship game. Basically what it is supposed
ID: 3538999 • Letter: H
Question
Hey everyone. So I need to make a battleship game. Basically what it is supposed to do is have a client and a second player. The game CAN be automatic or it can be user input. It NEEDS to have the board 100x100 for both screens. It also needs to have 4 ships that can be vertical or horizontal. The game is supposed to keep going until either one player shoots and hits the 4 ships. The 4 ships need to be 4 coordinates long. I am posting a picture of what the game is supposed to look like. The board needs to be in a JFrame or whatever works to make the board display like the picture I am posting. Thank you. FULL CREDIT will be awarded to the person to do this for me. i have EXISTING code I will be posting also. You can modify it to work how I need it or you can use your own. I would really appreciate it if you can make it work how I need it to. Thank you. Here is the picture: http://i44.tinypic.com/mw778l.jpgExplanation / Answer
#include "stdafx.h" #include <iostream> #include <time.h> #include <conio.h> #include <sstream> using namespace std; //Below is an enumerated data type so that I can use the labels 'VERTICAL', and 'HORIZONTAL' to designate the //direction of a ship. enum ShipDirection {VERTICAL, HORIZONTAL}; //This is the ship class, it is a blue print for a ship. //The game of battleship utilizes 5 ships of various sizes. class Ship { private: int numOfHits; ShipDirection shipDir; int startRow; int startCol; string shipType; public: Ship() { shipType = ""; shipDir = VERTICAL; startRow = 0; startCol = 0; numOfHits = 0; } Ship(int nHits, string name) { shipType = name; shipDir = VERTICAL; startRow = 0; startCol = 0; numOfHits = nHits; } //Created sets and gets for all of the data members in the private: section of this class. void setStartRow(int sR) { startRow = sR; } int getStartRow() { return startRow; } void setStartCol(int sC) { startCol = sC; } int getStartCol() { return startCol; } int getNumberOfHits() { return numOfHits; } ShipDirection getShipDir() { return shipDir; } void setShipDir(ShipDirection sD) { shipDir = sD; } string getNameOfShip() { return shipType; } void setNameOfShip(string nOS) { shipType = nOS; } }; //This is the board class. This class represents the board. The board class contains a 2 dimensional array. //That array is filled with zeros initially. If there is a ship at a certain coordinate at that array, it will contain a //'1', if there is a space of a ship that has been hit at a certain coordinate of the board array, it will contain a '2'. class Board { private: int board[10][10]; public: Board() { for (int row = 0; row < 10; row++) { for (int col = 0; col<10; col++) { board[row][col] = 0; } } } //This function places a ship on the board void placeShip(Ship ship) { //Create a random number between 0 and 1 to represent the direction of the ship int sD = rand()%2; int sR = rand()%10; int sC = rand()%10; if (sD==0) { ship.setShipDir(VERTICAL); } else { ship.setShipDir(HORIZONTAL); } while(!isClearSpace(sR, sC, ship.getNumberOfHits(), ship.getShipDir())) { sR = rand()%10; sC = rand()%10; } ship.setStartRow(sR); ship.setStartCol(sC); setShip(sR, sC,ship.getNumberOfHits(),ship.getShipDir()); } bool isClearSpace(int startRow, int startCol, int size, ShipDirection shipDir) { if(shipDir == VERTICAL) { if(startRow + size > 9) { return false; } for(int row = startRow; row < startRow+size; row++) { if(board[row][startCol] !=0) { return false; } } } else if(shipDir==HORIZONTAL) { if(startCol + size > 9) { return false; } for(int col = startCol; col < startCol+size; col++) { if(board[startRow][col] !=0) { return false; } } } return true; } void setShip(int startRow, int startCol, int size, ShipDirection shipDir) { if(shipDir == VERTICAL) { for(int row = startRow; row < startRow+size; row++) { board[row][startCol] = 1; } } else if(shipDir==HORIZONTAL) { for(int col = startCol; col < startCol+size; col++) { board[startRow][col] = 1; } } } //Print board void printBoard() { cout << " 0 1 2 3 4 5 6 7 8 9" << endl; char rowLabel = 'A'; for(int row = 0; row < 10; row++) { cout << rowLabel << " "; rowLabel++; for(int col = 0; col < 10; col++) { cout << board[row][col] << " "; } cout << endl; } } //This takes in a row and a col //it then checks to see if the guess is a hit or not //if there is not a zero at that place it has to be //a 1 or a 2, I will consider that to be a hit //my version of the game does not take into account //whether the user hits the boat at the same place //more then once bool isHit(int row, int col) { if(board[row][col] != '1') { board[row][col] = '2'; return true; } if(board[row][col] = '0') { return false; } } //Checks the entire board //if there are any 1's there is boat still on the board that is not yet hit //so return false if it finds a 1, otherwise the game is over bool isGameWon() { for(int row = 0; row < 10; row++) for(int col = 0; col < 10; col++) if(board[row][col] = 1) { return false; //game is not yet won } return true; } //Checks to see if this particular ship is sunk by checking to see //if the board at this ship's position is all 2s. bool isShipSunk(Ship ship, int startRow, int startCol, int size, ShipDirection shipDir) { if(shipDir == VERTICAL) { if(startRow + size > 9) { return false; } for(int row = startRow; row < startRow+size; row++) { if(board[row][startCol] !='2') { return false; } } } else if(shipDir==HORIZONTAL) { if(startCol + size > 9) { return false; } for(int col = startCol; col < startCol+size; col++) { if(board[startRow][col] !='2') { return false; } } } return true; } }; int _tmain(int argc, _TCHAR* argv[]) { //Seed the random number generator with the current time srand(time(0)); char guessRow; int guessCol; char playAgain(); char keepPlaying; bool isGameWon; bool isHit; //While the player intends to keep playing do { //Create each of the five ships Ship carrier = Ship(5, "Carrier"); Ship battleship = Ship(4, "Battleship"); Ship submarine = Ship(3, "Submarine"); Ship destroyer = Ship(3, "Destroyer"); Ship patrolboat = Ship(2, "Patrol boat"); //Create the board Board board = Board(); //place ships on board board.placeShip(carrier); board.placeShip(battleship); board.placeShip(submarine); board.placeShip(destroyer); board.placeShip(patrolboat); //print the board with the ships on it temporarily for testing, when the game is finished I will take this out. board.printBoard(); do { //Asks User for Coordinates of their choice. cout << " Please enter your coordinates: "; cout << "Enter row: "; cin >> guessRow; guessRow = toupper(guessRow); cout << "Enter column: "; cin >> guessCol; cout << endl; if (isHit != false)//"Run-Time Check Failure #3 - The variable 'isHit' is being used without being initialized." { cout << "Its a hit "; } //check each ship to see if it was sunk. //Need help here to to work out hits with sinks /*if(board.isShipSunk(carrier)) { cout << "You sunk my Carrier!"; } if(board.isShipSunk(battleship)) { cout << "You sunk my Battleship!"; } if(board.isShipSunk(submarine)) { cout << "You sunk my Submarine!"; } if(board.isShipSunk(destroyer)) { cout << "You sunk my Destroyer!"; } if(board.isShipSunk(patrolboat)) { cout << "You sunk my Partrol boat!"; }*/ //do that on all 5 ships else { cout << "It was a miss! "; } } while (isGameWon != true );//"Run-Time Check Failure #3 - The variable 'isGameWon' is being used without being initialized." //ask the player if he or she intends to keep playing keepPlaying = playAgain(); if ( keepPlaying == 'N') { cout << "Goodbye "; system("pause"); return 0; } } while (keepPlaying == 'Y'); } //play again char playAgain() { char keepPlaying; cout << "Would you like to play again? (y or n) "; do { cin >> keepPlaying; keepPlaying = toupper(keepPlaying); if (keepPlaying!='Y' && keepPlaying != 'N') cout << "Enter only a 'Y' or an 'N', please!" << endl << endl; } while ( keepPlaying !='Y' && keepPlaying != 'N'); return keepPlaying; }
#include "stdafx.h" #include <iostream> #include <time.h> #include <conio.h> #include <sstream> using namespace std; //Below is an enumerated data type so that I can use the labels 'VERTICAL', and 'HORIZONTAL' to designate the //direction of a ship. enum ShipDirection {VERTICAL, HORIZONTAL}; //This is the ship class, it is a blue print for a ship. //The game of battleship utilizes 5 ships of various sizes. class Ship { private: int numOfHits; ShipDirection shipDir; int startRow; int startCol; string shipType; public: Ship() { shipType = ""; shipDir = VERTICAL; startRow = 0; startCol = 0; numOfHits = 0; } Ship(int nHits, string name) { shipType = name; shipDir = VERTICAL; startRow = 0; startCol = 0; numOfHits = nHits; } //Created sets and gets for all of the data members in the private: section of this class. void setStartRow(int sR) { startRow = sR; } int getStartRow() { return startRow; } void setStartCol(int sC) { startCol = sC; } int getStartCol() { return startCol; } int getNumberOfHits() { return numOfHits; } ShipDirection getShipDir() { return shipDir; } void setShipDir(ShipDirection sD) { shipDir = sD; } string getNameOfShip() { return shipType; } void setNameOfShip(string nOS) { shipType = nOS; } }; //This is the board class. This class represents the board. The board class contains a 2 dimensional array. //That array is filled with zeros initially. If there is a ship at a certain coordinate at that array, it will contain a //'1', if there is a space of a ship that has been hit at a certain coordinate of the board array, it will contain a '2'. class Board { private: int board[10][10]; public: Board() { for (int row = 0; row < 10; row++) { for (int col = 0; col<10; col++) { board[row][col] = 0; } } } //This function places a ship on the board void placeShip(Ship ship) { //Create a random number between 0 and 1 to represent the direction of the ship int sD = rand()%2; int sR = rand()%10; int sC = rand()%10; if (sD==0) { ship.setShipDir(VERTICAL); } else { ship.setShipDir(HORIZONTAL); } while(!isClearSpace(sR, sC, ship.getNumberOfHits(), ship.getShipDir())) { sR = rand()%10; sC = rand()%10; } ship.setStartRow(sR); ship.setStartCol(sC); setShip(sR, sC,ship.getNumberOfHits(),ship.getShipDir()); } bool isClearSpace(int startRow, int startCol, int size, ShipDirection shipDir) { if(shipDir == VERTICAL) { if(startRow + size > 9) { return false; } for(int row = startRow; row < startRow+size; row++) { if(board[row][startCol] !=0) { return false; } } } else if(shipDir==HORIZONTAL) { if(startCol + size > 9) { return false; } for(int col = startCol; col < startCol+size; col++) { if(board[startRow][col] !=0) { return false; } } } return true; } void setShip(int startRow, int startCol, int size, ShipDirection shipDir) { if(shipDir == VERTICAL) { for(int row = startRow; row < startRow+size; row++) { board[row][startCol] = 1; } } else if(shipDir==HORIZONTAL) { for(int col = startCol; col < startCol+size; col++) { board[startRow][col] = 1; } } } //Print board void printBoard() { cout << " 0 1 2 3 4 5 6 7 8 9" << endl; char rowLabel = 'A'; for(int row = 0; row < 10; row++) { cout << rowLabel << " "; rowLabel++; for(int col = 0; col < 10; col++) { cout << board[row][col] << " "; } cout << endl; } } //This takes in a row and a col //it then checks to see if the guess is a hit or not //if there is not a zero at that place it has to be //a 1 or a 2, I will consider that to be a hit //my version of the game does not take into account //whether the user hits the boat at the same place //more then once bool isHit(int row, int col) { if(board[row][col] != '1') { board[row][col] = '2'; return true; } if(board[row][col] = '0') { return false; } } //Checks the entire board //if there are any 1's there is boat still on the board that is not yet hit //so return false if it finds a 1, otherwise the game is over bool isGameWon() { for(int row = 0; row < 10; row++) for(int col = 0; col < 10; col++) if(board[row][col] = 1) { return false; //game is not yet won } return true; } //Checks to see if this particular ship is sunk by checking to see //if the board at this ship's position is all 2s. bool isShipSunk(Ship ship, int startRow, int startCol, int size, ShipDirection shipDir) { if(shipDir == VERTICAL) { if(startRow + size > 9) { return false; } for(int row = startRow; row < startRow+size; row++) { if(board[row][startCol] !='2') { return false; } } } else if(shipDir==HORIZONTAL) { if(startCol + size > 9) { return false; } for(int col = startCol; col < startCol+size; col++) { if(board[startRow][col] !='2') { return false; } } } return true; } }; int _tmain(int argc, _TCHAR* argv[]) { //Seed the random number generator with the current time srand(time(0)); char guessRow; int guessCol; char playAgain(); char keepPlaying; bool isGameWon; bool isHit; //While the player intends to keep playing do { //Create each of the five ships Ship carrier = Ship(5, "Carrier"); Ship battleship = Ship(4, "Battleship"); Ship submarine = Ship(3, "Submarine"); Ship destroyer = Ship(3, "Destroyer"); Ship patrolboat = Ship(2, "Patrol boat"); //Create the board Board board = Board(); //place ships on board board.placeShip(carrier); board.placeShip(battleship); board.placeShip(submarine); board.placeShip(destroyer); board.placeShip(patrolboat); //print the board with the ships on it temporarily for testing, when the game is finished I will take this out. board.printBoard(); do { //Asks User for Coordinates of their choice. cout << " Please enter your coordinates: "; cout << "Enter row: "; cin >> guessRow; guessRow = toupper(guessRow); cout << "Enter column: "; cin >> guessCol; cout << endl; if (isHit != false)//"Run-Time Check Failure #3 - The variable 'isHit' is being used without being initialized." { cout << "Its a hit "; } //check each ship to see if it was sunk. //Need help here to to work out hits with sinks /*if(board.isShipSunk(carrier)) { cout << "You sunk my Carrier!"; } if(board.isShipSunk(battleship)) { cout << "You sunk my Battleship!"; } if(board.isShipSunk(submarine)) { cout << "You sunk my Submarine!"; } if(board.isShipSunk(destroyer)) { cout << "You sunk my Destroyer!"; } if(board.isShipSunk(patrolboat)) { cout << "You sunk my Partrol boat!"; }*/ //do that on all 5 ships else { cout << "It was a miss! "; } } while (isGameWon != true );//"Run-Time Check Failure #3 - The variable 'isGameWon' is being used without being initialized." //ask the player if he or she intends to keep playing keepPlaying = playAgain(); if ( keepPlaying == 'N') { cout << "Goodbye "; system("pause"); return 0; } } while (keepPlaying == 'Y'); } //play again char playAgain() { char keepPlaying; cout << "Would you like to play again? (y or n) "; do { cin >> keepPlaying; keepPlaying = toupper(keepPlaying); if (keepPlaying!='Y' && keepPlaying != 'N') cout << "Enter only a 'Y' or an 'N', please!" << endl << endl; } while ( keepPlaying !='Y' && keepPlaying != 'N'); return keepPlaying; }
#include "stdafx.h" #include <iostream> #include <time.h> #include <conio.h> #include <sstream> using namespace std; //Below is an enumerated data type so that I can use the labels 'VERTICAL', and 'HORIZONTAL' to designate the //direction of a ship. enum ShipDirection {VERTICAL, HORIZONTAL}; //This is the ship class, it is a blue print for a ship. //The game of battleship utilizes 5 ships of various sizes. class Ship { private: int numOfHits; ShipDirection shipDir; int startRow; int startCol; string shipType; public: Ship() { shipType = ""; shipDir = VERTICAL; startRow = 0; startCol = 0; numOfHits = 0; } Ship(int nHits, string name) { shipType = name; shipDir = VERTICAL; startRow = 0; startCol = 0; numOfHits = nHits; } //Created sets and gets for all of the data members in the private: section of this class. void setStartRow(int sR) { startRow = sR; } int getStartRow() { return startRow; } void setStartCol(int sC) { startCol = sC; } int getStartCol() { return startCol; } int getNumberOfHits() { return numOfHits; } ShipDirection getShipDir() { return shipDir; } void setShipDir(ShipDirection sD) { shipDir = sD; } string getNameOfShip() { return shipType; } void setNameOfShip(string nOS) { shipType = nOS; } }; //This is the board class. This class represents the board. The board class contains a 2 dimensional array. //That array is filled with zeros initially. If there is a ship at a certain coordinate at that array, it will contain a //'1', if there is a space of a ship that has been hit at a certain coordinate of the board array, it will contain a '2'. class Board { private: int board[10][10]; public: Board() { for (int row = 0; row < 10; row++) { for (int col = 0; col<10; col++) { board[row][col] = 0; } } } //This function places a ship on the board void placeShip(Ship ship) { //Create a random number between 0 and 1 to represent the direction of the ship int sD = rand()%2; int sR = rand()%10; int sC = rand()%10; if (sD==0) { ship.setShipDir(VERTICAL); } else { ship.setShipDir(HORIZONTAL); } while(!isClearSpace(sR, sC, ship.getNumberOfHits(), ship.getShipDir())) { sR = rand()%10; sC = rand()%10; } ship.setStartRow(sR); ship.setStartCol(sC); setShip(sR, sC,ship.getNumberOfHits(),ship.getShipDir()); } bool isClearSpace(int startRow, int startCol, int size, ShipDirection shipDir) { if(shipDir == VERTICAL) { if(startRow + size > 9) { return false; } for(int row = startRow; row < startRow+size; row++) { if(board[row][startCol] !=0) { return false; } } } else if(shipDir==HORIZONTAL) { if(startCol + size > 9) { return false; } for(int col = startCol; col < startCol+size; col++) { if(board[startRow][col] !=0) { return false; } } } return true; } void setShip(int startRow, int startCol, int size, ShipDirection shipDir) { if(shipDir == VERTICAL) { for(int row = startRow; row < startRow+size; row++) { board[row][startCol] = 1; } } else if(shipDir==HORIZONTAL) { for(int col = startCol; col < startCol+size; col++) { board[startRow][col] = 1; } } } //Print board void printBoard() { cout << " 0 1 2 3 4 5 6 7 8 9" << endl; char rowLabel = 'A'; for(int row = 0; row < 10; row++) { cout << rowLabel << " "; rowLabel++; for(int col = 0; col < 10; col++) { cout << board[row][col] << " "; } cout << endl; } } //This takes in a row and a col //it then checks to see if the guess is a hit or not //if there is not a zero at that place it has to be //a 1 or a 2, I will consider that to be a hit //my version of the game does not take into account //whether the user hits the boat at the same place //more then once bool isHit(int row, int col) { if(board[row][col] != '1') { board[row][col] = '2'; return true; } if(board[row][col] = '0') { return false; } } //Checks the entire board //if there are any 1's there is boat still on the board that is not yet hit //so return false if it finds a 1, otherwise the game is over bool isGameWon() { for(int row = 0; row < 10; row++) for(int col = 0; col < 10; col++) if(board[row][col] = 1) { return false; //game is not yet won } return true; } //Checks to see if this particular ship is sunk by checking to see //if the board at this ship's position is all 2s. bool isShipSunk(Ship ship, int startRow, int startCol, int size, ShipDirection shipDir) { if(shipDir == VERTICAL) { if(startRow + size > 9) { return false; } for(int row = startRow; row < startRow+size; row++) { if(board[row][startCol] !='2') { return false; } } } else if(shipDir==HORIZONTAL) { if(startCol + size > 9) { return false; } for(int col = startCol; col < startCol+size; col++) { if(board[startRow][col] !='2') { return false; } } } return true; } }; int _tmain(int argc, _TCHAR* argv[]) { //Seed the random number generator with the current time srand(time(0)); char guessRow; int guessCol; char playAgain(); char keepPlaying; bool isGameWon; bool isHit; //While the player intends to keep playing do { //Create each of the five ships Ship carrier = Ship(5, "Carrier"); Ship battleship = Ship(4, "Battleship"); Ship submarine = Ship(3, "Submarine"); Ship destroyer = Ship(3, "Destroyer"); Ship patrolboat = Ship(2, "Patrol boat"); //Create the board Board board = Board(); //place ships on board board.placeShip(carrier); board.placeShip(battleship); board.placeShip(submarine); board.placeShip(destroyer); board.placeShip(patrolboat); //print the board with the ships on it temporarily for testing, when the game is finished I will take this out. board.printBoard(); do { //Asks User for Coordinates of their choice. cout << " Please enter your coordinates: "; cout << "Enter row: "; cin >> guessRow; guessRow = toupper(guessRow); cout << "Enter column: "; cin >> guessCol; cout << endl; if (isHit != false)//"Run-Time Check Failure #3 - The variable 'isHit' is being used without being initialized." { cout << "Its a hit "; } //check each ship to see if it was sunk. //Need help here to to work out hits with sinks /*if(board.isShipSunk(carrier)) { cout << "You sunk my Carrier!"; } if(board.isShipSunk(battleship)) { cout << "You sunk my Battleship!"; } if(board.isShipSunk(submarine)) { cout << "You sunk my Submarine!"; } if(board.isShipSunk(destroyer)) { cout << "You sunk my Destroyer!"; } if(board.isShipSunk(patrolboat)) { cout << "You sunk my Partrol boat!"; }*/ //do that on all 5 ships else { cout << "It was a miss! "; } } while (isGameWon != true );//"Run-Time Check Failure #3 - The variable 'isGameWon' is being used without being initialized." //ask the player if he or she intends to keep playing keepPlaying = playAgain(); if ( keepPlaying == 'N') { cout << "Goodbye "; system("pause"); return 0; } } while (keepPlaying == 'Y'); } //play again char playAgain() { char keepPlaying; cout << "Would you like to play again? (y or n) "; do { cin >> keepPlaying; keepPlaying = toupper(keepPlaying); if (keepPlaying!='Y' && keepPlaying != 'N') cout << "Enter only a 'Y' or an 'N', please!" << endl << endl; } while ( keepPlaying !='Y' && keepPlaying != 'N'); return keepPlaying; }