Notes
Notes - notes.io |
from m5ui import *
from uiflow import *
setScreenColor(0x222222)
def make_point_from_indices(r, c):
return Point(80 + c * 80, 40 + r * 80)
class Point:
def __init__(self, x_, y_):
self.x = x_
self.y = y_
class Cross:
def __init__(self, r, c):
center = make_point_from_indices(r, c)
self.line0 = M5Line(M5Line.PLINE, center.x - 30,
center.y + 30, center.x + 30,
center.y - 30, 0xFFFFFF)
self.line1 = M5Line(M5Line.PLINE, center.x + 30,
center.y + 30, center.x - 30,
center.y - 30, 0xFFFFFF)
def hide(self):
self.line0.hide()
self.line1.hide()
class Circle:
def __init__(self, r, c):
center = make_point_from_indices(r, c)
self.circle = M5Circle(center.x, center.y,
30, 0x222222, 0xffffff)
def hide(self):
self.circle.hide()
CROSS = 1
CIRCLE = -1
EMPTY = 0
class Field:
def __init__(self):
self.fields = [[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
self.figures = []
def check_free_cell(self, i, j):
return self.fields[i][j] == EMPTY
def add_figure(self, r, c, player):
self.fields[r][c] = player.figure
if player.figure == CROSS:
self.figures.append(Cross(r, c))
else:
self.figures.append(Circle(r, c))
def check_win(self, player):
return self.check_rows(player.figure) or
self.check_diagonal(player.figure)
def check_draw(self):
return len(self.figures) == 9
def check_rows(self, figure):
for i in range(3):
rows = 0
cols = 0
for j in range(3):
if self.fields[i][j] == figure:
rows += 1
if self.fields[j][i] == figure:
cols += 1
if cols == 3 or rows == 3:
return True
return False
def check_diagonal(self, figure):
left = 0
right = 0
for i in range(3):
if self.fields[i][i] == figure:
left += 1
if self.fields[i][2 - i] == figure:
right += 1
if left == 3 or right == 3:
return True
return False
def draw(self):
self.lines = []
self.lines.append(M5Line(M5Line.PLINE, 40, 160, 280, 160, 0xFFFFFF))
self.lines.append(M5Line(M5Line.PLINE, 40, 80, 280, 80, 0xFFFFFF))
self.lines.append(M5Line(M5Line.PLINE, 120, 0, 120, 240, 0xFFFFFF))
self.lines.append(M5Line(M5Line.PLINE, 200, 0, 200, 240, 0xFFFFFF))
def clean(self):
for i in range(3):
for j in range(3):
self.fields[i][j] = EMPTY
for figure in self.figures:
figure.hide()
figures = []
for line in self.lines:
line.hide()
lines = []
class Player:
def __init__(self):
self.figure = CROSS
def next_figure(self):
if self.figure == CROSS:
self.figure = CIRCLE
else:
self.figure = CROSS
field = Field()
field.draw()
label_row = M5TextBox(0, 25, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
label_column = M5TextBox(0, 45, "Text", lcd.FONT_Default, 0xFFFFFF, rotate=0)
row = 0
column = 0
def increase_column():
global column
column = (column + 1) % 3
wait_ms(50)
def increase_row():
global row
row = (row + 1) % 3
wait_ms(50)
btnA.wasPressed(increase_row)
btnB.wasPressed(increase_column)
player = Player()
while True:
while btnC.isReleased():
label_row.setText("row: {}".format(row))
label_column.setText("column: {}".format(column))
while btnC.isPressed():
pass
if field.check_free_cell(row, column):
field.add_figure(row, column, player)
if field.check_win(player):
label_row.setText(str(player.figure) + " wins!")
break
if field.check_draw():
label_row.setText("Draw")
break
player.next_figure()
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team