1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 | """
File: t.py
Author: Proloy
Date: 5/7/2024
Description: TicTacToes
DEPTH = 1 is normal TicTacToe
DEPTH = 2 is ULTAMITE
any other depth no work prly
"""
from turtle import Turtle, Screen
DEPTH = 2
X_COLOR = 'red'
Y_COLOR = 'yellow'
PLAYER_COLORS = [X_COLOR, Y_COLOR]
SIZE = 18
def make_button(x, y, callback, builder):
button = Turtle()
button.speed(0)
button.hideturtle()
button.penup()
button.goto(x, y)
builder(button)
button.onclick(lambda x,y: draw_onclick(button, x, y))
button.showturtle()
def thing(x, y, builder):
button = Turtle()
button.speed(0)
button.hideturtle()
button.penup()
button.goto(x, y)
builder(button)
button.showturtle()
return button
def is_valid_move(turtle):
if turtle.super.fillcolor() in PLAYER_COLORS: return False
if turtle.fillcolor() in PLAYER_COLORS: return False
if current_index is None: return True
if turtle.super.super is None: return True
if turtle.super.super.subs[current_index].fillcolor() in PLAYER_COLORS: return True;
if current_index != turtle.super.index: return False
return True
def check_victory(values, player):
return (values[0] == player and values[1] == player and values[2] == player) or\
(values[3] == player and values[4] == player and values[5] == player) or\
(values[6] == player and values[7] == player and values[8] == player) or\
(values[0] == player and values[3] == player and values[6] == player) or\
(values[1] == player and values[4] == player and values[7] == player) or\
(values[2] == player and values[5] == player and values[8] == player) or\
(values[0] == player and values[4] == player and values[8] == player) or\
(values[2] == player and values[4] == player and values[6] == player)
def check_victories(turtle):
global current_index
if turtle is None: return
colors = list(map(lambda t: t.fillcolor(), turtle.subs))
if check_victory(colors, X_COLOR):
winning_color = X_COLOR
elif check_victory(colors, Y_COLOR):
winning_color = Y_COLOR
else:
return
turtle.fillcolor(winning_color)
check_victories(turtle.super)
def draw_onclick(turtle, x, y):
global current_index
if is_valid_move(turtle):
turtle.fillcolor(next(move))
current_index = turtle.index
check_victories(turtle.super)
def builder_builder(size, super):
def builder(turtle):
global mainturt
turtle.shape('square')
turtle.shapesize(size, size, size)
turtle.super = super
if super:
turtle.index = len(super.subs)
super.subs.append(turtle)
else:
mainturt = turtle
turtle.index = None
turtle.subs = []
turtle.fillcolor('white')
return builder
def make_board(size, x, y, depth, super=None):
if depth == 0:
make_button(x, y, draw_onclick, builder_builder(size, super))
return
turt = thing(x, y, builder_builder(size, super))
sizen = size/3
for xi in range(-1, 2):
for yi in range(-1, 2):
xn = x + xi * 20 * sizen
yn = y + yi * 20 * sizen
make_board(sizen, xn, yn, depth - 1, turt)
def moves():
while True:
yield X_COLOR
yield Y_COLOR
make_board(SIZE, 0, 0, DEPTH)
current_index = None
move = moves()
|
post a comment