Last active
February 28, 2019 22:39
-
-
Save descartez/8d2345a0b0bd8b998c7cfda9f4b58f4a to your computer and use it in GitHub Desktop.
tic tac toe with microbits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add your Python code here. E.g. | |
from microbit import * | |
import radio | |
nodes = ["00","01","02","10","11","12","20","21","22"] | |
board = [ | |
["","",""], | |
["","",""], | |
["","",""] | |
] | |
def check_victory(board): | |
combinations = [ | |
# vertical | |
((0,0), (1,0), (2,0)), | |
((0,1), (1,1), (2,1)), | |
((0,2), (1,2), (2,2)), | |
# horizontal | |
((0,0), (0,1), (0,2)), | |
((1,0), (1,1), (1,2)), | |
((2,0), (2,1), (2,2)), | |
# crossed | |
((0,0), (1,1), (2,2)), | |
((2,0), (1,1), (0,2)) | |
] | |
for coordinates in combinations: | |
letters = [board[y][x] for x,y in coordinates] | |
if len(set(letters)) == 1 and letters[0] != "": | |
return letters[0] # returns corresponding letter for winner (X/O) | |
return False | |
def clear_nodes(nodes): | |
for node in nodes: | |
radio.send(str(node)) | |
radio.on() | |
clear_nodes(nodes) | |
while True: | |
incoming = radio.receive() | |
if incoming: | |
x = int(str(incoming)[0]) | |
y = int(str(incoming)[1]) | |
marker = str(incoming)[2] | |
board[x][y] = marker | |
display.show(marker) | |
win = check_victory(board) | |
if win: | |
display.scroll("Winner is:" + win) | |
sleep(100) | |
display.scroll("Winner is:" + win) | |
clear_nodes(nodes) | |
board = [["","",""],["","",""],["","",""]] | |
radio.send("confirmed") | |
sleep(100) | |
else: | |
display.show("*") | |
if button_a.is_pressed(): | |
clear_nodes(nodes) | |
board = [["","",""],["","",""],["","",""]] | |
if button_b.is_pressed(): | |
clear_nodes(nodes) | |
board = [["","",""],["","",""],["","",""]] | |
sleep(100) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add your Python code here. E.g. | |
from microbit import * | |
import radio | |
radio.on() | |
# adjust these per node | |
coord_x = 0 | |
coord_y = 0 | |
chosen = "" | |
while True: | |
incoming = radio.receive() | |
if incoming == "confirmed": | |
display.show("-") | |
sleep(100) | |
elif incoming == str(coord_x) + str(coord_y): | |
chosen = "" | |
display.clear() | |
if button_a.is_pressed(): | |
chosen = "x" | |
display.show(chosen) | |
radio.send(str(coord_x) + str(coord_y) + chosen) | |
elif button_b.is_pressed(): | |
chosen = "o" | |
display.show(chosen) | |
radio.send(str(coord_x) + str(coord_y) + chosen) | |
display.show(chosen) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment