Contract con_go_v1


Contract Code


  
1 # con_go_v1
2
3 NUM_ROWS = NUM_COLS = 8
4 NUM_SQUARES = NUM_ROWS * NUM_COLS
5 INITIAL_BOARD = ' '
6
7 OPPOSING = {
8 'b': 'w',
9 'w': 'b',
10 }
11
12 INITIAL_STATE = {
13 'current_player': 'b',
14 'board': str(INITIAL_BOARD),
15 'creator_team': 'b',
16 'opponent_team': 'w',
17 }
18
19 assert len(INITIAL_BOARD) == NUM_SQUARES, 'Invalid initial board.'
20
21
22 def coords_to_index(x: int, y: int) -> int:
23 assert valid_coords(x, y), 'Invalid (x, y): ({}, {})'.format(x, y)
24 return x * NUM_ROWS + y
25
26
27 def index_to_coords(index: int) -> tuple:
28 assert valid_index(index), 'Invalid index: {}'.format(index)
29 x = index // NUM_ROWS
30 y = index % NUM_ROWS
31 return (x, y)
32
33
34 def valid_index(index: int) -> bool:
35 return index >= 0 and index < NUM_SQUARES
36
37
38 def valid_coords(x: int, y: int) -> bool:
39 return x >= 0 and x < NUM_ROWS and y >= 0 and y < NUM_COLS
40
41
42 @export
43 def get_initial_state(x: str = None) -> dict:
44 return INITIAL_STATE
45
46
47 def opposing_team(team: str) -> str:
48 return OPPOSING[team]
49
50
51 def get_adjacent_positions(x: int, y: int) -> list:
52 possible = [
53 (x - 1, y),
54 (x + 1, y),
55 (x, y - 1),
56 (x, y + 1)
57 ]
58 return [p for p in possible if valid_coords(p[0], p[1])]
59
60
61 def get_connecting_positions(x: int, y: int, board: list) -> set:
62 team = board[coords_to_index(x, y)]
63 current = [(x, y)]
64 visited = set([])
65 while len(current) > 0:
66 tmp_current = []
67 for pos in current:
68 visited.add(pos)
69 adjacent = get_adjacent_positions(pos[0], pos[1])
70 for pos2 in adjacent:
71 if pos2 not in visited:
72 if board[coords_to_index(pos2[0], pos2[1])] == team:
73 tmp_current.append(pos2)
74 current = tmp_current
75 return visited
76
77
78 def is_encroached(connecting_positions: set, board: list, opponent: str) -> bool:
79 for pos in connecting_positions:
80 adjacent = get_adjacent_positions(pos[0], pos[1])
81 for adj in adjacent:
82 if adj not in connecting_positions:
83 adj_index = coords_to_index(adj[0], adj[1])
84 if board[adj_index] != opponent:
85 return False
86 return True
87
88
89 @export
90 def force_end_round(state: dict, metadata: dict):
91 board = list(state['board'])
92
93 white_pieces = board.count('w')
94 black_pieces = board.count('b')
95
96 if white_pieces == black_pieces:
97 # Tie
98 winner = None
99 elif white_pieces > black_pieces:
100 winner = 'w'
101 else:
102 winner = 'b'
103
104 if winner is None:
105 state['stalemate'] = True
106 else:
107 state['winner'] = winner
108
109
110 @export
111 def move(caller: str, team: str, payload: dict, state: dict, metadata: dict):
112 # Assert it's the right player's piece
113 x1=payload['x1']
114 y1=payload['y1']
115
116 board = list(state['board'])
117
118 index = coords_to_index(x1, y1)
119 existing_piece = board[index]
120
121 assert state['current_player'] == team, 'It is not your turn to move.'
122 assert existing_piece == ' ', 'This tile has already been placed.'
123
124 opponent = opposing_team(team)
125
126 board[index] = team
127
128 # Calculate state
129 adjacent_positions = get_adjacent_positions(x1, y1)
130 connecting_positions = get_connecting_positions(x1, y1, board)
131 assert not is_encroached(connecting_positions, board, opponent), 'Cannot place a piece here. You are encroached.'
132 for adjacent_pos in adjacent_positions:
133 adjacent_index = coords_to_index(adjacent_pos[0], adjacent_pos[1])
134 if board[adjacent_index] == opponent:
135 # Check if we encroached them
136 opponent_connected_positions = get_connecting_positions(adjacent_pos[0], adjacent_pos[1], board)
137 if is_encroached(opponent_connected_positions, board, team):
138 for encroached_pos in opponent_connected_positions:
139 encroached_index = coords_to_index(encroached_pos[0], encroached_pos[1])
140 board[encroached_index] = ' '
141
142 board = ''.join(board)
143 state['current_player'] = opponent
144 state['board'] = board
145

