Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Скриптовые языки программирования > Python
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 29.07.2022, 22:08   #1
H_uman
Новичок
Джуниор
 
Регистрация: 29.07.2022
Сообщений: 2
По умолчанию Проблема с кодом крестики-нолики

Доброго дня форумчане!!!
Я новичок в программировании, конкретно я изучаю язык Python.
Так вот, хотел написать на нём игру Крестики-Нолики с ИИ, код ниже.
Всё работает, но появилась ошибка: когда на поле остаётся последнее не занятое место, а игра идёт в ничью, я хожу ставя крестик в это самое не занятое место --> он не отображается и ноутбук начинает сильно греть.
Пожалуйста подскажите как это исправить!!
Код:
# Переменные
from random import randint
lis, xod = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], []
viktori = 'неопределено'
rand = 0
flag = False


# Функция вывода таблицы
def kres_nol():
    print('     1   2   3')
    print('    ―――――――――――')
    print('1 | ', lis[0], '|', lis[3], '|', lis[7])
    print('  | -----------')
    print('2 | ', lis[5], '|', lis[1], '|', lis[4])
    print('  | -----------')
    print('3 | ', lis[8], '|', lis[6], '|', lis[2])


# Основной код
kres_nol()
while viktori == 'неопределено' :
    print('Ввидите координату крестика:')   
    x = input()
    xod.append(x)
    for i in range(9):
        if x == str(int(i+1)) + str(int(i+1)) and lis[i] == ' ':
            lis[i] = 'x'
        elif x == str(int(i+1)) + str(int(i+2)) and lis[i+3] == ' ':
            lis[i+3] = 'x'
        elif x == str(int(i+1)) + str(int(i))  and lis[i+4] == ' ':
            lis[i+4] = 'x'
        elif x == '13' and lis[7] == ' ':
            lis[7] = 'x'
        elif x == '31' and lis[8] == ' ':
            lis[8] = 'x'


          
    if lis[1] == ' ':
        lis[1] = 'o'
        
    elif lis[0] == 'x' and lis[3] == 'x' and lis[7] == ' ':    #1
        lis[7] = 'o'
    elif lis[0] == 'x' and lis[7] == 'x' and lis[3] == ' ':
        lis[3] = 'o'
    elif  lis[3] == 'x' and lis[7] == 'x' and lis[0] == ' ':
        lis[0] = 'o'
        
    elif lis[7] == 'x' and lis[4] == 'x' and lis[2] == ' ':    #2
        lis[2] = 'o'
    elif lis[2] == 'x' and lis[7] == 'x' and lis[4] == ' ':
        lis[4] = 'o'
    elif  lis[4] == 'x' and lis[2] == 'x' and lis[7] == ' ':
        lis[7] = 'o'

    elif lis[2] == 'x' and lis[6] == 'x' and lis[8] == ' ':    #3
        lis[8] = 'o'
    elif lis[2] == 'x' and lis[8] == 'x' and lis[6] == ' ':
        lis[6] = 'o'
    elif  lis[6] == 'x' and lis[8] == 'x' and lis[2] == ' ':
        lis[2] = 'o'

    elif lis[0] == 'x' and lis[5] == 'x' and lis[8] == ' ':    #4
        lis[8] = 'o'
    elif lis[5] == 'x' and lis[8] == 'x' and lis[0] == ' ':
        lis[0] = 'o'
    elif  lis[0] == 'x' and lis[8] == 'x' and lis[5] == ' ':
        lis[5] = 'o'

    elif lis[0] == 'x' and lis[1] == 'x' and lis[2] == ' ':    #5
        lis[2] = 'o'
    elif lis[1] == 'x' and lis[2] == 'x' and lis[0] == ' ':
        lis[0] = 'o'
    elif  lis[0] == 'x' and lis[2] == 'x' and lis[1] == ' ':
        lis[1] = 'o'
        
    elif lis[7] == 'x' and lis[1] == 'x' and lis[8] == ' ':    #6
        lis[8] = 'o'
    elif lis[8] == 'x' and lis[7] == 'x' and lis[1] == ' ':
        lis[1] = 'o'
    elif  lis[1] == 'x' and lis[8] == 'x' and lis[7] == ' ':
        lis[7] = 'o'

    elif lis[5] == 'x' and lis[1] == 'x' and lis[4] == ' ':    #7
        lis[4] = 'o'
    elif lis[5] == 'x' and lis[4] == 'x' and lis[1] == ' ':
        lis[1] = 'o'
    elif  lis[1] == 'x' and lis[4] == 'x' and lis[5] == ' ':
        lis[5] = 'o'

    elif lis[3] == 'x' and lis[1] == 'x' and lis[6] == ' ':    #8
        lis[6] = 'o'
    elif lis[6] == 'x' and lis[3] == 'x' and lis[1] == ' ':
        lis[1] = 'o'
    elif  lis[6] == 'x' and lis[1] == 'x' and lis[3] == ' ':
        lis[3] = 'o'
        

    elif lis[0] == 'o' and lis[3] == 'o' and lis[7] == ' ':    #1.()
        lis[7] = 'o'
    elif lis[0] == 'o' and lis[7] == 'o' and lis[3] == ' ':
        lis[3] = 'o'
    elif  lis[3] == 'o' and lis[7] == 'o' and lis[0] == ' ':
        lis[0] = 'o'
        
    elif lis[7] == 'o' and lis[4] == 'o' and lis[2] == ' ':    #2.()
        lis[2] = 'o'
    elif lis[2] == 'o' and lis[7] == 'o' and lis[4] == ' ':
        lis[4] = 'o'
    elif  lis[4] == 'o' and lis[2] == 'o' and lis[7] == ' ':
        lis[7] = 'o'

    elif lis[2] == 'o' and lis[6] == 'o' and lis[8] == ' ':    #3.()
        lis[8] = 'o'
    elif lis[2] == 'o' and lis[8] == 'o' and lis[6] == ' ':
        lis[6] = 'o'
    elif  lis[6] == 'o' and lis[8] == 'o' and lis[2] == ' ':
        lis[2] = 'o'

    elif lis[0] == 'o' and lis[5] == 'o' and lis[8] == ' ':    #4.()
        lis[8] = 'o'
    elif lis[5] == 'o' and lis[8] == 'o' and lis[0] == ' ':
        lis[0] = 'o'
    elif  lis[0] == 'o' and lis[8] == 'o' and lis[5] == ' ':
        lis[5] = 'o'

    elif lis[0] == 'o' and lis[1] == 'o' and lis[2] == ' ':    #5.()
        lis[2] = 'o'
    elif lis[1] == 'o' and lis[2] == 'o' and lis[0] == ' ':
        lis[0] = 'o'
    elif  lis[0] == 'o' and lis[2] == 'o' and lis[1] == ' ':
        lis[1] = 'o'
        
    elif lis[7] == 'o' and lis[1] == 'o' and lis[8] == ' ':    #6.()
        lis[8] = 'o'
    elif lis[8] == 'o' and lis[7] == 'o' and lis[1] == ' ':
        lis[1] = 'o'
    elif  lis[1] == 'o' and lis[8] == 'o' and lis[7] == ' ':
        lis[7] = 'o'

    elif lis[5] == 'o' and lis[1] == 'o' and lis[4] == ' ':    #7.()
        lis[4] = 'o'
    elif lis[5] == 'o' and lis[4] == 'o' and lis[1] == ' ':
        lis[1] = 'o'
    elif  lis[1] == 'o' and lis[4] == 'o' and lis[5] == ' ':
        lis[5] = 'o'

    elif lis[3] == 'o' and lis[1] == 'o' and lis[6] == ' ':    #8.()
        lis[6] = 'o'
    elif lis[6] == 'o' and lis[3] == 'o' and lis[1] == ' ':
        lis[1] = 'o'
    elif  lis[6] == 'o' and lis[1] == 'o' and lis[3] == ' ':
        lis[3] = 'o'

        
    else:
        flag = False
        while flag == False:
            rand = randint(0, 8)
            if lis[rand] == ' ':
                lis[rand] = 'o'
                flag = True
    kres_nol()

    
