ROCK PAPER SCISSORS ERRORS

I need to create a rock paper scissors game for a uni course that has the following problem.


"We want you to implement a simple Rock, Paper, Scissors game in C++. Two players compete and,independently, choose one of Rock, Paper, or Scissors. They then simultaneously declare their choices. The winner of the game is determined by comparing the choices of the players. Rock beats Scissors, Scissors beats Paper, Paper beats Rock.


Your task is to produce a set of classes that will allow a human player, typing instructions from the keyboard, to interact with a computer player. An independent referee class will decide whether the human or computer has lost. When you design this code you must have, at least, a human player class, a computer player class, and a referee class. You probably want to use a game controller class of some sort as well but the details are left up to you.


The human player’s strategy is read from the input. The input consists of one line. The line starts with a positive integer k, then followed by k moves (all separated by spaces). A move is a character among R, P, and S. For example, if the input is “3 S P R”, then it means that the human player plays three times: the moves are scissors, paper, and rock.


Your referee class will match two players and return their competition results. For this prac, your referee class needs to match the dumb computer player (who only plays rock) and the human player.


Your program should output the competition results between the human player and the dumb computer player. For the sample input “3 S P R”, the output should be “L W T” (L = human loses; W = human wins; T = tie). To give you another example, for input “4 R R P S”, the output should be “T T W L”."


I get the following compiler errors when I try to compile:

human.cpp: In member function ‘void Human::setNumGames()’:
human.cpp:25: error: ‘stoi’ was not declared in this scope
computer.cpp: In constructor ‘Computer::Computer(int)’:
computer.cpp:10: error: cannot convert ‘std::string’ to ‘char’ in assignment
referee.cpp: In member function ‘void Referee::play()’:
referee.cpp:15: error: ‘Human’ has not been declared
referee.cpp:21: error: ‘Human’ has not been declared
referee.cpp:27: error: ‘Human’ has not been declared
In file included from rpsmain.cpp:2:
human.cpp: In member function ‘void Human::setNumGames()’:
human.cpp:25: error: ‘stoi’ was not declared in this scope
In file included from rpsmain.cpp:3:
computer.cpp: In constructor ‘Computer::Computer(int)’:
computer.cpp:10: error: cannot convert ‘std::string’ to ‘char’ in assignment
In file included from rpsmain.cpp:4:
human.h: In member function ‘void Referee::play()’:
human.h:24: error: object missing in reference to ‘Human::hMove’
referee.cpp:15: error: from this location
human.h:24: error: object missing in reference to ‘Human::hMove’
referee.cpp:21: error: from this location
human.h:24: error: object missing in reference to ‘Human::hMove’
referee.cpp:27: error: from this location

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
 referee.h



#ifndef _REFEREE_H
#define _REFEREE_H

#include <string>
#include <iostream>

class Referee
{   
    public:
        Referee(int gamesNeededFor);

        int getNumGames();
        void setNumGames();

        void play();
        void results();

    //private:
        int numGames;
        std::string winner;
        std::string output;

};

#endif


computer.h



#ifndef _COMPUTER_H
#define _COMPUTER_H

#include "referee.h"

#include <string>
#include <iostream>

class Computer
{   
    friend class Referee;
    public:
        Computer(int games);

        std::string getCMove();

    private:
        std::string cMove;
};

#endif


human.h



#ifndef _HUMAN_H
#define _HUMAN_H

#include "referee.h"

#include <string>
#include <iostream>
#include <stdlib.h>

class Human
{   
    friend class Referee;
    public:
        Human();

        void setHMove();
        std::string getHMove();

        void setNumGames();
        int getNumGames();

    private:
        std::string hMove;
        int numGames;
};

#endif


referee.cpp



#include "referee.h"

using namespace std;

Referee::Referee(int gamesNeededFor)
{
    numGames = gamesNeededFor;
}

void Referee::play()
{
    for (int i = 2; i < (2 * numGames + 1); i+=2)
    {
        if (Human::hMove.at(i) == 'R')
        {
            output.at(i) = 'T';
            output.at(i+1) = ' ';
        }

        if (Human::hMove.at(i) == 'P')
        {
            output.at(i) = 'W';
            output.at(i+1) = ' ';
        }

        if (Human::hMove.at(i) == 'S')
        {
            output.at(i) = 'L';
            output.at(i+1) = ' ';
        }

    }
} 

void Referee::results()
{
    for (int i = 0; i < (2 * numGames - 1); i++)
    {
        cout << output.at(i);
    }
}


computer.cpp



#include "computer.h"
using namespace std;

Computer::Computer(int games)
{
    string move = "R";

    for (int i = 2; i < (2 * games); i+=2)
    {
        cMove.at(i) = move;
        cMove.at(i+1) = ' ';
    }

    cMove.at(0) = games;
    cout << games << endl;
}

string Computer::getCMove()
{
    cout << cMove << endl;
    return cMove;
}


human.cpp



#include "human.h"
using namespace std;

Human::Human()
{

}

void Human::setHMove()
{
    string str;
    getline(cin, str);
    hMove = str;
}

string Human::getHMove()
{
    cout << hMove << endl;
    return hMove;
}

void Human::setNumGames()
{
    int tempNumGames = stoi(hMove.at(0));
    numGames = tempNumGames;
}

int Human::getNumGames()
{
    cout << numGames << endl;
    return numGames;
}


main.cpp



#include <iostream>
#include "human.cpp"
#include "computer.cpp"
#include "referee.cpp"

using namespace std;

int main()
{

    Human player;
    player.setHMove();
    player.getHMove();

    player.setNumGames();
    player.getNumGames();

    int games;
    games = player.getNumGames();

    Computer comp(games);
    comp.getCMove();

    Referee ref(games);
    ref.play();
    ref.results();

    return 0;
}
70: The correct header file is <cstdlib> for C++ usage.
105: referee.play() assumes guilty knowledge that computer always returns R. If computer were enhanced to randomly return one of R,P,S, this function would no longer work correctly.
152: You can't assign a string to a char.
156: Why are you assigning an int to the first string position?
194: stoi requires a string as the first argument. Again, you shouldn't be trying to store an integer in a string.
109,115,121: You have to refer to an instance of a human. You can't simply refer to a class name.
Topic archived. No new replies allowed.