为了提高我的编码技能,我正在用c ++制作马里奥派对游戏。我希望它非常基本。它应该是一个双人游戏,允许每个用户从阵列中选择那里的角色。然后他们会掷骰子并尝试获得明星。它非常像棋盘游戏。获得在游戏开始时选择的一定数量的星星的人获胜。我的代码的问题是,当第一个人在那里掷骰子时,它会自动给他们赢得演讲,并询问他们是否想再次演出。
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <string>
using namespace std;
void ChooseAmountofStars(int& Starnumbers)
{
//Gets the number of stars that will be in the game
cout<<"How many stars do you want to search for, (1-3).\n";
cin>>Starnumbers;
cout<<"Great choice. The first player to retrieve "<<Starnumbers<<" Stars wins the game"<<endl;
}
int starplus(int Adder)
{
return Adder;
}
int PlaceStars()
{
//Place the star in a dice roll distance from the origin
srand(time(0));
int SpotNumber;
SpotNumber=(rand()%50)+1;
return SpotNumber;
}
int Winner(int Adder, int Starnumbers)
{
return starplus(Adder)==Starnumbers;
}
void coinplus(int& Adder)
{
int coin=0;
coin+=20;
if(coin>=100){
coin=0;
Adder+=1;
cout<<"You have "<<starplus(Adder)<<" stars"<<endl;
}
}
int WhoGoesFirst(){
srand(time(0));
int chance;
chance=(rand()%2)+1;
if(chance == 1){
return 1;}
else{
return 2;}
}
class Tools{
public:
void MiniGame(){
rematch:
string Choices[3]={"Rock","Paper","Scissors"};
string UserChoice;
cout<<"Type your choice of either Rock, Paper, or Scissors"<<endl;
cin>>UserChoice;
int RandomNum=rand()%3;
srand(time(0));
string ComputerChoice;
ComputerChoice=Choices[RandomNum];
cout<<"Challenger chose: "<<ComputerChoice<<"\n";
if(UserChoice=="rock"){
if(ComputerChoice=="Rock"){
cout<<"It Is a Tie\n\n";
goto rematch;}
else if(ComputerChoice=="Paper"){
cout<<"Sorry, You Lose. Better Luck Next Time\n\n";}
else{
cout<<"Nice Job. You Win\n\n";}
}
else if(UserChoice=="paper"){
if(ComputerChoice=="Paper"){
cout<<"It Is a Tie\n\n";
goto rematch;}
else if(ComputerChoice=="Scissors"){
cout<<"Sorry, You Lose. Better Luck Next Time\n\n";}
else{
cout<<"Nice Job. You Win\n\n";}
}
else if(UserChoice=="scissors"){
if(ComputerChoice=="Scissors"){
cout<<"It Is a Tie\n\n";
goto rematch;}
else if(ComputerChoice=="Rock"){
cout<<"Sorry, You Lose. Better Luck Next Time\n\n";}
else{
cout<<"Nice Job. You Win\n\n";}
}
else{
cout<<"Knock it off with the typos!!!\n\n";
goto rematch;
}
}
void Dice(string player, int Adder)
{
int face=0;
int SpotNumber=PlaceStars();
system("cls");
cout << endl <<"--------------DICE---------------" << endl;
srand((unsigned)time(0));
cout << "Rolling..." << endl;
Sleep(2000);
face = (rand()%6)+1;
int spot=0;
if (face == 1)
{
cout << " ___________" << endl;
cout << " | |" << endl;
cout << " | | " << endl;
cout << " | @ |" << endl;
cout << " | |" << endl;
cout << " |_________|" << endl;
spot+=1;
if(spot>=50){
spot-=spot;
cout<<player<<" landed on "<<spot<<endl;
}
else if(spot==9||spot==17||spot==32||spot==35||spot==47||spot==42||spot==25||spot==49){
cout<<player<<" landed on "<<spot<<endl;
coinplus(Adder);
}
else if(spot==SpotNumber){
cout<<player<<" landed on "<<spot<<endl;
Adder+=1;
cout<<"You have "<<starplus(Adder)<<" stars"<<endl;
}
else{
cout<<player<<" landed on "<<spot<<endl;
}
}
else if (face == 2)
{
cout << " ___________" << endl;
cout << " | |" << endl;
cout << " | @ | " << endl;
cout << " | |" << endl;
cout << " | @ |" << endl;
cout << " |_________|" << endl;
spot+=2;
if(spot>=50){
spot-=spot;
cout<<player<<" landed on "<<spot<<endl;
}
else if(spot==9||spot==17||spot==32||spot==35||spot==47||spot==42||spot==25||spot==49){
cout<<player<<" landed on "<<spot<<endl;
coinplus(Adder);
}
else if(spot==SpotNumber){
cout<<player<<" landed on "<<spot<<endl;
Adder+=1;
cout<<"You have "<<starplus(Adder)<<" stars"<<endl;
}
else{
cout<<player<<" landed on "<<spot<<endl;
}
}
else if (face == 3)
{
cout << " ___________" << endl;
cout << " | |" << endl;
cout << " | @ | " << endl;
cout << " | @ |" << endl;
cout << " | @ |" << endl;
cout << " |_________|" << endl;
spot+=3;
if(spot>=50){
spot-=spot;
cout<<player<<" landed on "<<spot<<endl;
}
else if(spot==9||spot==17||spot==32||spot==35||spot==47||spot==42||spot==25||spot==49){
cout<<player<<" landed on "<<spot<<endl;
coinplus(Adder);
}
else if(spot==SpotNumber){
cout<<player<<" landed on "<<spot<<endl;
Adder+=1;
cout<<"You have "<<starplus(Adder)<<" stars"<<endl;
}
else{
cout<<player<<" landed on "<<spot<<endl;
}
}
else if (face == 4)
{
cout << " ___________" << endl;
cout << " | |" << endl;
cout << " | @ @ |" << endl;
cout << " | |" << endl;
cout << " | @ @ |" << endl;
cout << " |_________|" << endl;
spot+=4;
if(spot>=50){
spot-=spot;
cout<<player<<" landed on "<<spot<<endl;
}
else if(spot==9||spot==17||spot==32||spot==35||spot==47||spot==42||spot==25||spot==49){
cout<<player<<" landed on "<<spot<<endl;
coinplus(Adder);
}
else if(spot==SpotNumber){
cout<<player<<" landed on "<<spot<<endl;
Adder+=1;
cout<<"You have "<<starplus(Adder)<<" stars"<<endl;
}
else{
cout<<player<<" landed on "<<spot<<endl;
}
}
else if (face == 5)
{
cout << " ___________" << endl;
cout << " | |" << endl;
cout << " | @ @ | " << endl;
cout << " | @ |" << endl;
cout << " | @ @ |" << endl;
cout << " |_________|" << endl;
spot+=5;
if(spot>=50){
spot-=spot;
cout<<player<<" landed on "<<spot<<endl;
}
else if(spot==9||spot==17||spot==32||spot==35||spot==47||spot==42||spot==25||spot==49){
cout<<player<<" landed on "<<spot<<endl;
coinplus(Adder);
}
else if(spot==SpotNumber){
cout<<player<<" landed on "<<spot<<endl;
Adder+=1;
cout<<"You have "<<starplus(Adder)<<" stars"<<endl;
}
else{
cout<<player<<" landed on "<<spot<<endl;
}
}
else if (face == 6)
{
cout << " ___________" << endl;
cout << " | |" << endl;
cout << " | @ @ | " << endl;
cout << " | @ @ |" << endl;
cout << " | @ @ |" << endl;
cout << " |_________|" << endl;
spot+=6;
if(spot>=50){
spot-=spot;
cout<<player<<" landed on "<<spot<<endl;
}
else if(spot==9||spot==17||spot==32||spot==35||spot==47||spot==42||spot==25||spot==49){
cout<<player<<" landed on "<<spot<<endl;
coinplus(Adder);
}
else if(spot==SpotNumber){
cout<<player<<" landed on "<<spot<<endl;
Adder+=1;
cout<<"You have "<<starplus(Adder)<<" stars"<<endl;
}
else{
cout<<player<<" landed on "<<spot<<endl;
}
}
}
};
int main()
{
int Score1=0;
//this will be used in parameter of dice for star count of plyr 1
int Score2=0;
//this will be used in parameter of dice for star count of plyr 2
Tools ToolsObject;
beginning:
//If user would like to play again, they will start back at beginning
cout<<"Plyr 1: Choose character from character list below\n";
string ChaList[9]={"Mario ", "Luigi ","Peach ","Daisy ","Wario ","Waluigi ","Bowser ","Toad ","Yoshi"};
for(int i=0; i<9; i++){
cout<<ChaList[i];
}
cout<<endl;
//Displays player options for user1
string Plyr1;
cin>>Plyr1;
cout<<"You chose "<<Plyr1<<endl;
//Allows user to choose character and saves it in variable called Plyr1
cout<<"Plyr 2: Choose character from character list below\n";
for(int i=0; i<9; i++){
cout<<ChaList[i];
}
cout<<endl;
//Displays player options for user2
string Plyr2;
cin>>Plyr2;
cout<<"You chose "<<Plyr2<<endl;
//Allows user to choose character and saves it in variable called Plyr2
int StarNums=0;
int StarNums2=0;
ChooseAmountofStars(StarNums);
//sets amount of stars to be found in order to win game. Parameter changes values the variable globally. it is set by reference.
int position=PlaceStars();
//Puts position of star from PlaceStars function into a variable that is local to main
cout<<"The star is placed at spot: "<<position<<endl;
int turn;
turn = WhoGoesFirst();
//Put the turn from WhoGoesFirst function into a variable that is local to main
bool GameIsPlaying=true;
while(GameIsPlaying==true){
if(turn==1){
ToolsObject.Dice(Plyr1, Score1);
if(Winner(Score1,StarNums)){
cout<<"Hurray you win"<<endl;
GameIsPlaying=false;
}
else{
turn=2;
}
}
else{
ToolsObject.Dice(Plyr2, Score2);
if(Winner(Score2,StarNums2)){
cout<<"Hurray you win"<<endl;
GameIsPlaying=false;}
else{
turn=1;
}
}
}
string desision;
while(desision!="y" || desision!="n" ){
cout<<"Would you like to play again? (y or n)"<<endl;
cin>>desision;
if(desision=="y"){
cout<<"Get ready"<<endl;
goto beginning;
}
else if(desision=="n"){
cout<<"Enjoy the rest of the day"<<endl;
}
else{
cout<<"Not a valid choice"<<endl;
}
}
}
答案 0 :(得分:2)
这里有很多问题,其中一个是你期待这个功能
void Dice(string player, int Adder)
更改您在此处传递给它的Adder
的值
ToolsObject.Dice(Plyr1, Score1);
当您通过值传递时,这不会改变Score1
。如果您希望它改变参数,您应该实际返回一个值或使用引用或指针参数。
因此,当您的代码到达此行时
if(Winner(Score1,StarNums)){
cout<<"Hurray you win"<<endl;
GameIsPlaying=false;
}
总是满足if
条件,因为您将2个变量传递给Winner
函数,当它们相等时返回true。正如你已将它们初始化为零而你的其他功能实际上并没有实际修改你
if (0 == 0)
总是如此。
如果您自学了正确使用调试器,您将能够比在此处发布问题所需的时间更快地识别此类问题。