| CompSci 190 Fall 2022 |
Programming Games FOCUS Section |
Loops are often a difficult concept to grasp at first, so don’t worry if you get stumped at some point while learning to code with them (but make sure to pay attention to the feeling of being “stuck” and also the feeling of accomplishment when it works!). Also, as the challenge level goes up, don’t forget that you have the logical problem solving process to help you think through the problem.
The game of Pig is a point-based dice game in which the first player to score at least 100 points is the winner. On a player’s turn, they roll two 6-sided dice:
The player can then decide to play again, risking the points earned this turn, or to "hold" — that is, keep the points for the turn and cede the turn.
Open button (or select File → Open), navigate to where you saved the folder, and select itIn the Project Explorer panel, click the arrow beside the lab_pig project to expand the folder and show the two Python files inside that represents the game of Pig:
GameGraphics.py: determines how the game is drawn (dice, progress, messages to the players, etc.)PigRules.py: determines the rules and the winner of the gameRight-click on the file PigRules.py in the Project Explorer and select Run PigRules to play the game. The game will start playing automatically, prompting you to choose to roll again or hold as appropriate (but you may notice some things are not quite right).
We will be focusing on the file PigRules.py and its two functions that are already completed:
play_turn(player_total_score)- This function plays one turn for one player. It should let the player roll until they either choose to stop or they roll a one. Its input is the score for the player at the beginning of their turn. If they roll a single one, it should return 0. If they roll double ones, (snake eyes), it should return -1. Otherwise, it should return the sum of the rolls.
play_game()- This function plays the entire game until a player wins. It should play each player's turn until one of them reaches 100 points.
Here is a video discussing using a debugger and getting started on the project
No matter how skilled a programmer is, no one programs perfectly the first time. Finding and fixing errors is as much a part of the programming process as typing in the program to start with! However, debugging often takes a different set of skills (observing and generating hypotheses) than does programming (problem solving) and, just like programming skills, debugging skills can be honed with some training and practice.
Just like we used a 7-step problem solving process to help get started programming, it can be useful to develop a debugging process (this one is based on a crime detective metaphor):
There are several bugs in the given code for you to find and fix. But before diving in:
When debugging (or just trying to understand code), it can often be helpful to print the values of your variables or use PyCharm's debugger to step through the code. Also, make note of any error messages you see — they are important clues!