/*
  Name: Assignment 3
  Author: muguangde
  Data: 2014-07-25-8.10.AM
  Description: This is a simple program used to play a game with
  two dices, with two number as initial input, print out winning
  info according to the sum of them. Then modify both, and repeat
  the procedure.
*/
#include <iostream>
#include <cstdlib>
#include <fstream>

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);
    string winOrLose(int);
    ifstream infile;
    ofstream outfile;
    infile.open("input.txt");
    outfile.open("output.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;
    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;
    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;
}


  
回到主页 回到目录