Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def GameBeg():
- print("Witaj w grze kółko i krzyżyk. Tak wygląda mapa współrzędnych w które będziesz wpisywał swój wybór: ")
- i = 1
- print(" ⎯⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯⎯")
- while i <= 9:
- print("| ",i ," ", sep = "", end = "")
- if i % 3 == 0:
- print("|\n ⎯⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯⎯")
- i += 1
- print("Powodzenia!\n")
- def MainGame(game):
- turn = 0
- while turn < 9:
- print("ruch:", turn)
- cords_good = 0
- if turn % 2 == 0:
- player_no = 1
- else:
- player_no = 2
- def GetAIMove():
- while True:
- if turn > 1:
- i = 0
- while i < 9:
- temp_str = ""
- temp_str += str(game[i])
- temp_str += str(game[i + 1])
- temp_str += str(game[i + 2])
- if temp_str == "011":
- return 0 + i
- if temp_str == "101":
- return 1 + i
- if temp_str == "110":
- return 2 + i
- i = int(i) + 3
- i = 0
- while i < 3:
- temp_str = ""
- temp_str += str(game[i])
- temp_str += str(game[i + 3])
- temp_str += str(game[i + 6])
- if temp_str == "011":
- return 0
- if temp_str == "101":
- return 3 + i
- if temp_str == "110":
- return 6 + i
- i = int(i) + 1
- temp_str = str(game[0]) + str(game[4]) + str(game[8])
- if temp_str == "011":
- return 0
- if temp_str == "101":
- return 4
- if temp_str == "110":
- return 8
- temp_str = str(game[2]) + str(game[4]) + str(game[6])
- if temp_str == "011":
- return 2
- if temp_str == "101":
- return 4
- if temp_str == "110":
- return 6
- i = random.randint(0, 8)
- if game[i] == 0:
- return int(i)
- else:
- pass
- def GetPlayerMove():
- while cords_good == 0:
- try:
- print("TWÓJ RUCH. Wprowadź miejsce: ", sep="", end="")
- i = int(input()) - 1
- if 0 <= int(i) < 9:
- if game[int(i)] == 0:
- return int(i)
- else:
- print('To miejsce jest zajęte. Spróbuj ponownie...')
- pass
- else:
- print('Spróbuj ponownie...')
- pass
- except ValueError as e:
- print('Spróbuj ponownie...')
- pass
- if player_no == 1:
- cord = GetPlayerMove()
- else:
- cord = GetAIMove()
- print('AI wybiera {}.'.format(cord + 1))
- game[cord] = player_no
- DrawGameBoard(game)
- turn += 1
- winner = CheckWinner(game)
- if turn == 9:
- print("Remis.")
- return 0
- if winner == 1:
- print("Brawo, wygrałeś.")
- return 0
- if winner == 2:
- print("Niestety tym razem przegrałeś.")
- return 0
- def DrawGameBoard(game):
- i = 1
- print(" ⎯⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯⎯")
- while i <= 9:
- print("| ",game[i - 1] ," ", sep = "", end = "")
- if i % 3 == 0:
- print("|\n ⎯⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯ ⎯⎯⎯⎯⎯⎯⎯⎯")
- i += 1
- def CheckWinner(game):
- for i in range(3):
- if game[i] == game[i + 1] == game[i + 2] != 0:
- return game[i]
- i += 3
- for i in range(3):
- if game[i] == game[i + 3] == game[i + 6] != 0:
- return game[i]
- i += 1
- if game[0] == game[4] == game[8] != 0:
- return game[0]
- elif game[2] == game[4] == game[6] != 0:
- return game[2]
- return 0
- my_game = [0]*9
- GameBeg()
- MainGame(my_game)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement