Closed va1entei closed 3 years ago
Я сделал это, но это было не легко. Было
board = list(range(1,10))
def draw_board(board):
print ("-" * 13)
for i in range(3):
print ("|", board[0+i*3], "|", board[1+i*3], "|", board[2+i*3], "|")
print ("-" * 13)
def take_input(player_token):
valid = False
while not valid:
player_answer = input("Куда поставим " + player_token+"? ")
try:
player_answer = int(player_answer)
except:
print ("Некорректный ввод. Вы уверены, что ввели число?")
continue
if player_answer >= 1 and player_answer <= 9:
if (str(board[player_answer-1]) not in "XO"):
board[player_answer-1] = player_token
valid = True
else:
print ("Эта клеточка уже занята")
else:
print ("Некорректный ввод. Введите число от 1 до 9 чтобы походить.")
def check_win(board):
win_coord = ((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))
for each in win_coord:
if board[each[0]] == board[each[1]] == board[each[2]]:
return board[each[0]]
return False
def main(board):
counter = 0
win = False
while not win:
draw_board(board)
if counter % 2 == 0:
take_input("X")
else:
take_input("O")
counter += 1
if counter > 4:
tmp = check_win(board)
if tmp:
print (tmp, "выиграл!")
win = True
break
if counter == 9:
print ("Ничья!")
break
draw_board(board)
main(board)
Стало
board = list(range(1,26))
def draw_board(board):
print ("-" * 21)
for i in range(5):
print ("|", board[0+i*5], "|", board[1+i*5], "|" , board[2+i*5], "|" , board[3+i*5], "|" , board[4+i*5], "|")
print ("-" * 21)
def take_input(player_token):
valid = False
while not valid:
player_answer = input("Куда поставим " + player_token+"? ")
try:
player_answer = int(player_answer)
except:
print ("Некорректный ввод. Вы уверены, что ввели число?")
continue
if player_answer >= 1 and player_answer <= 25:
if (str(board[player_answer-1]) not in "XO"):
board[player_answer-1] = player_token
valid = True
else:
print ("Эта клеточка уже занята")
else:
print ("Некорректный ввод. Введите число от 1 до 9 чтобы походить.")
def check_win(board):
win_coord = ((0,1,2,3,4),(5,6,7,8,9),(10,11,12,13,14),(15,16,17,18,19),(20,21,22,23,24),(0,5,10,15,20),(1,6,11,16,21),(2,7,12,17,22),(3,8,13,18,23),(4,9,14,19,24),(0,6,12,18,24),(4,8,12,16,20))
for each in win_coord:
if board[each[0]] == board[each[1]] == board[each[2]] == board[each[3]] == board[each[4]]:
return board[each[0]]
return False
def main(board):
counter = 0
win = False
while not win:
draw_board(board)
if counter % 2 == 0:
take_input("X")
else:
take_input("O")
counter += 1
if counter > 6:
tmp = check_win(board)
if tmp:
print (tmp, "выиграл!")
win = True
break
if counter == 25:
print ("Ничья!")
break
draw_board(board)
main(board)
[10,10][5,5]