gpt4 book ai didi

c++ - 为什么 push_back 在 upcasted 对象的字符串成员中吐出一个 char?

转载 作者:行者123 更新时间:2023-11-28 06:07:33 24 4
gpt4 key购买 nike

我正在制作剪刀石头布游戏。包含3个类如下:
人类类:-用户分别输入他想玩的游戏数量和选择
计算机类:- 玩与人类相同数量的游戏,但总是玩“ROCK”。
RandomComputer 类:- 计算机类的派生类,但播放随机移动或选择。
当我将任何 2 个对象(人和计算机)传递给 Refree 类时,它会通过比较两个玩家的每个 Action 来做出决定。Refree Decision 方法在 (Human, Computer) 通过时非常有效,但在通过 (Human,RandomComputer) 时出现意外的奇怪输出。示例:- 用户输入:3 R P S
Refree(Human A, Computer B) 时的输出:Tie Win Lose

Refree(Human A, RandomComputer C) 时的输出:输 ' ' 赢

#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 引用或指针。

关于c++ - 为什么 push_back 在 upcasted 对象的字符串成员中吐出一个 char?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32093111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com