1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <iostream>
using namespace std;
//global variables
int num,entry,wins,losses,ties,array[10];
//function for random numbers in specific range
int random(int low,int high)
{
srand(time(NULL));//set random number seed
return rand()%(high-low+1)+low;
}
//function for simplifying color changing console output
void changeColor(int color)
{
HANDLE hCon;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,color);
}
int main()
{
//1 is rock, 2 is paper, 3 is scissors
//loop ten times, once for each round
for (int i=1; i<11; i++)
{
num=random(1,3);//computer choice each turn
cout<<"Please make your move (1=rock, 2=paper, 3=scissors): ";
cin>>entry;
while (entry<1 || entry>3)
{
cout<<entry<<" is not a valid entry!\n";
cout<<"Please select 1 for rock, 2 for paper, or 3 for scissors: ";
cin>>entry;
}
//only continue to this point with a valid entry
//if win
if ((num==3 && entry==1) || (num==2 && entry==3) || (num==1 && entry==2))
{
cout<<"You chose ";
cout<<((entry==1) ? "rock" : (entry==2) ? "paper" : "scissors");
cout<<" and I chose ";
cout<<((num==1) ? "rock." : (num==2) ? "paper." : "scissors.");
changeColor(10);
cout<<" You win!\n";
wins++;
array[i-1]=1;
}
//if loss
if ((entry==3 && num==1) || (entry==2 && num==3) || (entry==1 && num==2))
{
cout<<"You chose ";
cout<<((entry==1) ? "rock" : (entry==2) ? "paper" : "scissors");
cout<<" and I chose ";
cout<<((num==1) ? "rock." : (num==2) ? "paper" : "scissors.");
changeColor(12);
cout<<" You lose!\n";
losses++;
array[i-1]=2;
}
//if tie
if (entry==num)
{
cout<<"We both chose ";
cout<<((entry==1) ? "rock." : (entry==2) ? "paper." : "scissors.");
changeColor(14);
cout<<" Tied!\n";
ties++;
array[i-1]=3;
}
changeColor(7);//switch back to default color before continuing to next round
}//after 10 rounds
changeColor(10);cout<<"\t\tW"; changeColor(7);cout<<" = Win ";
changeColor(12);cout<<"L"; changeColor(7);cout<<" = Loss ";
changeColor(14);cout<<"T"; changeColor(7);cout<<" = Tie "<<endl;
cout<<"Round: 1 2 3 4 5 6 7 8 9 10\n";
cout<<"Result: ";
//go through array, outputting results of each round
for (int i=1;i<11;i++)
{
//switch to appropriate color based on win/loss/tie
if (array[i-1]==1) changeColor(10);
if (array[i-1]==2) changeColor(12);
if (array[i-1]==3) changeColor(14);
cout<<((array[i-1]==1) ? "W" : (array[i-1]==2) ? "L" : "T")<<" ";
changeColor(7);//change back to default color
}
cout<<"\nYou won "<<wins;
cout<<((wins>1) ? " times, " : " time, ");
cout<<"and lost "<<losses;
cout<<((losses>1) ? " times, " : " time, ")<<"while ";
if (ties!=0) cout<<ties;
cout<<((ties>1) ? " games were ties.\n" : (ties==1) ? " game was a tie.\n" : " no games were ties.")<<"\n";
if (wins>losses) changeColor(10);
if (losses>wins) changeColor(12);
if (wins==losses) changeColor(14);
cout<<((wins>losses) ? "You win!" : (wins<losses) ? "You lose!" : "Tie game!");
changeColor(7);//switch back to default color before program termination
cout<<endl;
}
|