Play Game Flashcards
(13 cards)
what are the parameters called in playGame?
(List<int> Targets, List<int> NumbersAllowed, bool TrainingGame, int MaxTarget, int MaxNumber)</int></int>
State the local variables inside PlayGame
Score: Tracks how many points the user has.
GameOver: Controls when to break from the game loop. Gains when hitting a target, loses 1 each turn.
UserInput: The raw expression the user types in each turn.
UserInputInRPN: The user’s expression converted to Reverse Polish Notation.
Briefly explain the gameplay loop in playGame
Repeats until GameOver becomes true.
First calls DisplayState which in turn calls DisplayTargets, DisplayNumbersAllowed, and DisplayScore.
while (!GameOver) keeps running until a condition sets GameOver = true.
DisplayState(Targets, NumbersAllowed, Score);
How is the user input validated, in regards with the expression they type?
Reads the user’s typed math expression, like 2+3*8.
- Uses CheckIfUserInputValid (a regex) to ensure it’s a valid format.
If valid, it calls ConvertToRPN to transform a standard expression with operator precedence into a form that’s easy to evaluate.
UserInput = Console.ReadLine();
Console.WriteLine();
if (CheckIfUserInputValid(UserInput))
UserInputInRPN = ConvertToRPN(UserInput);
Explain what this code does:
if
{
(CheckNumbersUsedAreAllInNumbersAllowed(NumbersAllowed, UserInputInRPN, MaxNumber))
{
if (CheckIfUserInputEvaluationIsATarget(Targets, UserInputInRPN, ref Score))
{
RemoveNumbersUsed(UserInput, MaxNumber, NumbersAllowed);
NumbersAllowed = FillNumbers(NumbersAllowed, TrainingGame, MaxNumber);
}
}
Ensures the user only used numbers that appear in NumbersAllowed.
If the evaluated result matches one or more targets, award points, remove the target(s) by setting them to -1, and remove the used numbers from NumbersAllowed.
Then refill the NumbersAllowed list if needed.
How does the scoring system work , each time user enters an expression?
Each turn, the user loses 1 point for taking the turn, forcing them to solve targets efficiently rather than spamming random expressions.
Score–;
How does the game check whether the game ends while game loop is running?
If the first element in Targets is no longer -1, it means we have a “live” target in the front that the user hasn’t cleared in time, so the game ends.
if (Targets[0] != -1)
{
GameOver = true;
}
Otherwise, shift the queue of targets and generate a new one with UpdateTargets.
else
{
UpdateTargets(Targets, TrainingGame, MaxTarget);
}
What happens in PLayGame when checking for game over conditions?
If the first target in the list is not -1, that means we have a “live” target at position 0 that the user hasn’t cleared, so the game ends.
Otherwise, we shift the queue with UpdateTargets, removing the front item and adding a new one.
What are the main functions of the PlayGame method:
user input,
validating,
evaluating,
scoring,
checking game-over conditions,
updating the board.
what is target clear and how does it work?
Target Clear” is a game where you start with a queue of “targets,” each of which is an integer the user tries to “hit” by typing arithmetic expressions. The user also has a limited set of “numbers allowed” each turn.
explain the game loop for target clear
The program checks if the expression is valid (regex check).
It converts that expression to Reverse Polish Notation (RPN) to handle operator precedence.
It evaluates the RPN expression to a single result.
If that result matches any of the “targets,” the user scores points, and those matching targets are marked cleared (represented by -1).
The numbers used to form that expression are removed from the “numbers allowed” and partially replenished.
If the leftmost (front) target is no longer marked as cleared (meaning it’s not -1), the game ends; otherwise, new targets come in from the “back” (like a conveyor belt).
Each turn costs you 1 point from your score, so you need to solve targets efficiently
explain how ref Position is used in CheckIfUserInputEvaluationIsATarget and why the ref is calculated in this way?
ref means that if this function modifies Score, those modifications are reflected back in the variable in the calling method. It’s like passing a direct reference, not just a copy of the integer.
This approach is chosen so we don’t need to return the score from the function. Instead, the function returns a bool for whether the user’s expression matched a target, and uses ref Score for the actual numeric effect.
what are the conditions after the user hits the target?
Remove the numbers they used in the expression from NumbersAllowed.
Refill if the list now has fewer than 5 numbers.