摇滚,纸,剪刀程序游戏

时间:2019-12-28 10:48:04

标签: c++

所以我最近做了一个Rock-Paper-Scissors程序,但是我想尝试一种不同的方法来使代码更方便。因此,我认为使用一个函数来制作所有IF,否则将产生IF结果将是一个好主意。 (即,如果玩家选择岩石,而计算机选择纸张,则计算机获胜。)

但是,问题是,当我尝试对其进行编码时,程序显示“警告:控件可能会到达非空函数[-Wreturn-type]的末尾”。现在,从我从网络上收集的信息来看,这意味着一旦它们返回到我创建的另一个函数,它们就没有办法让我返回int main()函数。我以为我已经解决了,但是。有人可以看一下并告诉我我在做什么错吗?

代码在这里:

#include <bits/stdc++.h>
#include <time.h> 
#include <string.h>
#include <stdlib.h> 
#include <stdio.h> 
using namespace std;


int Player_Wins;
int com_wins;

string computer_choices [3] = {"Rock","Paper","Scissors"};
string Player_Choices [3] = {"Rock","Paper","Scissors"};

bool Game_Over =  false; 


int GameOutCome(int a,string b)
{

        int Player_Choice;
        string Computer_Choice;

        srand(time(0));

        string Picked_Choice = computer_choices[rand() % 3 ];

        string Player_Picked_Choice;

        a=Player_Choice;
        b=Picked_Choice;


        if (Player_Choice == 1)
        {
            Player_Picked_Choice= Player_Choices[0];

            if (Player_Picked_Choice== Player_Choices[0] and Picked_Choice==computer_choices [1])
            {

                cout<< "You have chosen " << Player_Picked_Choice <<endl;

                cout<<"\n"<<endl;

                cout<< "The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "The computer wins the round."<<endl;

                return com_wins++;

                cout<<"\n"<<endl;


                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                com_wins<<endl;     

                cout<<"\n"<<endl;

                if (Player_Wins ==3|| com_wins ==3)
                    {

                      Game_Over= true; 

                        if(Game_Over == true)
                           {

                            cout<<"The game is over!!!"<<endl;

                           }
                     }
              }
            }   
            else if (Player_Picked_Choice== Player_Choices[0] and 
                     Picked_Choice==computer_choices[0])
            {

              cout<<"You have chosen "<< Player_Picked_Choice <<endl;

              cout<<"\n"<<endl;

              cout<<" The Computer have chosen "<< Picked_Choice<<endl;

              cout<<"\n"<<endl;

              cout <<"It's a tie."<<endl;

            }
            else if (Player_Picked_Choice== Player_Choices[0] and 
                     Picked_Choice==computer_choices[2])
                {

                     cout<< "You have chosen "<< Player_Picked_Choice <<endl;

                     cout<< " The Computer have chosen"<< Picked_Choice <<endl;

                     cout<<"You won the round."<<endl;

                     return Player_Wins++;

                     cout<<"\n"<<endl;

                     cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                     com_wins<<endl;    

                     cout<<"\n"<<endl;

                     if (Player_Wins ==3|| com_wins ==3)
                          {

                             Game_Over= true; 

                             if(Game_Over == true)
                                 {

                                    cout<<"The game is over!!!"<<endl;

                                 }

                          } 
                   }
                  }

        else if (Player_Choice == 2)
        {

            Player_Picked_Choice= Player_Choices[1];


            if (Player_Picked_Choice== Player_Choices[1] and Picked_Choice==computer_choices [2])
            {

                cout<< "You have chosen "<< Player_Picked_Choice << " The Computer have chosen "<< 
                Picked_Choice <<endl;

                cout<< "The computer wins the round." <<endl;

                return com_wins++;

                cout<<"\n"<<endl;

                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                com_wins<<endl;     

                cout<<"\n"<<endl;

                if (Player_Wins ==3|| com_wins ==3)
                      {

                         Game_Over= true; 

                         if(Game_Over == true)
                             {

                              cout<<"The game is over!!!"<<endl;
                              return 0;
                             }

                       }
            }   
            else if (Player_Picked_Choice== Player_Choices[1] and 
            Picked_Choice==computer_choices[1])
            {

                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

                 cout<<"\n"<<endl;

                 cout<< "The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" It's a tie."<<endl;

                 cout<<"\n"<<endl;

                 cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                 com_wins<<endl;    
            }
            else if (Player_Picked_Choice== Player_Choices[1] and 
                     Picked_Choice==computer_choices[0])
                       {


                         cout<< "You have chosen "<< Player_Picked_Choice <<endl;

                         cout<<"\n"<<endl;

                         cout<< " The Computer have chosen "<< Picked_Choice <<endl;

                         cout<<" You won the round."<<endl;

                         return Player_Wins++;

                         cout<<"\n"<<endl;

                         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                         com_wins<<endl;

                         cout<<"\n"<<endl;

                         if (Player_Wins ==3|| com_wins ==3)
                         {

                             Game_Over= true; 

                             if(Game_Over == true)
                             {

                                cout<<"The game is over!!!"<<endl;

                                 return 0;
                             }
                          } 
                        }
                     }
        else if (Player_Choice == 3)
        {
            Player_Picked_Choice= Player_Choices[2];


            if (Player_Picked_Choice== Player_Choices[2] and Picked_Choice==computer_choices [0])
            {

                cout<< "You have chosen "<< Player_Picked_Choice<<endl;

                cout<<"\n"<<endl;

                cout<<" The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "  The computer wins the round."<<endl;

                return com_wins++;

                cout<<"\n"<<endl;

                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                com_wins<<endl; 

                cout<<"\n"<<endl;


        if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;

            return 0;


          }

        }

            }   
            else if (Player_Picked_Choice== Player_Choices[2] and 
                     Picked_Choice==computer_choices[2])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl; 

                 cout<<"\n"<<endl;

                 cout<<" The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" It's a tie."<<endl;

                 cout<<"\n"<<endl;

                 cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                 com_wins<<endl;

                 return a;  

            }
            else if (Player_Picked_Choice== Player_Choices[2] and 
                     Picked_Choice==computer_choices[1])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

                 cout<<"\n"<<endl;

                 cout<< " The Computer have chosen \n"<< Picked_Choice <<endl;

                 cout<<" You won the round."<<endl;

                 return Player_Wins++;

                 cout<<"\n"<<endl;

                 cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< 
                 com_wins<<endl;    

                 cout<<"\n"<<endl;

             }


    }

 }


int main()
{

  cout<<"Welcome to Rock, Paper Scissors!!"<<endl;

    while (Player_Wins !=3 || com_wins != 3)
    {
        int Player_Choice;

        cout<<"\n"<<endl;

        cout<<"First to three wins. \n To pick rock, press 1\n To pick paper, press 2 \n To pick 
        scissors, Press 3"<<endl;

        cout<<"\n"<<endl;

        cout<<"Please make a choice: ";cin>>Player_Choice;

        string Picked_Choice = computer_choices[rand() % 3 ];

        cout<<"\n"<<endl;

       GameOutCome(Player_Choice,Picked_Choice);


        if (Player_Wins ==3|| com_wins ==3)
             {

               Game_Over= true; 

                 if(Game_Over == true)
                      {

                        cout<<"The game is over!!!"<<endl;


                      }

              }

        }            


      if (Player_Wins==3)
      {
        cout<<" The Player Wins!!"<< endl;  

      }
      else if (com_wins==3)
      {

        cout<< "The Computer Wins!!"<<endl;


      }

  return 0;    
} 

编辑:19年12月29日更新

好的,所以我使代码工作到可以给您选择的地方,仅此而已。我还有什么问题呢?

#include <bits/stdc++.h>
#include <time.h> 
#include <string.h>
#include <stdlib.h> 
#include <stdio.h> 
using namespace std;


int Player_Wins;
int com_wins;

string computer_choices [3] = {"Rock","Paper","Scissors"};
string Player_Choices [3] = {"Rock","Paper","Scissors"};

bool Game_Over =  false; 


