Найти - Пользователи
Полная версия: У меня проблема с кодом
Начало » Python для новичков » У меня проблема с кодом
1
K1ssuke1
У меня задача максимально оптимизировать код и исправить проблему с ничьей. Если что код под крестики нолики. Если сможете помочь то прошу вас.
вот код:
import random

mss1 =
mss2 =
mss3 =

step_count = 0

def printP():
print(“ 1 2 3”)
print(“A”, mss1)
print(“ ”)
print(“B”, mss2)
print(“ ”)
print(“C”, mss3)

def win():
if mss1 == “X” and mss1 == “X” and mss1 == “X”:
print(“Win User”)
return True
elif mss2 == “X” and mss2 == “X” and mss2 == “X”:
print(“Win User”)
return True
elif mss3 == “X” and mss3 == “X” and mss3 == “X”:
print(“Win User”)
return True
elif mss1 == “X” and mss2 == “X” and mss3 == “X”:
print(“Win User”)
return True
elif mss1 == “X” and mss2 == “X” and mss3 == “X”:
print(“Win User”)
return True
elif mss1 == “X” and mss2 == “X” and mss3 == “X”:
print(“Win User”)
return True
elif mss1 == “X” and mss2 == “X” and mss3 == “X”:
print(“Win User”)
return True
elif mss3 == “X” and mss2 == “X” and mss1 == “X”:
print(“Win User”)
return True

elif mss1 == “0” and mss1 == “0” and mss1 == “0”:
print(“Win Comp”)
return True
elif mss2 == “0” and mss2 == “0” and mss2 == “0”:
print(“Win Comp”)
return True
elif mss3 == “0” and mss3 == “0” and mss3 == “0”:
print(“Win Comp”)
return True
elif mss1 == “0” and mss2 == “0” and mss3 == “0”:
print(“Win Comp”)
return True
elif mss1 == “0” and mss2 == “0” and mss3 == “0”:
print(“Win Comp”)
return True
elif mss1 == “0” and mss2 == “0” and mss3 == “0”:
print(“Win Comp”)
return True
elif mss1 == “0” and mss2 == “0” and mss3 == “0”:
print(“Win Comp”)
return True
elif mss3 == “0” and mss2 == “0” and mss1 == “0”:
print(“Win Comp”)
return True

def Bot():
step_count = 0
r = random.randint(1, 3)
if r == 1:
r = random.randint(0, 2)
if mss1 == “_”:
mss1 = “0”
step_count += 1
else:
Bot()
elif r == 2:
r = random.randint(0, 2)
if mss2 == “_”:
mss2 = “0”
step_count += 1
else:
Bot()
elif r == 3:
r = random.randint(0, 2)
if mss3 == “_”:
mss3 = “0”
step_count += 1
else:
Bot()


while True:
printP()
#if win():
# break
#else:
user = input(“-> ”)
if user == “A1” or user == “a1”:
if mss1 == “_”:
mss1 = “X”
step_count += 1
if step_count == 9:
print(“Ничья”)
break
else:
Bot()
else:
print(“No ”)
elif user == “A2” or user == “a2”:
if mss1 == “_”:
mss1 = “X”
step_count += 1
if step_count == 9:
print(“Ничья”)
break
else:
Bot()

else:
print(“No ”)
elif user == “A3” or user == “a3”:
if mss1 == “_”:
mss1 = “X”
step_count += 1
if step_count == 9:
print(“Ничья”)
break
else:
Bot()

else:
print(“No ”)
elif user == “B1” or user == “b1”:
if mss2 == “_”:
mss2 = “X”
step_count += 1
if step_count == 9:
print(“Ничья”)
break
else:
Bot()

else:
print(“No ”)
elif user == “B2” or user == “b2”:
if mss2 == “_”:
mss2 = “X”
step_count += 1
if step_count == 9:
print(“Ничья”)
break
else:
Bot()

else:
print(“No ”)
elif user == “B3” or user == “b3”:
if mss2 == “_”:
mss2 = “X”
step_count += 1
if step_count == 9:
print(“Ничья”)
break
else:
Bot()
else:
print(“No ”)
elif user == “C1” or user == “c1”:
if mss3 == “_”:
mss3 = “X”
step_count += 1
if step_count == 9:
print(“Ничья”)
break
else:
Bot()
else:
print(“No ”)
elif user == “C2” or user == “c2”:
if mss3 == “_”:
mss3 = “X”
step_count += 1
if step_count == 9:
print(“Ничья”)
break
else:
Bot()
else:
print(“No ”)
elif user == “C3” or user == “c3”:
if mss3 == “_”:
mss3 = “X”
step_count += 1
if step_count == 9:
print(“Ничья”)
break
else:
Bot()
else:
print(“No ”)
AD0DE412
.
K1ssuke1
AD0DE412
.
 import random
mss1 = ["_", "_", "_"]
mss2 = ["_", "_", "_"]
mss3 = ["_", "_", "_"]
def printP():
    print("    1    2    3")
    print("A", mss1)
    print(" ")
    print("B", mss2)
    print(" ")
    print("C", mss3)
