Created
March 16, 2025 18:42
-
-
Save AlexDrts/f725f0e3d67d95424b64a5898c8d4f4e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| def drawboard(board): | |
| print(" | |") | |
| print(" "+board[7]+" | "+board[8]+" | "+board[9]) | |
| print(" | |") | |
| print("---+---+---") | |
| print(" | |") | |
| print(" "+board[4]+" | "+board[5]+" | "+board[6]) | |
| print(" | |") | |
| print("---+---+---") | |
| print(" | |") | |
| print(" "+board[1]+" | "+board[2]+" | "+board[3]) | |
| print(" | |") | |
| def inputplayerletter(): | |
| letter = "" | |
| while not(letter == 'X' or letter == 'O'): | |
| print("Обери Х або О") | |
| letter = input().upper() | |
| if letter == 'X': | |
| return ['X', 'O'] | |
| else: | |
| return ['O', 'X'] | |
| def whogofirst(): | |
| if random.randint(0,1) == 0: | |
| return 'computer' | |
| else: | |
| return 'player' | |
| def playagain(): | |
| print("продовжити гру, yes або no") | |
| return input().lower().startswith('y') | |
| def makemove(board,letter,move): | |
| board[move]=letter | |
| def iswinner(bo,le): | |
| return((bo[7]==le and bo[8]==le and bo[9]==le)or | |
| (bo[4]==le and bo[5]==le and bo[6]==le)or | |
| (bo[1]==le and bo[2]==le and bo[3]==le)or | |
| (bo[7]==le and bo[4]==le and bo[1]==le)or | |
| (bo[8]==le and bo[5]==le and bo[2]==le)or | |
| (bo[9]==le and bo[6]==le and bo[3]==le)or | |
| (bo[7]==le and bo[5]==le and bo[3]==le)or | |
| (bo[9]==le and bo[5]==le and bo[1]==le)) | |
| def getboardcopy(board): | |
| dupeboard = [] | |
| for i in board: | |
| dupeboard.append(i) | |
| return dupeboard | |
| def isspacefree(board,move): | |
| return board[move] == ' ' | |
| def getplayermove(board): | |
| move = ' ' | |
| while move not in '1 2 3 4 5 6 7 8 9'.split() or not isspacefree(board,int(move)): | |
| print("твій наступний хід, обери від 1 до 9") | |
| move = input() | |
| return int(move) | |
| def choserandommove(board,moveslist): | |
| possiblemoves = [] | |
| for i in moveslist: | |
| if isspacefree(board,i): | |
| possiblemoves.append(i) | |
| if len(possiblemoves) != 0: | |
| return random.choice(possiblemoves) | |
| else: | |
| return None | |
| def getcomputermove(board,computerletter, difficulty): | |
| if computerletter == 'X': | |
| playerletter = 'O' | |
| else: | |
| playerletter = 'X' | |
| if difficulty == 'easy': | |
| return choserandommove(board, [1, 2, 3, 4, 5, 6, 7, 8, 9]) | |
| if difficulty == 'medium': | |
| for i in range(1,10): | |
| copy = getboardcopy(board) | |
| if isspacefree(copy, i): | |
| makemove(copy, computerletter, i) | |
| if iswinner(copy, computerletter): | |
| return i | |
| for i in range(1,10): | |
| copy = getboardcopy(board) | |
| if isspacefree(copy, i): | |
| makemove(copy, playerletter, i) | |
| if iswinner(copy, playerletter): | |
| return i | |
| return choserandommove(board, [1, 2, 3, 4, 5, 6, 7, 8, 9]) | |
| if difficulty == 'hard': | |
| for i in range(1,10): | |
| copy = getboardcopy(board) | |
| if isspacefree(copy, i): | |
| makemove(copy, computerletter, i) | |
| if iswinner(copy, computerletter): | |
| return i | |
| for i in range(1,10): | |
| copy = getboardcopy(board) | |
| if isspacefree(copy, i): | |
| makemove(copy, playerletter, i) | |
| if iswinner(copy, playerletter): | |
| return i | |
| move = choserandommove(board, [1, 3, 7, 9]) | |
| if move != None: | |
| return move | |
| if isspacefree(board, 5): | |
| return 5 | |
| return choserandommove(board, [2, 4, 6, 8]) | |
| def isboardfull(board): | |
| for i in range(1,10): | |
| if isspacefree(board,i): | |
| return False | |
| return True | |
| def getcomputerlevel(): | |
| level = "" | |
| while level not in ['easy', 'medium', 'hard']: | |
| print("Оберіть рівень складності: easy, medium або hard") | |
| level = input().lower() | |
| return level | |
| print("гра хрестик - нуль") | |
| while True: | |
| theboard = [' ']*10 | |
| playerletter, computerletter = inputplayerletter() | |
| difficulty = getcomputerlevel() | |
| turn = whogofirst() | |
| print("рандом обрав, що першим ходе - ", turn) | |
| gameisplaying = True | |
| while gameisplaying: | |
| if turn == 'player': | |
| drawboard(theboard) | |
| move = getplayermove(theboard) | |
| makemove(theboard, playerletter, move) | |
| if iswinner(theboard, playerletter): | |
| drawboard(theboard) | |
| print("тобі вдалось обіграти штучний інтелект") | |
| gameisplaying = False | |
| else: | |
| if isboardfull(theboard): | |
| drawboard(theboard) | |
| print("сьогодні нічія") | |
| break | |
| else: | |
| turn = 'computer' | |
| else: | |
| move = getcomputermove(theboard, computerletter, difficulty) | |
| makemove(theboard, computerletter, move) | |
| if iswinner(theboard, computerletter): | |
| drawboard(theboard) | |
| print("комп виграв, користувач плакі плакі") | |
| gameisplaying = False | |
| else: | |
| if isboardfull(theboard): | |
| drawboard(theboard) | |
| print("нічія") | |
| break | |
| else: | |
| turn = 'player' | |
| if not playagain(): | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment