我正在做石头剪刀布游戏。包含3个类,如下所示:
人类类:-用户分别输入要玩的游戏数量和选择
电脑课:-玩与人类相同数量的游戏,但始终玩“ ROCK”。
RandomComputer类:-计算机类的派生类,但播放随机移动或选择。
当我通过任何2个对象(人类和计算机要释放类别)时,它都会通过比较两个玩家的每个动作来做出决定。
当(Human,Computer)传递但通过(Human,RandomComputer)传递了意外的奇怪输出时,Refree Decision方法是完美的。
示例:-用户输入:3 R P S
Refree(人A,计算机B)释放时的输出:Tie Win Lose
Refree(人类A,RandomComputer C)时的输出:Loss''Win
#ifndef _HUMAN_H
#define _HUMAN_H
#include <string>
#include <iostream>
using namespace std;
class Human{
public:
Human();
Human(int number, string game);
public:
int h_number;
string h_game;
};
#endif //_HUM
//=========HUMAN.CPP=====
#include "human.h"
Human::Human(int number, string game){
h_number = number;
h_game = game;
}
//========COMPUTER.h=====
#ifndef _COMPUTER_H
#define _COMPUTER_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Computer{
public:
Computer();
Computer(int number);
int get_c_number();
public:
string c_game;
int c_number;
};
#endif //_COMPUTER_H
//========COMPUTER.CPP=====
#include "computer.h"
Computer::Computer(){
}
Computer::Computer(int number){
c_number = number;
for(int i=0; i<c_number; i++)
{
c_game.push_back('R');
}
}
int Computer::get_c_number()
{
return c_number;
}
//=============RANDOMCOMPUTER.H========
#ifndef _RANDOMCOMPUTER_H
#define _RANDOMCOMPUTER_H
#include "computer.h"
#include <string>
#include <iostream>
#include <time.h>
#include <ctime>
#include <stdlib.h>
using namespace std;
class RandomComputer:public Computer{
public:
RandomComputer();
RandomComputer(int number);
private:
static const char r_games[3];
};
#endif //_RANDOMCOMPUTER_H
//===============RANDOMCOMPUTER.CPP=========
#include "randomcomputer.h"
const char RandomComputer::r_games[3]= {'R','P','S'};
RandomComputer::RandomComputer(){}
RandomComputer::RandomComputer(int number){
c_number = number;
srand ( time(NULL) );
for(int i=0; i<c_number; i++)
{
int RandIndex = rand() % 3;
c_game.push_back(r_games[RandIndex]);
}
}
//==============REFREE.H======
#ifndef _REFREE_H
#define _REFREE_H
#include "computer.h"
#include <iostream>
#include <string>
#include "human.h"
#include "randomcomputer.h"
using namespace std;
class Refree{
public:
Refree();
Refree (Human h_temp, Computer c_temp);
void decision();
Human *x;
Computer *y;
private:
int r_number;
string output;
};
#endif //_REFREE_H
//============REFREE.CPP========
#include "refree.h"
#include <iostream>
#include <string>
Refree::Refree(){}
Refree::Refree (Human h_temp, Computer c_temp){
x = &h_temp;
y = &c_temp;
r_number = x->h_number;
cout<<"HUMAN CHOICES: "<<x->h_game<<" COMPUTER CHOICES: "<<y->c_game<<endl;
}
void Refree::decision(){
for(int i =0; i<r_number; i++){
cout<<x->h_game[i]<<"-----------------------"<<y->c_game[i]<<endl;
}
for(int j =0; j<r_number; j++)
{
cout<<x->h_game[j]<<"---COMPARING WITH---"<<y->c_game[j]<<endl;
if(x->h_game[j] == 'R')
{
if(y->c_game[j] == 'P')
{
output.push_back('L');
output.push_back(' ');
}
else if(y->c_game[j]=='S')
{
output.push_back('W');
output.push_back(' ');
}
else if(y->c_game[j]=='R')
{
output.push_back('T');
output.push_back(' ');
}
}
else if(x->h_game[j] == 'P')
{
if(y->c_game[j] == 'P')
{
output.push_back('T');
output.push_back(' ');
}
else if(y->c_game[j]=='S')
{
output.push_back('L');
output.push_back(' ');
}
else if(y->c_game[j]=='R')
{
output.push_back('W');
output.push_back(' ');
}
}
else if(x->h_game[j] == 'S')
{
if(y->c_game[j] == 'P')
{
output.push_back('W');
output.push_back(' ');
}
else if(y->c_game[j]=='S')
{
output.push_back('T');
output.push_back(' ');
}
else if(y->c_game[j]=='R')
{
output.push_back('L');
output.push_back(' ');
}
}
}//forrr looop
size_t endpos = output.find_last_not_of(" \t");
if( string::npos != endpos )
{
output = output.substr( 0, endpos+1 );
}
cout<<output<<endl;
output.clear();
}
//============================MAIN.CPP====================
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <algorithm>
#include "human.h"
#include "computer.h"
#include "refree.h"
#include "randomcomputer.h"
using namespace std;
int main()
{
string user_input;
getline(cin, user_input);
//converting the numbers in the string to int.
int number_of_games = atoi(user_input.c_str());
//remove the digits and the spaces
user_input.erase(remove_if(user_input.begin(), user_input.end(), ::isdigit), user_input.end());
user_input.erase(remove_if(user_input.begin(), user_input.end(), ::isspace), user_input.end());
Human player_1(number_of_games, user_input); //HUMAN PLAYER OBJECT
Computer computer_1(number_of_games); // COMPUTER PLAYER TAKES ONLY innt number because it will always play ROCK in this class
Refree refree_1(player_1, computer_1);
refree_1.decision();
RandomComputer random_computer(number_of_games);
cout<<"HERE IS THE RANDOM CHOICES FROM COMPUTER AI: "<<random_computer.c_game<<endl; // make random choices instead of only ROCKS.
Refree refree_2(player_1, random_computer);
refree_2.decision();
return 0;
}
最佳答案
将人和计算机对象传递给裁判时,您正在复制它们:
Refree::Refree (Human h_temp, Computer c_temp){
您应该在此处使用const引用或指针。