Week 3 : Answer
#include <iostream>
using namespace std;
void main(){
int
r, c;
for(r=0;
r<6; r++){
for(c=0; c<=r; c++)
cout << "*";
cout << endl;
}
}
int main(){
int
userchoice; // the user's selection
int
computerchoice; // the computer's selection
srand(time(0));
// initialize random number generator
cout
<< "Paper, Scissors, Rock" << endl;
while(true){
computerchoice = rand()%3; // randomly assigns values 0, 1, or 2
// ask the user for their selection
do{
cout << "Enter 0 for paper, 1 for scissors, or 2 for rock: ";
cin >> userchoice; // ask the user for their choice
// print out user's choice (e.g., "Player picks rock")
// if not 0, 1, or 2, print "Invalid Selection" and quit
if(userchoice == -1){
cout << "Game over!" << endl;
return 0;
}
else if(userchoice == 0)
cout << "Player picks paper" << endl;
else if(userchoice == 1)
cout << "Player picks scissors" << endl;
else if(userchoice == 2)
cout << "Player picks rock" << endl;
else
cout << "Invalid selection, please re-enter." << endl <<
endl;
}while(userchoice<-1 || userchoice>2);
// print out computer's choice (e.g., "Computer picks rock")
if(computerchoice == 0)
cout << "Computer picks paper" << endl;
else if(computerchoice == 1)
cout << "Computer picks scissors" << endl;
else
cout << "Computer picks rock" << endl;
// print results (either "Draw", "Computer Wins", or "Player Wins")
if(computerchoice == userchoice)
cout << "Draw\n";
else{
if(computerchoice == 2 && userchoice == 1)
cout << "Computer Wins\n";
else if(computerchoice == 1 && userchoice == 0)
cout << "Computer Wins\n";
else if(computerchoice == 0 && userchoice == 2)
cout << "Computer Wins\n";
else
cout << "Player Wins\n";
}
cout << endl;
}
return
0;
}