So, this is my version of battleship with single space ships instead of a ship with 2,3,4, and 5. The only issue is the cpu I programmed isn't always choosing five, or it's choosing spots not on the map, I am completely lost if you can offer any help at all that would be great.
edit: I have the cpu's ships marked as L's for debugging reasons
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdio.h>
using namespace std;
int convert(char num)
{
int nom = 0;
if(num == 'A'||num == 'a')
{
nom = 1;
}
if(num == 'B'||num == 'b')
{
nom = 2;
}
if(num == 'C'||num == 'c')
{
nom = 3;
}
if(num == 'D'||num == 'd')
{
nom = 4;
}
if(num == 'E'||num == 'e')
{
nom = 5;
}
if(num == 'F'||num == 'f')
{
nom = 6;
}
if(num == 'G'||num == 'g')
{
nom = 7;
}
if(num == 'H'||num == 'h')
{
nom = 8;
}
return nom;
}
void grid(string userShips[][8], string compShips[][8])
{
string dot = "॰";
cout<<" Your Grid His Grid\n";
for(int x = 7; x >= 0; x--)
{
cout<<x+1;
for(int y = 0; y <= 7; y++)
{
if(userShips[x][y] == "0")
{
cout<<" "<<dot;
}
if(userShips[x][y] == "H")
{
cout<<" H";
}
if(userShips[x][y] == "M")
{
cout<<" M";
}
if(userShips[x][y] == "1")
{
cout<<" S";
}
}
cout<<" ";
cout<<x+1;
for(int o = 0; o <= 7; o++)
{
if(compShips[x][o] == "0")
{
cout<<" "<<dot;
}
if(compShips[x][o] == "H")
{
cout<<" H";
}
if(compShips[x][o] == "M")
{
cout<<" M";
}
if(compShips[x][o] == "1")
{
cout<<" L";
}
//cout<<compShips[x][o];
}
cout<<endl;
}
cout<<" A B C D E F G H A B C D E F G H\n";
}
void compShip(string compShips[][8])
{
for(int x = 0; x < 5; x++)
{
int random = 0;
random = (rand()%8);
int randm = 0;
randm = (rand()%8);
//cout<<random<<endl<<randm<<endl<<endl;
compShips[random][randm] = "1";
}
}
void playerChoose(string userShips[][8], string compShips[][8])
{
grid(userShips, compShips);
string one = "";
int onez = 0;
int two = 0;
for(int i = 1; i<=5; i++)
{
cout<<"Choose the position of ship #"<<i<<": ";
cin>>one;
onez = convert(one[0]);
two = atoi(&one[1]);
userShips[two-1][onez-1] = "1";
}
}
int compChoose(string userShips[][8], int userShipz)
{
int x = 1;
while(x == 1)
{
int r = (rand()%8)-1;
int r1 = (rand()%8)-1;
if(userShips[r][r1] == "1")
{
userShips[r][r1] = "H";
userShipz--;
x = 0;
}
if(userShips[r][r1] == "0")
{
userShips[r][r1] = "M";
x = 0;
}
}
return userShipz;
}
void play(string userShips[][8], string compShips[][8], int userShipz, int compShipz)
{
string one = "";
int onez = 0;
int two = 0;
int x = 1;
while(userShipz > 0 && compShipz > 0)
{
grid(userShips,compShips);
while(x == 1)
{
cout<<"Where would you like to hit: ";
cin>>one;
onez = convert(one[0]);
two = atoi(&one[1]);
onez--;
two--;
if(compShips[two][onez] == "1")
{
cout<<"HIT!\n";
compShips[two][onez] = "H";
compShipz--;
break;
}
if(compShips[two][onez] == "0")
{
cout<<"MISS!\n";
compShips[two][onez] = "M";
break;
}
}
userShipz = compChoose(userShips, userShipz);
}
if(userShipz == 0)
{
grid(userShips,compShips);
cout<<"HAHA YOU LOST!\n";
}
if(compShipz == 0)
{
grid(userShips,compShips);
cout<<"GRATZ YOU WON!\n";
}
}
int main()
{
srand(time(0));
int userShipz = 5;
int compShipz = 5;
string userShips[8][8] = {
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"}};
string compShips[8][8] = {
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"},
{"0", "0", "0", "0", "0", "0", "0", "0"}};
compShip(compShips);
playerChoose(userShips, compShips);
play(userShips,compShips, userShipz, compShipz);
}