 |

24.05.2024, 20:31
|
|
Познающий
Регистрация: 05.05.2022
Сообщений: 82
С нами:
2119802
Репутация:
8
|
|
Делаю крестики нолики с графикой для проекта. когда на поле более 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()
|
|
|

26.05.2024, 19:31
|
|
Участник форума
Регистрация: 08.02.2023
Сообщений: 280
С нами:
1718378
Репутация:
33
|
|
Сообщение от 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
(
)
|
|
|

27.05.2024, 12:40
|
|
Познающий
Регистрация: 05.05.2022
Сообщений: 82
С нами:
2119802
Репутация:
8
|
|
Сообщение от tyukapa
Привет
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
(
)
хоть твой код и работает, но у меня были бесконечные крестики нолики,а у тебя строчка с ними пропала,и они получились конечные. Можешь переделать пожалуйста?
|
|
|

28.05.2024, 00:58
|
|
Участник форума
Регистрация: 08.02.2023
Сообщений: 280
С нами:
1718378
Репутация:
33
|
|
Сообщение от anklain
хоть твой код и работает, но у меня были бесконечные крестики нолики,а у тебя строчка с ними пропала,и они получились конечные. Можешь переделать пожалуйста?
Я чутка переделал код, надеюсь разберешься что и как там, если что-то непонятно, спрашивай
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
(
)
|
|
|

28.05.2024, 10:57
|
|
Познающий
Регистрация: 05.05.2022
Сообщений: 82
С нами:
2119802
Репутация:
8
|
|
Сообщение от tyukapa
Я чутка переделал код, надеюсь разберешься что и как там, если что-то непонятно, спрашивай
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
(
)
О
Сообщение от tyukapa
Опять таки, работает, но не то что нужно. У меня у коде который был выше есть функции
Сообщение от tyukapa
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 то они пропадают, а почему-то когда я переношу эти функции в твой она перестаёт работать либо ломается. Можешь помочь мне с этим ? Буду благодарен
|
|
|

28.05.2024, 16:04
|
|
Участник форума
Регистрация: 08.02.2023
Сообщений: 280
С нами:
1718378
Репутация:
33
|
|
Сообщение от anklain
Вот здесь она проверяет все ли крестики нолики израсходованы и если их больше 3 то они пропадают, а почему-то когда я переношу эти функции в твой она перестаёт работать либо ломается. Можешь помочь мне с этим ? Буду благодарен
Не до конца понял твой вопрос, скинь код где ты вставляешь эти функции
|
|
|

29.05.2024, 00:09
|
|
Познающий
Регистрация: 05.05.2022
Сообщений: 82
С нами:
2119802
Репутация:
8
|
|
Сообщение от tyukapa
Не до конца понял твой вопрос, скинь код где ты вставляешь эти функции
Вот
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)
Сообщение от anklain
Вот
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 заходил, так мне не смогли помочь
|
|
|

30.05.2024, 22:41
|
|
Познающий
Регистрация: 21.08.2021
Сообщений: 41
С нами:
2490042
Репутация:
8
|
|
Я делал свои крестики нолики так:
Берётся матрица 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
может не будет полезно но чем смог тем помог
|
|
|
|
 |
|
Здесь присутствуют: 1 (пользователей: 0 , гостей: 1)
|
|
|
|