【问题标题】:Calculating the distance between two random points in a C++ Battleship game [closed]计算 C++ 战舰游戏中两个随机点之间的距离 [关闭]
【发布时间】:2019-05-17 01:38:12
【问题描述】:

所以,我的问题是如何计算两个随机点 (x,y) 之间的距离,这样我就可以使用 abs 方法(就像我在 Function bool CheckColpo 中开始做的那样)并告诉玩家他的投篮是否实际上是在敌舰位置的范围内。你如何计算两个随机事物之间的距离?我应该如何处理这个?我知道我需要在找到两点之间的距离之后使用 abs 函数。我有一个朋友试图向我解释,但我想我太笨了。我是编程新手,C++ 是我学习的第一门语言,希望有人能指出方法。

这是我的代码:

#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;
}

非常感谢, 如果我以错误的方式发布这篇文章,我会立即采取行动纠正它。

【问题讨论】:

  • 嗯,首先你需要定义distance
  • 如果你想以程序员的身份去任何地方,你应该停止使用全局变量,而是学习如何将参数传递给函数。
  • 相关代码应该在问题中,而不是通过链接

标签: c++ arrays distance 2d-games


【解决方案1】:

首先 (x, y) 只是一个随机点。您的函数将需要两个点来计算它们之间的距离,无论点是否随机,计算距离的方式都是相同的。

距离你没有定义你的意思。您可能指的是毕达哥拉斯距离,但您不需要abs。也许您的意思只是垂直和水平距离差的总和,这称为曼哈顿距离,您需要abs,但不是您计算了距离之后。 p>

由于不清楚,这里有几个距离计算函数,选择你认为最好的一个

#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);
}

【讨论】:

  • 除了约翰的回答之外,如果您的唯一目的是检查某个点是否与某物有一定距离,则实际上不需要计算平方根。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 2014-05-06
相关资源
最近更新 更多