コード例 #1
0
/*******************************************************************************
                                    move
 *******************************************************************************
 * purpose: To move Player around the board
 * input:
 *      p1loc-> takes in the players location on the board
 *      p1->    to move computer around the board c1 times
 *      p1bal-> if passed go add $200
 *      pmove[]->sets new location for computer
 * output:
 *      p1bal->if passed go collect $200
 *      p1loc->if passed go starts at the beginning
 */
int move(Human &h,int pmove[]){
    //Used to make sure the board is set to 0
    for(int i=0;i<SIZE;i++){
        pmove[i]=0;
    }
    for(int i=0;i<=h.getroll();i++){
        if(h.getloc()>39){
            h.setloc(h.getloc()-39);
            cout<<"You passed 'Go' please collect $200\n";
            h.setmoney(h.getMoney()+200);
        }else{
            pmove[h.getloc()]=1;
        }
    }
}
コード例 #2
0
//Execution Begins here
int main(int argc, char** argv) {
    //Set the random number seed and size the array
    srand(static_cast<unsigned int>(time(0)));
    
    //Declare Variables
    Human h;
    
//    int p1=0;//player dice ===== is now roll
//    int p1bal=1500;//player bank account === is now bal
//    int p1loc=0;//player location on the board ==== is now loc
    int c1=0; //The computer's dice 
    int c1bal=1500;//The computer's bank account
    int c1loc=0;//computer's location on the board
    
    bool who=true;//used to see who's turn it is
    bool prison=true;//If the player is in jail 
    bool cprison=true;//If the computer is in jail
    bool game=true;//to continue the game until there is a winner
    char check;//is the player would like to buy
    int play=0;//play the game or see rules
    int buyans;//If the user would like to buy the property
    int turn=1; //Turns if the player or computer land on jail
    
    House *board=new House[SIZE];//dynamic array to hold the name, buy and rent of the property
    string property[SIZE]={"Go","Mediterranean Avenue","Community Chest","Baltic Avenue"," Income Tax","Reading Railroad",
                         "Oriental Avenue","Chance","Vermont Avenue","Connecticut Avenue","In Jail or Just visiting","St. Charles Place",
                         "Electric Company","States Avenue"," Virginia Avenue","Pennsylvania RailRoad","St. James Place",
                         "Community Chest","Tennessee Avenue"," New York Avenue","Free Parking","Kentucky Avenue","Chance",
                         "Indiana Avenue","Illinois Avenue"," B. & O. Railroad","Atlantic Avenue","Ventnor Avenue",
                         "Water Works","Marvin Gardens","Go to Jail","Pacific Avenue","North Carolina Avenue",
                         "Community Chest","Pennsylvania Avenue","Short Line","Chance","Park Place","Luxury Tax","BoardWalk"};
    int buy[SIZE]={0,60,0,60,0,200,100,0,100,120,0,140,150,140,160,200,180,0,180,
                   200,0,220,0,220,240,200,260,260,150,280,0,300,300,0,320,200,0,350,0,400};
    int rent[SIZE]={0,20,0,40,0,25,60,0,60,80,0,100,75,100,120,250,140,0,140,160,
                    0,180,0,180,200,250,220,220,75,240,0,260,260,0,280,250,0,350,0,500};
     for(int i=0;i<SIZE;i++){
    board[i].setName(property[i]);
    board[i].setBuy(buy[i]);
    board[i].setRent(rent[i]);
    }
    
    bool avail[SIZE];//if the house is owned or available
    //sets the whole board to true available to buy
    
    for(int i=0;i<SIZE;i++){
        avail[i]=true;
        //Sets the chance,go,income tax, et. to not able to buy
        if (i==0 || i==2 || i==4 || i==7 ||i==10 || i==17 || i==20 || i==22 || i==30 || i==33 || i==36 || i==38){
            avail[i]=false;
        }
    }
    
    int pmove[SIZE];//is the player's board
    int cmove[SIZE];//the computers board
    vector <int> p1prop;
    vector <int> c1prop;
    //sets the whole array to 0
    for(int i=0;i<SIZE;i++){
        pmove[i]=0;
        cmove[i]=0;
    }
    
    do{
        do{
            cout<<"Welcome to the Monopoly game!!!\n";
            cout<<"Each player starts off with $1500\n";
            cout<<"1. To play the game\n2. To see the rules\n";
            cin>>play;
            if(play<1 || play>2){
                cout<<"ERROR: Invalid Input\n";
            }
        }while(play<1 || play>2);
        
        switch(play){
            case 1:{
                do{
                    if(game!=false){
                        if(cprison==false){
                            turn=2;  
                            cprison=true;      
                        }else{
                            turn=1;
                        }
                        for(int i=1;i<=turn;i++){
                            who=true;
                            h.setroll(roll(6,2));
                            cout<<"Player 1 you rolled your dice and got "<<h.getroll()<<endl;
                            cout<<"Move forward "<<h.getroll()<<" spaces."<<endl; 
                            pmove[h.getloc()]=0;
                            h.setloc(h.getloc()+h.getroll());

                            move(h,pmove);
                            
                            cout<<"You landed on "<<property[h.getloc()]<<endl;
                            if(h.getloc()==4 || h.getloc()==38){
                                pay(h,c1bal,c1loc,who);
                            }
                            if(h.getloc()==7 || h.getloc()==22 || h.getloc()==36){
                                chance(h,pmove,c1loc,cmove,c1,c1bal,prison,cprison,who);
                            }
                            if(h.getloc()==2 || h.getloc()==17 || h.getloc()==33){
                                chest(h,pmove,c1loc,cmove,c1,c1bal,prison,cprison,who);
                            }
                            if(h.getloc()==30){
                                prison=false;
                                jail(h,pmove,c1loc,cmove,c1,who);
                            }
                            cout<<"Your balance is $"<<h.getMoney()<<endl;
                            //Output the board
                            display(pmove,cmove);
                            //Used to check if player lands on a computer owned location
                            for(int i=0;i<c1prop.size();i++){
                                if(h.getloc()==c1prop[i]){
                                    h.setmoney(h.getMoney()-rent[h.getloc()]);
                                    c1bal+=rent[h.getloc()];
                                    cout<<"Computer owns "<<property[h.getloc()]<<" you owe $"<<rent[h.getloc()]<<" for rent\n";
                                }
                            }
                            //To determine the winner
                            win(h,c1bal,game);
                            if(game==false){
                                break; 
                            }
                                do{
                                    do{
                                        cout<<"1. Buy Property\n2. See your property\n3. End turn\n";
                                        cin>>buyans;
                                        if(buyans<1 || buyans>3){
                                            cout<<"Invalid Input\n";
                                        }
                                    }while(buyans<1 || buyans>3);
                                    switch(buyans){
                                        case 1:{
                                            if(avail[h.getloc()]==false){
                                                cout<<"You cannot buy this property\n";
                                            }else if(avail[h.getloc()]==true){
                                                cout<<"You can buy this property\n";
                                                board[h.getloc()].print();
                                                do{
                                                    cout<<"Press 'Y' if you want to buy this property,or 'N' if you do not\n";
                                                    cin>>check;
                                                    if(check!='Y' && check!='y' && check!='N' && check!='n'){
                                                        cout<<"Invalid Input\n";                         
                                                    }
                                                }while(check!='Y' && check!='y' && check!='N' && check!='n');

                                                if(check=='Y' || check=='y'){
                                                    if(h.getMoney()<buy[h.getloc()]){
                                                        cout<<"You can not afford this property\n";
                                                    }else if(h.getMoney()>=buy[h.getloc()]){
                                                        cout<<"You purchased "<<property[h.getloc()]<<endl;
                                                        h.setmoney(h.getMoney()-buy[h.getloc()]);//Subtracts the cost to buy from the players balance
                                                        avail[h.getloc()]=false;//Is no longer available
                                                        p1prop.push_back(h.getloc());//stores the location of where the player owns
                                                    }
                                                }
                                            }
                                            break;
                                        }
                                        case 2:{
                                            if(p1prop.size()==0){
                                                cout<<"You do not own any property\n";
                                            }else{
                                                //Cout the property the player owns
                                                cout<<"\n************************************************\n\n";
                                                for(int i=0;i<p1prop.size();i++){
                                                    cout<<"You own "<<property[p1prop[i]]<<endl;
                                                }
                                                cout<<"\n************************************************\n\n";
                                            }
                                            break;
                                        }
                                        case 3:{
                                           cout<<"Turn ended!\n\n\n";
                                           break; 
                                        }
                                        default:cout<<"Error: In the switch statment\n";
                                    }
                                }while(buyans!=3);
                        }
                    }
                    if(game!=false){
                        if(prison==false){
                                turn=2;
                                prison=true;    
                            }else{
                                turn=1;
                            }
                            for(int i=1;i<=turn;i++){
                                who=false;
                                c1=roll(6,2);
                                cout<<"Now it is the computers turn, balance $"<<c1bal<<"\n";
                                cout<<"The computer moved forward "<<c1<<" spaces."<<endl;
                                cmove[c1loc]=0;
                                c1loc+=c1;
                                compmove(c1loc,c1,c1bal,cmove);

                                cout<<"The computer landed on "<<property[c1loc]<<endl;
                                win(h,c1bal,game);//determines the winner
                                if(game==false)break;//doesn't let the computer turn happen if player wins
                                //calls the income tax function
                                if(c1loc==4 || c1loc==38){
                                     pay(h,c1bal,c1loc,who);
                                }
                                //calls the chance function
                                if(c1loc==7 || c1loc==22 || c1loc==36){
                                    chance(h,pmove,c1loc,cmove,c1,c1bal,prison,cprison,who);
                                }
                                //calls the chest function
                                if(c1loc==2 || c1loc==17 || c1loc==33){
                                    chest(h,pmove,c1loc,cmove,c1,c1bal,prison,cprison,who);
                                }
                                //Calls the jail function
                                if(c1loc==30){
                                    cprison=false;
                                   jail(h,pmove,c1loc,cmove,c1,who);
                                }

                                //Used to check if player lands on a computer owned location
                                for(int i=0;i<p1prop.size();i++){
                                    if(c1loc==p1prop[i]){
                                        c1bal-=rent[c1loc];
                                        h.setmoney(h.getMoney()+rent[c1loc]);                                        
                                        cout<<"You own "<<property[c1loc]<<" the computer owes you $"<<rent[c1loc]<<" for rent\n";
                                    }
                                }
                                int Cbuy=rand()%3+1;
                                if(avail[c1loc]==true){
                                   if(c1bal>buy[c1loc]){
                                       if(Cbuy==2){
                                          cout<<"The computer purchased "<<property[c1loc]<<endl;
                                          c1bal-=buy[c1loc];//Subrtracts the cost to buy from the players balance
                                          avail[c1loc]=false;//Is no longer available
                                          c1prop.push_back(c1loc);//stores the location of where the player owns 
                                        }
                                    }
                                }
                                display(pmove,cmove);
                                cout<<"Computer's turn ended, balance $"<<c1bal<<"\n";
                                cout<<"\n\n";
                            }

                    }
                }while(game);
                break;
            }
            case 2:{
                rules();
                break;
            }
        }
    }while(play!=1);
    over();
    //Writes to a file::binary the p1 balance and property's
    ofstream fout("scores.txt",ios::binary);
    cout<<"Writing down your score to a file..\n";
    fout<<"Player 1 balance: $"<<h.getMoney()<<endl;
    //Cout the property the player owns
    int counter=0;
    for(int i=0;i<p1prop.size();i++){
        fout<<"You own "<<property[p1prop[i]]<<", ";
        counter++;
        if(counter>3){
            fout<<endl;
            counter=0;
        }
    }
    fout.close();
    cout<<"Thank you for playing!!"<<endl;
    return 0;
}