Просмотр полной версии : Что не так с кодом?
Делаю крестики нолики с графикой для проекта. когда на поле более 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()
goodflex
26.05.2024, 19:31
Делаю крестики нолики с графикой для проекта. когда на поле более 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
(
)
Привет
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
(
)
хоть твой код и работает, но у меня были бесконечные крестики нолики,а у тебя строчка с ними пропала,и они получились конечные. Можешь переделать пожалуйста?
goodflex
28.05.2024, 00:58
хоть твой код и работает, но у меня были бесконечные крестики нолики,а у тебя строчка с ними пропала,и они получились конечные. Можешь переделать пожалуйста?
Я чутка переделал код, надеюсь разберешься что и как там, если что-то непонятно, спрашивай
Python:
from
tkinter
import
*
root
=
Tk
(
)
root
.
title
(
"Tic Tac Toe"
)
frm
=
[
]
btn
=
[
]
who
=
True
playArea
=
[
0
]
*
9
def
reset
(
)
:
global
who
,
playArea
for
i
in
range
(
9
)
:
btn
[
i
]
.
config
(
text
=
' '
,
state
=
NORMAL
)
playArea
=
[
0
]
*
9
who
=
True
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
!=
0
:
winner_text
=
'Player X has won!'
if
winner
==
1
else
'Player 0 has won!'
#1 строчка кода вместо 10+, не это ли счастье?
print
(
winner_text
)
message_label
.
config
(
text
=
winner_text
)
reset
(
)
elif
0
not
in
playArea
:
print
(
'Draw!'
)
message_label
.
config
(
text
=
'Draw!'
)
reset
(
)
else
:
who
=
not
who
for
i
in
range
(
3
)
:
frm
.
append
(
Frame
(
root
)
)
frm
[
i
]
.
pack
(
expand
=
YES
,
fill
=
BOTH
)
for
j
in
range
(
3
)
:
n
=
i
*
3
+
j
btn
.
append
(
Button
(
frm
[
i
]
,
text
=
' '
,
font
=
(
'mono'
,
20
,
'bold'
)
,
width
=
3
,
height
=
2
,
command
=
lambda
n
=
n
:
play
(
n
)
)
)
btn
[
n
]
.
pack
(
expand
=
YES
,
fill
=
BOTH
,
side
=
LEFT
,
padx
=
1
,
pady
=
1
)
message_label
=
Label
(
root
,
text
=
"Player X's turn"
,
font
=
(
'mono'
,
14
)
)
message_label
.
pack
(
side
=
TOP
,
pady
=
10
)
root
.
mainloop
(
)
Я чутка переделал код, надеюсь разберешься что и как там, если что-то непонятно, спрашивай
Python:
from
tkinter
import
*
root
=
Tk
(
)
root
.
title
(
"Tic Tac Toe"
)
frm
=
[
]
btn
=
[
]
who
=
True
playArea
=
[
0
]
*
9
def
reset
(
)
:
global
who
,
playArea
for
i
in
range
(
9
)
:
btn
[
i
]
.
config
(
text
=
' '
,
state
=
NORMAL
)
playArea
=
[
0
]
*
9
who
=
True
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
!=
0
:
winner_text
=
'Player X has won!'
if
winner
==
1
else
'Player 0 has won!'
#1 строчка кода вместо 10+, не это ли счастье?
print
(
winner_text
)
message_label
.
config
(
text
=
winner_text
)
reset
(
)
elif
0
not
in
playArea
:
print
(
'Draw!'
)
message_label
.
config
(
text
=
'Draw!'
)
reset
(
)
else
:
who
=
not
who
for
i
in
range
(
3
)
:
frm
.
append
(
Frame
(
root
)
)
frm
[
i
]
.
pack
(
expand
=
YES
,
fill
=
BOTH
)
for
j
in
range
(
3
)
:
n
=
i
*
3
+
j
btn
.
append
(
Button
(
frm
[
i
]
,
text
=
' '
,
font
=
(
'mono'
,
20
,
'bold'
)
,
width
=
3
,
height
=
2
,
command
=
lambda
n
=
n
:
play
(
n
)
)
)
btn
[
n
]
.
pack
(
expand
=
YES
,
fill
=
BOTH
,
side
=
LEFT
,
padx
=
1
,
pady
=
1
)
message_label
=
Label
(
root
,
text
=
"Player X's turn"
,
font
=
(
'mono'
,
14
)
)
message_label
.
pack
(
side
=
TOP
,
pady
=
10
)
root
.
mainloop
(
)
О
Опять таки, работает, но не то что нужно. У меня у коде который был выше есть функции
def on_click(event):
print(f"Clicked at x={standings}")
moves =
array.append
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)
Вот здесь она проверяет все ли крестики нолики израсходованы и если их больше 3 то они пропадают, а почему-то когда я переношу эти функции в твой она перестаёт работать либо ломается. Можешь помочь мне с этим ? Буду благодарен
goodflex
28.05.2024, 16:04
Вот здесь она проверяет все ли крестики нолики израсходованы и если их больше 3 то они пропадают, а почему-то когда я переношу эти функции в твой она перестаёт работать либо ломается. Можешь помочь мне с этим ? Буду благодарен
Не до конца понял твой вопрос, скинь код где ты вставляешь эти функции
Не до конца понял твой вопрос, скинь код где ты вставляешь эти функции
Вот
def on_click(event):
print(f"Clicked at x={standings}")
moves =
array.append
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)
Вот
def on_click(event):
print(f"Clicked at x={standings}")
moves =
array.append
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)
Скажи что не так, пжлст. Мне этот проект нужно сдать к 4 июня. Я много че в интернете посмотрел, но нихрена не понял. Даже на stackflow заходил, так мне не смогли помочь
Я делал свои крестики нолики так:
Берётся матрица 3 на 3 заполненная нулями ( я думаю сможешь определять куда кликнул игрок ). После чего можно использовать any() для сравнивания клеточек вот пример
Python:
if
all
(
field
[
i
]
[
i
]
for
i
in
range
(
len
(
field
)
)
)
or
all
(
field
[
i
]
[
-
(
i
+
1
)
]
for
i
in
range
(
len
(
field
)
)
)
:
# диагонали
return
True
if
any
(
all
(
kletka
==
1
for
kletka
in
stroka
)
for
stroka
in
field
)
or
any
(
all
(
kletka
==
2
for
kletka
in
stroka
)
for
stroka
in
field
)
:
# Горизонталь
return
True
if
any
(
all
(
field
[
stroka
]
[
kletka
]
==
1
for
stroka
in
range
(
len
(
field
)
)
)
for
kletka
in
range
(
len
(
field
)
)
)
:
# Вертикально
return
True
может не будет полезно но чем смог тем помог
vBulletin® v3.8.14, Copyright ©2000-2026, vBulletin Solutions, Inc. Перевод: zCarot