# Оператор победы
    if lis[0] == 'x' and lis[3] == 'x' and lis[7] == 'x':
        viktori = 'x'
    if lis[5] == 'x' and lis[5] == 'x' and lis[4] == 'x':
        viktori = 'x'
    if lis[8] == 'x' and lis[6] == 'x' and lis[2] == 'x':
        viktori = 'x'
    if lis[0] == 'x' and lis[1] == 'x' and lis[2] == 'x':
        viktori = 'x'
    if lis[7] == 'x' and lis[1] == 'x' and lis[8] == 'x':
        viktori = 'x'
    if lis[3] == 'x' and lis[1] == 'x' and lis[6] == 'x':
        viktori = 'x'
    if lis[0] == 'x' and lis[5] == 'x' and lis[8] == 'x':
        viktori = 'x'
    if lis[2] == 'x' and lis[4] == 'x' and lis[7] == 'x':
        viktori = 'x'

    if lis[0] == 'o' and lis[3] == 'o' and lis[7] == 'o':
        viktori = 'o'
    if lis[5] == 'o' and lis[5] == 'o' and lis[4] == 'o':
        viktori = 'o'
    if lis[8] == 'o' and lis[6] == 'o' and lis[2] == 'o':
        viktori = 'o'
    if lis[0] == 'o' and lis[1] == 'o' and lis[2] == 'o':
        viktori = 'o'
    if lis[7] == 'o' and lis[1] == 'o' and lis[8] == 'o':
        viktori = 'o'
    if lis[3] == 'o' and lis[1] == 'o' and lis[6] == 'o':
        viktori = 'o'
    if lis[0] == 'o' and lis[5] == 'o' and lis[8] == 'o':
        viktori = 'o'
    if lis[2] == 'o' and lis[4] == 'o' and lis[7] == 'o':
        viktori = 'o'


