/*
Name: Assignment 3 with random number predefined
Author: muguangde
Data: 2014-07-25-8.10.AM
Description: This is a simple program used to play a game with
two dices. The user can choose to generate two initial random numbers,
or read them from a file. Later on printing out winning
info according to the sum of them. Then modify both, and repeat
the procedure.
*/
#include
#include
#include
#include
using namespace std;
int main()
{
int result1=0,result2=0;
int num1=0,num2=0;
int secondNum1=0,secondNum2=0;
int firstWin=0,firstLose=0,firstDraw=0;
int secondWin=0,secondLose=0,secondDraw=0;
int doubleWin=0,doubleLose=0;
int outcome(int,int);
int newNum(int);
int newNum2(int);
int randomOrNot;
string winOrLose(int);
ofstream outfile;
ifstream infile;
outfile.open("output.txt");
//To decide whether or not to use random number
cout<<"Do you want to use random number? YES:1 NO:0"<<endl;
cin>>randomOrNot;
while(randomOrNot!=0 && randomOrNot!=1) {
cout<<"Error: input 1 for YES and 0 for NO"<<endl;
cin>>randomOrNot;
}
outfile<<"**************************************************"<<endl;
outfile<<"*Dice game: if sum of two number = 5,7 or 12,win**"<<endl;
outfile<<"*if sum = 2,4 or 11,lose, other = draw************"<<endl;
outfile<<"**************************************************"<<endl;
outfile<<"\nOriginal pair"<<"\tWinning info"<<" |"<<" New pair"
<<"\t\tWinning info"<<endl;
// Get random seed
srand (time(NULL));
if(randomOrNot) {
for(int i=0; i<20; i++) {
// generate random number between 1 and 6
num1 = rand() % 6 + 1;
num2 = rand() % 6 + 1;
//To get first winning result, because the numbers are
//fixed in range 1-6, no need to check whether it's valid.
result1=outcome(num1,num2);
if(result1==0)
firstDraw++;
else if(result1==-1)
firstLose++;
else if(result1==1)
firstWin++;
//To calculate the second winning result
secondNum1=newNum(num1);
secondNum2=newNum2(num2);
result2=outcome(secondNum1,secondNum2);
if(result2==0)
secondDraw++;
else if(result2==-1)
secondLose++;
else if(result2==1)
secondWin++;
if(result1==1 && result2==1)
doubleWin++;
if(result1==-1 && result2==-1)
doubleLose++;
outfile<<num1<<"\t"<<num2<<"\t"<<winOrLose(result1)
<<"\t"<<" | "<<secondNum1<<"\t"<<secondNum2<<"\t"
<<winOrLose(result2)<<endl;
}
} else {
ifstream infile;
infile.open("input.txt");
if(!infile.is_open()) {
//If the file can't be opened
cout<<endl<<"Error: unable to open file."<<endl;
system("PAUSE");
exit(1);
}
infile>>num1>>num2;
while(!infile.eof()) {
//To the first winning result, if data valid then continue
if(num1<=6 && num1>0 && num2<=6 && num2>0) {
result1=outcome(num1,num2);
if(result1==0)
firstDraw++;
else if(result1==-1)
firstLose++;
else if(result1==1)
firstWin++;
//To calculate the second winning result
secondNum1=newNum(num1);
secondNum2=newNum2(num2);
result2=outcome(secondNum1,secondNum2);
if(result2==0)
secondDraw++;
else if(result2==-1)
secondLose++;
else if(result2==1)
secondWin++;
if(result1==1 && result2==1)
doubleWin++;
if(result1==-1 && result2==-1)
doubleLose++;
outfile<<num1<<"\t"<<num2<<"\t"<<winOrLose(result1)
<<"\t"<<" | "<<secondNum1<<"\t"<<secondNum2<<"\t"
<<winOrLose(result2)<<endl;
} else
//If the input not valid, return invalid info
outfile<<num1<<"\t"<<num2<<"\t"
<<"*******Invalid data*******"<<endl;
infile>>num1>>num2;
}
}
outfile<<"\nThe original pair of dice gave "<<firstWin<<" winners, "
<<firstLose<<" losers and "<<firstDraw<<" draws."<<endl;
outfile<<"\nThe new pair of dice gave "<<secondWin<<" winners, "
<<secondLose<<" losers and "<<secondDraw<<" draws."<<endl;
outfile<<"\nThere are "<<doubleWin<<" double winners"
<<" and "<<doubleLose<<" double losers."<<endl;
infile.close();
outfile.close();
system("PAUSE");
return 0;
}
//Function to calculate outcome
int outcome(int num1,int num2)
{
int sum=0,result=0;
sum=num1+num2;
if(sum==5 || sum==7 || sum==12)
result=1;
else if(sum==2 || sum==4 || sum==11)
result=-1;
else
result=0;
return result;
}
//Function to add two and adjust value accordingly
int newNum(int num)
{
int addTwo=0;
addTwo=num+2;
if(addTwo>6)
addTwo=addTwo-6;
return addTwo;
}
int newNum2(int num)
{
int addTwo=0;
addTwo=num+3;
if(addTwo>6)
addTwo=addTwo-6;
return addTwo;
}
//Return winning situation
string winOrLose(int result)
{
string winCase;
if(result==1)
winCase="Win";
else if(result==-1)
winCase="Lose";
else if(result==0)
winCase="Draw";
return winCase;
}
回到主页
回到目录