Byte Code

e300000000000000000000000007000000400000007326010000640004005a005a016500650114005a0264015a036402640364049c025a0464036505650383016403640264059c045a0665076503830165026b027344740864068301820165096509650964079c036408640984045a0a6509650b640a9c02640b640c84045a0c6509650d640a9c02640d640e84045a0e65096509650d64079c03640f641084045a0f65106411830164276505651164139c0264146415840583015a126505650564169c026417641884045a1365096509651464079c036419641a84045a156509650965146516641b9c04641c641d84045a17651665146505650d641e9c04641f642084045a186510641183016511651164219c0264226423840483015a196510641183016505650565116511651164249c0564256426840483015a1a641253002928e9080000007a4020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020da0177da01622902720300000072020000002904da0e63757272656e745f706c61796572da05626f617264da0c63726561746f725f7465616dda0d6f70706f6e656e745f7465616d7a16496e76616c696420696e697469616c20626f6172642e2903da0178da0179da0672657475726e630200000000000000020000000400000043000000732600000074007c007c018302731a740164016a027c007c018302830182017c00740314007c011700530029024e7a18496e76616c69642028782c2079293a20287b7d2c207b7d292904da0e5f5f76616c69645f636f6f726473da0e417373657274696f6e4572726f72da06666f726d6174da084e554d5f524f5753290272080000007209000000a900720f000000da00da115f5f636f6f7264735f746f5f696e6465780b000000730400000000011a0172110000002902da05696e646578720a000000630100000000000000030000000300000043000000732e00000074007c0083017316740164016a027c008301830182017c0074031a007d017c00740316007d027c017c026602530029024e7a11496e76616c696420696e6465783a207b7d2904da0d5f5f76616c69645f696e646578720c000000720d000000720e0000002903721200000072080000007209000000720f000000720f0000007210000000da115f5f696e6465785f746f5f636f6f7264731000000073080000000001160108010801721400000063010000000000000001000000020000004300000073100000007c0064016b056f0e7c0074006b00530029024ee9000000002901da0b4e554d5f5351554152455329017212000000720f000000720f000000721000000072130000001700000073020000000001721300000063020000000000000002000000020000004300000073200000007c0064016b056f1e7c0074006b006f1e7c0164016b056f1e7c0174016b00530029024e72150000002902720e000000da084e554d5f434f4c53290272080000007209000000720f000000720f0000007210000000720b0000001b00000073020000000001720b000000da09636f6e5f676f5f76314e29027208000000720a00000063010000000000000001000000010000004300000073040000007400530029014e2901da0d494e495449414c5f535441544529017208000000720f000000720f0000007210000000da116765745f696e697469616c5f73746174651f00000073020000000002721a0000002902da047465616d720a000000630100000000000000010000000200000043000000730800000074007c001900530029014e2901da084f50504f53494e472901721b000000720f000000720f0000007210000000da0f5f5f6f70706f73696e675f7465616d2400000073020000000001721d000000630200000000000000030000000600000043000000733a0000007c00640118007c0166027c00640117007c0166027c007c016401180066027c007c0164011700660267047d026402640384007c0244008301530029044ee901000000630100000000000000020000000600000053000000732200000067007c005d1a7d0174007c01640019007c0164011900830272047c0191027104530029027215000000721e0000002901720b0000002902da022e30da0170720f000000720f0000007210000000fa0a3c6c697374636f6d703e2a000000730200000006007a2c5f5f6765745f61646a6163656e745f706f736974696f6e732e3c6c6f63616c733e2e3c6c697374636f6d703e720f000000290372080000007209000000da08706f737369626c65720f000000720f0000007210000000da185f5f6765745f61646a6163656e745f706f736974696f6e7328000000730400000000012c0172230000002904720800000072090000007205000000720a0000006303000000000000000a000000070000004300000073a20000007c0274007c007c01830219007d037c007c01660267017d047401670083017d05787c74027c04830164016b04729c67007d0678627c0444005d5a7d077c056a037c078301010074047c07640119007c076402190083027d0878387c0844005d307d097c097c056b07725e7c0274007c09640119007c0964021900830219007c036b02725e7c066a057c0983010100715e5700713857007c067d04712257007c05530029034e7215000000721e00000029067211000000da03736574da036c656eda036164647223000000da06617070656e64290a720800000072090000007205000000721b000000da0763757272656e74da0776697369746564da0b746d705f63757272656e74da03706f73da0861646a6163656e74da04706f7332720f000000720f0000007210000000da1a5f5f6765745f636f6e6e656374696e675f706f736974696f6e732d000000731c00000000010e010a0108010e0104010a010a0112010a0108011a0112010801722e0000002904da14636f6e6e656374696e675f706f736974696f6e737205000000da086f70706f6e656e74720a000000630300000000000000070000000600000043000000735c00000078567c0044005d4e7d0374007c03640119007c036402190083027d0478367c0444005d2e7d057c057c006b07722274017c05640119007c056402190083027d067c017c0619007c026b0372226403530071225700710657006404530029054e7215000000721e00000046542902722300000072110000002907722f00000072050000007230000000722b000000722c000000da0361646ada0961646a5f696e646578720f000000720f0000007210000000da0f5f5f69735f656e63726f61636865643e000000731000000000020a0112010a01080112010c010c0172330000002902da057374617465da086d65746164617461630200000000000000060000000300000043000000735e00000074007c006401190083017d027c026a01640283017d037c026a01640383017d047c037c046b02722e64007d056e127c037c046b04723c64027d056e0464037d057c0564006b08725264047c0064053c006e087c057c0064063c006400530029074e72050000007202000000720300000054da097374616c656d617465da0677696e6e65722902da046c697374da05636f756e742906723400000072350000007205000000da0c77686974655f706965636573da0c626c61636b5f7069656365737237000000720f000000720f0000007210000000da0f666f7263655f656e645f726f756e644a000000731600000000020c010a010a010801060108010602040108010a02723c0000002905da0663616c6c6572721b000000da077061796c6f61647234000000723500000063050000000000000012000000060000004300000073200100007c02640119007d057c02640219007d0674007c036403190083017d0774017c057c0683027d087c077c0819007d097c03640419007c016b02734274026405830182017c0964066b027352740264078301820174037c0183017d0a7c017c077c083c0074047c057c0683027d0b74057c057c067c0783037d0c74067c0c7c077c0a83030c00738e740264088301820178727c0b44005d6a7d0d74017c0d640919007c0d640a190083027d0e7c077c0e19007c0a6b02729474057c0d640919007c0d640a19007c0783037d0f74067c0f7c077c018303729478267c0f44005d1e7d1074017c10640919007c10640a190083027d1164067c077c113c0071dc570071945700640b6a077c0783017d077c0a7c0364043c007c077c0364033c0064005300290c4eda027831da027931720500000072040000007a1c4974206973206e6f7420796f7572207475726e20746f206d6f76652efa01207a22546869732074696c652068617320616c7265616479206265656e20706c616365642e7a2e43616e6e6f7420706c616365206120706965636520686572652e20596f752061726520656e63726f61636865642e7215000000721e0000007210000000290872380000007211000000720c000000721d0000007223000000722e0000007233000000da046a6f696e2912723d000000721b000000723e00000072340000007235000000723f000000724000000072050000007212000000da0e6578697374696e675f70696563657230000000da1261646a6163656e745f706f736974696f6e73722f000000da0c61646a6163656e745f706f73da0e61646a6163656e745f696e646578da1c6f70706f6e656e745f636f6e6e65637465645f706f736974696f6e73da0e656e63726f61636865645f706f73da10656e63726f61636865645f696e646578720f000000720f0000007210000000da046d6f76655b00000073340000000002080108010c010a01080114011001080108010a010c01100106010a0112010c01020112010c010a0108010a0110010a010801724a00000029014e291b720e00000072170000007216000000da0d494e495449414c5f424f415244721c000000da0373747272190000007225000000720c000000da03696e747211000000da057475706c657214000000da04626f6f6c7213000000720b000000da085f5f6578706f7274da0464696374721a000000721d000000723800000072230000007224000000722e0000007233000000723c000000724a000000720f000000720f000000720f0000007210000000da083c6d6f64756c653e01000000732a0000000801080204010a0108010a01140312051007100412040601140410041205141106010e0b060112100601