-5

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.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93

1 Answers1

4

Firstly (x, y) is only one random point. Your function is going to need two points to calculate the distance between them, and the way you calculate distance is the same whether points are random or not.

You don't define what you mean by distance. You could mean Pythagorean distance but you don't need abs for that. Maybe you mean just the sum of the differences in the vertical and horizontal distances, that's called the Manhatten distance and you would need abs for that, but not after you've calculated the distance.

Since it's not clear, here's a couple of distance calculating functions, pick whichever one you think is best

#include <cstdlib>
#include <cmath>

double pythagorean_distance(int x1, int y1, int x2, int y2)
{
    return sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
}

int manhatten_distance(int x1, int y1, int x2, int y2)
{
    return abs(x2 - x1) + abs(y2 - y1);
}
john
  • 85,011
  • 4
  • 57
  • 81
  • Adding to john's answer, you do not actually need to calculate the square root if your only intent is to check whether a point is at a certain distance to something. – Elvir Crncevic Dec 16 '18 at 14:01