Contract con_tictactoe_v14


Contract Code


  
1
2
3 ### # This is a simple smart contract # All Lamden smart contracts are programmed in Python # For complete contracting documentation please visit https://contracting.lamden.io/ ###
4
5 # import random
6
7 random.seed()
8 games = Hash(default_value=False)
9 metadata = Hash()
10
11 @construct
12 def seed():
13 metadata['operator'] = ctx.caller
14
15 @export
16 def create_game():
17 sender = ctx.caller
18 assert(games[sender] == False, 'Game already in progress')
19 first = random.randint(0, 99)
20 # 0 = not set, 1 = O, 2 = X
21 if(first <50): first = 1 # Player goes first else: first = 2 # Computer goes first
22 games[sender] = {'first': first, 'state': [0, 0, 0, 0, 0, 0, 0, 0, 0], 'turn': 0}
23 return games[sender]
24
25 @export
26 def end_game():
27 games[ctx.caller] = False
28
29 @export
30 def player_selection(i: int):
31 sender = ctx.caller
32 game = games[sender]
33 assert(not(game == False), 'No game already in progress')
34 game['state'][i] = 1
35 assert(game['state'][i] == 0, f'square {i} is already closed')
36 game['turn'] = game['turn'] + 1
37 win = False
38 if(game['turn'] >= 9):
39 games[sender] = False
40 return "{'winner': 0, 'state': " + f"{game['state']}" + "}"
41 if(game['turn'] >= 5):
42 win = checkWin(game['state'], 1)
43 elif(win == True):
44 games[sender] = False
45 return "{'winner': 1, 'state': " + f"{game['state']}" +"}"
46 else:
47 computerSelects = random.randint(0, 8)
48 while(game['state'][computerSelects] != 0):
49 computerSelects = random.randint(0, 8)
50 game['state'][computerSelects] = 2
51 game['turn'] = game['turn'] + 1
52 if(game['turn'] >= 9):
53 games[sender] = False
54 return "{'winner': 0, 'state': " + f"{game['state']}" + "}"
55 if(game['turn'] >= 5):
56 win = checkWin(game['state'], 2)
57 elif(win == True):
58 games[sender] = False
59 return "{'winner': 2, 'state': " + f"{game['state']}" + "}"
60 games[sender] = game
61 return f'{game}'
62
63 def checkWin(state, x: int):
64 if(state[0] == x): # top left
65 if(state[1] == x and state[1] == x): #top row
66 return True
67 if(state[3] == x and state[6] == x): #left column
68 return x
69 if(state[4] == x and state[8] == x): # diagnal top left to bottom right
70 return True
71 if(state[1] == x): # top middle
72 if(state[4] == x and state[7] == x): # middle column
73 return True
74 if(state[2] == x): # top right
75 if(state[5] == x and state[8] == x): #right column
76 return True
77 if(state[4] == x and state[6] == x): # diagnal top right to bottom left
78 return True
79 if(state[3] == x): # top middle
80 if(state[4] == x and state[5] == x): # middle row
81 return True
82 if(state[6] == x): # top middle
83 if(state[7] == x and state[8] == x): # bottom row
84 return True
85 return False
86
87 # print(checkWin([1, 2, 0, 2, 1, 0, 0, 0, 1], 1))

Byte Code

