我正在尝试用 C++ 编写一个面向对象的 501 飞镖游戏,我已经设置了所有功能,但是当我运行该程序时,尽管没有错误消息,但它不显示玩家的分数。显然我编写了一个逻辑错误,但我正在努力调试它。
Player类头文件:
#pragma once
#include <string>
using namespace std;
class player
{
private:
string playername;
int hitrate;
int score;
bool won;
public:
void setup_playername(string newplayername)
{
playername = newplayername;
}
void setup_hitrate(int newhitrate)
{
hitrate = newhitrate;
}
void setup_score(int newscore)
{
score = newscore;
}
void setup_won(int newwon)
{
won = newwon;
}
};
C++ 文件代码:
#include <iostream>
#include <cstdlib>
#include <time.h>
#include "Player.h"
using namespace std;
int create_player()
{
int joerate = 71, sidrate = 73;
int joescore = 501, sidscore = 501;
bool joewon = false, sidwon = false;
player joe;
joe.setup_playername("Joe");
joe.setup_hitrate(joerate);
joe.setup_score(joescore);
joe.setup_won(joewon);
player sid;
sid.setup_playername("Sid");
sid.setup_hitrate(sidrate);
sid.setup_score(sidscore);
sid.setup_won(sidwon);
return joerate, sidrate, joescore, sidscore, joewon, sidwon;
}
string decide_stratagey(int joescore, int sidscore)
{
string jstrat = "";
if (joescore > 170)
{
jstrat = "throwtreble20";
}
else if (joescore > 131)
{
jstrat = "throw treble";
}
else if (joescore > 41)
{
jstrat = "throw double";
}
else if (joescore == 2)
{
jstrat = "throw single";
}
else if (joescore == 1)
{
jstrat = "bullseye";
}
return jstrat;
}
int calcreqdouble(string jstrat, int joescore)
{
int reqdbl = joescore/2;
return reqdbl;
}
int throw_bull(int p) {
// Throw for the bull with accuracy p% (20<p<85)
int r = rand() % 100;
if (r < (p - 20))
return 50;
else if (r < 85)
return 25;
else
return 1 + rand() % 20;
}
int throw_treble(int d, int p) {
// return result of throwing for treble d with accuracy p% (o<90)
// Board neighbours ignoring slot zero
int bd[2][21] = { {0,20,15,17,18,12,13,19,16,14,6,8,9,4,11,10,7,2,1,3,5},
{0,18,17,19,13,20,10,16,11,12,15,14,5,6,9,2,8,3,4,7,1} };
int r = rand() % 100;
if (r < p)
return 3 * d;
else if (r < 90)
return d;
else if (r < 93)
return 3 * bd[0][d];
else if (r < 96)
return 3 * bd[1][d];
else if (r < 98)
return bd[0][d];
else
return bd[1][d];
}
int throw_double(int d) {
// return result of throwing for double d with accuracy 80%
// Board neighbours ignoring slot zero
int bd[2][21] = { {0,20,15,17,18,12,13,19,16,14,6,8,9,4,11,10,7,2,1,3,5},
{0,18,17,19,13,20,10,16,11,12,15,14,5,6,9,2,8,3,4,7,1} };
int r = rand() % 100;
if (r < 80)
return 2 * d;
else if (r < 85)
return 0;
else if (r < 90)
return d;
else if (r < 93)
return 2 * bd[0][d];
else if (r < 96)
return 2 * bd[1][d];
else if (r < 98)
return bd[0][d];
else
return bd[1][d];
}
int throw_single(int d) {
// return result of throwing for single d with accuracy 88% (or 80% for the outer)
// Board neighbours ignoring slot zero
int bd[2][21] = { {0,20,15,17,18,12,13,19,16,14,6,8,9,4,11,10,7,2,1,3,5},
{0,18,17,19,13,20,10,16,11,12,15,14,5,6,9,2,8,3,4,7,1} };
int r = rand() % 100;
if (d == 25) { // outer 80%
if (r < 80)
return 25;
else if (r < 90)
return 50;
else
return 1 + rand() % 20;
}
else // 1 to 20 single
if (r < 88)
return d;
else if (r < 92)
return bd[0][d];
else if (r < 96)
return bd[1][d];
else if (r < 98)
return 3 * d;
else
return 2 * d;
}
void dartThrowLoop(string jstrat, int joescore,int joerate, int reqdbl, bool joewon)
{
int randThrow = rand() %(21) + 10;
while (joewon == false)
{
for (int i = 0; i < 3; i++)
{
int randThrow = rand() % (21) + 10;
if (jstrat == "throwtreble20")
{
joescore = joescore - throw_treble(20, joerate);
}
else if (jstrat == "throw treble")
{
joescore = joescore - throw_treble(randThrow, joerate);
}
else if (jstrat == "throw single")
{
joescore = joescore - throw_single(randThrow);
}
else if (jstrat == "bullseye")
{
joescore = joescore - throw_bull(randThrow);
}
}
}
}
int main(string jstrat, int joescore, int joerate, int reqdbl, bool joewon,int sidscore, int sidrate, bool sidwon, int randThrow)
{
decide_stratagey(int(joescore), int(sidscore));
calcreqdouble(string(jstrat), int(joescore));
throw_bull(int(joerate));
throw_treble(int(randThrow), int(joerate));
throw_double(int(randThrow));
throw_single(int(randThrow));
dartThrowLoop(string(jstrat), int(joescore), int(joerate), int(reqdbl), bool(joewon));
cout << "score is" << joescore;
return 0;
}
我尝试在代码中添加断点来找出问题所在,但我没有找到问题所在。我尝试过手动向程序插入固定分数,看看这是否是我的飞镖投掷的问题,但这也不起作用。
main() 是一个特殊函数。你不能像这样简单地添加参数。
它总是这样
int main(int argc, char *argv[])
{
return 0;
}
要从命令行将参数传递给 main,您需要迭代数组 argv,其中包含 argc 条目数。