int GameOutCome(int a,string b)
{

        int Player_Choice;


        srand(time(0));

        string Picked_Choice = computer_choices[rand() % 3 ];

        string Player_Picked_Choice;

        a=Player_Choice;
    b=Picked_Choice;


        if (Player_Choice == 1)
        {
            Player_Picked_Choice= Player_Choices[0];

            if (Player_Picked_Choice== Player_Choices[0] and Picked_Choice==computer_choices [1])
            {

                cout<< "You have chosen " << Player_Picked_Choice <<endl;

        cout<<"\n"<<endl;

        cout<< "The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "The computer wins the round."<<endl;


        cout<<"\n"<<endl;

        cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;       

        cout<<"\n"<<endl;

        return com_wins++;

        if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;

            return 0;


          }

          return a;

        }
                return a;
            }   
            else if (Player_Picked_Choice== Player_Choices[0] and Picked_Choice==computer_choices[0])
            {

            cout<<"You have chosen "<< Player_Picked_Choice <<endl;

      cout<<"\n"<<endl;

      cout<<" The Computer have chosen "<< Picked_Choice<<endl;

      cout<<"\n"<<endl;

            cout <<"It's a tie."<<endl;

      return 0;

            }
            else if (Player_Picked_Choice== Player_Choices[0] and Picked_Choice==computer_choices[2])
                {

                     cout<< "You have chosen "<< Player_Picked_Choice <<endl;

           cout<< " The Computer have chosen"<< Picked_Choice <<endl;

                     cout<<"You won the round."<<endl;

           cout<<"\n"<<endl;

                  cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl; 

          cout<<"\n"<<endl;

          return Player_Wins++;

          if (Player_Wins ==3|| com_wins ==3)
          {

            Game_Over= true; 

            if(Game_Over == true)
            {

              cout<<"The game is over!!!"<<endl;

              return 0;



            }

            return 0;

          } 

          return 0;
                }

     return 0;
      }





        else if (Player_Choice == 2)
        {

            Player_Picked_Choice= Player_Choices[1];


            if (Player_Picked_Choice== Player_Choices[1] and Picked_Choice==computer_choices [2])
            {

                cout<< "You have chosen "<< Player_Picked_Choice << " The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "The computer wins the round." <<endl;

        cout<<"\n"<<endl;

                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;       

        cout<<"\n"<<endl;

        return com_wins++;

        if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;
            return 0;


          }
          return 0;

        }

        return 0;
            }   
            else if (Player_Picked_Choice== Player_Choices[1] and Picked_Choice==computer_choices[1])
            {

                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

         cout<<"\n"<<endl;

         cout<< "The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" It's a tie."<<endl;

         cout<<"\n"<<endl;

         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;  

         return a;
            }
            else if (Player_Picked_Choice== Player_Choices[1] and Picked_Choice==computer_choices[0])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

         cout<<"\n"<<endl;

         cout<< " The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" You won the round."<<endl;

         cout<<"\n"<<endl;

         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;

         cout<<"\n"<<endl;

         return Player_Wins++;

         if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;

            return 0;


          }

        return 0;

        }   

                return 0;

            }
         return 0;
        }
        else if (Player_Choice == 3)
        {
            Player_Picked_Choice= Player_Choices[2];

            return a;

            if (Player_Picked_Choice== Player_Choices[2] and Picked_Choice==computer_choices [0])
            {

                cout<< "You have chosen "<< Player_Picked_Choice<<endl;

        cout<<"\n"<<endl;

        cout<<" The Computer have chosen "<< Picked_Choice <<endl;

                cout<< "  The computer wins the round."<<endl;


        cout<<"\n"<<endl;

                cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;   

        cout<<"\n"<<endl;

        return com_wins++;


        if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;

            return 0;


          }

          return 0;

        }
            return 0;   
            }   
            else if (Player_Picked_Choice== Player_Choices[2] and Picked_Choice==computer_choices[2])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl; 

         cout<<"\n"<<endl;

         cout<<" The Computer have chosen "<< Picked_Choice <<endl;

                 cout<<" It's a tie."<<endl;

         cout<<"\n"<<endl;

         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;

         return 0;  

            }
            else if (Player_Picked_Choice== Player_Choices[2] and Picked_Choice==computer_choices[1])
            {


                 cout<< "You have chosen "<< Player_Picked_Choice <<endl;

         cout<<"\n"<<endl;

         cout<< " The Computer have chosen \n"<< Picked_Choice <<endl;

                 cout<<" You won the round."<<endl;

         cout<<"\n"<<endl;

         cout<<"The score is: \n Player: "<< Player_Wins << "\n Computer : "<< com_wins<<endl;  

        cout<<"\n"<<endl;

        return Player_Wins++;

      }
      return 0;    
    }
return 0;
}


