C++ 在更新名称的单独数组时将新分数插入排序数组
C++ 在更新名称的单独数组时将新分数插入排序数组,c++,C++,score是一个由10个分数按升序排列的数组。 scoreName是与每个分数关联的名称数组。 我需要检查新分数是否足以进入高分表,如果是,请将其插入正确的位置,并确保scoreName也已更新以反映任何更改 我的当前代码有问题: //桌子 for (int i = 0; i < leaderBoardSize; i++) { cout << scoreName[i] << "\t\t" << score[i] &
score
是一个由10个分数按升序排列的数组。
scoreName
是与每个分数关联的名称数组。
我需要检查新分数是否足以进入高分表,如果是,请将其插入正确的位置,并确保scoreName
也已更新以反映任何更改
我的当前代码有问题:
//桌子
for (int i = 0; i < leaderBoardSize; i++)
{
cout << scoreName[i] << "\t\t" << score[i] << endl;
}
system("pause");
//Check to see if you made the highScore
if (diceTotal >= score[9])
{
cout << "Congrats, you have made the HighScore Table !\nEnter Your Name.";
cin >> playerName;
for (int i = 9; i < leaderBoardSize; i--)
{
if (diceTotal <= score[i])
{
scoreName[i = i + 1] = scoreName[i];
score[i = i + 1] = score[i];
scoreName[i] = playerName;
score[i] = diceTotal;
break;
}
scoreName[i = i + 1] = scoreName[i];
score[i = i + 1] = score[i];
}
}
Here is the entire code:
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int main()
{
//dice game variables
int dice1 = 0;
int dice2 = 0;
int diceTotal = 0;
int round = 0;
string choice;
bool isDone = true;
//Scoreboard
const int leaderBoardSize = 10;
int score[10] = { 40, 33, 29, 24, 22, 19, 15, 12, 11, 9 };
string scoreName[leaderBoardSize] = { "Jason", "Steve", "Bob", "Timberduck", "Eric", "Susan", "Tyler", "Nick", "NinjaDave", "RaidenGunfire" };
string playerName;
//random number seeder
srand((unsigned int)time(NULL));
//Game instructions
cout << "dice game\n---------\nCreated By: Darcy Tellier\n--------------------------\nInstructions:\nRoll 2 dices. Try to get as close to 50 without going over." << endl;
//The Game loop
do
{
//resets game variables
diceTotal = 0;
round = 1;
//in game match loop
do
{
// display round #, current dice total, ask user to quit or re-roll.
cout << "Round\n-----\n " << round << endl;
cout << "current total:" << diceTotal << endl;
cout << "Roll dice (y/n)?";
cin >> choice;
//Checks the users imput for invalid characters
while (choice != "Y" && choice != "y" && choice != "N" && choice != "n")
{
cout << "invalid option. Choose y/n:" << endl;
cin >> choice;
}
if (choice == "Y" || choice == "y")
{
//roll dice
round += 1;
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
diceTotal = diceTotal + dice1 + dice2;
cout << "you have rolled a " << dice1 << " and a " << dice2 << endl;
if (diceTotal > 50)
{
isDone = false;
}
}
else
{
//break was used because "isDone = false" does not work here. The debugger shows that when the variable is set to false, it still ignores it and skips to the next round.
break;
}
} while (isDone == true || diceTotal < 50);
//end of round
if (diceTotal > 50)
{
cout << "\nGameOver" << endl;
cout << "You went over in " << round << " turns. You Lose!!! " << endl;
}
else
{
cout << "You stopped at " << round << " turns. Final score: " << diceTotal << "." << endl;
system("pause");
system("cls");
}
//Table
for (int i = 0; i < leaderBoardSize; i++)
{
cout << scoreName[i] << "\t\t" << score[i] << endl;
}
system("pause");
//Check to see if you made the highScore
if (diceTotal >= score[9])
{
cout << "Congrats, you have made the HighScore Table !\nEnter Your Name.";
cin >> playerName;
for (int i = 9; i < leaderBoardSize; i--)
{
if (diceTotal <= score[i])
{
scoreName[i] = playerName;
score[i] = diceTotal;
break;
}
}
}
//board display #2
for (int i = 0; i < leaderBoardSize; i++)
{
cout << scoreName[i] << "\t\t" << score[i] << endl;
}
system("pause");
//do you want to play again?
cout << "Do you want to play again";
cin >> choice;
while (choice != "Y" && choice != "y" && choice != "N" && choice != "n")
{
cout << "invalid option. Choose y/n:" << endl;
cin >> choice;
}
if (choice == "Y" || choice == "y")
{
system("cls");
isDone = true;
}
else
{
cout << "game over" << endl;
isDone = false;
}
} while (isDone);
system("pause");
return 0;
}
for(int i=0;iscore
是一个int[10]
,但您有几个问题:
一,
二,
这是将i
赋值给一个新值,这也会破坏for
循环。您应该只执行以下操作
scoreName[i + 1] = scorename[i];
尝试以繁琐的方式交换数组中的所有值,而您尝试手动执行所有操作,因此我为您提供:
解决方案:使用标准库!
C++是一种伟大的语言,如果没有其他原因的话,那就是它包含了一个标准库:一个函数和类库,可以为您解决许多基本问题
使用标准容器排序、插入和删除元素要容易得多。让我们使用:
这样,你就可以继续玩你的游戏,获得玩家的名字和他们的分数。我不介意,只会为分数创造一个值:
auto score = 10123u;
// Check if this score beats any of the current high scores:
auto it = std::find_if(hiScores.begin(), hiScores.end(), [score](PlayerScore ps){ return score > ps.score; });
if (it != hiScores.end())
{
// Yes! We beat a score!
std::cout << "Found score: " << it->score << std::endl;
// Insert this score before the other score
hiScores.insert(it, {"NewScore", score});
// Remove the last score:
hiScores.pop_back();
}
自动评分=10123u;
//检查此分数是否超过任何当前高分:
autoit=std::find_if(hiScores.begin(),hiScores.end(),[score](PlayerScore-ps){return score>ps.score;});
if(it!=hiScores.end())
{
//是的!我们赢了!
标准::cout
如果是这样,请将其插入正确的位置,并确保scoreName也已更新
这是一个糟糕的设计。如果分数和名称需要保持在一起,则应将它们设置为单个实体。定义一个结构或类来保存名称和关联分数,然后将该实体的实例存储在单个数组中。您的实际问题是什么?没有人能猜出您期望的是什么,而仅仅是基于它实际得到的是什么一段声明性文字:)首先,我不认为scoreName[I=I+1]=scoreName[I];
做了你认为它做的事。我会玩弄这句话(以及类似的)看看它是如何工作的。你已经解释了你想做什么,并发布了一些代码。你真正的问题是什么?你发布的代码有问题吗?如果有,具体是什么问题?我所要做的就是找出我做错了什么。你做错的不是问一个具体的问题。:-)
scoreName[i = i + 1] = scoreName[i];
score[i = i + 1] = score[i];
scoreName[i + 1] = scorename[i];
// Simple class to handle a player score: a name and score
struct PlayerScore
{
std::string name;
unsigned int score;
};
// Create some test scores. I assume you have another means of storing high scores, perhaps in a file, but for this small purpose I am just hard-coding some scores:
std::vector<PlayerScore> hiScores = { {"ABC", 5000}, {"XJK", 10000}, {"FOO", 20000}, {"EGG", 4000}, {"HI", 50000} };
std::sort(hiScores.begin(), hiScores.end(), [](PlayerScore ps1, PlayerScore ps2){ return ps1.score > ps2.score; });
auto score = 10123u;
// Check if this score beats any of the current high scores:
auto it = std::find_if(hiScores.begin(), hiScores.end(), [score](PlayerScore ps){ return score > ps.score; });
if (it != hiScores.end())
{
// Yes! We beat a score!
std::cout << "Found score: " << it->score << std::endl;
// Insert this score before the other score
hiScores.insert(it, {"NewScore", score});
// Remove the last score:
hiScores.pop_back();
}