Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In the board game Terra Mystica, each player has exactly 12 power units to spend

ID: 3870168 • Letter: I

Question

In the board game Terra Mystica, each player has exactly 12 power units to spend performing actions in the game. Power units are distributed across three "bowls", labeled I, II, and III (the sum of units in these bowls is always exactly 12). As the game progresses, power is gained or spent by shifting units between bowls according to the rules below. If you gain or spend multiple units at once, process them one at a time according to these rules (in other words, use a for loop to repeat your if-elif chain for each spending/gaining operation).
a.   To spend a unit of power:
i.   If you have at least 1 unit in bowl III, move 1 unit from bowl III to bowl I.
ii.   If bowl III is empty, nothing happens.
b.   To gain a unit of power:
i.   If all 12 units are already in bowl III, nothing happens. Otherwise:
ii.   If there is a unit in bowl I, move it to bowl II.
iii.   Otherwise, if bowl I is empty but there is a unit in bowl II, move one unit from bowl II to bowl III.
iv.   If bowls I and II are both empty, then nothing happens.

Complete the updatePower() function. This function takes two arguments: a three-element list of integers (representing bowls I, II, and III, in that order) and a non-zero integer representing the number of power units being gained or spent (a positive value indicates that power is being gained, while a negative value indicates that power is being spent). The function does not return anything, but directly modies its list parameter according to the rules above.

Examples:

•   updatePower([5, 7, 0], 3) ("gain 3 units of power") would update the list contents to [2, 10, 0](just move 3 units from bowl I to bowl II).
•   updatePower([3, 4, 5], 6) ("gain 6 units of power") would update the list contents to [0, 4, 8](move 3 units from bowl I to bowl II, then move 3 units from bowl II to bowl III).
•   updatePower([2, 7, 3], -2) ("spend 2 units of power") would update the list contents to [4, 7, 1](move 2 units from bowl III to bowl I).
•   updatePower([7, 5, 0], -4) ("spend 4 units of power") would leave the list unchanged (bowl III is empty, so there are no units to move to bowl I).

Explanation / Answer

** providing the solution in C#(can be readily ported to other languages as well)

Providing the detailed comments inline -

public List<int> UpdatePower(List<int> bowlContent,int units)
{
int sum = 0;

//Determine the scenario based on the value of units provided
string scenario = units < 0 ? "spend" : "gain";
//Down the line we will be using the Absolue value on the units
int value = Math.Abs(units);
//Calculate the sum of the Bowl Content values
foreach (int item in bowlContent) sum += item;
//if valid input is passed we will continue
if (sum == 12)
{
//For the SPEND scenario
if (scenario == "spend")
{
//if the bowl 3 is already empty, simply return the bowl content
if (bowlContent[2] == 0)
return bowlContent;
else
{
//loop through for adjusting the units
for (int i = value; i > 0; i--)
{
//always check the threshold point to exit the loop, ie Bowl 3 is already empty or not
if (bowlContent[2] == 0)
break;
bowlContent[2]--;
bowlContent[0]++;
}
}
return bowlContent;
}
//Else part for the GAIN scenario
else
{
//If the Bowl 3 is already full,exit
if (bowlContent[2] == 12)
return bowlContent;
else
{
//loop through for adjusting the units
for (int i = value; i > 0; i--)
{
//follow the instruction only unit Bowl 3 is not full
if (bowlContent[2] < 12)
{
//Instruction b.2
if (bowlContent[0] > 0)
{
bowlContent[0]--;
bowlContent[1]++;
}
//Instruction b.3
else if (bowlContent[1] > 0)
{
bowlContent[1]--;
bowlContent[2]++;
}
}
else break;
}
}
return bowlContent;
}
}
else
{
Console.WriteLine("Invalid bowl values are passed, Please try again!");
return bowlContent;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote