Ребят, хелп! Есть тут те кто разбирается в Python? В Приложении (Урок 4) приведен код игры «Крестики-
нолики». Создайте диалоговое окно, где пользователь
сможет выбрать тип игры: один игрок / два игрока.
Вот код
from tkinter import *
from tkinter import messagebox
from random import randint
from tkinter import simpledialog
ActivePlayer = 1
p1 = []
p2 = []
window = Tk()
window.title("Game")
button1 = Button(window, text = "")
name1 = simpledialog.askstring("Input", "Input Palyer 1 name", parent = window)
if name1 is not None:
print("Player 1 name", name1)
else:
print("No name")
name2 = simpledialog.askstring("Input", "Input Player 2 name", parent = window)
if name2 is not None:
print("Player 2 name", name2)
else:
print("No name")
button1.grid(row = 0, column = 0, sticky = "snew", ipadx = 40, ipady = 40)
button1.config(command = lambda: ButtonClick(1))
button2 = Button(window, text = "")
button2.grid(row = 0, column = 1, sticky = "snew", ipadx = 40, ipady = 40)
button2.config(command = lambda: ButtonClick(2))
button3 = Button(window, text = "")
button3.grid(row = 0, column = 2, sticky = "snew", ipadx = 40, ipady = 40)
button3.config(command = lambda: ButtonClick(3))
button4 = Button(window, text = "")
button4.grid(row = 1, column = 0, sticky = "snew", ipadx = 40, ipady = 40)
button4.config(command = lambda: ButtonClick(4))
button5 = Button(window, text = "")
button5.grid(row = 1, column = 1, sticky = "snew", ipadx = 40, ipady = 40)
button5.config(command = lambda: ButtonClick(5))
button6 = Button(window, text = "")
button6.grid(row = 1, column = 2, sticky = "snew", ipadx = 40, ipady = 40)
button6.config(command = lambda: ButtonClick(6))
button7 = Button(window, text = "")
button7.grid(row = 2, column = 0, sticky = "snew", ipadx = 40, ipady = 40)
button7.config(command = lambda: ButtonClick(7))
button8 = Button(window, text = "")
button8.grid(row = 2, column = 1, sticky = "snew", ipadx = 40, ipady = 40)
button8.config(command = lambda: ButtonClick(8))
button9 = Button(window, text = "")
button9.grid(row = 2, column = 2, sticky = "snew", ipadx = 40, ipady = 40)
button9.config(command = lambda: ButtonClick(9))
def ButtonClick(id):
global ActivePlayer
global p1
global p2
print("ID:{}".format(id))
if (ActivePlayer == 1):
SetLayout(id, "X")
p1.append(id)
ActivePlayer = 2
print("P1:{}".format(p1))
elif (ActivePlayer == 2):
SetLayout(id, "O")
p2.append(id)
ActivePlayer = 1
print("P2:{}".format(p2))
ChooseWinner()
def SetLayout(id, PlayerSymbol):
if (id == 1):
button1.config(text = PlayerSymbol,
state = DISABLED)
elif (id == 2):
button2.config(text = PlayerSymbol,
state = DISABLED)
elif (id == 3):
button3.config(text = PlayerSymbol,
state = DISABLED)
elif (id == 4):
button4.config(text = PlayerSymbol,
state = DISABLED)
elif (id == 5):
button5.config(text = PlayerSymbol,
state = DISABLED)
elif (id == 6):
button6.config(text = PlayerSymbol,
state = DISABLED)
elif (id == 7):
button7.config(text = PlayerSymbol,
state = DISABLED)
elif (id == 8):
button8.config(text = PlayerSymbol,
state = DISABLED)
elif (id == 9):
button9.config(text = PlayerSymbol,
state = DISABLED)
def ChooseWinner():
Winner = -1
''' W I N N E R - 1 '''
if ((1 in p1) and (2 in p1) and (3 in p1)):
Winner = 1
if ((1 in p2) and (2 in p2) and (3 in p2)):
Winner = 2
if ((4 in p1) and (5 in p1) and (6 in p1)):
Winner = 1
if ((4 in p2) and (5 in p2) and (6 in p2)):
Winner = 2
if ((7 in p1) and (8 in p1) and (9 in p1)):
Winner = 1
if ((7 in p2) and (8 in p2) and (9 in p2)):
Winner = 2
''' W I N N E R - 2 '''
if ((1 in p1) and (4 in p1) and (7 in p1)):
Winner = 1
if ((1 in p2) and (4 in p2) and (7 in p2)):
Winner = 2
if ((2 in p1) and (6 in p1) and (8 in p1)):
Winner = 1
if ((2 in p2) and (6 in p2) and (8 in p2)):
Winner = 2
if ((3 in p1) and (7 in p1) and (9 in p1)):
Winner = 1
if ((3 in p2) and (7 in p2) and (9 in p2)):
Winner = 2
''' W I N N E R - 3 '''
if ((1 in p1) and (5 in p1) and (9 in p1)):
Winner = 1
if ((1 in p2) and (5 in p2) and (9 in p2)):
Winner = 2
if ((3 in p1) and (5 in p1) and (7 in p1)):
Winner = 1
if ((3 in p2) and (5 in p2) and (7 in p2)):
Winner = 2
if Winner == 1:
messagebox.showinfo("Winner", f"Player 1 {name1} is Winner")
elif Winner == 2:
messagebox.showinfo("Winner", f"Player 2 {name2} is Winner")
def AutoPlay():
global p1
global p2
EmplyCells = []
for i in range(9):
if ( (i+1 in p1) or (i+1 in p2)):
EmplyCells.append(i+1)
RandomIndex = randint(0, len(EmplyCells)-1)
ButtonClick(EmplyCells[RandomIndex])
window.mainloop()
263
380
Ответы на вопрос:
Объяснение:
https://ru.stackoverflow.com/questions/900763/%D0%9A%D1%80%D0%B5%D1%81%D1%82%D0%B8%D0%BA%D0%B8-%D0%BD%D0%BE%D0%BB%D0%B8%D0%BA%D0%B8
Перейди по ссылке, тут подробно объяснено.
vark: integer; s: string; beginreadln(s); k: =pos('то',s); while k< > 0 dobegin k: =pos(' то ',s); if k< > 0 then begin delete(s,k+1,2); insert('это',s,k+1); end; end; writeln(s); end.
Реши свою проблему, спроси otvet5GPT
-
Быстро
Мгновенный ответ на твой вопрос -
Точно
Бот обладает знаниями во всех сферах -
Бесплатно
Задай вопрос и получи ответ бесплатно
Популярно: Информатика
-
ksenchhh30.05.2021 04:45
-
KEK2281337148828.12.2022 22:56
-
demoooon2408.05.2022 20:40
-
Валерия11111221lera08.11.2021 00:13
-
Neznaikakrasno13.05.2021 10:26
-
кика2005124.05.2021 04:43
-
saitieva0307.06.2021 19:42
-
yudaeva7817.05.2021 00:29
-
KATE27070120.07.2020 15:34
-
0372120.01.2020 15:30
Есть вопросы?
-
Как otvet5GPT работает?
otvet5GPT использует большую языковую модель вместе с базой данных GPT для обеспечения высококачественных образовательных результатов. otvet5GPT действует как доступный академический ресурс вне класса. -
Сколько это стоит?
Проект находиться на стадии тестирования и все услуги бесплатны. -
Могу ли я использовать otvet5GPT в школе?
Конечно! Нейросеть может помочь вам делать конспекты лекций, придумывать идеи в классе и многое другое! -
В чем отличия от ChatGPT?
otvet5GPT черпает академические источники из собственной базы данных и предназначен специально для студентов. otvet5GPT также адаптируется к вашему стилю письма, предоставляя ряд образовательных инструментов, предназначенных для улучшения обучения.