Un jeu de Tic Tac Toe avec les champs de bits.
from kandinsky import * from ion import * # Logic def isbitset(bts, i): dec = 8 - i return bool(bts & (0b1 << dec)) def setbit(bts, i): dec = 8 - i return bts | (0b1 << dec) def win(bts): wl = [0b111000000, 0b000111000, 0b000000111, 0b100100100, 0b010010010, 0b001001001, 0b100010001, 0b001010100] for w in wl: if bts & w == w: return True return False def isoccupied(j1, j2, i): dec = 8 - i return (j1 | j2) & (1 << dec) def display(j1, j2): buffer = [' '] * 9 for i in range(9): if isbitset(j1, i): buffer[i] = 'X' elif isbitset(j2, i): buffer[i] = 'O' for i in range(3): print("".join(buffer[3*i:3*i+3])) def tbe(bts): # To BE bitfield return ((bts & 0xFF00) >> 8) | ((bts & 0xFF) << 8) def play(): j1, j2 = 0, 0 j = 1 print("Joueur 1 (X) et Joueur 2 (O)") display(j1, j2) while not win(j1) and not win(j2): i = int(input("Joueur {} : ".format(j))) if i < 0 or i > 8 or isoccupied(j1, j2, i): print("Entree invalide") continue if j == 1: j1 = setbit(j1, i) else: j2 = setbit(j2, i) display(j1, j2) j = 2 if j == 1 else 1 print("Le joueur {} a gagne !".format(1 if j == 2 else 2)) # Graphical implementation WIDTH = 320 HEIGHT = 222 CURRENT_PLAYER_X = 6 CURRENT_PLAYER_Y = 6 CURRENT_PLAYER_SIZE = 30 GRID_SIZE = 214 CASE_SIZE = 66 BORDER = 4 SIGN_SIZE = 50 def dg(): fill_rect((WIDTH - GRID_SIZE) // 2, (HEIGHT - GRID_SIZE) // 2, GRID_SIZE, GRID_SIZE, (50, 50, 50)) for x in range((WIDTH - GRID_SIZE) // 2 + BORDER, (WIDTH - GRID_SIZE) // 2 + (BORDER + CASE_SIZE) * 3, CASE_SIZE + BORDER): for y in range((HEIGHT - GRID_SIZE) // 2 + BORDER, (HEIGHT-GRID_SIZE) // 2 + (BORDER + CASE_SIZE) * 3, CASE_SIZE + BORDER): fill_rect(x, y, CASE_SIZE, CASE_SIZE, (80, 255, 80)) def line(x1, y1, x2, y2, w, col): for y in range(min([y1, y2]), max([y1, y2])): for x in range(min([x1, x2]), max([x1, x2])): if abs(((x2 - x1) * (y - y1)) - ((y2 - y1) * (x - x1))) <= w*50: set_pixel(x, y, col) def circle(x, y, r, w, col): for yp in range(y - r, y + r): for xp in range(x - r, x + r): if (r - w)**2 <= (xp - x)**2 + (yp - y)**2 <= r**2: set_pixel(xp, yp, col) def dplayer(p): fill_rect(CURRENT_PLAYER_X, CURRENT_PLAYER_Y, CURRENT_PLAYER_SIZE, CURRENT_PLAYER_SIZE, (255, 255, 255)) if p == "X": line(CURRENT_PLAYER_X, CURRENT_PLAYER_Y, CURRENT_PLAYER_X + CURRENT_PLAYER_SIZE, CURRENT_PLAYER_Y + CURRENT_PLAYER_SIZE, 2, (200, 0, 0)) line(CURRENT_PLAYER_X, CURRENT_PLAYER_Y + CURRENT_PLAYER_SIZE, 36, 6, 2, (200, 0, 0)) else: circle(CURRENT_PLAYER_SIZE // 2 + CURRENT_PLAYER_X, CURRENT_PLAYER_SIZE // 2 + CURRENT_PLAYER_Y, CURRENT_PLAYER_SIZE // 2, 4, (0, 0, 200)) def launch(): p1, p2 = 0, 0 keys = [KEY_SEVEN, KEY_EIGHT, KEY_NINE, KEY_FOUR, KEY_FIVE, KEY_SIX, KEY_ONE, KEY_TWO, KEY_THREE] end = False p = "X" dg() dplayer(p) while not end: for n in range(9): if keydown(keys[n]) and not isoccupied(p1,p2,n): x = (WIDTH - GRID_SIZE) // 2 + BORDER + (CASE_SIZE + BORDER) * (n % 3) y = (HEIGHT - GRID_SIZE) // 2 + BORDER + (CASE_SIZE+BORDER) * (n//3) if p == "X": p1 = setbit(p1, n) line(x + (CASE_SIZE - SIGN_SIZE) // 2, y + (CASE_SIZE - SIGN_SIZE) // 2, x + (CASE_SIZE - SIGN_SIZE) // 2 + SIGN_SIZE, y + (CASE_SIZE - SIGN_SIZE) // 2 + SIGN_SIZE, 3, (255, 0, 0)) line(x + (CASE_SIZE - SIGN_SIZE) // 2, y + (CASE_SIZE - SIGN_SIZE) // 2 + SIGN_SIZE, x + (CASE_SIZE - SIGN_SIZE) // 2 + SIGN_SIZE, y + (CASE_SIZE - SIGN_SIZE) // 2, 3, (255, 0, 0)) else: p2 = setbit(p2, n) circle(x + CASE_SIZE // 2, y + CASE_SIZE // 2, SIGN_SIZE // 2, 4,(0, 0, 255)) if win(p1) or win(p2): end = True draw_string("{} wins !".format(p), 120, 100) break p = "X" if p == "O" else "O" dplayer(p) launch()