I am trying to make Battleships in C++. I am trying to make a system to store the locations of all the ships, so they cannot overlap.
However, part of the code is giving me an **expected unqualified id **Error for seemingly no reason once I added the systems to do so. Even after reverting to an earlier version, it still does not appear to work.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
// Try Tuple, Vector or Struct for this std::vector<std::vector<std::tuple<int, int>>> coords;
/* struct Coordinate {
int x, y;
};
struct Ship {
vector<Coordinate> coords;
};
vector<Ship> ships;*/
int urships = 5;
/*
for (int i = 0; i < urships; i++) {
Ship newShip;
ships.push_back(newShip);
}
*/
int aiships = 5;
int shipsplaced = 0;
int ur2 = 1;
int ai2 = 1;
int ur3 = 2;
int ai3 = 2;
int ur4 = 1;
int ai4 = 1;
int ur5 = 1;
int ai5 = 1;
bool D_placed = false;
bool S_placed = false;
bool B_placed = false;
bool C_placed = false;
bool valid_square = true;
char vert;
int coordx;
int coordy;
int placelength;
string urboard [10][10] = {
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
{"\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A","\U0001F30A"},
};
string aiboard [10][10] = {
{"W","W","W","W","W","W","W","W","W","W"},
{"W","W","W","W","W","W","W","W","W","W"},
{"W","W","W","W","W","W","W","W","W","W"},
{"W","W","W","W","W","W","W","W","W","W"},
{"W","W","W","W","W","W","W","W","W","W"},
{"W","W","W","W","W","W","W","W","W","W"},
{"W","W","W","W","W","W","W","W","W","W"},
{"W","W","W","W","W","W","W","W","W","W"},
{"W","W","W","W","W","W","W","W","W","W"},
{"W","W","W","W","W","W","W","W","W","W"},
};
void printboard(){
ifstream letters("letters.txt");
string line;
cout<<"1 2 3 4 5 6 7 8 9 10";
for (int y = 0; y<10; y++ ){
cout<<endl;
for (int x = 0; x<10; x++){
cout<<urboard[y][x];
}
(getline(letters,line));
cout<<" "<<line;
}
cout<<endl<<endl;
}
void verticalchange(char a){
switch(a){
case 'A':
case 'a':
coordy = 0;
break;
case 'B':
case 'b':
coordy = 1;
break;
case 'C':
case 'c':
coordy = 2;
break;
case 'D':
case 'd':
coordy = 3;
break;
case 'E':
case 'e':
coordy = 4;
break;
case 'F':
case 'f':
coordy = 5;
break;
case 'G':
case 'g':
coordy = 6;
break;
case 'H':
case 'h':
coordy = 7;
break;
case 'I':
case 'i':
coordy = 8;
break;
case 'J':
case 'j':
coordy = 9;
break;
default:
cout<<"letter error\n";
valid_square = false;
}
}
void choosesquare(){
cout<<"Please input the desired coordinates\n";
cin>>vert;
verticalchange(vert);
cin>>coordx;
if (coordx>10){
cout<<"number error";
valid_square = false;
}
coordx--;
}
char choose;
char dir;
char placeval = ' ';
int picksleft = 100;
void pickships(){
cout<<"Which ships would you like to place? Enter your desired option below. \n1: Destroyers\n2: Submarine \n3: Battleships\n4: Carriers"<<endl;
cin>>choose;
switch(choose){
case'1':
cout<<"picking destroyers\n\n\n";
if (D_placed ==true){
cout<<"You have already placed that ship\n\nPlease pick again\n\n";
return;
}
picksleft = ur2;
placelength = 2;
cout<<picksleft;
}
switch(choose){
case'2':
cout<<"picking submarines\n\n\n";
if (S_placed ==true){
cout<<"You have already placed that ship\n\nPlease pick again\n\n";
return;
}
picksleft = ur3;
placelength = 3;
}
switch(choose){
case'3':
cout<<"picking battleships\n\n\n";
if (B_placed ==true){
cout<<"You have already placed that ship\n\nPlease pick again\n\n";
return;
}
picksleft = ur4;
placelength = 4;
}
switch(choose){
case'4':
cout<<"picking carriers\n\n\n";
if (C_placed ==true){
cout<<"You have already chosen that ship\n\nPlease pick again\n\n";
return;
}
picksleft = ur5;
placelength = 5;
}
while(picksleft > 0){
cout<<"Choose the starting point of your ship. You have " <<picksleft<<" 2-segment ships left.\n";
printboard();
choosesquare();
}
urboard[coordy][coordx] = "❎";
printboard();
cout<<"now decide which direction you would like the ship to point in. Type 'u' for up, 'd' for down, l for left and 'r for right'";
cin>>dir;
for (int x = 1; x<placelength; x++){
switch(dir){
case 'u':
coordy--;
urboard[coordy][coordx] = "❎";
printboard();
}
}
for (int x = 1; x<placelength; x++){
switch(dir){
case 'd':
coordy++;
urboard[coordy][coordx] = "❎";
printboard();
}
}
for (int x = 1; x<placelength; x++){
switch(dir){
case 'l':
coordx--;
urboard[coordy][coordx] = "❎";
printboard();
}
}
for (int x = 1; x<placelength; x++){
switch(dir){
case 'r':
coordx++;
urboard[coordy][coordx] = "❎";
printboard();
}
}
picksleft--;
}
switch(placelength) {
case 2:
D_placed = true;
break;
case 3:
S_placed = true;
break;
case 4:
B_placed = true;
break;
case 5:
C_placed = true;
break;
default:
cout<<"testing";
}
int main() {
cout<<"Welcome to Battleships! :)"<<endl;
cout<<"Lets's start by placing your ships!\n\n";
printboard();
cout<<"you have "<<urships<<" total ships, composed of "<<ur2<<" 2 segment destroyers,"<<ur3<<" 3-segment submarine(s), "<<ur4<<" 4-segment battleship(s) and "<<ur5<<" 5-segment carrier(s) that can be place anywhere on the 10-by-10 grid.\n\n";
while (D_placed == false or S_placed == false or C_placed == false or B_placed == false ){
pickships();
}
cout<<"test( ͡° ͜ʖ ͡°)";
//
//Notes: use choose+1 to firgure out the number of repeats for placement of ships
//store ships coords in external files. Upon successful hit, read position data from file and check if any segments are still standing. If no, then the ship is destroyed.
}