boston.cpp

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc2040/fall2026/notes/dice/code/boston.cpp
 
#include <iostream>
#include <iomanip>
#include "die.h"

using namespace std;

const int MAX_PLAYERS{10};
int PlayTurn(int playerNumber, DieT & die1, DieT & die2, DieT  & die3);

int Max(int a, int b);

void ScoreRound(int score[], int roundsWon[] ,
                int & mostRoundsWon, int highestScore);
void PrintWinner(int roundsWon[], int mostRoundsWon, int players);

int main() {
    int players = 3;
    int rounds =  2;

    srand(static_cast<int>(time(nullptr)));
    DieT die1, die2, die3;
    InitDie(die1, 6);
    InitDie(die2, 6);
    InitDie(die3, 6);

    int score[MAX_PLAYERS];
    int roundsWon[MAX_PLAYERS];

    for(int player =0; player < players; ++player) {
        roundsWon[player] = 0;
    }

    int mostRoundsWon = 0;

    for( int round = 1; round <= rounds; ++round) {
       int highestScore = 0;
       //ResetScores(score);

       cout << "Round " << round << endl;
       for(int player = 0; player < players; ++player) {
           score[player] = PlayTurn(player+1, die1, die2, die3);
           cout << "\tPlayer " << player+1 
                << " scores " << score[player] << endl;
           highestScore = Max(highestScore, score[player]);
       }

       ScoreRound(score, roundsWon, mostRoundsWon,  highestScore);
       cout << endl;
    }

    PrintWinner(roundsWon, mostRoundsWon, players);
    return 0;
}

void PrintWinner(int roundsWon[], int mostRoundsWon, int players){

    cout << endl;
    cout << "Final Scores: " << endl;
    for(int player = 0; player < players; ++player) {
       cout << player + 1 << " won " << roundsWon[player] << " rounds";

       if (roundsWon[player] == mostRoundsWon) {
          cout << ", winner";
       }
       cout << "." << endl;
    }
}

void ScoreRound(int score[], int roundsWon[] , int & mostRoundsWon, int highestScore){
       for(int player = 0; player < MAX_PLAYERS ; ++player) {
           if (score[player] == highestScore) {
               roundsWon[player]++;
               mostRoundsWon = Max(mostRoundsWon, roundsWon[player]);
           }
       }
}

int Max(int a, int b){
    int big{a};
    if (a < b) {
       big = b;
    }
    return big;
}

void Swap(DieT &  a, DieT &  b) {
   DieT tmp;
   tmp = a;
   a = b;
   b = tmp;
}

void FirstRoll(DieT & die1, DieT & die2, DieT & die3){
     RollDie(die1);
     RollDie(die2);
     RollDie(die3);

     cout << "\t\tFirst Roll, the player rolled " 
          << GetDieValue(die1) << ", " 
          << GetDieValue(die2) << ", " 
          << GetDieValue(die3) << endl;

     if (GetDieValue(die1) > GetDieValue(die2)
           && GetDieValue(die1) > GetDieValue(die3)) {
         Swap(die1, die3);
     } else if (GetDieValue(die2) > GetDieValue(die3)) {
         Swap(die2, die3);
     }
     cout << "\t\t\tThe highest is " << GetDieValue(die3) << endl;
     return;
}

void SecondRoll(DieT  & die1, DieT & die2) {
     RollDie(die1);
     RollDie(die2);

     cout << "\t\tSecond Roll, the player rolled " 
          << GetDieValue(die1) << ", " 
          << GetDieValue(die2) << endl;

     if (GetDieValue(die1) > GetDieValue(die2)) {
        Swap(die1, die2);
     }
     cout << "\t\t\tThe highest is " << GetDieValue(die2) << endl;
     return;
}

void ThirdRoll(DieT & die1){
     RollDie(die1);
     cout << "\t\tThird Roll, the player rolled " 
          << GetDieValue(die1) << endl;
}

int PlayTurn(int playerNumber, DieT & die1, DieT & die2, DieT & die3){

    cout << endl;
    cout << "\tTaking a turn for player " << playerNumber << endl;
    FirstRoll(die1, die2, die3);
    SecondRoll(die1, die2);
    ThirdRoll(die1);
    int score = GetDieValue(die1) + GetDieValue(die2) + GetDieValue(die3);
    cout << "\t\tThe final die are " << GetDieValue(die1)  << ", " 
                           << GetDieValue(die2) << ", " 
                           << GetDieValue(die3) << endl;
    cout << "\t\tThe total score is " << score << endl;

    return score;
}