1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
| from random import random def is_same(a : list, b : list, mode : int) -> bool: """ find out wether two board are same """ for i in range(mode+2): for j in range(mode+2): if(a[i][j] != b[i][j]): return False return True
def limt_rand(lst : list) -> int: """ this function choose the number from a to b-1 randomly that is in list """ rand_variale = 0 ans = 0 rand_variale = int(random()*len(lst)) ans = lst[rand_variale] lst.remove(ans) return ans
def print_board(board : list, mode : int) -> None: """ print the board for 3*3 (mode 1) and 4*4 (mode 2) puzzle """ value = 0 print() for i in range(mode+2): for j in range(mode+2): value = board[i][j] if(value != 0): if(value < 10): print('0', value, end = ' ', sep = '') else: print(value, end = ' ') else: print(' ', end = ' ') print() print() return
def movement(zero_position : tuple[int], board : list, type : str) -> tuple[int]: """ the movement if you have determined what situation you need """ zero = board[zero_position[0]][zero_position[1]] tile1 = 0 if(type == 'l'): tile1 = board[zero_position[0]][zero_position[1] - 1] board[zero_position[0]][zero_position[1] - 1] = zero board[zero_position[0]][zero_position[1]] = tile1 return (zero_position[0], zero_position[1] - 1) elif(type == 'r'): tile1 = board[zero_position[0]][zero_position[1] + 1] board[zero_position[0]][zero_position[1] + 1] = zero board[zero_position[0]][zero_position[1]] = tile1 return (zero_position[0], zero_position[1] + 1) elif(type == 'u'): tile1 = board[zero_position[0] - 1][zero_position[1]] board[zero_position[0] - 1][zero_position[1]] = zero board[zero_position[0]][zero_position[1]] = tile1 return (zero_position[0] - 1, zero_position[1]) elif(type == 'd'): tile1 = board[zero_position[0] + 1][zero_position[1]] board[zero_position[0] + 1][zero_position[1]] = zero board[zero_position[0]][zero_position[1]] = tile1 return (zero_position[0] + 1, zero_position[1]) else: print("\ninvalid input, plz enter again!\n") return zero
def operate(zero_position : tuple[int], board : list, mode : int, l : str, r : str, u : str, d : str, total_steps : int) -> tuple: """ test where the tile can go the return value is an integer that represent: 0:no limits, 1:left_limit, 2:righ_limit, 3:up_limit, 4:down_limit, 5:left_up limit, 6:right_up limit, 7:left_down limit, 8:right_down limit and do the operation for each term. finally, return the new_zero tile """ print_board(board, mode) size = len(board[0])-1 total_steps += 1 ipt = 0 if((zero_position[0] == 0) and (zero_position[1] != 0) and (zero_position[1] != size)): ipt = input("Enter your move (left-"+ l +", right-"+ r +", down-"+ d +") > ") if(ipt == l): return (movement(zero_position, board,'l'),total_steps) elif(ipt == r): return (movement(zero_position, board, 'r'),total_steps) elif(ipt == d): return (movement(zero_position, board, 'd'),total_steps) else: total_steps -= 1 print("\ninvalid input, plz enter again!\n") return (zero_position, total_steps) elif((zero_position[1] == 0) and (zero_position[0] != 0) and (zero_position[0] != size)): ipt = input("Enter your move (right-"+ r +", up-"+ r +",down-"+ d +") > ") if(ipt == r): return (movement(zero_position, board, 'r'),total_steps) elif(ipt == u): return (movement(zero_position, board, 'u'),total_steps) elif(ipt == d): return (movement(zero_position, board, 'd'),total_steps) else: total_steps -= 1 print("\ninvalid input, plz enter again!\n") return (zero_position, total_steps) elif((zero_position[0] == size) and (zero_position[1] !=0) and(zero_position[1] != size)): ipt = input("Enter your move (left-"+ l +", right-"+ r +", up-"+ u +") > ") if(ipt == l): return (movement(zero_position, board,'l'),total_steps) elif(ipt == r): return (movement(zero_position, board, 'r'),total_steps) elif(ipt == u): return (movement(zero_position, board, 'u'),total_steps) else: total_steps -= 1 print("\ninvalid input, plz enter again!\n") return (zero_position, total_steps) elif((zero_position[1] == size) and (zero_position[0] != 0) and (zero_position[0] != size)): ipt = input("Enter your move (left-"+ l +", up-"+ u +", down-"+ d +") > ") if(ipt == l): return (movement(zero_position, board,'l'),total_steps) elif(ipt == u): return (movement(zero_position, board, 'u'),total_steps) elif(ipt == d): return (movement(zero_position, board, 'd'),total_steps) else: total_steps -=1 print("\ninvalid input, plz enter again!\n") return (zero_position, total_steps) elif((zero_position[0] == 0) and (zero_position[1] == 0)): ipt = input("Enter your move (right-"+ r +", down-"+ d +") > ") if(ipt == r): return (movement(zero_position, board,'r'),total_steps) elif(ipt == d): return (movement(zero_position, board, 'd'),total_steps) else: total_steps -= 1 print("\ninvalid input, plz enter again!\n") return (zero_position, total_steps) elif((zero_position[0] == size) and (zero_position[1] == 0)): ipt = input("Enter your move (right-"+ r +", up-"+ r +") > ") if(ipt == r): return (movement(zero_position, board,'r'),total_steps) elif(ipt == u): return (movement(zero_position, board, 'u'),total_steps) else: total_steps -= 1 print("\ninvalid input, plz enter again!\n") return (zero_position, total_steps) elif((zero_position[0] == 0) and (zero_position[1] == size)): ipt = input("Enter your move (left-"+ l +", down-"+ d +") > ") if(ipt == l): return (movement(zero_position, board,'l'),total_steps) elif(ipt == d): return (movement(zero_position, board, 'd'),total_steps) else: total_steps -= 1 print("\ninvalid input, plz enter again!\n") return (zero_position, total_steps) elif((zero_position[0] == size) and (zero_position[1] == size)): ipt = input("Enter your move (left-"+ l +", up-"+ u +") > ") if(ipt == l): return (movement(zero_position, board,'l'),total_steps) elif(ipt == u): return (movement(zero_position, board, 'u'),total_steps) else: total_steps -= 1 print("\ninvalid input, plz enter again!\n") return (zero_position, total_steps) else: ipt = input("Enter your move (left-"+ l +", right-"+ r +", up-"+ u +", down-"+ d +") > ") if(ipt == l): return (movement(zero_position, board, 'l'),total_steps) elif(ipt == r): return (movement(zero_position, board,'r'),total_steps) elif(ipt == u): return (movement(zero_position, board, 'u'),total_steps) elif(ipt == d): return (movement(zero_position, board, 'd'),total_steps) else: total_steps -= 1 print("\ninvalid input, plz enter again!\n") return (zero_position, total_steps)
def is_valid(test : list[int], zero_position : tuple[int], mod : int) -> bool: """ test whether the game is valid true for valid, false for invalid """ ans = 0 for i in range(len(test)): for j in range(i+1,len(test)): if(test[j] < test[i]): ans+=1 if(mod == 1): if((ans % 2) == 0): return True else: return False else: if((ans % 2) == 0): if(((zero_position[0] - 3) % 2) == 0): return True else: return False else: if(((zero_position[0] - 3) % 2) != 0): return True else: return False
def eight_tile_generator() -> tuple: """ generate 8 tiles mode """ test = [0]*9 is_first = True lst = [] zero_position = (2,2) pointor = 0 board = [] while(not is_valid(test, zero_position, 1) or is_first): test = [0]*8 pointor = 0 lst=[0,1,2,3,4,5,6,7,8] board = [[0,0,0], [0,0,0], [0,0,0]] for i in range(3): for j in range(3): ans = limt_rand(lst) board[i][j] = ans if(ans != 0): test[pointor] = ans pointor += 1 if(ans == 0): zero_position = (i,j) is_first = False return (board, zero_position)
def fifteen_tile_generator() -> tuple: """ generate 15 tiles mode """ is_first = True zero_position = (3,3) test = [0]*16 lst = [] board = [] ans = 0 while(not is_valid(test, zero_position, 2) or is_first): lst=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] board = [[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0]] test = [0]*15 pointor = 0 for i in range(4): for j in range(4): ans = limt_rand(lst) board[i][j] = ans if(ans != 0): test[pointor] = ans pointor += 1 if(ans == 0): zero_position = (i,j) is_first = False return (board, zero_position) def mode_1(l : str, r : str, u : str, d : str, total_steps : int) -> None: """ mode for 8 tiles """ board, zero_position = eight_tile_generator() final = [[0,0,0], [0,0,0], [0,0,0]] poiner = 1 for i in range(3): for j in range(3): final[i][j] = poiner poiner+=1 final[2][2] = 0 while(not is_same(board,final,1)): zero_position, total_steps = operate(zero_position, board, 1, l, r, u, d, total_steps) print("\nstep : %d" %total_steps) print_board(board,1) print("\n//////////// well done!, total steps : " + str(total_steps) +" ///////////\n")
def mode_2(l : str, r : str, u : str, d : str, total_steps : int) -> None: """ mode for 15 tiles """ board, zero_position = fifteen_tile_generator() final = [[0,0,0,0], [0,0,0,0], [0,0,0,0], [0,0,0,0]] poiner = 1 for i in range(4): for j in range(4): final[i][j] = poiner poiner+=1 final[3][3] = 0 while(not is_same(board,final,2)): zero_position, total_steps = operate(zero_position, board, 2, l, r, u, d, total_steps) print("\nstep : %d" %total_steps) print_board(board,2) print("\n//////////// well done! total steps : " + str(total_steps) + " ///////////\n")
def main() -> None: mode_name = '' total_steps = 0 mode_name = 0 print(""" Welcome to Kinley’s puzzle game, you will be prompted to choose playing mode, 8-number mode(mode 1) or 15-number mode(mode 2): An 8-number puzzle has a square-framed board consisting of 8 square tiles, numbered 1 to 8, initially placed in random order, while a 15-number puzzle there are 15 numbered square tiles, from 1 to 15. The game board has an empty space where one of adjacent tilesslides to. The objective of the game is to re-arrange the tiles into a sequential order by their numbers (left to right, top to bottom) by repeatedly making sliding moves (left, right, up or down). The following figure shows an example of one 8-number puzzle where “INITIAL” is the starting point of the game, and the player needs to repeatedly slide one adjacent tile, one at a time, to the unoccupied space (the empty space) until all numbers appear sequentially, ordered from left to right, top to bottom, shown as “FINAL”. """) while(True): total_steps = 0 mode_name = input("Enter “1” for 8-puzzle, “2” for 15-puzzle or “q” to end the game > ") l,r,u,d = 'l','r','u','d' correct_input = True if(mode_name == 'q'): return elif(mode_name == '1'): correct_input = False while(not correct_input): try: l,r,u,d= input("plz enter letter represent left, right, up ,down > ") correct_input = True if(l == r or l ==u or l == d or r == u or r == d or u == d): correct_input = False print("don't use same letter !") else: print("reminder: left-" + l + ", right-" + r + ", up-" + u + ", down-" + d) except: print("invalid input, plz do not use anything to divide 4 leters, eg: 'lrud'") mode_1(l,r,u,d, total_steps) elif(mode_name == '2'): correct_input = False while(not correct_input): try: l,r,u,d= input("plz enter letter represent left, right, up ,down > ") correct_input = True print("reminder: left-" + l + ", right-" + r + ", up-" + u + ", down-" + d) except: print("invalid input, plz do not use anything to divide 4 leters, eg: 'lrud'") mode_2(l,r,u,d, total_steps) else: print(" invalid input, plz enter again!") continue
main()
|