def win():
    if mss1[0] == "X" and mss1[1] == "X" and mss1[2] == "X":
        print("Win User")
        return True
    elif mss2[0] == "X" and mss2[1] == "X" and mss2[2] == "X":
        print("Win User")
        return True
    elif mss3[0] == "X" and mss3[1] == "X" and mss3[2] == "X":
        print("Win User")
        return True
    elif mss1[0] == "X" and mss2[0] == "X" and mss3[0] == "X":
        print("Win User")
        return True
    elif mss1[1] == "X" and mss2[1] == "X" and mss3[1] == "X":
        print("Win User")
        return True
    elif mss1[2] == "X" and mss2[2] == "X" and mss3[2] == "X":
        print("Win User")
        return True
    elif mss1[0] == "X" and mss2[1] == "X" and mss3[2] == "X":
        print("Win User")
        return True
    elif mss3[0] == "X" and mss2[1] == "X" and mss1[2] == "X":
        print("Win User")
        return True
    elif mss1[0] == "0" and mss1[1] == "0" and mss1[2] == "0":
        print("Win Comp")
        return True
    elif mss2[0] == "0" and mss2[1] == "0" and mss2[2] == "0":
        print("Win Comp")
        return True
    elif mss3[0] == "0" and mss3[1] == "0" and mss3[2] == "0":
        print("Win Comp")
        return True
    elif mss1[0] == "0" and mss2[0] == "0" and mss3[0] == "0":
        print("Win Comp")
        return True
    elif mss1[1] == "0" and mss2[1] == "0" and mss3[1] == "0":
        print("Win Comp")
        return True
    elif mss1[2] == "0" and mss2[2] == "0" and mss3[2] == "0":
        print("Win Comp")
        return True
    elif mss1[0] == "0" and mss2[1] == "0" and mss3[2] == "0":
        print("Win Comp")
        return True
    elif mss3[0] == "0" and mss2[1] == "0" and mss1[2] == "0":
        print("Win Comp")
        return True
def Bot():
    r = random.randint(1, 3)
    if r == 1:
        r = random.randint(0, 2)
        if mss1[r] == "_":
            mss1[r] = "0"
        else:
            Bot()
    elif r == 2:
        r = random.randint(0, 2)
        if mss2[r] == "_":
            mss2[r] = "0"
        else:
            Bot()
    elif r == 3:
        r = random.randint(0, 2)
        if mss3[r] == "_":
            mss3[r] = "0"
        else:
            Bot()
while True:
    printP()
    if win():
        break
    else:
        user = input("-> ")
        if user == "A1" or user == "a1":
            if mss1[0] == "_":
                mss1[0] = "X"
            else:
                print("No ")
        elif user == "A2" or user == "a2":
            if mss1[1] == "_":
                mss1[1] = "X"
            else:
                print("No ")
        elif user == "A3" or user == "a3":
            if mss1[2] == "_":
                mss1[2] = "X"
            else:
                print("No ")
        elif user == "B1" or user == "b1":
            if mss2[0] == "_":
                mss2[0] = "X"
            else:
                print("No ")
        elif user == "B2" or user == "b2":
            if mss2[1] == "_":
                mss2[1] = "X"
            else:
                print("No ")
        elif user == "B3" or user == "b3":
            if mss2[2] == "_":
                mss2[2] = "X"
            else:
                print("No ")
        elif user == "C1" or user == "c1":
            if mss3[0] == "_":
                mss3[0] = "X"
            else:
                print("No ")
        elif user == "C2" or user == "c2":
            if mss3[1] == "_":
                mss3[1] = "X"
            else:
                print("No ")
        elif user == "C3" or user == "c3":
            if mss3[2] == "_":
                mss3[2] = "X"
            else:
                print("No ")
так?

AD0DE412
ага а что это вобще а да кнешно крестики нолики
а не проще было создать описания состояния победы и сравнивать походово если ходы кончились и победы не случилось то ничья
типа хз
 win_state = [[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1,  1, 1, 0, 0, 0, 0, 0, 0], ...]
не не так хм
 all_state = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
player_1 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
player_2 = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
flag = 2
def test(player):
    """1 is win 0 is lose"""
    if [1, 1, 1] in player:
        return 1
    elif [1, 1, 1] == [player[0][0], player[1][1], player[2][2]]:
        return 1
    elif [1, 1, 1] == [player[0][2], player[1][1], player[2][0]]:
        return 1
    elif [1, 1, 1] == [player[0][0], player[1][0], player[2][0]]:
        return 1
    elif [1, 1, 1] == [player[0][1], player[1][1], player[2][1]]:
        return 1
    elif [1, 1, 1] == [player[0][2], player[1][2], player[2][2]]:
        return 1
    else:
        return 0
def print_state():
    print(f'{all_state[0]}\n{all_state[1]}\n{all_state[2]}\n')
while sum(all_state[0] + all_state[1] + all_state[2]) != 13:
    print_state()
    state_test = 1
    if flag == 2:
        while state_test:
            row = int(input(f'player_1 row: '))
            col = int(input(f'player_1 col: '))
            if all_state[row][col] == 0:
                all_state[row][col] = 1
                player_1[row][col] = 1
                flag = 1
                state_test = 0
            else:
                print('---')
        else:
            if test(player_1):
                print_state()
                print('player_1 win')
                break
    elif flag == 1:
        while state_test:
            row = int(input(f'player_2 row: '))
            col = int(input(f'player_2 col: '))
            if all_state[row][col] == 0:
                all_state[row][col] = 2
                player_2[row][col] = 1
                flag = 2
                state_test = 0
            else:
                print('---')
        else:
            if test(player_2):
                print_state()
                print('player_2 win')
                break
else:
    print('no winners')
зы рандом прикрутете сами эээ надеюсь
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB