Το πρόγραμμα που κάναμε σήμερα και θα αποτελέσει τη βάση για το παιχνίδι Πέτρα-Ψαλίδι-Χαρτί.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
srand(time(NULL));
cout << " ____________________________" << endl;
cout << "| |" << endl;
cout << "| Rock Papers Scissors |" << endl;
cout << "| by Zanneio Gymnasio |" << endl;
cout << "|____________________________|" << endl;
cout << " ****************************" << endl << endl;
int player, computer, wins, losses;
wins = losses = 0;
player = 0;
while (player != -1) {
computer = rand()%3;
cout << "Enter -1 to end game!" << endl;
cout << "Enter your Choice (0,1,2) : " ;
cin >> player;
if (player == computer) {
cout << " *** You Win ***" << endl;
wins = wins + 1;
}
else if (player == -1) {
cout << "*** GAME OVER ***" << endl;
cout << "*** GOOD BYE ***" << endl;
}
else {
cout << "I win you lose!" << endl;
losses = losses + 1;
}
cout << "----------------------------" << endl;
cout << " PLAYER - COMPUTER " << endl;
cout << " " << wins << " - " << losses << endl;
cout << "----------------------------" << endl;
}
return 0;
}
Παρακάτω τροποποιούμε το πρόγραμμα έτσι ώστε ο χρήστης να μην δίνει τους αριθμούς 0,1,2 αλλά τα γράμματα P,R,S για Paper, Scissors, Rock.
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
srand(time(NULL));
cout << " ____________________________" << endl;
cout << "| |" << endl;
cout << "| Rock Papers Scissors |" << endl;
cout << "| by Zanneio Gymnasio |" << endl;
cout << "|____________________________|" << endl;
cout << " ****************************" << endl << endl;
int index=0, wins, losses;
char player, computer;
wins = losses = 0;
player = 'P';
while (player=='P' || player=='S' || player=='R') {
index = rand()%3;
if (index==0) {
computer = 'R';
}
else if (index==1) {
computer = 'P';
}
else {
computer = 'S';
}
cout << "Enter -1 to end game!" << endl;
cout << "Enter R for Rock, P for Paper and S for Scissors" << endl ;
cout << "Enter your Choice (R,P,S) : " ;
cin >> player;
if (computer=='S' && player=='R') {
cout << "Rock beats Scissors: You win!" << endl;
wins++;
}
else if (computer=='R' && player=='S') {
cout << "Rock beats Scissors: You lose!" << endl;
losses++;
}
// here write your code for all other cases
else {
cout << "*** GAME OVER ***" << endl;
cout << "*** GOOD BYE ***" << endl;
}
cout << "----------------------------" << endl;
cout << " PLAYER - COMPUTER " << endl;
cout << " " << wins << " - " << losses << endl;
cout << "----------------------------" << endl;
}
return 0;
}