Question
Backgammon is a two-player game played on a board with 24 triangles known as points . Each player starts with 15 checkers, with an initial
Backgammon is a two-player game played on a board with 24 triangles known as points. Each player starts with 15 checkers, with an initial configuration as shown above. The points alternate in color as you go around the board, and there is a clear top-to-bottom symmetry to the placement of the checkers.
We have chosen this assignment as a way to exercise your use of control structures. Although the board is fixed and finite, and you could presumably create the image above by manually configuring the geometry for 24 different triangles, 30 circles, 24 text labels, and so on, you will only receive significant credit if you use control structures to express these patterns. As a general rule, if you ever find yourself "copying-and-pasting" code from one part of a program to another, there should be a more elegant way to express the repetition and variation.
You should maintain a size variable that determines the width of each of those grid cells, so that you can do all other geometry computations based on the concept of such a width. This will allow you to draw the board at whatever scale you want, while keeping the rest of your geometric computations relatively clean.
You will notice that the points themselves are labeled from 1 to 24 in a clockwise fashion starting at the bottom-left. Each triangle has a width of precisely one cell and a height of 5 cells. There is a vertical separation of one cell width between the top and bottom points, as well as the horizontal separation between the left-half and right-half of the board.
The only place that we deviate from the unit grid size measure is for the checkers. Although they appear to have a diameter of one cell width, they are actually slightly smaller. In particular, we have given each a diameter of 0.9 relative to the cell width, and we have stacked them against each other based on their actual size; this makes a better appearance as more of the underlying point shows. But this means that the checker centers are not aligned precisely with the center of the grid cells.
The other issue with the checkers is that we would like you to use control structures rather than to draw 30 different checkers, but the "pattern" of the initial checkers is not regular. What is needed to code this cleanly is to understand what aspects of the checkers change. There are three such aspects:
- How many checkers lie on the given point
- Which point the checkers are on
- Whether white's checkers are on the top-half or bottom-half of the board.
To best manage this complexity, we simply need to carefully encode the settings for these choices. In particular, there are four sets of checkers, going from left to right:
- two checkers on the first (and 24th) point, with white on top
- five checkers on the sixth (and 19th) point, with white on bottom
- three checkers on the eighth (and 17th) point, with white on bottom
- five checkers on the twelfth (and 13th) point, with white on top
To get this type of irregular repetition, we can rely on a for-loop that uses an explicit sequence of values to encode the pattern we seek. In particular, we can hardcode the pattern using the following syntax.
for num,pt,whiteOnTop in [(2,1,True), (5,6,False), (3,8,False), (5,12,True)]:
This is a bit new, in that we are using a for loop to set a triple of variables rather than just one. That is, we're used to seeing something like
for x in sequence:
But if the sequence is a sequence of tuples, you are allowed to set multiple variables to the values in those tuples. That is, we could do something like
for x,y in [ (1,2), (7,5), (4,8), (3,13), (2,15)]:
as a way to describe a loop with five passes, such that in the first pass x=1 and y=2, and in the second pass x=7 and y=5, and so on.
Going back to our backgammon applications, we have four different repetitions we need, where each one is defined by a triple designating the number of checkers, the point for those checkers, and whether white checkers are at the top of the board (rather than the bottom).
what is the code for this in python?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started