i am creating a very simple and "robust" version of battleship for my c++ class. the game is just pretty much the computer placing a board of his own and you try to find his locations but you do not have your own board or locations (for now). i tam almsot positive the problem lies in the setup_board method... i have only done the set up for the aircraft carrier (you will see what i mean), because the problem would be the same for the other ships as well.
p.s. i am aware of alot of useless code, and things that i have written here that havent been implemented, this is a very very very early rough draft and i want to at least get the basics working before i clean it up (because there is alot of cleaning up to do)
p.s.s. please ignore the comments, i may have written them at an earlier time and forgot to change or delete them accordingly so just completely ignore all of them
/
#include <iostream>
usingnamespace std;
struct ship {
bool horiz_vert; //assign int 1-0 for true or fast. true is horizontal and false is vertical
int coord1;
int coord2;
int size; //for health values!!!
};
//For all the different type of shifts
ship aircraft_carrier;
ship battleship;
ship submarine;
ship cruiser;
ship destroyer;
int usernum;
int grid[9][9]; //grid for battleship
//COLISION DETECTION!!!!
int coord1; //made only in method scope to not make anything overconfusion
int coord2;
//JUST MAKE SO YOu GUESS COMOUTER< You DONT PLACE THE BOAS IN THIS VERSION
void clrscrn() { //only way to clear screen
cout << "\n \n \n \n \n \n \n \n " << endl;
}
void print_grid() { //function for refreshing grid
cout << "1-2-3-4-5-6-7-8-9-10" << endl;
for (int j = 0; j <= 9; j++) { //column for loop
for (int i = 0; i <= 9; i++) { //row for loop
cout << grid[i][j];
if (i <= 8)
cout << "-"; //hyphens make game easier to read
}
cout << " " << j + 1 << endl; //for formatting, makes grid correct
}
}
void player_move() {
char comma; //to take up space of comma when asking coorinates.
cout << "please type in your coordinates ex. 5,2" << endl;
cin >> coord1 >> comma >> coord2;
coord1 -= 1; //-1 because of zero adding problem. only way to fix it
coord2 -= 1;
if (coord1 > 9 || coord2 > 9) {
cout << "invalid coordinates, must be in between 1 and ten" << endl;
player_move(); //INCURSION
}
}
void setup_board() { //only sets up for computer...
aircraft_carrier.size = 5;
aircraft_carrier.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
aircraft_carrier.coord1 = rand() % 9 + 1;
aircraft_carrier.coord2 = rand() % 9 + 1;
battleship.size = 4;
battleship.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
battleship.coord1 = rand() % 9 + 1;
battleship.coord2 = rand() % 9 + 1;
submarine.size = 4;
submarine.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
submarine.coord1 = rand() % 9 + 1;
submarine.coord2 = rand() % 9 + 1;
destroyer.size = 4;
destroyer.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
destroyer.coord1 = rand() % 9 + 1;
destroyer.coord2 = rand() % 9 + 1;
if (aircraft_carrier.horiz_vert = 1) { // if it is true... then solving for horizontal
for (int i = 0; i <= 5; i++) { // 5 because that is the amount of health...
if (coord1 == aircraft_carrier.coord1 + i
|| coord2 == aircraft_carrier.coord2 + i) {
grid[aircraft_carrier.coord1][aircraft_carrier.coord2] = 1; //1 signfies hit...
clrscrn();
print_grid();
cout << "YOU HAVE HIT MY AIRCRAFT CARRIER!" << endl;
}
}
} else {
grid[coord1][coord2] = 2; // 2 signifies miss...
clrscrn();
player_move();
setup_board();
cout << "MISS" << endl;
}
}
//ADD COLLISION DETECTION!
void comp_move() { //FOR COMPUTER GENERETAED MOVE
}
int main() {
cout
<< "Welcome to Battleship!, when selecting coordinates please keep in mind the first coordinate is the column and the econd is the row \n the 1 symbol signifies a hit while the 2 symbol signifies a miss"
<< endl;
for (int j = 0; j <= 9; j++) { //column for loop
for (int i = 0; i <= 9; i++) { //row for loop
grid[j][i] = 0; //sets everything to 0 for
}
}
print_grid();
//grid[2][3] = 1; // changing group 2/3 will change the coordinates 3 and 4 on the rid, because 0 counts as a number...
//clrscrn();
//print_grid();
player_move();
setup_board();
//guess from 1-
return 0;
}
- First if the aircraft carrier is vertical the player automatically misses.
- You never call "srand(...)" to seed the random number generator. So random numbers aren't actually being generated.
- You should seperate the "collision detection" and the board setup into seperate functions. As it stands in order to detect a 'hit' or 'miss', you have to reassign the ships coordinates. That's not how I remember battleship being played.
- Your "collision detection" seems to be a little off, Lines 89 and 90. Right now it only matters if coord1 OR coord2 are guessed correctly and it will be registered as a hit. Also you don't have anything displayed if the player misses.
yea im still a beginner, and i wanted to do something cool for the final project.thank for your help, could you i assumed that the for loop would check if any of the coordinates said were contained by the ship by checking the starting points, then checking both horizontal, and vertical points,
ok i kind of got it working.. here take a look, i took aout one of the for loops and it there are some problems with the coordinates 9,10 and 3,3. also i cant seem to fint the battleship (i have only done one...) i dont know if that is working correctly
#include <iostream>
usingnamespace std;
struct ship {
bool horiz_vert; //assign int 1-0 for true or fast. true is horizontal and false is vertical
int coord1;
int coord2;
int size; //for health values!!!
};
//For all the different type of shifts
ship aircraft_carrier;
ship battleship;
ship submarine;
ship cruiser;
ship destroyer;
int usernum;
int grid[9][9]; //grid for battleship
//COLISION DETECTION!!!!
int coord1; //made only in method scope to not make anything overconfusion
int coord2;
//JUST MAKE SO YOu GUESS COMOUTER< You DONT PLACE THE BOAS IN THIS VERSION
void clrscrn() { //only way to clear screen
cout << "\n \n \n \n \n \n \n \n " << endl;
}
void print_grid() { //function for refreshing grid
cout << "1-2-3-4-5-6-7-8-9-10" << endl;
for (int j = 0; j <= 9; j++) { //column for loop
for (int i = 0; i <= 9; i++) { //row for loop
cout << grid[i][j];
if (i <= 8)
cout << "-"; //hyphens make game easier to read
}
cout << " " << j + 1 << endl; //for formatting, makes grid correct
}
}
void player_move() {
char comma; //to take up space of comma when asking coorinates.
cout << "please type in your coordinates ex. 5,2" << endl;
cin >> coord1 >> comma >> coord2;
coord1 -= 1; //-1 because of zero adding problem. only way to fix it
coord2 -= 1;
if (coord1 > 9 || coord2 > 9) {
cout << "invalid coordinates, must be in between 1 and ten" << endl;
player_move(); //INCURSION
}
}
void setup_board() { //only sets up for computer...
aircraft_carrier.size = 5;
aircraft_carrier.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
aircraft_carrier.coord1 = rand() % 9 + 1;
aircraft_carrier.coord2 = rand() % 9 + 1;
battleship.size = 4;
battleship.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
battleship.coord1 = rand() % 9 + 1;
battleship.coord2 = rand() % 9 + 1;
submarine.size = 4;
submarine.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
submarine.coord1 = rand() % 9 + 1;
submarine.coord2 = rand() % 9 + 1;
destroyer.size = 4;
destroyer.horiz_vert = rand() % 1; //0 or 1 for boolean!!!!!
destroyer.coord1 = rand() % 9 + 1;
destroyer.coord2 = rand() % 9 + 1;
for (int i = 0; i <= 5; i++) { // 5 because that is the amount of health...
if (coord1 == aircraft_carrier.coord1 + i
|| coord2 == aircraft_carrier.coord2 + i) {
grid[aircraft_carrier.coord1][aircraft_carrier.coord2] = 1; //1 signfies hit...
clrscrn();
print_grid();
cout << "YOU HAVE HIT MY AIRCRAFT CARRIER!" << endl;
}
else {
grid[coord1][coord2] = 2; // 2 signifies miss...
clrscrn();
print_grid();
cout << "MISS" << endl;
player_move();
setup_board();
}
}}
//ADD COLLISION DETECTION!
void comp_move() { //FOR COMPUTER GENERETAED MOVE
}
int main() {
cout
<< "Welcome to Battleship!, when selecting coordinates please keep in mind the first coordinate is the column and the econd is the row \n the 1 symbol signifies a hit while the 2 symbol signifies a miss"
<< endl;
for (int j = 0; j <= 9; j++) { //column for loop
for (int i = 0; i <= 9; i++) { //row for loop
grid[j][i] = 0; //sets everything to 0 for
}
}
print_grid();
//grid[2][3] = 1; // changing group 2/3 will change the coordinates 3 and 4 on the rid, because 0 counts as a number...
//clrscrn();
//print_grid();
player_move();
setup_board();
//guess from 1-
return 0;
}
but the problem is in the 9,10 and 3,3 coordinates, you will see what i mean if you run it. also, i dont think ive set the board up correctly. as i am guesing the coordinates of th ship, random values across the board change to 1 and then i guess all the coordinates around it, nothing comes of it
Also your use of recursion is a bit strange. Why are you calling setup_board() from setup_board()? When you do this you move the aircraft_carrier to a new location each time so it could just as well go to a location I have already bombed.
yes thank you i was having trouble and that was why, but still, it on coordinates 3-3 and 9,9. it messes up. here is my revised code, can you look it over, because it stil isnt quite right..
#include <iostream>
usingnamespace std;
struct ship {
bool horiz_vert; //assign int 1-0 for true or fast. true is horizontal and false is vertical
int coord1;
int coord2;
int size; //for health values!!!
};
//For all the different type of shifts
ship aircraft_carrier;
ship battleship;
ship submarine;
ship cruiser;
ship destroyer;
int usernum;
int grid[9][9]; //grid for battleship
//COLISION DETECTION!!!!
int coord1; //made only in method scope to not make anything overconfusion
int coord2;
//JUST MAKE SO YOu GUESS COMOUTER< You DONT PLACE THE BOAS IN THIS VERSION
void clrscrn() { //only way to clear screen
cout << "\n \n \n \n \n \n \n \n " << endl;
}
void print_grid() { //function for refreshing grid
cout << "1-2-3-4-5-6-7-8-9-10" << endl;
for (int j = 0; j <= 9; j++) { //column for loop
for (int i = 0; i <= 9; i++) { //row for loop
cout << grid[i][j];
if (i <= 8)
cout << "-"; //hyphens make game easier to read
}
cout << " " << j + 1 << endl; //for formatting, makes grid correct
}
}
void player_move() {
char comma; //to take up space of comma when asking coorinates.
cout << "please type in your coordinates ex. 5,2" << endl;
cin >> coord1 >> comma >> coord2;
coord1 -= 1; //-1 because of zero adding problem. only way to fix it
coord2 -= 1;
print_grid();
if(grid[coord1][coord2] == 2){
cout<<"you have already used this space! "<<endl;
player_move();
}
if (coord1 > 9 || coord2 > 9) {
cout << "invalid coordinates, must be in between 1 and ten" << endl;
player_move(); //RECURSION
}
}
void setup_board() { //only sets up for computer...
aircraft_carrier.size = 5;
aircraft_carrier.coord1 = rand() % 9 + 1;
aircraft_carrier.coord2 = rand() % 9 + 1;
battleship.size = 4;
battleship.coord1 = rand() % 9 + 1;
battleship.coord2 = rand() % 9 + 1;
submarine.size = 4;
submarine.coord1 = rand() % 9 + 1;
submarine.coord2 = rand() % 9 + 1;
destroyer.size = 4;
destroyer.coord1 = rand() % 9 + 1;
destroyer.coord2 = rand() % 9 + 1;
for (int i = 0; i <= 5; i++) { // 5 because that is the amount of health...
if (coord1 == aircraft_carrier.coord1 + i
|| coord2 == aircraft_carrier.coord2 + i) {
grid[aircraft_carrier.coord1][aircraft_carrier.coord2] = 1; //1 signfies hit...
clrscrn();
print_grid();
cout << "YOU HAVE HIT MY AIRCRAFT CARRIER!" << endl;
}
else {
grid[coord1][coord2] = 2; // 2 signifies miss...
clrscrn();
print_grid();
cout << "MISS" << endl;
player_move();
}
}}
//ADD COLLISION DETECTION!
void comp_move() { //FOR COMPUTER GENERETAED MOVE
}
int main() {
cout
<< "Welcome to Battleship!, when selecting coordinates please keep in mind the first coordinate is the column and the second is the row \n the 1 symbol signifies a hit while the 2 symbol signifies a miss \n \n at any time type in 99 and it will reveal the enemies hiding spots!"
<< endl;
for (int j = 0; j <=10; j++) { //column for loop
for (int i = 0; i <= 10; i++) { //row for loop
grid[j][i] = 0; //sets everything to 0 for
}
}
print_grid();
//grid[2][3] = 1; // changing group 2/3 will change the coordinates 3 and 4 on the rid, because 0 counts as a number...
//clrscrn();
//print_grid();
setup_board();
//guess from 1-
return 0;
}