-1

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.

mpp
  • 308
  • 1
  • 14
Cedric xu
  • 13
  • 5
  • _"My idea is to use global variables"_ <-- Famous last words... – Dai Aug 14 '22 at 02:08
  • No, this does not need any global variable, of any kind. Do you know what class members are, and how to use them? – Sam Varshavchik Aug 14 '22 at 02:18
  • 1
    @Sam Varshavchik Three types of public protection private? – Cedric xu Aug 14 '22 at 02:22
  • No, that's not what a "class member" is. Which C++ textbook are you using, that already discussed inheritance and subclasses, but without explaining what class members are??? That does not make any sense. Sounds like a pretty bad textbook. – Sam Varshavchik Aug 14 '22 at 02:24
  • @ Sam Varshavchik As a matter of fact, the professor didn't recommend books to us. I taught myself based on videos. – Cedric xu Aug 14 '22 at 02:26
  • 3
    Well, here's the problem. Any clown can upload a video to Youtube, even I can. But it takes money to publish a textbook. A publisher won't risk their money without verifying the quality and usability of published material. C++ is the most complicated and hardest to learn programming language in use today. It cannot be learned by watching random Youtube videos, or solving dumb coding puzzles from spam-pushing web sites promising that you'll become an instant C++ uberhacker, simply by doing coding puzzles. The only way to learn C++ is from a textbook. Stackoverflow is not a textbook replacement. – Sam Varshavchik Aug 14 '22 at 02:28
  • @ Sam Varshavchik I just found a book called Problem Solving with C++. I found class memebers. Is this book available and good? – Cedric xu Aug 14 '22 at 02:32
  • From looking at the edit history of [Stackoverflow's curated list of C++ textbook](https://stackoverflow.com/questions/388242/), this textbook was deleted from the recommended list several years ago. So, the consensus of Stackoverflow's C++ community is that it is not one of the better textbooks. Every C++ textbook will cover class members, but not every textbook will teach C++ correctly. – Sam Varshavchik Aug 14 '22 at 02:36
  • @Sam Varshavchik My new idea is to use a private domain to create a number that I will set to 1 in the default constructor, create a function in the public domain so that the member function can be incremented by 1, and call the function inside the function every time the computer outputs it. Does this work? – Cedric xu Aug 14 '22 at 03:01
  • @Sam Varshavchik such as set int count; on private on Crescendo.h and int increment() on public on Crescendo.h , set int incremenet() {count ++;} on Crescendo.cpp – Cedric xu Aug 14 '22 at 03:03

1 Answers1

0

As others point out global variables are something to avoid if at all possible.

You might need more than one instance of your subclass and you most likely want them to make a different sequence of moves.

I've put it all in a single file for illustrative purposes. Notice how the two instances of Crescendo behave independently.

Expected Output:

Computer...
R
R
R
Now Crescendo...
P
S
R
P
S
R
P
P
S

#include <iostream>

class Computer{

    public:
        Computer();
        virtual char makeMove(); //virtual means subclasses will override this. 
};

Computer::Computer(){ }

char Computer::makeMove(){
    return 'R';
}

class Crescendo : public Computer {
    public:
        Crescendo();
        virtual char makeMove();//Not sure why you chose c_moves().
    private:
        int count;
};

Crescendo::Crescendo() : count{0} {//This initialises the count of moves.
 
}

static const char moves[]{'P','S','R'};//Store the move sequence as an array.

char Crescendo::makeMove() {
    
    char move{moves[count]};
    
    ++count;
    if(count>2){//Remember to loop round!
        count=0;
    }
    
    return move;
 }


int main() {
    Computer computer;
    
    std::cout << "Computer...\n";
    std::cout << computer.makeMove()<<'\n';
    std::cout << computer.makeMove()<<'\n';
    std::cout << computer.makeMove()<<'\n';

    
    Crescendo crescendo;
    std::cout << "Now Crescendo...\n";
    std::cout << crescendo.makeMove()<<'\n';
    
    std::cout << crescendo.makeMove()<<'\n';
    std::cout << crescendo.makeMove()<<'\n';
    std::cout << crescendo.makeMove()<<'\n';
    std::cout << crescendo.makeMove()<<'\n';
    std::cout << crescendo.makeMove()<<'\n';

    Crescendo crescendoB;
    
    std::cout << crescendoB.makeMove()<<'\n';
    std::cout << crescendo.makeMove()<<'\n';
    std::cout << crescendoB.makeMove()<<'\n';
    
    return 0;
}
Persixty
  • 8,165
  • 2
  • 13
  • 35