/*******************************************************************************
                                    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;
        }
    }
}
int jail(Human &h,int pmove[],int &c1loc,int cmove[],int &c1,bool &who){
    if(who==true){
        pmove[h.getloc()]=0;
        h.setloc(10);
        h.setroll(0);//Dont know if i need it??????????????????????????????????????????????
        pmove[h.getloc()]=1;
        cout<<"You are in Jail\n";
    }else if(who==false){
        cmove[c1loc]=0;
        c1loc=10;
        c1=0;
        cmove[c1loc]=1;
        cout<<"The computer is in Jail\n";
    }
}
int pay(Human &h,int &c1bal,int c1loc,bool who){
    if(who==true){
        if(h.getloc()==4){
            cout<<"You paid $200 to the bank\n";
            h.setmoney(h.getMoney()-200);
        }else if(h.getloc()==38){
            cout<<"You paid $100 to the bank\n";
            h.setmoney(h.getMoney()-100);
        }
    }else if(who==false){
        if(c1loc==4){
            cout<<"The computer paid $200 to the bank\n";
            c1bal-=200;
        }else if(c1loc==38){
            cout<<"The computer paid $100 to the bank\n";
            c1bal-=100;
        }
    }
}
//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;
}
int chest(Human &h, int pmove[], int &c1loc, int cmove[],int &c1,int &c1bal,bool &prison,bool &cprison,bool &who){
    int random=rand()%15+1;
    if(who==true){
        switch(random){
            case 1:{
                cout<<"Community Chest: Make it Big in HollyWood. Collect $2000 in a movie deal\n";
                h.setmoney(h.getMoney()+2000);
                break;
            }
            case 2:{
                cout<<"Community Chest: You promote your new Book on tv morning news. Receive $100 in bonus sales\n";
                h.setmoney(h.getMoney()+100);
                break;
            }
            case 3:{
                cout<<"Community Chest: Advance to 'Go'(Collect $200)\n";
                pmove[h.getloc()]=0;
                h.setroll(0);//IDK if i need it????????????????????????????????????????
                h.setloc(0);
                move(h,pmove);
                cout<<"You passed 'Go' collect $200\n";
                h.setmoney(h.getMoney()+200);
                break;
            }
            case 4:{
                cout<<"Community Chest: Your trust fund becomes available. Collect $500\n";
                h.setmoney(h.getMoney()+500);
                break;
            }
            case 5:{
                cout<<"Community Chest: Take dance lessons with a celebrity coach. Pay $500\n";
                h.setmoney(h.getMoney()-500);
                break;
            }
            case 6:{
                cout<<"Community Chest: You run for mayor. Collect $100 from each player to fund your campaign\n";
                c1bal-=100;
                h.setmoney(h.getMoney()+100);
                break;
            }
            case 7:{
                cout<<"Community Chest: You coordinate opening day activities at progressive field. Collect $250 for your services\n";
                h.setmoney(h.getMoney()+250);
                break;
            }
            case 8:{
                cout<<"Community Chest: Your computer network gets hit with a virus. Pay $1000\n";
                h.setmoney(h.getMoney()-1000);
                break;
            }
            case 9:{
                cout<<"Community Chest: Sell your lifetime, 50-yard line, season tickets on e-bay. Collect $200\n";
                h.setmoney(h.getMoney()+200);
                break;
            }
            case 10:{
                cout<<"Community Chest: Win big at the casino. Collect $1000\n";
                h.setmoney(h.getMoney()+1000);
                break;
            }
            case 11:{
                cout<<"Community Chest: You owe back taxes. Pay $500 in fines\n";
                h.setmoney(h.getMoney()-500);
                break;
            }
            case 12:{
                cout<<"Community Chest: You win the lottery. Collect $1000\n";
                h.setmoney(h.getMoney()+1000);
                break;
            }
            case 13:{
                cout<<"Community Chest: You are a runner up on a reality Tv show. Collect $100\n";
                h.setmoney(h.getMoney()+100);
                break;
            }
            case 14:{
                cout<<"Community Chest: Arrested for insider trading. Go to jail. DO NOT PASS 'GO', DO NOT COLLECT $200\n";
                prison=false;
                jail(h,pmove,c1loc,cmove,c1,who);
                break;
            }
            case 15:{
                cout<<"Community chest: Received a presidential pardon. GET OUT OF JAIL FREE. This card may be kept until needed.\n";
                break;
            }
            default: cout<<"Error: In the switch statment\n";
            }
    }else if(who==false){
        switch(random){
            case 1:{
                cout<<"Community Chest: Make it Big in HollyWood. Collect $2000 in a movie deal\n";
                c1bal+=2000;
                break;
            }
            case 2:{
                cout<<"Community Chest: You promote your new Book on tv morning news. Receive $100 in bonus sales\n";
                c1bal+=100;
                break;
            }
            case 3:{
                cout<<"Community Chest: Advance to 'Go'(Collect $200)\n";
                cmove[c1loc]=0;
                c1=0;
                c1loc=0;
                move(h,pmove);
                cout<<"You passed 'Go' collect $200\n";
                c1bal+=200;
                break;
            }
            case 4:{
                cout<<"Community Chest: Your trust fund becomes available. Collect $500\n";
                c1bal+=500;
                break;
            }
            case 5:{
                cout<<"Community Chest: Take dance lessons with a celebrity coach. Pay $500\n";
                c1bal-=500;
                break;
            }
            case 6:{
                cout<<"Community Chest: You run for mayor. Collect $100 from each player to fund your campaign\n";
                h.setmoney(h.getMoney()-100);
                c1bal+=100;
                break;
            }
            case 7:{
                cout<<"Community Chest: You coordinate opening day activities at progressive field. Collect $250 for your services\n";
                c1bal+=250;
                break;
            }
            case 8:{
                cout<<"Community Chest: Your computer network gets hit with a virus. Pay $1000\n";
                c1bal-=1000;
                break;
            }
            case 9:{
                cout<<"Community Chest: Sell your lifetime, 50-yard line, season tickets on e-bay. Collect $200\n";
                c1bal+=200;
                break;
            }
            case 10:{
                cout<<"Community Chest: Win big at the casino. Collect $1000\n";
                c1bal+=1000;
                break;
            }
            case 11:{
                cout<<"Community Chest: You owe back taxes. Pay $500 in fines\n";
                c1bal-=500;
                break;
            }
            case 12:{
                cout<<"Community Chest: You win the lottery. Collect $1000\n";
                c1bal+=1000;
                break;
            }
            case 13:{
                cout<<"Community Chest: You are a runner up on a reality Tv show. Collect $100\n";
                c1bal+=100;
                break;
            }
            case 14:{
                cout<<"Community Chest: Arrested for insider trading. Go to jail. DO NOT PASS 'GO', DO NOT COLLECT $200\n";
                cprison=false;
                jail(h,pmove,c1loc,cmove,c1,who);
                break;
            }
            case 15:{
                cout<<"Community chest: Received a presidential pardon. GET OUT OF JAIL FREE. This card may be kept until needed.\n";
                break;
            }
            default: cout<<"Error: In the switch statment\n";
            }
    }
}
int chance(Human &h, int pmove[], int &c1loc, int cmove[],int &c1,int &c1bal,bool &prison,bool &cprison,bool &who){
    int random=rand()%12+1;
    if(who==true){
        switch(random){
            case 1:{
                cout<<"Chance: Convicted of Identity theft, Go to Jail. Do not pass go. Do not collect $200\n";
                prison=false;
                jail(h,pmove,c1loc,cmove,c1,who);
                break;
            }
            case 2:{
                cout<<"Chance: A group of guest wins a class action suit against your hotel pay each player $500\n";
                h.setmoney(h.getMoney()-500);
                c1bal+=500;
                break;
            }
            case 3:{
                cout<<"Chance: Ride First class to reading railroad. If you pass go collect $200\n";
                pmove[h.getloc()]=0;
                h.setroll(5);//IDK IF I NEED IT?????????????????????????
                h.setloc(5);
                move(h,pmove);
                break;
            }
            case 4:{
                cout<<"Chance: Make a donation for disaster relief. Pay $150\n";
                h.setmoney(h.getMoney()-150);
                break;
            }
            case 5:{
                cout<<"Chance: Get a tax break for driving a Hybrid. Collect $50\n";
                h.setmoney(h.getMoney()+50);
                break;
            }
            case 6:{
                cout<<"Chance: Advance to go collect $200\n";
                pmove[h.getloc()]=0;
                h.setroll(0);//IDK IF I NEED IT?????????????????????????
                h.setloc(0);
                move(h,pmove);
                cout<<"You passed 'Go' collect $200\n";
                h.setmoney(h.getMoney()+200);
                break;
            }
            case 7:{
                cout<<"Chance: Summoned for jury duty. Go back three spaces.\n";
                pmove[h.getloc()]=0;
                h.setloc(h.getloc()-3);
                pmove[h.getloc()]=1;
                break;
            }
            case 8:{
                cout<<"Chance: Take a helicopter ride to BoardWalk. If you pass go collect $200\n";
                pmove[h.getloc()]=0;
                h.setroll(39);//IDK IF I NEED IT?????????????????????????
                h.setloc(39);
                move(h,pmove);
                break;
            }
            case 9:{
                cout<<"Chance: Accept the position of CEO of a high powered investment banking firm. Collect a signing bonus of $500\n";
                h.setmoney(h.getMoney()+500);
                break;
            }
            case 10:{
                cout<<"Chance: Jump on a plane to Illinois Avenue. If you pass go collect $200\n";
                pmove[h.getloc()]=0;
                h.setroll(24);//IDK IF I NEED IT?????????????????????????
                h.setloc(24);
                move(h,pmove);
                break;
            }
            case 11:{
                cout<<"Chance: Splash out on a trip to St. Charles place. If you pass go collect $200\n";
                pmove[h.getloc()]=0;
                h.setroll(11);//IDK IF I NEED IT?????????????????????????
                h.setloc(11);
                move(h,pmove);
                break;
            }
            case 12:{
                cout<<"Chance: You are acquitted. GET OUT OF JAIL FREE. This card may be kept until needed.\n";
                break;
            }
            default: cout<<"Error: In the switch statment\n";
            }
    }else if(who==false){
        switch(random){
            case 1:{
                cout<<"Chance: Convicted of Identity theft, Go to Jail. Do not pass go. Do not collect $200\n";
                cprison=false;
                jail(h,pmove,c1loc,cmove,c1,who);
                break;
            }
            case 2:{
                cout<<"Chance: A group of guest wins a class action suit against your hotel pay each player $500\n";
                c1bal-=500;
                h.setmoney(h.getMoney()+500);
                break;
            }
            case 3:{
                cout<<"Chance: Ride First class to reading railroad. If you pass go collect $200\n";
                cmove[c1loc]=0;
                c1=5;
                c1loc=5;
                compmove(c1loc,c1,c1bal,cmove);
                break;
            }
            case 4:{
                cout<<"Chance: Make a donation for disaster relief. Pay $150\n";
                c1bal-=150;
                break;
            }
            case 5:{
                cout<<"Chance: Get a tax break for driving a Hybrid. Collect $50\n";
                c1bal+=50;
                break;
            }
            case 6:{
                cout<<"Chance: Advance to go collect $200\n";
                cmove[c1loc]=0;
                c1=0;
                c1loc=0;
                compmove(c1loc,c1,c1bal,cmove);
                cout<<"You passed 'Go' collect $200\n";
                c1bal+=200;
                break;
            }
            case 7:{
                cout<<"Chance: Summoned for jury duty. Go back three spaces.\n";
                cmove[c1loc]=0;
                c1loc-=3;
                cmove[c1loc]=1;
                break;
            }
            case 8:{
                cout<<"Chance: Take a helicopter ride to BoardWalk. If you pass go collect $200\n";
                cmove[c1loc]=0;
                c1=39;
                c1loc=39;
                compmove(c1loc,c1,c1bal,cmove);
                break;
            }
            case 9:{
                cout<<"Chance: Accept the position of CEO of a high powered investment banking firm. Collect a signing bonus of $500\n";
                c1bal+=500;
                break;
            }
            case 10:{
                cout<<"Chance: Jump on a plane to Illinois Avenue. If you pass go collect $200\n";
                cmove[c1loc]=0;
                c1=24;
                c1loc=24;
                compmove(c1loc,c1,c1bal,cmove);
                break;
            }
            case 11:{
                cout<<"Chance: Splash out on a trip to St. Charles place. If you pass go collect $200\n";
                cmove[c1loc]=0;
                c1=11;
                c1loc=11;
                compmove(c1loc,c1,c1bal,cmove);
                break;
            }
            case 12:{
                cout<<"Chance: You are acquitted. GET OUT OF JAIL FREE. This card may be kept until needed.\n";
                break;
            }
            default: cout<<"Error: In the switch statment\n";
            } 
    }
}