from random import randint mesto = { 'A1': 1, 'A2': 2, 'A3': 3, 'B1': 4, 'B2': 5, 'B3': 6, 'C1': 7, 'C2': 8, 'C3': 9, } lifi = { 'A1': 0, 'A2': 1, 'A3': 2, 'B1': 0, 'B2': 1, 'B3': 2, 'C1': 0, 'C2': 1, 'C3': 2, } combination = [ [[1, 1, 1], [0, 0, 0], [0, 0, 0]], # 0 [[0, 0, 0], [1, 1, 1], [0, 0, 0]], # 1 [[0, 0, 0], [0, 0, 0], [1, 1, 1]], # 2 [[0, 0, 1], [0, 0, 1], [0, 0, 1]], # 3 [[1, 0, 0], [1, 0, 0], [1, 0, 0]], # 4 [[0, 1, 0], [0, 1, 0], [0, 1, 0]], # 5 [[1, 0, 0], [0, 1, 0], [0, 0, 1]], # 6 [[0, 0, 1], [0, 1, 0], [1, 0, 0]], # 7 [[2, 2, 2], [0, 0, 0], [0, 0, 0]], # 8 [[0, 0, 0], [2, 2, 2], [0, 0, 0]], # 9 [[0, 0, 0], [0, 0, 0], [2, 2, 2]], # 10 [[2, 0, 0], [2, 0, 0], [2, 0, 0]], # 11 [[0, 2, 0], [0, 2, 0], [0, 2, 0]], # 12 [[0, 0, 2], [0, 0, 2], [0, 0, 2]], # 13 [[2, 0, 0], [0, 2, 0], [0, 0, 2]], # 14 [[0, 0, 2], [0, 2, 0], [2, 0, 0]] # 15 ] #0 - это пустая клетка #1 - это крестик #2 - это нолик FIELD = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] # низ середина верх hod = [0] * 9 numbh = 0 def drawField(): print(' 1 2 3') print('A', FIELD[2][0], FIELD[2][1], FIELD[2][2]) print('B', FIELD[1][0], FIELD[1][1], FIELD[1][2]) print('C', FIELD[0][0], FIELD[0][1], FIELD[0][2]) return 0 def Comuter_dodje(vibcomp, igrvib): for i in range(0, 16): sofp = 0 for j in range(0, 3): for x in range(0, 3): if combination[i][j][x] == FIELD[j][x] and FIELD[j][x] == igrvib: sofp += 1 if sofp == 2: for y in range(0, 3): for g in range(0, 3): if FIELD[g][y] == 0 and combination[i][g][y] == igrvib: FIELD[g][y] = vibcomp combination[i] = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] return 1 return 0 def Computer_hod(hodit): if hodit == 0: kol = 0 while True: a1 = randint(0, 2) a2 = randint(0, 2) if FIELD[a1][a2] == 0: FIELD[a1][a2] = vibcomp break if kol == 9: break kol += 1 pass def Comuter_dodje2(vibcomp, igrvib, hod): if hod == 0: for i in range(0, 16): sofp = 0 for j in range(0, 3): for x in range(0, 3): if combination[i][j][x] == FIELD[j][x] and FIELD[j][x] == igrvib: sofp += 1 if sofp == 2: for y in range(0, 3): for g in range(0, 3): if FIELD[g][y] == 0 and combination[i][g][y] == igrvib: FIELD[g][y] = vibcomp combination[i] = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] return 1 return 0 def Comuter_dodje1(hod, vibcomp, igrvib, sechod): if hod == 0 and sechod == 0: for i in range(0, 16): for j in range(0, 3): for x in range(0, 3): if combination[i][j][x] == FIELD[j][x] and FIELD[j][x] == igrvib: for y in range(0, 3): for g in range(0, 3): if FIELD[g][y] == 0 and combination[i][g][y] == igrvib: FIELD[g][y] = vibcomp return 1 return 0 def special_comp(vibcomp): for i in range(0, 16): sofp = 0 for j in range(0, 3): for x in range(0, 3): if combination[i][j][x] == FIELD[j][x] and FIELD[j][x] == vibcomp: sofp += 1 if sofp == 2: for y in range(0, 3): for g in range(0, 3): if FIELD[g][y] == 0 and combination[i][g][y] == vibcomp: FIELD[g][y] = vibcomp return 1 return 0 def hum_hod(igrvib): drawField() while True: if igrvib == 1: cr = input("Крестики ходят: ") elif igrvib == 2: cr = input("Нолики ходят: ") crhod = mesto[cr] if crhod <= 3: if FIELD[2][lifi[cr]] == 0: FIELD[2][lifi[cr]] = igrvib break else: print('Это место занято!') elif crhod <= 6: if FIELD[1][lifi[cr]] == 0: FIELD[1][lifi[cr]] = igrvib break else: print('Это место занято!') elif crhod <= 9: if FIELD[0][lifi[cr]] == 0: FIELD[0][lifi[cr]] = igrvib break else: print('Это место занято!') else: print('Выходит за рамки поля!!') if checkState() == 1: break if checkState() == 2: break if checkState() == -1: break #-1 - ничья #0 - игра продолжается #1 - крестики выйграли #2 - ноликики выйграли def checkState(): if FIELD[0][0] == 1 and FIELD[0][1] == 1 and FIELD[0][2] == 1 \ or FIELD[1][0] == 1 and FIELD[1][1] == 1 and FIELD[1][2] == 1 \ or FIELD[2][0] == 1 and FIELD[2][1] == 1 and FIELD[2][2] == 1 \ or FIELD[0][0] == 1 and FIELD[1][0] == 1 and FIELD[2][0] == 1 \ or FIELD[0][1] == 1 and FIELD[1][1] == 1 and FIELD[2][1] == 1 \ or FIELD[0][2] == 1 and FIELD[1][2] == 1 and FIELD[2][2] == 1 \ or FIELD[0][0] == 1 and FIELD[1][1] == 1 and FIELD[2][2] == 1 \ or FIELD[2][0] == 1 and FIELD[1][1] == 1 and FIELD[0][2] == 1: return 1 if FIELD[0][0] == 2 and FIELD[0][1] == 2 and FIELD[0][2] == 2 \ or FIELD[1][0] == 2 and FIELD[1][1] == 2 and FIELD[1][2] == 2 \ or FIELD[2][0] == 2 and FIELD[2][1] == 2 and FIELD[2][2] == 2 \ or FIELD[0][0] == 2 and FIELD[1][0] == 2 and FIELD[2][0] == 2 \ or FIELD[0][1] == 2 and FIELD[1][1] == 2 and FIELD[2][1] == 2 \ or FIELD[0][2] == 2 and FIELD[1][2] == 2 and FIELD[2][2] == 2 \ or FIELD[0][0] == 2 and FIELD[1][1] == 2 and FIELD[2][2] == 2 \ or FIELD[2][0] == 2 and FIELD[1][1] == 2 and FIELD[0][2] == 2: return 2 zap = 0 for i in range(0, 3): for j in range(0, 3): if FIELD[i][j] != 0: zap += 1 if zap == 9: return -1 return 0 vib = int(input('Количество человек (1 или 2)')) if vib == 2: while True: drawField() if checkState() == 1: print('крестики выйграли!') break if checkState() == 2: print('нолики выйграли!') break if checkState() == -1: print('Ничья!') break while True: cr = input("Крестики ходят: ") crhod = mesto[cr] if crhod <= 3: if FIELD[2][lifi[cr]] == 0: FIELD[2][lifi[cr]] = 1 break else: print('Это место занято!') elif crhod <= 6: if FIELD[1][lifi[cr]] == 0: FIELD[1][lifi[cr]] = 1 break else: print('Это место занято!') elif crhod <= 9: if FIELD[0][lifi[cr]] == 0: FIELD[0][lifi[cr]] = 1 break else: print('Это место занято!') else: print('Выходит за рамки поля!!') if checkState() == 1: print('крестики выйграли!') break if checkState() == 2: print('нолики выйграли!') break if checkState() == -1: print('Ничья!') break drawField() while True: nl = input("Нолики ходят: ") nlhod = mesto[nl] if nlhod <= 3: if FIELD[2][lifi[nl]] == 0: FIELD[2][lifi[nl]] = 2 break else: print('Это место занято!') elif nlhod <= 6: if FIELD[1][lifi[nl]] == 0: FIELD[1][lifi[nl]] = 2 break else: print('Это место занято!') elif nlhod <= 9: if FIELD[0][lifi[nl]] == 0: FIELD[0][lifi[nl]] = 2 break else: print('Это место занято!') else: print('Выходит за рамки поля!!') else: vibsl = int(input('Какая сложность (1 или 2)')) vib = int(input('За кого будете играть(1 или 2)')) if vib == 1 and vibsl == 1: igrvib = 1 vibcomp = 2 while True: ret = hum_hod(igrvib) if checkState() == 1: print('крестики выйграли!') drawField() break if checkState() == 2: print('нолики выйграли!') drawField() break if checkState() == -1: print('Ничья!') drawField() break hod = Comuter_dodje(vibcomp, igrvib) Computer_hod(hod) if checkState() == 1: print('крестики выйграли!') drawField() break if checkState() == 2: print('нолики выйграли!') drawField() break if checkState() == -1: print('Ничья!') drawField() break elif vib == 1 and vibsl == 2: igrvib = 1 vibcomp = 2 while True: ret = hum_hod(igrvib) if checkState() == 1: print('крестики выйграли!') drawField() break if checkState() == 2: print('нолики выйграли!') drawField() break if checkState() == -1: print('Ничья!') drawField() break hod = special_comp(vibcomp) sechod = Comuter_dodje2(vibcomp, igrvib, hod) Comuter_dodje1(sechod, vibcomp, igrvib, hod) if checkState() == 1: print('крестики выйграли!') drawField() break if checkState() == 2: print('нолики выйграли!') drawField() break if checkState() == -1: print('Ничья!') drawField() break if vib == 2 and vibsl == 1: igrvib = 2 vibcomp = 1 while True: hod = Comuter_dodje(vibcomp, igrvib) Computer_hod(hod) if checkState() == 1: print('крестики выйграли!') drawField() break if checkState() == 2: print('нолики выйграли!') drawField() break if checkState() == -1: print('Ничья!') drawField() break ret = hum_hod(igrvib) if checkState() == 1: print('крестики выйграли!') drawField() break if checkState() == 2: print('нолики выйграли!') drawField() break if checkState() == -1: print('Ничья!') drawField() break elif vib == 2 and vibsl == 2: igrvib = 2 vibcomp = 1 FIELD[randint(0, 2)][randint(0, 2)] = 1 while True: hod = special_comp(vibcomp) sechod = Comuter_dodje2(vibcomp, igrvib, hod) Comuter_dodje1(sechod, vibcomp, igrvib, hod) if checkState() == 1: print('крестики выйграли!') drawField() break if checkState() == 2: print('нолики выйграли!') drawField() break if checkState() == -1: print('Ничья!') drawField() break ret = hum_hod(igrvib) if checkState() == 1: print('крестики выйграли!') drawField() break if checkState() == 2: print('нолики выйграли!') drawField() break if checkState() == -1: print('Ничья!') drawField() break