| goodflex |
26.05.2024 19:31 |
Цитата:
Сообщение от anklain
Делаю крестики нолики с графикой для проекта. когда на поле более 3 крестиков/ноликов начальный пропадает. Проблема в том, что сломались комбинации для победы. Не работают по диагонали комбинации, по бокам. Просьба помочь, т.к. не особо горю желанием потратить еще пару часов за данной работой. Заранее спасибо
крестики нолики:
Код:
from tkinter import *
frm= []; btn = []; who = True
playArea = []
standings = [] # Турнирная таблица (приложение)
moves = []
array = []
def play(n):
global who
btn[n].config(text= 'X' if who else '0', state=DISABLED)
playArea[n] = 1 if who else -1
standings[0] = playArea[0] + playArea[1] + playArea[2]
standings[1] = playArea [3] + playArea[4] + playArea[5]
standings[2] = playArea [6] + playArea[7] + playArea[8]
standings[3] = playArea[0] + playArea[3] + playArea[6]
standings[4] = playArea [1] + playArea[4] + playArea[7]
standings [5] = playArea [2] + playArea [5] + playArea[8]
standings[6] = playArea[0] + playArea [4] + playArea[8]
standings [7] = playArea [2] + playArea[4] + playArea[6]
print(playArea,n, standings [0:8])
for i in range(3):
if standings[i] == 3:
root = Tk()
root.title('Player X Won!')
lbl1 = Label(root, text='Player X has won! Congrats!')
lbl1.grid(column=0, row=0)
root.geometry('350x175')
lbl1.place(x=110, y=70)
root.mainloop()
elif standings[i] == -3:
root = Tk()
root.title('Player 0 Won!')
lbl1 = Label(root, text='Player 0 has won! Congrats!')
lbl1.grid(column=0, row=0)
root.geometry('350x175')
lbl1.place(x=110, y=70)
root.mainloop()
who = not(who)
def on_click(event):
print(f"Clicked at x={standings}")
moves = (n)
array.append(n)
g = (array[0])
desired_length = 5
def check_array_length(array, desired_length):
if len(array) > desired_length:
return True
else:
return False
if check_array_length(array, desired_length):
btn[g].config(text='', state=NORMAL)
array.pop(0)
playArea[g] = 0
for i in range(3):
frm.append(Frame())
frm[i].pack(expand=YES, fill=BOTH)
for j in range(3):
btn.append(Button(frm[i], text=' ', font=('mono', 20, 'bold'), width=3, height=2))
btn[i*3+j].config(command=lambda n=i*3+j:play(n))
btn[i*3+j].pack(expand=YES, fill=BOTH, side=LEFT, padx=1, pady=1)
playArea.append(0)
standings.append(0)
mainloop()
|
Привет
Python:
Код:
from
tkinter
import
*
frm
=
[
]
;
btn
=
[
]
;
who
=
True
playArea
=
[
]
standings
=
[
]
# Турнирная таблица (приложение)
moves
=
[
]
array
=
[
]
def
check_winner
(
)
:
for
i
in
range
(
3
)
:
if
playArea
[
i
*
3
]
==
playArea
[
i
*
3
+
1
]
==
playArea
[
i
*
3
+
2
]
!=
0
:
return
playArea
[
i
*
3
]
if
playArea
[
i
]
==
playArea
[
i
+
3
]
==
playArea
[
i
+
6
]
!=
0
:
return
playArea
[
i
]
if
playArea
[
0
]
==
playArea
[
4
]
==
playArea
[
8
]
!=
0
or
playArea
[
2
]
==
playArea
[
4
]
==
playArea
[
6
]
!=
0
:
return
playArea
[
4
]
return
0
def
play
(
n
)
:
global
who
btn
[
n
]
.
config
(
text
=
'X'
if
who
else
'0'
,
state
=
DISABLED
)
playArea
[
n
]
=
1
if
who
else
-
1
winner
=
check_winner
(
)
if
winner
==
1
:
print
(
'Player X has won!'
)
root
=
Tk
(
)
root
.
title
(
'Player X Won!'
)
lbl1
=
Label
(
root
,
text
=
'Player X has won! Congrats!'
)
lbl1
.
grid
(
column
=
0
,
row
=
0
)
root
.
geometry
(
'350x175'
)
lbl1
.
place
(
x
=
110
,
y
=
70
)
root
.
mainloop
(
)
elif
winner
==
-
1
:
print
(
'Player 0 has won!'
)
root
=
Tk
(
)
root
.
title
(
'Player 0 Won!'
)
lbl1
=
Label
(
root
,
text
=
'Player 0 has won! Congrats!'
)
lbl1
.
grid
(
column
=
0
,
row
=
0
)
root
.
geometry
(
'350x175'
)
lbl1
.
place
(
x
=
110
,
y
=
70
)
root
.
mainloop
(
)
else
:
who
=
not
who
for
i
in
range
(
3
)
:
frm
.
append
(
Frame
(
)
)
frm
[
i
]
.
pack
(
expand
=
YES
,
fill
=
BOTH
)
for
j
in
range
(
3
)
:
btn
.
append
(
Button
(
frm
[
i
]
,
text
=
' '
,
font
=
(
'mono'
,
20
,
'bold'
)
,
width
=
3
,
height
=
2
)
)
btn
[
i
*
3
+
j
]
.
config
(
command
=
lambda
n
=
i
*
3
+
j
:
play
(
n
)
)
btn
[
i
*
3
+
j
]
.
pack
(
expand
=
YES
,
fill
=
BOTH
,
side
=
LEFT
,
padx
=
1
,
pady
=
1
)
playArea
.
append
(
0
)
mainloop
(
)
|