simple Rock paper scissors

Updated Bumpehh 0 Tallied Votes 1K Views Share

This is a very simple Rock Paper scissors game i made.

i showed it to my cousin and he played it for like 20 mins lol.

Its against an AI and you pick by number, not words.

#include <iostream>
#include <cstdlib>
#include <ctime>

int ai;
int user;
using namespace std;

int main()
{
    srand (time(0));
    ai = rand() % 3;
    cout<<"Welcome to my RPS!\n0 = paper, 1= rock, 2 = scissors\n";
    cin>>user;
        if(user == 0 && ai == 1){
    cout<<"Paper beats rock! you win\n";}

    if(user == 0 && ai == 2){
    cout<<"Scissors cuts paper! you lose\n";}

    if(user == 2 && ai == 1){
    cout<<"rock beats scissor! you lose!\n";}

    if(user == 2 && ai == 0){
    cout<<"Scissors cuts paper! you win\n";}

    if(user == 1 && ai == 2){
    cout<<"rock beats scissors! you wiN";}

    if(user == 1 && ai == 0){
    cout<<"paper beats rock! you lose\n";}

    if(user == ai){
    cout<<"Tie game";}
    cout<<"\n\nAi picked: "<<ai;
    cout<<endl;

    return ai + user;

}
Lucaci Andrew 140 Za s|n

A slightly different approach, or not... here it goes anyway:

/*
 * game.cpp
 * game.cpp is licensed under GNU GENERAL PUBLIC LICENSE
 *  Created on: Aug 15, 2012
 *      Author: sin
 */
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int plays=0, comps=0;

int computer(){
    return (rand()%3);
}

string decision(int player, int computer){
    if (player==0 and computer==1){
        plays++;
        return ("Player wins.\n");
    }
    else if (player==1 and computer==2){
        plays++;
        return ("Player wins.\n");
    }
    else if (player==2 and computer==0){
        plays++;
        return ("Player wins.\n");
    }
    else if (player==computer)
        return ("Draw.\n");
    else{
        comps++;
        return ("Computer wins.\n");
    }
}

string stats(int comp){
    if (comp==0) return ("Rock.\n");
    else if (comp==1) return ("Scissors.\n");
    else return ("Paper.\n");
}

void reset(){
    plays=comps=0;
    cout<<"\nScore was reseted.\n"
        <<"Player: 0.\nComputer: 0.\n";
}

void print(int player, int computer){
    cout<<"Player: "<<stats(player);
    cout<<"Computer: "<<stats(computer);
    cout<<"Decision: "<<decision(player, computer);
    cout<<"Player's score: "<<plays<<endl;
    cout<<"Computer's score: "<<comps<<endl;
}

void menu(){
    cout<<"Choose from the below options: \n"
        <<"1. Rock.\n"
        <<"2. Scissors.\n"
        <<"3. Paper.\n"
        <<"Type \"q\" to exit.\n"
        <<"Type \"menu\" to view again this menu.\n"
        <<"Type \"/reset\" to reset your score.\n";
}

int main(){
    string answer;
    menu();
    for (;;){
        cout<<"\\\\: ";
        cin>>answer;
        int comp=computer();
        if (answer=="1." or answer=="1") print(0,comp);
        else if (answer=="2." or answer=="2") print(1, comp);
        else if (answer=="3." or answer=="3") print(2, comp);
        else if (answer=="q") break;
        else if (answer=="menu") menu();
        else if (answer=="/reset") reset();
        else cout<<"Invalid command.\n";
    }
    return (0);
}
mrnutty 761 Senior Poster

Didnt compile it but just to show some of the things you should be doing is breaking things into functions and using constants.

#include <iostream>
using namespace std;

enum WeaponType{ ROCK, PAPER, SCISSOR, MAX_OPT};
enum GameStatus{ PLAYER_WIN, DRAW_GAME, PLAYER_LOSS };
static const WeaponType options[MAX_OPT] = { ROCK, PAPER, SCISSOR };

define stringify(str) #str

WeaponType getUserChoice(){
 bool isValid = false
 WeaponType choice;
 while(!isValid){
   cout << "Pick one, rock = 1 , paper = 2, scissor = 3 " << endl;
   int opt = 0;
   bool streamValid = (cin >> opt );
   if(streamValid && opt > 0 && opt <= MAX_OPT ){ 
       choice =  options[ opt - 1]; 
       isValid = true;
   }
   else if(!streamValid){
     cin.clear();
     cin.getline(60,'\n')
   }
   cout << "Please Enter a valid options...\n\n" << endl;
 }
 return choice;
}

GameStatus evaluate(WeaponType c1, WeaponType c2){
 //switch or static int arrays is fine
  switch( c1 ){
    case ROCK:
          switch(c2){
            case ROCK: return DRAW_GAME;
            case PAPER: return PLAYER_LOSS ;
            case SCISSOR: return PLAYER_WIN;
          } break;
    case PAPER:
          switch(c2){
            case ROCK: return PLAYER_WIN;
            case PAPER: return DRAW_GAME;
            case SCISSOR: return PLAYER_LOSS ;
          } break;
    case SCISSOR:
          switch(c2){
            case ROCK: return PLAYER_LOSS ;
            case PAPER: return PLAYER_WIN;
            case SCISSOR: return DRAW_GAME;
          } break;
  }

}

void displayStats(const unsigned playerWin, const unsigned drawGames, const unsigned gamesPlayed){
 cout << "Number of times you have won thus far = " << playerWin << endl;
     cout << "Number of times you have loss thus far = " << (gamesPlayed - playerWin - drawGames) << endl;
 cout << "Number of draw games thus far = " << drawGames << endl;
 cout << "Number of games played thus far = " << gamesPlayed << endl;
}
int main(){
 bool isRunning = true;
 unsigned playerWin = 0;
 unsigned drawGame = 0;
 unsigned gamesPlayed = 0;
 while(isRunning){
    ++gamesPlayed;
    switch( evaluate( getUserChoice() , options[ rand() % MAX_OPT ] ) ){
       case PLAYER_WIN: ++playerWin; break;
       case DRAW_GAME: ++drawGame; break;
       case PLAYER_LOSS: break; //don't need todo anything
    }
    cout << endl << endl;
    displayStats( playerWin, drawGame, gamesPlayed );
 }

  cout << "Thanks for playing..." << endl;
  return 0;
}
m4ster_r0shi 142 Posting Whiz in Training

Now, let's add some basic AI. Nothing too serious, just a simple, suboptimal (you could use the KMP algorithm to improve it), history matcher. It searches the user's previous moves for recurring patterns and uses this information to predict the user's next move. For anyone interested in more advanced stuff, this could be of help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.