Python代写 | Assignment # 6 COMP1005B – Winter 2020

这个作业是用Python完成一个棋盘游戏
Assignment # 6
COMP1005B – Winter 2020
Due on Friday, April 17th by 11:00 pm.
Submission Guidelines:
• Ensure your name and student number are at the top of any files submitted.
• Submit all code files as .py files
• Create a .zip file of all your files, even if there’s only one (this helps TAs keep organized):
o On Windows, select the files you want to include, right click, choose “Send to…”
then “Compressed (zipped) folder”
o On a mac, select the files you want to include, right click, and choose “Compress
X Items”
• Name your zip file Firstname_Lastname_StudID_A6.zip, where Firstname and
Lastname are to be replaced by your names, and StudID is to be replaced by your student
Id number.
• Submit your assignment using cuLearn.
• Late assignments will not be accepted and will receive a mark of 0.
• Submissions that crash (i.e., terminate with an error) on execution will receive a mark of
0.
• If your internet connection at home is down or does not work, we will not accept this as a
reason for handing in an assignment late, so do not wait until the last minute to submit your
assignment!
• You are responsible for submitting all files and ENSURING that the correct file is
submitted, and the submission is completed successfully. If you are having issues with
submission, contact the instructor before the due date.
• You are not allowed to use any python library modules that were not explicitly covered in
the lecture notes.
• Absolutely NO collaborations are allowed.
In this assignment, you will create a text-based board game in Python that will use 2-dimentional
lists, recursions, loops and other programming logics. Create a single Python file called a6.py.
You are expected to add and test several functions in this file that will help the main function to
start and continue the game.
Rules of the game:
This game will be played using a 2D-list (board) that must always have the same number of rows
and columns. The board will be displayed to the user in the following format.
Page 2 of 15
0123456
+=======+
0|G——|0
1|PPPPPPP|1
2|——-|2
3|——-|3
4|——-|4
5|——-|5
6|B-RB–R|6
+=======+
0123456
Please note the following:
• This example game board has 7 rows and 7 columns (7×7).
• The row and column numbers (0,1,..,6 in this example), and the additional symbols (+,=,|)
are not part of the 2D-list. These symbols must be displayed whenever the board is
displayed to the user.
• G: the general will be placed in some random column on row 0.
• P: pawns will be placed in all columns on row 1.
• B: two (2) bishops will be placed in random columns on the last row (row 6 in this
example).
• R: two (2) rooks will be placed in random columns on the last row as well (row 6 in this
example).
• B and R are called soldiers. The user of your program should be able to move these soldiers
(see Valid moves below) to some other locations on this board and try to capture G to win
the game.
Valid moves:
• G can move to any empty column on row 0 only.
• P does not move at all.
• B can move diagonally in four different directions, i.e., left-up, right-up, left-down and
right-down.
• R can move in a straight line in four different directions, i.e., left, right, up, and down.
• B and R cannot pass a P on their way to a new location row,column, unless (a) B/R
captures P on their first (valid) move and (b) then go to row,column in the following
(valid) move.
• G, B, R cannot move to a location if that location is already occupied by another soldier.
Your program must also create another 2D list (trap_map – see below) that should not be visible
to the user but the total number of hidden traps should be displayed in the beginning of the game.
Page 3 of 15
Your program should randomly place some traps (T) on rows 2 to the second last rows on the board
with some predefined probability (see rules in later part of the specifications).
0123456
+=======+
0| |0
1| |1
2| T |2
3| T T |3
4|T T |4
5|T T T |5
6| |6
+=======+
0123456
This board has 8 hidden traps.
How the game continues:
The game starts by displaying the game board and the total number of hidden traps. Then the game
will continue in a loop until (a) G is captured by the soldiers or (b) all four soldiers die. The
maximum board size can be 10×10 and the minimum board size can be 5×5.
At every iteration of the game,
1. Your program should ask the user which soldier they want to move and where.
2. The user must enter these two information (locations) as row,column – where row and
column are valid integer numbers (refer to the max/min board size to check the validity),
separated by a comma. In case the user enters bad inputs (anything other than two valid
integers separated by a comma), they should be asked again unless valid inputs are entered.
3. Next, your program must check if a soldier is present in the current location entered by the
user, if not a message will be displayed to the user and nothing changes.
4. If a soldier is available in the current location, your program must validate the move of that
soldier (see Valid Moves) from their current location to the new location. If the move is
not valid, the soldier will not move, a message will be displayed to the user and nothing
changes.
5. If the move is valid, your program should check if there is any hidden trap on the track
from the current location to the new location. If a trap is there, that soldier dies, and the
user loses 1 point. Subsequently, G will move to a random free location on row 0. The board
should be displayed again showing that trap. A message and the current points will be
shown to the users.
6. If there is no trap on the track, the soldier moves to the new location and one of these three
cases may occur. (a) If the new location contains P, the soldier captures (replaces) P and
the user gets 2 points. G will move to a random free location on row 0. The updated board
should be displayed again. A message and the current points will be shown to the users. (b)
Page 4 of 15
If the new location contains G, the soldier captures G and the user gets 10 points. The
updated board should now display all previously hidden traps. A message and the current
points will be shown to the users. (c) If the new location is empty, the soldier just moves
there, and the updated board should be displayed. A message and the current points will
also be shown to the users.
See the sample runs at the end of this specifications.
Required Functions:
You must write the following functions (not necessarily in this order) as they are the required
components for this assignment. You are encouraged to create any additional helper functions, if
you prefer, to better organize your code.
• create_board() – does not take any parameter and creates a 2D-list to represent the board
with a predefined SIZE, where SIZE is a global constant, with 5 ≤ SIZE ≤ 10. The board
should contain G, P, B, R according to the given rules above. The empty locations should
have ‘-‘ symbols.
• set_traps() – does not take any parameter and creates a 2D-list to represent the trap_map
with a predefined SIZE, where SIZE is a global constant, with 5 ≤ SIZE ≤ 10. The
trap_map should contain traps (T) placed randomly according to the given specifications
above. Set the probability value to 20%, that is your program should place a trap in the current
location with a 20 in 100 chance (or a 2 in 10 chance). This function should also display how
many hidden traps the game board has.
• display_board() – takes a 2D-list as an argument and prints the board to the terminal is
the specified format.
• validate_moves() – takes the following arguments, four integers representing the rows
and columns of the current location and the new location, respectively, and a 2D-list
representing the board. This function returns True if the proposed move is valid (see rules),
otherwise returns False.
• check_traps() – this must be a recursive function that takes the following arguments,
four integers representing the rows and columns of the current location and the new location,
respectively, and a 2D-list representing the trap_map. This function checks for traps on the
proposed track of the soldier from their current location to the new location, and returns the
location of the trap, if exists. Otherwise, it returns False.
Page 5 of 15
• move_general() – takes a 2D-list as an argument representing the board. This function
moves the location of G (see the rules) and returns the updated 2D-list.
• move_soldier() – takes the following arguments, four integers representing the rows and
columns of the current location and the new location, respectively, and two 2D-lists
representing the board and trap_map, respectively. This function follows the rules of the
game and move the soldier to the new location if everything goes according to the rules. It
should also update the board, displays necessary messages to the terminal and returns the
score that the user earned during this move.
• main() – creates two 2D-lists representing the board and the trap_map, respectively. Then
it must start the game that should repeat the steps of the game (see How the game continues),
until the user wins or loses. If the user loses (when all soldiers die), it should display a ‘Game
Over’ message. If the user wins (when G is captured), a ‘You Win’ message should be
displayed with the final score.
——————————————– SAMPLE RUNS ————————————————–
Sample run – 1 (6×6 board):
Game starts now >>
012345
+======+
0|—-G-|0
1|PPPPPP|1
2|——|2
3|——|3
4|——|4
5|B-R-RB|5
+======+
012345
This board has 3 hidden traps!
To move your soldier enter it’s current position <row,col>: 5,2
Enter the new position <row,col>: 1,2
Page 6 of 15
012345
+======+
0|-G—-|0
1|PPRPPP|1
2|——|2
3|——|3
4|——|4
5|B—RB|5
+======+
012345
Your score: 1
To move your soldier enter it’s current position <row,col>: 1,2
Enter the new position <row,col>: 1,3
012345
+======+
0|—G–|0
1|PP-RPP|1
2|——|2
3|——|3
4|——|4
5|B—RB|5
+======+
012345
Your score: 2
To move your soldier enter it’s current position <row,col>: 1,3
Enter the new position <row,col>: 1,4
012345
+======+
0|G—–|0
1|PP–RP|1
2|——|2
3|——|3
4|——|4
5|B—RB|5
+======+
012345
Your score: 3
To move your soldier enter it’s current position <row,col>: 1,4
Enter the new position <row,col>: 0,4
Page 7 of 15
012345
+======+
0|-G–R-|0
1|PP—P|1
2|——|2
3|——|3
4|——|4
5|B—RB|5
+======+
012345
Your score: 3
To move your soldier enter it’s current position <row,col>: 0,4
Enter the new position <row,col>: 0,1
012345
+======+
0|-R—-|0
1|PP—P|1
2|T—T-|2
3|——|3
4|-T—-|4
5|B—RB|5
+======+
012345
Your score: 13
You Win! Final score: 13
Sample run – 2 (5×5 board):
Game starts now >>
01234
+=====+
0|—-G|0
1|PPPPP|1
2|—–|2
3|—–|3
4|-RRBB|4
+=====+
01234
This board has 3 hidden traps!
To move your soldier enter it’s current position <row,col>: 4,2
Enter the new position <row,col>: 1,2
Page 8 of 15
There was a trap at [2,2]. Your soldier dies!
01234
+=====+
0|-G—|0
1|PPPPP|1
2|–T–|2
3|—–|3
4|-R-BB|4
+=====+
01234
Your score: -2
To move your soldier enter it’s current position <row,col>: 4,3
Enter the new position <row,col>: 3,2
There was a trap at [3,2]. Your soldier dies!
01234
+=====+
0|–G–|0
1|PPPPP|1
2|–T–|2
3|–T–|3
4|-R–B|4
+=====+
01234
Your score: -4
To move your soldier enter it’s current position <row,col>: 4,1
Enter the new position <row,col>: 4,0
01234
+=====+
0|-G—|0
1|PPPPP|1
2|–T–|2
3|–T–|3
4|R—B|4
+=====+
01234
Your score: -4
To move your soldier enter it’s current position <row,col>: 4,0
Enter the new position <row,col>: 1,0
There was a trap at [2,0]. Your soldier dies!
Page 9 of 15
01234
+=====+
0|–G–|0
1|PPPPP|1
2|T-T–|2
3|–T–|3
4|—-B|4
+=====+
01234
Your score: -6
To move your soldier enter it’s current position <row,col>: 4,4
Enter the new position <row,col>: 1,1
There was a trap at [2,2]. Your soldier dies!
01234
+=====+
0|—-G|0
1|PPPPP|1
2|T-T–|2
3|–T–|3
4|—–|4
+=====+
01234
Your score: -8
Game over!
Sample run – 3 (7×7 board):
Game starts now >>
0123456
+=======+
0|G——|0
1|PPPPPPP|1
2|——-|2
3|——-|3
4|——-|4
5|——-|5
6|BRR–B-|6
+=======+
0123456
This board has 4 hidden traps!
To move your soldier enter it’s current position <row,col>: 6,0
Enter the new position <row,col>: 5,2
Page 10 of 15
Invalid move for B.
Your score: 0
To move your soldier enter it’s current position <row,col>: okay
Invalid input, enter again.
To move your soldier enter it’s current position <row,col>: 6,0
Enter the new position <row,col>: five,one
Invalid input, enter again.
Enter the new position <row,col>: 5,1
0123456
+=======+
0|—G—|0
1|PPPPPPP|1
2|——-|2
3|——-|3
4|——-|4
5|-B—–|5
6|-RR–B-|6
+=======+
0123456
Your score: 0
To move your soldier enter it’s current position <row,col>: 6,2
Enter the new position <row,col>: 1,2
There was a trap at [5,2]. Your soldier dies!
0123456
+=======+
0|-G—–|0
1|PPPPPPP|1
2|——-|2
3|——-|3
4|——-|4
5|-BT—-|5
6|-R—B-|6
+=======+
0123456
Your score: -2
To move your soldier enter it’s current position <row,col>: 6,1
Enter the new position <row,col>: 6,5
Another soldier is already there. Invalid Move!
Your score: -2
To move your soldier enter it’s current position <row,col>: 6,4
Enter the new position <row,col>: 5,4
Nobody is there at 6,4
Your score: -2
To move your soldier enter it’s current position <row,col>: 6,5
Enter the new position <row,col>: 3,2
Page 11 of 15
0123456
+=======+
0|G——|0
1|PPPPPPP|1
2|——-|2
3|–B—-|3
4|——-|4
5|-BT—-|5
6|-R—–|6
+=======+
0123456
Your score: -2
To move your soldier enter it’s current position <row,col>: 3,2
Enter the new position <row,col>: 0,5
The soldier cannot pass a Pawn.
0123456
+=======+
0|G——|0
1|PPPPPPP|1
2|——-|2
3|–B—-|3
4|——-|4
5|-BT—-|5
6|-R—–|6
+=======+
0123456
Your score: -2
To move your soldier enter it’s current position <row,col>: 3,2
Enter the new position <row,col>: 1,5
Invalid move for B.
Your score: -2
To move your soldier enter it’s current position <row,col>: 3,2
Enter the new position <row,col>: 1,4
0123456
+=======+
0|–G—-|0
1|PPPPBPP|1
2|——-|2
3|——-|3
4|——-|4
5|-BT—-|5
6|-R—–|6
+=======+
0123456
Your score: -1
Page 12 of 15
To move your soldier enter it’s current position <row,col>: 6,1
Enter the new position <row,col>: 6,3
0123456
+=======+
0|—G—|0
1|PPPPBPP|1
2|——-|2
3|——-|3
4|——-|4
5|-BT—-|5
6|—R—|6
+=======+
0123456
Your score: -1
To move your soldier enter it’s current position <row,col>: 6,3
Enter the new position <row,col>: 1,3
0123456
+=======+
0|–G—-|0
1|PPPRBPP|1
2|——-|2
3|——-|3
4|——-|4
5|-BT—-|5
6|——-|6
+=======+
0123456
Your score: 0
To move your soldier enter it’s current position <row,col>: 1,3
Enter the new position <row,col>: 0,3
0123456
+=======+
0|G–R—|0
1|PPP-BPP|1
2|——-|2
3|——-|3
4|——-|4
5|-BT—-|5
6|——-|6
+=======+
0123456
Your score: 0
To move your soldier enter it’s current position <row,col>: 0,3
Enter the new position <row,col>: 0,0
Page 13 of 15
0123456
+=======+
0|R——|0
1|PPP-BPP|1
2|T——|2
3|—T—|3
4|—T—|4
5|-BT—-|5
6|——-|6
+=======+
0123456
Your score: 10
You Win! Final score: 10
Sample run – 4 (valid moves for soldiers):
Game starts now >>
012345
+======+
0|—-G-|0
1|PPPPPP|1
2|——|2
3|——|3
4|——|4
5|-BBR-R|5
+======+
012345
This board has 2 hidden traps!
To move your soldier enter it’s current position <row,col>: 5,5
Enter the new position <row,col>: 2,5
012345
+======+
0|—–G|0
1|PPPPPP|1
2|—–R|2
3|——|3
4|——|4
5|-BBR–|5
+======+
012345
Your score: 0
To move your soldier enter it’s current position <row,col>: 2,5
Enter the new position <row,col>: 2,4
Page 14 of 15
012345
+======+
0|-G—-|0
1|PPPPPP|1
2|—-R-|2
3|——|3
4|——|4
5|-BBR–|5
+======+
012345
Your score: 0
To move your soldier enter it’s current position <row,col>: 2,4
Enter the new position <row,col>: 5,4
012345
+======+
0|—G–|0
1|PPPPPP|1
2|——|2
3|——|3
4|——|4
5|-BBRR-|5
+======+
012345
Your score: 0
To move your soldier enter it’s current position <row,col>: 5,1
Enter the new position <row,col>: 3,3
012345
+======+
0|—–G|0
1|PPPPPP|1
2|——|2
3|—B–|3
4|——|4
5|–BRR-|5
+======+
012345
Your score: 0
To move your soldier enter it’s current position <row,col>: 3,3
Enter the new position <row,col>: 2,2
Page 15 of 15
012345
+======+
0|—G–|0
1|PPPPPP|1
2|–B—|2
3|——|3
4|——|4
5|–BRR-|5
+======+
012345
Your score: 0
To move your soldier enter it’s current position <row,col>: 2,2
Enter the new position <row,col>: 3,1
012345
+======+
0|–G—|0
1|PPPPPP|1
2|——|2
3|-B—-|3
4|——|4
5|–BRR-|5
+======+
012345
Your score: 0
To move your soldier enter it’s current position <row,col>: 3,1
Enter the new position <row,col>: 4,2
012345
+======+
0|G—–|0
1|PPPPPP|1
2|——|2
3|——|3
4|–B—|4
5|–BRR-|5
+======+
012345
Your score: 0
To move your soldier enter it’s current position <row,col>: