Problem 5 (400pts)
注意!!!助教不知道脑子哪里抽风了把错的文件打包了进去!!!
如果你是在10月14日11点之前在QQ群里获得的代码(文件名版本号小于1.6)或者在课程网站获得的代码,请重新下载最新的代码包,并且用
tests
文件夹中的05.py
、06.py
、……、11.py
这些文件覆盖你之前已经解压出来的对应文件。在解锁Problem 05测试点的过程中,你会遇到这样一个问题:
>>> # Hint: use Python! >>> s0, s1 = hog.play(always(3), always(5), goal=20, dice=always_four) >>> s0 ?
这个问题的答案不是15。如果你输入15却显示Ok,说明你的文件是错误的,用错误的文件测试代码会导致你的代码不正确。
再注意一下!!!!!!助教不知道脑子哪里抽风了把错的文件打包了进去!!!
如果你是在10月13日23点之前在QQ群里获得的代码(文件名版本号小于1.5)或者在课程网站获得的代码,请重新下载最新的代码包,并且覆盖
hog_gui.py
这个文件和gui_files
这个文件夹。不操作的话,对于你完成作业没有影响,但是你玩不到好玩的图形界面了,嘿嘿嘿。
Implement the play
function, which simulates a full game of Hog.
Players take turns rolling dice until one of the players reaches the goal
score.
A turn is defined as one roll of the dice.
To determine how many dice are rolled each turn, each player uses their respective strategy (Player 0 uses strategy0
and Player 1 uses strategy1
).
A strategy is a function that, given a player's score and their opponent's score, returns the number of dice that the current player will roll in the turn.
Don't worry about implementing strategies yet; you'll do that in Phase 3.
Important: Your implementation should only need to use a single loop; you don't need multiple loops. Additionally, each strategy function should be called only once per turn. This means you only want to call strategy0 when it is Player 0's turn and only call strategy1 when it is Player 1's turn. Otherwise, the GUI and some ok tests may get confused.
If a player achieves the goal score by the end of their turn, i.e. after all applicable rules have been applied, the game ends.
play
will then return the final total scores of both players, with Player 0's score first and Player 1's score second.
Hints:
- You should call the functions you have implemented already.
- Call
take_turn
with four arguments (don't forget to pass in thegoal
). Only calltake_turn
once per turn.- Call
swine_swap
to determine if two players will swap their scores.- You can get the number of the next player (either 0 or 1) by calling the provided function `next_player.
- You can ignore the
say
argument to theplay
function for now. You will use it in Phase 2 of the project. For the unlocking tests,hog.always_roll
refers to thealways_roll
function defined inhog.py
.
Before writing any code, unlock the tests to verify your understanding of the question.
$ python ok -q 05 -u
Once you are done unlocking, begin implementing your solution. You can check your correctness with:
$ python ok -q 05
Once you are finished, you will be able to play a graphical version of the game. We have provided a file called hog_gui.py
that you can run from the terminal:
$ python hog_gui.py
The GUI relies on your implementation, so if you have any bugs in your code, they will be reflected in the GUI. This means you can also use the GUI as a debugging tool; however, it's better to run the tests first.
Congratulations! You have finished Phase 1 of this project!