Create a base class named Computer, and create two subclasses that inherit from this base class. One subclass needs to output rock, paper, scissors in a fixed order: paper, scissors, rock. And the other subclass computer randomly outputs rock, paper, scissors.
I already know how to make a computer randomly output rock, paper, scissors, but how to make a computer output paper, scissors, rock in fixed order? My idea is to use global variables. I set a global variable to add one after each output of the computer, and used an if-statement to achieve a fixed output order.
But this is not a good method because if the computer is going to output rock, rock, scissors and there are repeated actions, using if
doesn't seem to be a good choice. Is there a better method?
Here is my code:
computer.h
#ifndef COMPUTER
#define COMPUTER
class Computer{
public:
Computer();
char makeMove();
};
#endif
computer.cpp
#include "Computer.h"
Computer::Computer() {
}
char Computer::makeMove() {
return 'R';
}
Fixed order to print
#ifndef CRESCENDO
#define CRESCENDO
int count = 1;
class Crescendo : public Computer {
public:
Crescendo();
char C_moves();
}
#endif
function file
#include "Crescendo.h"
Crescendo::Crescendo() {
}
char Crescendo::C_moves() {
if (count == 1) {
return 'P';
}
else if(count == 2) {
return 'S';
}
else if (count == 3){
return 'R';
}
count++;
}
Thank you all.