3 Replies - 822 Views - Last Post: 30 June 2010 - 04:33 PM Rate Topic: -----

#1 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Rock paper scissors program

Posted 30 June 2010 - 01:34 PM

I had an idea earlier for how I could make a rock, paper, scissors game without checking for each possible combination, and I figured that it would be good practice, because frankly, I suck at pointers :). I threw this together and I don't need help, I just want your thoughts on whether or not it's a good way to go about it.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class object {
public:
    object *defeats;
};
class rps {
public:
    rps();
    void play();
private:
    object rock;
    object paper;
    object scissors;
};
int main() {
    rps game;
    game.play();
}
rps::rps() {
    rock.defeats = &scissors;
    paper.defeats = &rock;
    scissors.defeats = &paper;
}
void rps::play() {
    char choice;
    object *playerChoice;
    object *computerChoice;
    cout << "(R)ock, (P)aper, or (S)cissors? ";
    cin >> choice;
    switch(choice) {
        case 'r':
        case 'R':
            playerChoice = &rock;
            cout << "Player: Rock" << endl;
        break;
        case 'p':
        case 'P':
            playerChoice = &paper;
            cout << "Player: Paper" << endl;
        break;
        case 's':
        case 'S':
            playerChoice = &scissors;
            cout << "Player: Scissors" << endl;
        break;
        default:
            cout << "Invalid input." << endl;
        break;
    }
    srand(time(NULL));
    int compChoice = rand() % 3;
    switch(compChoice) {
        case 0:
            computerChoice = &rock;
            cout << "Computer: Rock" << endl;
        break;
        case 1:
            computerChoice = &paper;
            cout << "Computer: Paper" << endl;
        break;
        case 2:
            computerChoice = &scissors;
            cout << "Computer: Scissors" << endl;
        break;
    }
    if (playerChoice == computerChoice) {
        cout << "Draw!" << endl;
    }
    else if (playerChoice == computerChoice->defeats) {
        cout << "You lose!" << endl;
    }
    else {
        cout << "You win!" << endl;
    }
    cout << "Do you want to play again? Y/N ";
    cin >> choice;
    switch (choice) {
        case 'y':
        case 'Y':
            return play();
        break;
        default:
            return;
        break;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Rock paper scissors program

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Rock paper scissors program

Posted 30 June 2010 - 01:42 PM

I think that you handled the things pretty well, and I don't think that you suck. I'd say that you have done a pretty good work.
EDIT: that srand() shouldn't be in play() function. This function should be called only once in the entire program. So, cut it from here & paste it in your main() function, where srand() should belong :)

This post has been edited by sarmanu: 30 June 2010 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 854
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Rock paper scissors program

Posted 30 June 2010 - 03:47 PM

Someone once said 'Things should be made as simple as possible, but no simpler'; For a simple rock/paper/scissors game, I think you've found a pretty neat, elegant solution.



Just for kicks, i thought i'd knock together an over-complicated version using templates and type traits. Personally, i think yours is much easier to read.
#define REGISTER_OBJECT_TRAITS( obj_type ) \
    template<> struct object_impl::object_traits<obj_type> { static const std::string name; }; \
    const std::string object_impl::object_traits<obj_type>::name = #obj_type ;

#include <string>
#include <iostream>

struct rock;
struct paper;
struct scissors;

struct basic_object
{
    virtual void versus(rock&) = 0;
    virtual void versus(paper&) = 0;
    virtual void versus(scissors&) = 0;
};

namespace object_impl
{
    template<typename T> struct object_traits;
    REGISTER_OBJECT_TRAITS ( rock )
    REGISTER_OBJECT_TRAITS ( paper )
    REGISTER_OBJECT_TRAITS ( scissors )

    template<typename T, typename Beats, typename Draws, typename Loses>
    struct object : public basic_object
    {
        void versus(Beats& obj) { obj.result(object_traits<T>::name, " beats "); }
        void versus(Draws& obj) { obj.result(object_traits<T>::name, " draws with "); }
        void versus(Loses& obj) { obj.result(object_traits<T>::name, " is beaten by "); }
        void result(std::string opponent, std::string result)
        {
            std::cout << "My " << opponent << result << "your "
                      << object_impl::object_traits<T>::name 
                      << std::endl;
        }
    };
}

struct rock     : public object_impl::object<rock, scissors, rock, paper> {};
struct paper    : public object_impl::object<paper, rock, paper, scissors> {};
struct scissors : public object_impl::object<scissors, paper, scissors, rock> {};

#include <ctime>
#include <cctype>
#include <cstdlib>

int main() 
{
    rock r;
    paper p;
    scissors s;
    basic_object* computer[] = { &r, &p, &s };
    std::string input;

    srand(static_cast<unsigned int>(time(0)));

    std::cout << "[R]ock, [P]aper, [S]cissors, or [Q]uit: ";
    while( std::getline(std::cin, input) )
    {
        int rn(rand() % 3);
        switch(std::tolower(input[0]))
        {
        case 'r' : 
            computer[rn]->versus(r);
            break;
        case 'p' : 
            computer[rn]->versus(p);
            break;
        case 's' : 
            computer[rn]->versus(s);
            break;
        case 'q' : exit(0);
        default  : std::cout << "Unknown choice." << std::endl;
        }
        std::cout << "[R]ock, [P]aper, [S]cissors, or [Q]uit: ";
    }
}

Was This Post Helpful? 0
  • +
  • -

#4 c0dy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 34
  • Joined: 29-June 10

Re: Rock paper scissors program

Posted 30 June 2010 - 04:33 PM


It works well. Maybe add a static score keeper. This makes me want to
make some games. Maybe a terminal game for Texas hold'em. Don't need
no graphics. :P

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1