|
| 1 | +# kims game |
| 2 | + |
| 3 | +# imports |
| 4 | +import random as r |
| 5 | + |
| 6 | +# ESCAPE SEQUENCE CURSOR MOVEMENT |
| 7 | +# \033[<L>;<C>H Positions the cursor. Puts the cursor at line L and column C. |
| 8 | +# \033[<N>A Move the cursor up by N lines. |
| 9 | +# \033[<N>B Move the cursor down by N lines. |
| 10 | +# \033[<N>C Move the cursor forward by N columns. |
| 11 | +# \033[<N>D Move the cursor backward by N columns. |
| 12 | +# \033[2J Clear the screen, move to (0,0) |
| 13 | +# \033[K Erase the end of line. |
| 14 | + |
| 15 | +UP = '\033[1A' |
| 16 | +CLEAR = '\x1b[2K' |
| 17 | +RESET = '\033[2J' |
| 18 | +BLINKON = '\033[32;5m' |
| 19 | +BLINKOFF = '\033[0m' |
| 20 | +#print ( UP, end=CLEAR) |
| 21 | + |
| 22 | +# inits |
| 23 | +def build_grid(): |
| 24 | + d_grid=[] |
| 25 | + for i in range(4): |
| 26 | + d_grid.append([{"val": "", "matched": 0, "check" : 0}, |
| 27 | + {"val": "", "matched": 0, "check" : 0}, |
| 28 | + {"val": "", "matched": 0, "check" : 0}, |
| 29 | + {"val": "", "matched": 0, "check" : 0}]) |
| 30 | + |
| 31 | + l_cards=[] |
| 32 | + for i in range(4): |
| 33 | + l_cards.append("J") |
| 34 | + l_cards.append("Q") |
| 35 | + l_cards.append("K") |
| 36 | + l_cards.append("A") |
| 37 | + |
| 38 | + r.shuffle(l_cards) |
| 39 | + |
| 40 | + for i in range(4): |
| 41 | + for j in range(4): |
| 42 | + d_grid[i][j]["val"]=l_cards.pop() |
| 43 | + |
| 44 | + return d_grid |
| 45 | + |
| 46 | +def print_grid(d_grid, i_clear=0, i_reveal = 0 ): |
| 47 | + if i_clear == 1 : |
| 48 | + #print ( UP * 5, end=CLEAR) |
| 49 | + print (RESET) |
| 50 | + print ( "Kims Game v0.1") |
| 51 | + print ( "--------------------------") |
| 52 | + print ( "" ) |
| 53 | + |
| 54 | + print ( " " * 5 + " C 0 1 2 3 C" ) |
| 55 | + print ( " " * 5 + "R |---------|" ) |
| 56 | + |
| 57 | + for x in range(4): |
| 58 | + print ( " " * 5 + str(x) + " |", end = " " ) |
| 59 | + for y in range(4): |
| 60 | + if d_grid[x][y]["check"]==1 or i_reveal==1: |
| 61 | + print (d_grid[x][y]["val"], end=" ") |
| 62 | + elif d_grid[x][y]["matched"]==1: |
| 63 | + print ("X", end=" ") |
| 64 | + else: |
| 65 | + print ( "#", end = " " ) |
| 66 | + print ("|") |
| 67 | + |
| 68 | + print ( " " * 5 + "R |---------|" ) |
| 69 | + print ("") |
| 70 | + |
| 71 | + |
| 72 | +def get_location(d_grid): |
| 73 | + while True: |
| 74 | + try: |
| 75 | + loc1 = input ( "Please enter a location to flip (RC e.g 12) (ENTER to Quit) :" ) |
| 76 | + if loc1=="": |
| 77 | + if check_exit() == "Y": |
| 78 | + break # exit function |
| 79 | + else: |
| 80 | + continue # go back and try again |
| 81 | + |
| 82 | + if len(loc1) !=2: |
| 83 | + raise ValueError |
| 84 | + if int(loc1[0]) < 0 or int(loc1[0])> 3: |
| 85 | + raise ValueError |
| 86 | + |
| 87 | + x1=int(loc1[0]) |
| 88 | + y1=int(loc1[1]) |
| 89 | + |
| 90 | + if d_grid[x1][y1]["matched"]==1: |
| 91 | + print ( f"Location {loc1} is already matched " ) |
| 92 | + raise Exception |
| 93 | + if d_grid[x1][y1]["check"]==1: |
| 94 | + print ( f"Location {loc1} is already flipped " ) |
| 95 | + raise Exception |
| 96 | + else: |
| 97 | + break |
| 98 | + except ValueError: |
| 99 | + # Input failure |
| 100 | + print ("Invalid location entered") |
| 101 | + except: |
| 102 | + # it failed |
| 103 | + print ("Invalid location entered") |
| 104 | + return loc1 |
| 105 | + |
| 106 | +def check_exit(): |
| 107 | + f_check=input("Are you sure you wish to quit? (Y/N) :").upper() |
| 108 | + return f_check |
| 109 | + |
| 110 | +# more inits |
| 111 | +i_matches=0 |
| 112 | +i_guesses=0 |
| 113 | +d_grid=build_grid() |
| 114 | + |
| 115 | +# Actually play the game |
| 116 | +while i_matches < 8: |
| 117 | + i_guesses += 1 |
| 118 | + |
| 119 | + print_grid ( d_grid, 1 ) |
| 120 | + |
| 121 | + loc1 = get_location(d_grid) |
| 122 | + |
| 123 | + if loc1=="": # Exit condition |
| 124 | + break |
| 125 | + |
| 126 | + x1=int(loc1[0]) |
| 127 | + y1=int(loc1[1]) |
| 128 | + d_grid[x1][y1]["check"]=1 |
| 129 | + print_grid ( d_grid, 1 ) |
| 130 | + |
| 131 | + loc2 = get_location(d_grid) |
| 132 | + |
| 133 | + if loc2=="": # Exit condition |
| 134 | + break |
| 135 | + |
| 136 | + x2=int(loc2[0]) |
| 137 | + y2=int(loc2[1]) |
| 138 | + |
| 139 | + d_grid[x2][y2]["check"]=1 |
| 140 | + print_grid ( d_grid, 1 ) |
| 141 | + |
| 142 | + if d_grid[x2][y2]["val"] == d_grid[x1][y1]["val"]: |
| 143 | + d_grid[x1][y1]["matched"]=1 |
| 144 | + d_grid[x2][y2]["matched"]=1 |
| 145 | + i_matches += 1 |
| 146 | + |
| 147 | + # motivation |
| 148 | + print ( f"Great job. Match confirmed!", end=" ") |
| 149 | + if i_matches< 8: |
| 150 | + print ( f"Only {8-i_matches} pairs left." ) |
| 151 | + else: |
| 152 | + print ( BLINKON + "You've won!" + BLINKOFF) |
| 153 | + else : |
| 154 | + # motivation |
| 155 | + print ( f"Keep trying, only {8-i_matches} pairs left." ) |
| 156 | + |
| 157 | + d_grid[x1][y1]["check"]=0 |
| 158 | + d_grid[x2][y2]["check"]=0 |
| 159 | + |
| 160 | + x = input (BLINKON + "Press ENTER to continue" + BLINKOFF) |
| 161 | + |
| 162 | +print_grid( d_grid, 1, 1) |
| 163 | + |
| 164 | +if i_matches==8: |
| 165 | + print ( f"{BLINKON}Congratulations{BLINKOFF}. You cleared the board in {i_guesses} rounds! ") |
| 166 | +else: |
| 167 | + print ( f"Thanks for playing. You made {i_matches} matches in {i_guesses} rounds! ") |
| 168 | + |
| 169 | +print ( "We hope to see you again soon." ) |
| 170 | +print ( "" ) |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
0 commit comments