# Победа
    if viktori == 'o':
        print()
        print('Победил ИИ')
    elif viktori == 'x':
        print()
        print('Победил человек')
    elif ' ' not in lis:
        print()
        print('Ничья')
Пожалуйста не пишите что код громоздкий и корявый, так как я это сам понимаю и буду его оптимизировать по мере полученных мною знаний)

Последний раз редактировалось H_uman; 29.07.2022 в 22:11.
H_uman вне форума Ответить с цитированием
Старый 29.07.2022, 22:50   #2
macomics
Участник клуба
 
Регистрация: 17.04.2022
Сообщений: 1,833
По умолчанию

Код:
        flag = False
        while flag == False:
            rand = randint(0, 8)
            if lis[rand] == ' ':
                lis[rand] = 'o'
                flag = True
Вот это исправить. Добавьте проверку на наличие свободных полей перед запуском этого цикла. Иначе он будет бесконечным. Отсюда и 100% загрузка CPU, из-за которой начинает греться ноутбук.
macomics вне форума Ответить с цитированием
Старый 30.07.2022, 09:40   #3
H_uman
Новичок
Джуниор
 
Регистрация: 29.07.2022
Сообщений: 2
Хорошо

Спасибо, исправил, всё работает!
H_uman вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Крестики-нолики.Проблема при выводе поля. kofeinshik Помощь студентам 2 23.12.2011 15:36
Крестики нолики aud Паскаль, Turbo Pascal, PascalABC.NET 2 15.06.2009 22:32
Крестики - Нолики Иваненко Помощь студентам 6 09.05.2008 22:09
Еще раз крестики - нолики. Иваненко Помощь студентам 2 17.12.2007 20:09