辅导python game-playing strategy程序、辅导python留学生game

- 首页 >> Python编程


Finally now, implement your game-playing strategy, optionally making use of your own implementations of  comp10001bo_match_build, comp10001bo_match_discard, and comp10001bo_is_valid_play  (in which case, copy your code from the previous questions into common.py along with any constants and helper functions).

Write the function comp10001bo_play(player_no, hand,stockpiles, discard_piles, build_piles, play_history) that takes the following arguments:

player_no: an integer representing the player number of the player attempting the play (of value 0-3)

hand: a list of cards held by the current player

stockpiles: a 4-tuple describing the content of the stockpiles for each of the four players, each of which is in the form of a 2-tuple, made up of the top card (a string) and the number of cards in the stockpile (including the top card); note that the stockpiles are indexed according to player number

discard_piles: a 4-tuple describing the content of the discard piles for each of the four players, each of which is in the form of a 4-tuple of lists of cards (with the final card in the list being the top card of that pile); note that the discard piles are indexed according to player number

build_piles: a 4-tuple of lists of cards describing the content of the build piles, each of which is in the form of a list of cards (with the final card in the list being the top card of that pile)

play_history: a list of 2-tuples specifying the sequence of plays to this point in the game, where each 2-tuple is made up of: (1) the ID of the player making the move, and (2) the play, based on the same structure as comp10001_is_valid_play, namely:

oplay_type: an integer, where 0 indicates a play from the hand, 1 indicates a play from a discard pile, 2 indicates a play from the player's stockpile, and 3 indicates that no play is possible

osource: a specification of where the card is to be taken from; for a play from the hand, this is the card to be played (a string); for a play from a discard pile, this is a 2-tuple made up of the card and the identity of the discard pile (itself a 2-tuple, made up of the player number and discard pile number, e.g. (2, 1) indicates Player 2, discard pile 1); for a play from the player's stockpile, this is the card to be played (a string); and in the instance of there being no valid play, the value should be None

odestination: a specification of where the card is to be played to, in the form of a 2-tuple indicating the destination pile type (0 = build pile; 1 = discard pile), and the identity of the pile (an integer 0-3 in the case of a build pile, and a 2-tuple of the player number and discard pile number in the case of discard pile, e.g. (0, 3)indicates Player 0 build pile 3); in the instance that there is no valid play, the value should be (None, None)

The function should return a 3-tuple stipulating the play you wish to make, based on the same play format as for play_history above.

Note that your hand will automatically be replenished to 5 cards before each turn, and that your function will only be called in you haven't exhausted your plays for the turn/explicitly terminated your turn. Additionally, if a build pile is completed, it will automatically be removed and shuffled in with the draw pile.

Here are some example calls to the comp10001bo_play function (noting that these require that there is a single possible play possible for the given game state; to simplify the test cases, we have created an impossible game state, namely that all hands are empty and the draw pile is also empty):


>>> # no possible play

>>> comp10001bo_play(0, [], (('7H', 7), ('3C', 8), ('3H', 8), ('KD', 8)), ((['7H'], [], [], []), ([], [], [], []), ([], [], [], []), ([], [], [], [])), (['2C'], [], [], []), [(0, (2, '2C', (0, 0)))])

(3, None, (None, None))

>>> # play from stockpile to build pile

>>> comp10001bo_play(1, [], (('7H', 7), ('3C', 8), ('3H', 8), ('KD', 8)), ((['7H'], [], [], []), ([], [], [], []), ([], [], [], []), ([], [], [], [])), (['2C'], [], [], []), [(0, (2, '2C', (0, 0))), (0, (3, None, (None, None))), (1, (2, '3C', (0, 0)))])

(2, '3C', (0, 0))

>>> # play from stockpile to build pile

>>> comp10001bo_play(1, [], (('7H', 7), ('4S', 7), ('3H', 8), ('KD', 8)), ((['7H'], [], [], []), ([], [], [], []), ([], [], [], []), ([], [], [], [])), (['2C', '3C'], [], [], []), [(0, (2, '2C', (0, 0))), (0, (3, None, (None, None))), (1, (2, '3C', (0, 0)))])

(2, '4S', (0, 0))

>>> # play from stockpile to build pile, with example play

>>> comp10001bo_play(1, [], (('7H', 7), ('3S', 6), ('3H', 8), ('KD', 8)), ((['7H'], [], [], []), ([], [], [], []), ([], [], [], []), ([], [], [], [])), (['2C', '3C', '4S'], [], [], []), [(0, (2, '2C', (0, 0))), (0, (3, None, (None, None))), (1, (2, '3C', (0, 0))), (1, (2, '4S', (0, 0)))])

(2, '3S', (0, 0))

>>> # no valid play possible

>>> comp10001bo_play(1, [], (('7H', 7), ('0D', 5), ('3H', 8), ('KD', 8)), ((['7H'], [], [], []), ([], [], [], []), ([], [], [], []), ([], [], [], [])), (['2C', '3C', '4S', '3S'], [], [], []), [(0, (2, '2C', (0, 0))), (0, (3, None, (None, None))), (1, (2, '3C', (0, 0))), (1, (2, '4S', (0, 0))), (1, (2, '3S', (0, 0)))])

(3, None, (None, None))


站长地图