e30000000000000000000000000500000040000000737200000065006a0183000100650264006401640264038d035a0365026401640464058d025a046406640784005a0565066401830164086409840083015a07650664018301640a640b840083015a086506640183016509640c9c01640d640e840483015a0a6509640f9c016410641184045a0b64125300291346da11636f6e5f746963746163746f655f763134da0567616d65732903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65da086d65746164617461290272040000007205000000630000000000000000000000000300000043000000730e00000074006a01740264013c006400530029024eda086f70657261746f722903da03637478da0663616c6c6572da0a5f5f6d65746164617461a900720b000000720b000000da00da045f5f5f5f0600000073020000000001720d000000630000000000000000020000000a00000043000000735c00000074006a017d0074027c00190064016b0264026602731a7403820174046a056403640483027d017c0164056b00723264067d017c016403640364036403640364036403640364036709640364079c0374027c003c0074027c001900530029084e467a1847616d6520616c726561647920696e2070726f6772657373e900000000e963000000e932000000e9010000002903da056669727374da057374617465da047475726e290672080000007209000000da075f5f67616d6573da0e417373657274696f6e4572726f72da0672616e646f6dda0772616e64696e742902da0673656e6465727212000000720b000000720b000000720c000000da0b6372656174655f67616d650a00000073100000000002060114010c010801040116010c01721a000000630000000000000000000000000300000043000000730e0000006401740074016a023c006400530029024e462903721500000072080000007209000000720b000000720b000000720b000000720c000000da08656e645f67616d651600000073020000000002721b0000002901da0169630100000000000000050000000400000043000000738601000074006a017d0174027c0119007d027c0264016b020c006402660273207403820164037c02640419007c003c007c02640419007c00190064056b0264067c009b0064079d036602734c740382017c0264081900640317007c0264083c0064017d037c026408190064096b057286640174027c013c00640a7c02640419009b001700640b170053007c0264081900640c6b0572a274047c0264041900640383027d036ed67c03640d6b0272c4640174027c013c00640e7c02640419009b001700640b1700530074056a066405640f83027d0478207c02640419007c04190064056b0372f074056a066405640f83027d0471d2570064107c02640419007c043c007c0264081900640317007c0264083c007c026408190064096b0590017236640174027c013c00640a7c02640419009b001700640b170053007c0264081900640c6b059001725474047c0264041900641083027d036e247c03640d6b0290017278640174027c013c0064117c02640419009b001700640b170053007c0274027c013c007c029b00530029124e467a1b4e6f2067616d6520616c726561647920696e2070726f677265737372110000007213000000720e0000007a07737175617265207a1220697320616c726561647920636c6f7365647214000000e9090000007a177b2777696e6e6572273a20302c20277374617465273a20da017de905000000547a177b2777696e6e6572273a20312c20277374617465273a20e908000000e9020000007a177b2777696e6e6572273a20322c20277374617465273a2029077208000000720900000072150000007216000000da0a5f5f636865636b57696e721700000072180000002905721c0000007219000000da0467616d65da0377696eda0f636f6d707574657253656c65637473720b000000720b000000720c000000da10706c617965725f73656c656374696f6e1b000000733c00000000020601080112010c012001100104010c01080112010c0110010801080112020c01120110010c0110010e01080112010e0110010a0108011201080172260000002901da017863020000000000000002000000020000004300000073260100007c00640119007c016b0272607c00640219007c016b0272287c00640219007c016b027228640353007c00640419007c016b0272447c00640519007c016b0272447c0153007c00640619007c016b0272607c00640719007c016b027260640353007c00640219007c016b0272887c00640619007c016b0272887c00640819007c016b027288640353007c00640919007c016b0272cc7c00640a19007c016b0272b07c00640719007c016b0272b0640353007c00640619007c016b0272cc7c00640519007c016b0272cc640353007c00640419007c016b0272f47c00640619007c016b0272f47c00640a19007c016b0272f4640353007c00640519007c016b02900172227c00640819007c016b02900172227c00640719007c016b029001722264035300640b5300290c4e720e000000721100000054e903000000e906000000e9040000007220000000e9070000007221000000721f00000046720b000000290272130000007227000000720b000000720b000000720c00000072220000003e000000732c00000000010c011801040118010401180104010c01180104010c0118010401180104010c01180104010e011c01040172220000004e290c7217000000da0473656564da04486173687215000000720a000000720d000000da085f5f6578706f7274721a000000721b000000da03696e7472260000007222000000720b000000720b000000720b000000720c000000da083c6d6f64756c653e01000000731000000008010e010c030804100c100506011022