int main()
{

  cout<<"Welcome to Rock, Paper Scissors!!"<<endl;

    while (Player_Wins !=3 || com_wins != 3)
    {
        int Player_Choice;

        cout<<"\n"<<endl;

        cout<<"First to three wins. \n To pick rock, press 1\n To pick paper, press 2 \n To pick scissors, Press 3"<<endl;

    cout<<"\n"<<endl;

        cout<<"Please make a choice: ";cin>>Player_Choice;

    string Picked_Choice = computer_choices[rand() % 3 ];

    cout<<"\n"<<endl;

    GameOutCome(Player_Choice,Picked_Choice);


      if (Player_Wins ==3|| com_wins ==3)
        {

          Game_Over= true; 

          if(Game_Over == true)
          {

            cout<<"The game is over!!!"<<endl;


          }

        }

  }          








      if (Player_Wins==3)
      {
        cout<<" The Player Wins!!"<< endl;  

      }
      else if (com_wins==3)
      {

        cout<< "The Computer Wins!!"<<endl;


      }

  return 0;    
} 

3 个答案:

答案 0 :(得分:2)

即使主要问题很简单,我也想在此处添加一些说明:

没有退货单时会发生什么?

  

现在,从我从网络上收集的信息来看,这意味着一旦它们返回到我创建的另一个函数,他们就没有办法让我返回int main()函数。

错了。一旦到达函数主体的末尾,您的函数将始终返回。如果您错过分支中的return语句,您的代码不会突然停止执行,这样您的代码就不会“卡住”。

可以发生的事情是不确定的行为,当您尝试读取并非所有路径都返回某些东西的函数的返回值时。如果您从不读取返回值,那么一切都很好。

返回后代码会发生什么?

您有时在return之后的 之后得到代码。此代码将永远不会执行,并且您的编译器也应对此发出警告。

可能的解决方案

  1. 检查您的代码,并确保每个可能的分支中都有一个return

  2. 您甚至不需要在这里返回任何内容,因为您从不使用返回值。因此您的函数可以返回void

答案 1 :(得分:1)

using namespace std不是一个好习惯,您应该避免这种情况。  变量的命名不是最佳的,尽管名称本身也不错,因为它告诉您变量的内容(函数中的ab除外),您通常以大写字母(例如Picked_ChoicePlayer_Choice等)-不符合正常的c ++命名约定。  为什么要使用两个单独的数组来保存计算机和播放器的选项,仅在确实需要一个数组来保存可能的选项的字符串时才使用一个数组。 您让计算机选择两次,一次在主循环中,然后再次在GameOutcome函数中。  您的输入没有健全性检查,玩家可以输入随机垃圾,而不是1-3。

GameOutCome函数的结构非常混乱。 首先,尝试始终初始化变量,所以不要int Player_Choice来进行int player_choice = 0。那么srand(time(0))的目的是什么? a = Player_Choice行覆盖了您的函数参数(实际上为null),这就是为什么您没有任何有用的返回值的原因。

我建议仅使用整数,而不需要从字符串数组中获取数字。

然后有类似

的内容
Game_Over= true; 
if(Game_Over == true)
{
  cout<<"The game is over!!!"<<endl;
  return 0;        
}

出于显而易见的原因,这没有多大意义。 代码太复杂了,请尝试简化它。 俗话说:“ 两周的编码可以轻松为您节省一小时的规划。”。

因此,解决问题的一种简便方法是定义一个2d数组,该数组已包含单个游戏的所有可能结果。

int outcome[3][3] =     {
                        {0, 1, 2}, 
                        {2, 0, 1}, 
                        {1, 2, 0}
                        } ;
int result = outcome[player_choice][computer_choice];
if (result == 0){
    std::cout << "we have draw\n";
} else if (result == 1){
    std::cout << "you win\n";
} else {
    std::cout << "you lost\n";
}

一个玩家的选择定义x,而另一个玩家的选择定义y。这简化了整个过程。

答案 2 :(得分:0)

问题出在 GameOutCome 功能中。您的函数是非空函数,但是您不能确保向编译器返回一定的返回值。换句话说,编译器说在某些特殊情况下,如果到达if-else分支,则函数不会返回值。

此外,我建议您避免代码中死代码(永远不会执行)。在这种情况下,返回后的所有代码。