So, my problem is how to calculate the distance between the two random points (x,y) so i can use the abs method (as i was starting to do in the Function bool CheckColpo) and tell the player if his shot was actually in range of the enemy ship's position. How can you calculate the distance between two things that'll be random? How should i approach this? i know that i need to use the abs function AFTER i found the distance between the two points. I had a friend that was trying to explain it to me, but i guess i'm too thickheaded. I'm new to programming and C++ is the first language i'm learning, i hope someone can point out the way.
Here's my code :
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
const int RIGHE = 10;
const int COLONNE = 10;
const char NAVE = 'N';
const char MARE = '~';
int x = -1;
int y = -1;
int choice1;
int choice2;
char board[RIGHE][COLONNE];
//Funzione per stampare la griglia 10x10 con il For//
void StampaGriglia() {
cout << "Here's the board : \n";
for (int i = 0; i < RIGHE; ++i)
{
for (int k = 0; k < COLONNE; ++k)
{
cout << board[i][k];
}
cout << endl;
}
}
void TurnoPlayer() {
do {
cout << "Enter the ship to sink :";
cin >> choice1;
cout << endl;
cout << "Enter your second ship :";
cin >> choice2;
if (choice1 == x && choice2 == y) // IN CASO AVESSI VOLUTO FARE CON IL CHAR NAVE : board[choice1][choice2] == nave;//
{
cout << "Boom, you sank it! ";
}
else
{
cout << "Retry! ";
}
cout << endl;
} while (choice1 < 0 || choice1 > RIGHE || choice2 < 0 || choice2 > COLONNE);
}
bool PlayAgain(string question)
{
char again;
cout << question;
cin >> again;
return again == 'y';
}
bool CheckColpo() {
x = abs
}
int main()
{
do {
//INSERISCI IN TUTTE LE CELLE UN VALORE BASE (MARE)//
for (int i = 0; i < RIGHE; ++i)
{
for (int k = 0; k < COLONNE; ++k)
{
board[i][k] = MARE;
}
}
srand(static_cast<unsigned int>(time(0)));
x = rand() % 10;
y = rand() % 10;
board[x][y] = NAVE;
cout << "x :" << x << "y: " << y;
do
{
StampaGriglia();
TurnoPlayer();
} while (choice1 != x || choice2 != y);
} while (PlayAgain("Vuoi Giocare Ancora? (y/n)"));
return 0;
}
Thanks a lot , and if i made this post in a wrong way i'll take action to correct it right away.