Contract con_tictactoe_v3


Contract Code


  
1 ###
2 # This is a simple smart contract
3 # All Lamden smart contracts are programmed in Python
4 # For complete contracting documentation please visit https://contracting.lamden.io/
5 ###
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 #ctx.caller is a global variable and is the public key of the person who sent the transaction
18 sender = ctx.caller
19 assert(games[sender] != False, 'Game already in progress')
20 first = random.randint(0, 99)
21 # 0 = not set, 1 = O, 2 = X
22 if(first <50):
23 first = 1 # Player goes first
24 else:
25 first = 2 # Computer goes first
26 games[sender] = {'first': first, 'state': [0, 0, 0, 0, 0, 0, 0, 0, 0], 'turn': 0}
27 return games[sender]
28
29 @export
30 def end_game():
31 games[ctx.caller] = False
32
33 @export
34 def player_selection(i: int):
35 sender = ctx.caller
36 assert(games[sender] == False, 'No game already in progress')
37 games[sender]['state'][i] = 1
38 games[sender]['turn'] = game[sender]['turn'] + 1
39 win = False
40 if(turns >= 9):
41 return {'winner': 'none', 'state': games[sender]['state']}
42 if(turn >= 5):
43 win = checkWin(games[sender]['state'], 1)
44 elif(win == 1):
45 games[sender] = False
46 return {'winner': sender, 'state': games[sender]['state'] }
47 else:
48 computerSelects = random.random(9)
49 while(games[sender]['state'][computerSelects] != 0):
50 computerSelects = random.random(9)
51 games[sender]['state'][computerSelects] = 1
52 games[sender]['turn'] = game['turn'] + 1
53 if(turns >= 9):
54 games[sender] = False
55 return {'winner': 'none', 'state': games[sender]['state']}
56 if(turn >= 5):
57 win = checkWin(games[sender]['state'], 2)
58 elif(win == 2):
59 games[sender] = False
60 return "{'winner': 'computer', 'state': " + f"{games[sender]['state']}" + "}"
61 return f'{games[sender]}'
62
63 def checkWin(state, x):
64 if(state[0] == x): # top left
65 if(state[1] == x and state[1] == x): #top row
66 return x
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 X
71 if(state[1] == x): # top middle
72 if(state[4] == x and state[7] == x): # middle column
73 return x
74 if(state[2] == x): # top right
75 if(state[5] == x and state[8] == x): #right column
76 return x
77 if(state[4] == x and state[6] == x): # diagnal top right to bottom left
78 return X
79 if(state[3] == x): # top middle
80 if(state[4] == x and state[5] == x): # middle row
81 return x
82 if(state[6] == x): # top middle
83 if(state[7] == x and state[8] == x): # bottom row
84 return x
85
86
87
88
89
90
91

Byte Code

e30000000000000000000000000500000040000000736c00000065006a0183000100650264006401640264038d035a0365026401640464058d025a046406640784005a0565066401830164086409840083015a07650664018301640a640b840083015a086506640183016509640c9c01640d640e840483015a0a640f641084005a0b64115300291246da10636f6e5f746963746163746f655f7633da0567616d65732903da0d64656661756c745f76616c7565da08636f6e7472616374da046e616d65da086d65746164617461290272040000007205000000630000000000000000000000000300000043000000730e00000074006a01740264013c006400530029024eda086f70657261746f722903da03637478da0663616c6c6572da0a5f5f6d65746164617461a900720b000000720b000000da00da045f5f5f5f0600000073020000000001720d000000630000000000000000020000000a00000043000000736200000074006a017d0074027c00190064016b0364026602731a7403820174046a056403640483027d017c0164056b00723464067d016e0464077d017c016403640364036403640364036403640364036709640364089c0374027c003c0074027c001900530029094e467a1847616d6520616c726561647920696e2070726f6772657373e900000000e963000000e932000000e901000000e9020000002903da056669727374da057374617465da047475726e290672080000007209000000da075f5f67616d6573da0e417373657274696f6e4572726f72da0672616e646f6dda0772616e64696e742902da0673656e6465727213000000720b000000720b000000720c000000da0b6372656174655f67616d650a00000073120000000002060114010c0108010602040116010c01721b000000630000000000000000000000000300000043000000730e0000006401740074016a023c006400530029024e462903721600000072080000007209000000720b000000720b000000720b000000720c000000da08656e645f67616d651800000073020000000002721c0000002901da0169630100000000000000040000000300000043000000736401000074006a017d0174027c01190064016b0264026602731a74038201640374027c011900640419007c003c0074047c011900640519006403170074027c01190064053c0064017d02740564066b057260640774027c0119006404190064089c025300740664096b05727c740774027c01190064041900640383027d026ede7c0264036b02729e640174027c013c007c0174027c0119006404190064089c02530074086a08640683017d03782274027c011900640419007c031900640a6b0372ca74086a08640683017d0371aa5700640374027c011900640419007c033c007404640519006403170074027c01190064053c00740564066b0590017214640174027c013c00640774027c0119006404190064089c025300740664096b0590017232740774027c01190064041900640b83027d026e287c02640b6b029001725a640174027c013c00640c74027c011900640419009b001700640d1700530074027c0119009b005300290e4e467a1b4e6f2067616d6520616c726561647920696e2070726f6772657373721100000072140000007215000000e909000000da046e6f6e652902da0677696e6e65727214000000e905000000720e00000072120000007a207b2777696e6e6572273a2027636f6d7075746572272c20277374617465273a20da017d29097208000000720900000072160000007217000000da0467616d65da057475726e737215000000da0a5f5f636865636b57696e72180000002904721d000000721a000000da0377696eda0f636f6d707574657253656c65637473720b000000720b000000720c000000da10706c617965725f73656c656374696f6e1d000000733400000000020601140110011801040108011201080114010801080112020a0116010e01100114010a01080112010a0114010a0108021601722800000063020000000000000002000000020000004300000073260100007c00640119007c016b0272607c00640219007c016b0272287c00640219007c016b0272287c0153007c00640319007c016b0272447c00640419007c016b027244740053007c00640519007c016b0272607c00640619007c016b027260740053007c00640219007c016b0272887c00640519007c016b0272887c00640719007c016b0272887c0153007c00640819007c016b0272cc7c00640919007c016b0272b07c00640619007c016b0272b07c0153007c00640519007c016b0272cc7c00640419007c016b0272cc740053007c00640319007c016b0272f47c00640519007c016b0272f47c00640919007c016b0272f47c0153007c00640419007c016b02900172227c00640719007c016b02900172227c00640619007c016b02900172227c01530064005300290a4e720e0000007211000000e903000000e906000000e904000000e908000000e907000000721200000072210000002901da015829027214000000da0178720b000000720b000000720c00000072250000003d000000732a00000000010c011801040118010401180104010c01180104010c0118010401180104010c01180104010e011c0172250000004e290c7218000000da0473656564da04486173687216000000720a000000720d000000da085f5f6578706f7274721b000000721c000000da03696e7472280000007225000000720b000000720b000000720b000000720c000000da083c6d6f64756c653e01000000731000000008010e010c030804100e10050601101f