本文整理汇总了Python中gamestate.GameState.apply方法的典型用法代码示例。如果您正苦于以下问题:Python GameState.apply方法的具体用法?Python GameState.apply怎么用?Python GameState.apply使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gamestate.GameState
的用法示例。
在下文中一共展示了GameState.apply方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_build_house
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_build_house(self):
state = GameState(1)
# Set up a player to own oranges with no houses
player = state.players[0]
oranges = [ST_JAMES_PLACE, TENNESSEE_AVENUE, NEW_YORK_AVENUE]
changes = []
for prop_name in oranges:
changes.append(GameStateChange.buy_property(state.squares[INDEX[prop_name]],
player, state.bank))
state.apply(GroupOfChanges(changes))
# Test house build
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.build(state.squares[INDEX[NEW_YORK_AVENUE]], state)
]))
str_after = str(state)
expected_diff = [
('Cash: 940', 'Cash: 840'), # player cash
('Cash: 560', 'Cash: 660'), # bank cash
('Num houses: 0', 'Num houses: 1'), # new york avenue
('Houses remaining: 32', 'Houses remaining: 31')
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='House build was not applied properly')
示例2: test_unmortgage
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_unmortgage(self):
state = GameState(1)
player = state.players[0]
# Set up player to own a mortgaged utility
state.apply(GroupOfChanges([
GameStateChange.buy_property(state.squares[INDEX[ELECTRIC_COMPANY]],
player, state.bank, mortgaged=True)
]))
# Test unmortgage
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.unmortgage(state.squares[INDEX[ELECTRIC_COMPANY]], state)
]))
str_after = str(state)
expected_diff = [
# Player cash
('Cash: 1425', 'Cash: 1342'),
# Bank cash
('Cash: 75', 'Cash: 158'),
# Electric Company stats
('Mortgaged: True', 'Mortgaged: False')
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='Electric Company was not unmortgaged properly')
示例3: test_buy_property_mortgaged
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_buy_property_mortgaged(self):
state = GameState(1)
player = state.players[0]
# Test buying Pennsylvania Avenue mortgaged
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.buy_property(state.squares[INDEX[PENNSYLVANIA_AVENUE]],
player, state.bank, mortgaged=True)
]))
str_after = str(state)
expected_diff = [
# Player stats
('Cash: 1500', 'Cash: 1340'),
('', 'Pennsylvania Avenue, '),
('6: 0', '6: 1'),
# Bank stats
('Cash: 0', 'Cash: 160'),
('Mediterranean Avenue, Baltic Avenue, Reading Railroad, Oriental Avenue, Vermont Avenue, Connecticut Avenue, St. Charles Place, Electric Company, States Avenue, Virginia Avenue, Pennsylvania Railroad, St. James Place, Tennessee Avenue, New York Avenue, Kentucky Avenue, Indiana Avenue, Illinois Avenue, B. & O. Railroad, Atlantic Avenue, Ventnor Avenue, Water Works, Marvin Gardens, Pacific Avenue, North Carolina Avenue, Pennsylvania Avenue, Short Line Railroad, Park Place, Boardwalk, ',
'Mediterranean Avenue, Baltic Avenue, Reading Railroad, Oriental Avenue, Vermont Avenue, Connecticut Avenue, St. Charles Place, Electric Company, States Avenue, Virginia Avenue, Pennsylvania Railroad, St. James Place, Tennessee Avenue, New York Avenue, Kentucky Avenue, Indiana Avenue, Illinois Avenue, B. & O. Railroad, Atlantic Avenue, Ventnor Avenue, Water Works, Marvin Gardens, Pacific Avenue, North Carolina Avenue, Short Line Railroad, Park Place, Boardwalk, '),
('6: 3', '6: 2'),
# Pennsylvania Avenue stats
('Mortgaged: False', 'Mortgaged: True')
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='Pennsylvania Avenue was not mortgaged properly')
示例4: test_buy_property_from_something
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_buy_property_from_something(self):
state = GameState(1)
player = state.players[0]
# Set up player with some properties (two of the three greens)
changes = []
for prop_name in [PACIFIC_AVENUE, PENNSYLVANIA_AVENUE]:
changes.append(GameStateChange.buy_property(state.squares[INDEX[prop_name]],
player, state.bank))
state.apply(GroupOfChanges(changes))
# Test buying North Carolina Avenue
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.buy_property(state.squares[INDEX[NORTH_CAROLINA_AVENUE]],
player, state.bank)
]))
str_after = str(state)
expected_diff = [
# Player 1 stats
('Cash: 880', 'Cash: 580'),
('Pacific Avenue, Pennsylvania Avenue, ', 'Pacific Avenue, Pennsylvania Avenue, North Carolina Avenue, '),
('6: 2', '6: 3'),
# Bank stats
('Cash: 620', 'Cash: 920'),
('Mediterranean Avenue, Baltic Avenue, Reading Railroad, Oriental Avenue, Vermont Avenue, Connecticut Avenue, St. Charles Place, Electric Company, States Avenue, Virginia Avenue, Pennsylvania Railroad, St. James Place, Tennessee Avenue, New York Avenue, Kentucky Avenue, Indiana Avenue, Illinois Avenue, B. & O. Railroad, Atlantic Avenue, Ventnor Avenue, Water Works, Marvin Gardens, North Carolina Avenue, Short Line Railroad, Park Place, Boardwalk, ',
'Mediterranean Avenue, Baltic Avenue, Reading Railroad, Oriental Avenue, Vermont Avenue, Connecticut Avenue, St. Charles Place, Electric Company, States Avenue, Virginia Avenue, Pennsylvania Railroad, St. James Place, Tennessee Avenue, New York Avenue, Kentucky Avenue, Indiana Avenue, Illinois Avenue, B. & O. Railroad, Atlantic Avenue, Ventnor Avenue, Water Works, Marvin Gardens, Short Line Railroad, Park Place, Boardwalk, '),
('6: 1', '6: 0')
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='North Carolina Avenue was not purchased properly')
示例5: test_transfer_money_bank_to_player
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_transfer_money_bank_to_player(self):
import random
state = GameState(1)
player = state.players[0]
# Transfer random amounts of money, and test that GameState is correct
for trial in range(0, 100):
player_cash_before = player.cash
bank_cash_before = state.bank.cash
amount = random.randint(1, player_cash_before)
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.transfer_money(state.bank, player, amount)]))
str_after = str(state)
expected_diff = [
# Player cash
('Cash: %d' % player_cash_before,
'Cash: %d' % (player_cash_before + amount)),
# Bank cash
('Cash: %d' % bank_cash_before,
'Cash: %d' % (bank_cash_before - amount))
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='$%d was not transferred to player correctly. Here is diff:' % amount)
示例6: test_change_position_landing_on_go
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_change_position_landing_on_go(self):
state = GameState(1)
player = state.players[0]
# Set up player's initial position at Short Line Railroad
state.apply(GroupOfChanges([
GameStateChange.change_position(player, INDEX[SHORT_LINE_RAILROAD], state.bank,
state.squares)
]))
# Test player changing position to Go
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.change_position(player, INDEX[GO], state.bank, state.squares)
]))
str_after = str(state)
expected_diff = [
# Player stats
('Position: %d' % INDEX[SHORT_LINE_RAILROAD],
'Position: %d' % INDEX[GO]),
('Cash: 1500', 'Cash: 1700'),
# Bank stats
('Cash: 0', 'Cash: -200')
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='Player did not pass Go properly')
示例7: test_mortgage
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_mortgage(self):
state = GameState(1)
player = state.players[0]
# Set up player to own a railroad
state.apply(GroupOfChanges([
GameStateChange.buy_property(state.squares[INDEX[PENNSYLVANIA_RAILROAD]],
player, state.bank)
]))
# Test mortgage
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.mortgage(state.squares[INDEX[PENNSYLVANIA_RAILROAD]], state)
]))
str_after = str(state)
expected_diff = [
# Player cash
('Cash: 1300', 'Cash: 1400'),
# Bank cash
('Cash: 200', 'Cash: 100'),
# Pennsylvania Railroad stats
('Mortgaged: False', 'Mortgaged: True')
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='Pennsylvania Railroad was not mortgaged properly')
示例8: test_eliminate_to_player
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_eliminate_to_player(self):
state = GameState(2)
player_eliminated = state.players[0]
player_eliminator = state.players[1]
# Set up players to have some clout
self.setup_eliminated_player(state)
self.setup_eliminator_player(state)
# Move player_eliminated to a square where he would likely lose to the
# other player (e.g. Marvin Gardens)
state.apply(GroupOfChanges([
GameStateChange.change_position(player_eliminated, INDEX[MARVIN_GARDENS],
state.bank, state.squares)
]))
# Eliminate player_eliminated to player_eliminator, and test that
# player_eliminated's belongings are properly transferred to the
# player_eliminator and that no other changes are made to the state.
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.eliminate(player_eliminated, player_eliminator, state)]))
str_after = str(state)
expected_diff = [
# Eliminated player stats
('Position: %d' % INDEX[MARVIN_GARDENS], 'Position: -1'),
('Cash: 560', 'Cash: 0'),
('Oriental Avenue, Vermont Avenue, Connecticut Avenue, States Avenue, Short Line Railroad, ', ''),
('1: 3', '1: 0'),
('2: 1', '2: 0'),
('100: 1', '100: 0'),
('Jail free count: 1', 'Jail free count: 0'),
('Is in game: True', 'Is in game: False'),
# Eliminator player stats
('Cash: 250', 'Cash: 1035'),
('Atlantic Avenue, Ventnor Avenue, Marvin Gardens, Reading Railroad, Pennsylvania Railroad, B. & O. Railroad, ',
'Atlantic Avenue, Ventnor Avenue, Marvin Gardens, Reading Railroad, Pennsylvania Railroad, B. & O. Railroad, Oriental Avenue, Vermont Avenue, Connecticut Avenue, States Avenue, Short Line Railroad, '),
('1: 0', '1: 3'),
('2: 0', '2: 1'),
('100: 3', '100: 4'),
('Jail free count: 0', 'Jail free count: 1'),
# Property stats
('Num houses: 3', 'Num houses: 0'), # Oriental Avenue
('Num houses: 3', 'Num houses: 0'), # Vermont Avenue
('Num houses: 3', 'Num houses: 0'), # Connecticut Avenue
# Housing stats
('Houses remaining: 14', 'Houses remaining: 23') # 9 houses from light blues
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='Player was not eliminated properly. The following changes were made to the GameState:')
示例9: test_eliminate_to_bank
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_eliminate_to_bank(self):
state = GameState(1)
player = state.players[0]
# Set up player to have some clout
self.setup_eliminated_player(state)
# Move player to a square where he would likely lose to the bank (e.g.
# Luxury Tax)
state.apply(GroupOfChanges([
GameStateChange.change_position(player, INDEX[LUXURY_TAX], state.bank,
state.squares)
]))
# Eliminate the player to the bank, and test that the player's belongings
# are properly transferred to the bank and that no other changes are
# made to the state.
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.eliminate(player, state.bank, state)]))
str_after = str(state)
expected_diff = [
# Player stats
('Position: %d' % INDEX[LUXURY_TAX], 'Position: -1'),
('Cash: 560', 'Cash: 0'),
('Oriental Avenue, Vermont Avenue, Connecticut Avenue, States Avenue, Short Line Railroad, ', ''),
('1: 3', '1: 0'),
('2: 1', '2: 0'),
('100: 1', '100: 0'),
('Jail free count: 1', 'Jail free count: 0'),
('Is in game: True', 'Is in game: False'),
# Bank stats
('Cash: 940', 'Cash: 1725'),
('Mediterranean Avenue, Baltic Avenue, Reading Railroad, St. Charles Place, Electric Company, Virginia Avenue, Pennsylvania Railroad, St. James Place, Tennessee Avenue, New York Avenue, Kentucky Avenue, Indiana Avenue, Illinois Avenue, B. & O. Railroad, Atlantic Avenue, Ventnor Avenue, Water Works, Marvin Gardens, Pacific Avenue, North Carolina Avenue, Pennsylvania Avenue, Park Place, Boardwalk, ',
'Mediterranean Avenue, Baltic Avenue, Reading Railroad, St. Charles Place, Electric Company, Virginia Avenue, Pennsylvania Railroad, St. James Place, Tennessee Avenue, New York Avenue, Kentucky Avenue, Indiana Avenue, Illinois Avenue, B. & O. Railroad, Atlantic Avenue, Ventnor Avenue, Water Works, Marvin Gardens, Pacific Avenue, North Carolina Avenue, Pennsylvania Avenue, Park Place, Boardwalk, Oriental Avenue, Vermont Avenue, Connecticut Avenue, States Avenue, Short Line Railroad, '),
('1: 0', '1: 3'),
('2: 2', '2: 3'),
('100: 3', '100: 4'),
# Property stats
('Num houses: 3', 'Num houses: 0'), # Oriental Avenue
('Num houses: 3', 'Num houses: 0'), # Vermont Avenue
('Num houses: 3', 'Num houses: 0'), # Connecticut Avenue
('Mortgaged: True', 'Mortgaged: False'), # States Avenue
('Mortgaged: True', 'Mortgaged: False'), # Short Line Railroad
# Housing stats
('Houses remaining: 23', 'Houses remaining: 32') # 9 houses from light blues
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='Player was not eliminated properly. The following changes were made to the GameState:')
示例10: test_demolish_house
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_demolish_house(self):
state = GameState(1)
# Set up a player to own a property with 1 house
player = state.players[0]
park_place = state.squares[INDEX[PARK_PLACE]]
boardwalk = state.squares[INDEX[BOARDWALK]]
state.apply(GroupOfChanges([
GameStateChange.buy_property(park_place, player, state.bank)]))
state.apply(GroupOfChanges([
GameStateChange.buy_property(boardwalk, player, state.bank)]))
state.apply(GroupOfChanges([
GameStateChange.build(boardwalk, state)]))
# Test applying the changes by comparing differences in their string
# encodings. Ensure that no additional changes were made to the state.
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.demolish(boardwalk, state)]))
str_after = str(state)
expected_diff = [
('Cash: 550', 'Cash: 650'), # player cash
('Cash: 950', 'Cash: 850'), # bank cash
('Num houses: 1', 'Num houses: 0'),
('Houses remaining: 31', 'Houses remaining: 32')
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='House demolition was not applied correctly')
示例11: test_draw_card
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_draw_card(self):
state = GameState(1)
player = state.players[0]
# Test every card in both decks
dict_card_types = { CHANCE_CARD: 'Chance', COMMUNITY_CHEST_CARD: 'Community Chest' }
for card_type, card_str in dict_card_types.iteritems():
deck = state.decks[card_type]
# Draw every card in the deck, check that cards are handled correctly
# and that nothing else in the state is changed
card = None
for i in range(0, deck.size()):
card = deck.peek()
jail_free_count_before = player.jail_free_count
str_before = str(state)
expected_diff = None # initialized in the following if-block
state.apply(GroupOfChanges([
GameStateChange.draw_card(deck, player)]))
if card == LMBDA_GET_OUT_OF_JAIL_FREE:
# Check that the card is not replaced on the deck, and that the
# player's Jail Free card count is incremented.
self.assertEqual(player.jail_free_count, jail_free_count_before + 1)
for j in range(0, deck.size()):
self.assertNotEqual(deck.draw(), LMBDA_GET_OUT_OF_JAIL_FREE)
# Initialize. Used after this if-block to ensure that nothing else
# in the state was changed.
expected_diff = [
('Jail free count: %d' % jail_free_count_before,
'Jail free count: %d' % player.jail_free_count)
]
else:
# Check that the card is replaced on the bottom if it is not the
# Jail Free card.
for j in range(0, deck.size() - 1):
deck.draw()
self.assertEqual(deck.draw(), card) # compare with last card
# Initialize
expected_diff = []
# Check that the rest of the state is unchanged by comparing the
# string encodings of the GameStates
str_after = str(state)
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='The GameState was not modified correctly')
示例12: test_change_position_from_start
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_change_position_from_start(self):
state = GameState(1)
player = state.players[0]
# Test player changing position from initial position (Go) to first
# Chance square
chance1 = INDEX[CHANCE_1]
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.change_position(player, chance1, state.bank, state.squares)
]))
str_after = str(state)
expected_diff = [
('Position: 0', 'Position: %d' % chance1)
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='Player was not moved to first Chance square properly')
示例13: test_send_to_jail
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_send_to_jail(self):
state = GameState(1)
player = state.players[0]
# Send the player to jail, and compare GameState's string encodings to
# ensure that only the player's position was changed
position_before = player.position
position_jail = INDEX[JAIL]
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.send_to_jail(player)]))
str_after = str(state)
expected_diff = [
('Position: %d' % position_before, 'Position: %d' % position_jail),
('Jail moves: 0', 'Jail moves: 3')
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='Player was not sent to jail properly')
示例14: test_transfer_money_player_to_player
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_transfer_money_player_to_player(self):
import random
state = GameState(2)
# Transfer money, and test that GameState is correct
for trial in range(0, 100):
# Restock players if they run out of cash during trials
for i in range(0, 2):
if state.players[i].cash == 0:
state.players[i].cash = 1500
# Test transfer
cash_before = [0] * 2
for i in range(0, 2):
cash_before[i] = state.players[i].cash
pfrom = random.randint(0, 1)
pto = 1 - pfrom
amount = random.randint(1, cash_before[pfrom])
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.transfer_money(state.players[pfrom], state.players[pto],
amount)
]))
str_after = str(state)
expected_diff = [
# pfrom cash
('Cash: %d' % cash_before[pfrom],
'Cash: %d' % (cash_before[pfrom] - amount)),
# pto cash
('Cash: %d' % cash_before[pto],
'Cash: %d' % (cash_before[pto] + amount))
]
# Player stats must be listed in numerical order, so swap the order
# if pfrom is not the 0'th player
if pfrom == 1:
expected_diff.reverse()
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='$%d was not transferred between players correctly. Here is diff:' % amount)
示例15: test_decrement_jail_moves
# 需要导入模块: from gamestate import GameState [as 别名]
# 或者: from gamestate.GameState import apply [as 别名]
def test_decrement_jail_moves(self):
state = GameState(1)
player = state.players[0]
# Set up player in jail
state.apply(GroupOfChanges([
GameStateChange.send_to_jail(player)]))
# Decrement jail moves, and test that the player's jail moves were changed
# correctly and that no other changes were made to the state.
str_before = str(state)
state.apply(GroupOfChanges([
GameStateChange.decrement_jail_moves(player)]))
str_after = str(state)
expected_diff = [
('Jail moves: 3', 'Jail moves: 2')
]
self.assertDiffGameStates(str_before, str_after, expected_diff,
msg='Player jail moves were not decremented properly')