Toggle Navigation
Hatchery
Eggs
Tic Tac Toe
__init__.py
Login
Register
__init__.py
Content
import display, system, ugfx, sys, samd, time centers = [[42,10,0,0],[64,10,0,0],[85,10,0,0],[42,32,0,0],[64,32,0,0],[85,32,0,0],[42,54,0,0],[64,54,0,0],[85,54,0,0]] #filled = 1 ; non-filled = 2 current_pos = 0 radius = 8 filled = False winner = "Filled" winning_patterns = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]] def action_exit(pushed): if(pushed): system.home() def place_stuff(pushed): global filled global current_pos global radius global centers if(pushed and is_empty(current_pos)): display.drawCircle(centers[current_pos][0],centers[current_pos][1],radius,0,360,filled,0x000000) display.flush() centers[current_pos][2] = 1 if(filled): centers[current_pos][3] = 1 else: centers[current_pos][3] = 2 filled = not filled if(check_win()): game_over() def game_over(): global winner display.drawFill(0xFFFFFF) display.drawText(0,0,"Game Over! " + winner + "won the game",0x000000) display.flush() def check_win(): global winner global centers global winning_patterns for pattern in winning_patterns: if(centers[pattern[0]][3] == 1 and centers[pattern[1]][3] == 1 and centers[pattern[2]][3] == 1): winner = "Filled" return True elif(centers[pattern[0]][3] == 2 and centers[pattern[1]][3] == 2 and centers[pattern[2]][3] == 2): winner = "Empty" return True return False def is_empty(current_pos): if(centers[current_pos][2] == 0): return True else: return False def cp_plus(pushed): global current_pos if(pushed): if(current_pos == 8): current_pos = 0 else: current_pos = current_pos + 1 print(current_pos) show_pos() def cp_minus(pushed): global current_pos if(pushed): if(current_pos == 0): current_pos = 8 else: current_pos = current_pos - 1 print(current_pos) show_pos() def draw_table(): display.drawFill(0xFFFFFF) display.drawLine(53,0,53,63,0x000000) display.drawLine(75,0,75,63,0x000000) display.drawLine(32,21,96,21,0x000000) display.drawLine(32,43,96,43,0x000000) display.flush() def show_pos(): global current_pos display.drawRect(0,0,20,20,True,0xFFFFFF) display.drawText(0,0,str(current_pos),0x000000) display.flush() def main(): ugfx.input_init() ugfx.input_attach(ugfx.BTN_B, action_exit) ugfx.input_attach(ugfx.BTN_A, place_stuff) ugfx.input_attach(ugfx.JOY_DOWN, cp_minus) ugfx.input_attach(ugfx.JOY_UP, cp_plus) draw_table() show_pos() sys.stdin.read(1) action_exit(True) main()