Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- player_won = 0
- computer_won = 0
- draws = 0
- who_begins = 2
- whose_turn = 2
- all_pos = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
- player_choices = []
- computer_choices = []
- edges = [(0, 0), (0, 2), (2, 0), (2, 2)]
- left_edges = [(0, 0), (2, 0)]
- right_edges = [(0, 2), (2, 2)]
- non_edges = [(0, 1), (1, 0), (1, 2), (2, 1)]
- print("Player begins and uses <X>, Computer uses <O>.")
- def print_board(b):
- s = ""
- for i in range(4):
- s += "\n --- --- ---\n"
- if i < 3:
- for j in range(3):
- if b[i][j] == 1:
- s += "| X "
- elif b[i][j] == 2:
- s += "| O "
- else:
- s += "| "
- s += "|"
- print(s)
- def who_won(b):
- mid = b[1][1]
- if mid != 0:
- if (b[0][0] == mid == b[2][2]) or (b[2][0] == mid == b[0][2]):
- return mid
- for i in range(3):
- elem = b[i][i]
- if elem != 0:
- if (b[i][(i-1) % 3] == elem == b[i][(i+1) % 3]) or (b[(i-1) % 3][i] == elem == b[(i+1) % 3][i]):
- return elem
- return 0
- def player_move(b):
- global all_pos
- global player_choices
- x, y = [(int(z.strip())-1) for z in input("Player, your turn: ").split(",")]
- while (x, y) not in all_pos:
- x, y = [(int(z.strip())-1) for z in input("Wrong input, try again: ").split(",")]
- b[x][y] = 1
- all_pos.remove((x, y))
- player_choices.append((x, y))
- def is_neighbor(x, y):
- i, j = x
- k, l = y
- if i == k:
- return abs(j-l) == 1
- elif j == l:
- return abs(i-k) == 1
- def same_row(x, y):
- i, j = x
- k, l = y
- return i == k
- def same_column(x, y):
- i, j = x
- k, l = y
- return j == l
- def computer_starter(b, free):
- global edges
- global right_edges
- global left_edges
- global non_edges
- global all_pos
- global player_choices
- global computer_choices
- x = y = -1
- print("Computer, your turn: ")
- if free == 9:
- x, y = random.choice(edges + [(1, 1)])
- elif free == 7:
- if computer_choices[0] == (1, 1):
- if player_choices[0] in non_edges:
- i, j = player_choices[0]
- if j == 0:
- x, y = (0, 2)
- elif j == 2:
- x, y = (0, 0)
- else:
- x, y = random.choice(edges)
- else:
- x, y = random.choice(all_pos)
- else:
- if player_choices[0] == (1, 1):
- i, j = computer_choices[0]
- x, y = (2-i, 2-j)
- else:
- comp_tup = computer_choices[0]
- player_tup = player_choices[0]
- i, j = computer_choices[0]
- if is_neighbor(comp_tup, player_tup):
- x, y = (1, 1)
- elif same_row(comp_tup, player_tup) or same_column(comp_tup, player_tup):
- x, y = (2-i, 2-j)
- else:
- x, y = (1, 1)
- elif free == 5:
- choice = search_choice(b)
- if choice:
- x, y = choice
- else:
- if computer_choices[0] == (1, 1):
- i, j = computer_choices[1]
- x, y = (2-i, j)
- else:
- if is_neighbor(computer_choices[0], player_choices[0]):
- i, j = player_choices[1]
- if same_row(computer_choices[0], player_choices[0]):
- x, y = (i, 2-j)
- elif same_column(computer_choices[0], player_choices[0]):
- x, y = (2-i, j)
- else:
- x, y = random.choice(all_pos)
- elif free == 3:
- choice = search_choice(b)
- if choice:
- x, y = choice
- else:
- x, y = random.choice(all_pos)
- elif free == 1:
- x, y = all_pos[0]
- b[x][y] = 2
- all_pos.remove((x, y))
- computer_choices.append((x, y))
- def player_starter(b, free):
- return 0
- def search_choice(b):
- choice = []
- mid = b[1][1]
- if mid != 0:
- for i in range(3):
- for j in range(3):
- if b[i][j] == mid and b[2-i][2-j] == 0:
- pos = [2-i, 2-j]
- if mid == 2:
- print("a")
- return pos
- else:
- choice = pos
- if 0 != b[0][0] == b[2][2]:
- if b[0][0] == 2:
- return [1, 1]
- else:
- choice = [1, 1]
- elif 0 != b[0][2] == b[2][0]:
- if b[0][2] == 2:
- return [1, 1]
- else:
- choice = [1, 1]
- for i in range(3):
- row = b[i]
- if row.count(2) == 2 and row.count(0) == 1:
- return [i, row.index(0)]
- elif row.count(1) == 2 and row.count(0) == 1:
- choice = [i, row.index(0)]
- for j in range(3):
- column = []
- for i in range(3):
- column.append(b[i][j])
- if column.count(2) == 2 and column.count(0) == 1:
- return [column.index(0), j]
- elif column.count(1) == 2 and column.count(0) == 1:
- choice = [column.index(0), j]
- return choice
- def play_game():
- b = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
- global all_pos
- all_pos = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
- global player_choices
- player_choices = []
- global computer_choices
- computer_choices = []
- free_fields = 9
- winner = 0
- global who_begins
- while winner == 0 and free_fields > 0:
- print_board(b)
- global whose_turn
- if whose_turn == 1:
- whose_turn = 2
- player_move(b)
- else:
- whose_turn = 1
- if who_begins == 2:
- computer_starter(b, free_fields)
- else:
- player_starter(b, free_fields)
- free_fields -= 1
- winner = who_won(b)
- print_board(b)
- if winner == 1:
- print("The Game is over. The Player won.")
- global player_won
- player_won += 1
- elif winner == 2:
- print("The Game is over. The Computer won.")
- global computer_won
- computer_won += 1
- else:
- print("The Game is over. Nobody won.")
- global draws
- draws += 1
- s = input("Do you want to play again? Press yes!").upper()
- if s == "Y" or s == "YE" or s == "YES" or s == "J" or s == "JA" or s == "1":
- if who_begins == 1:
- who_begins = 2
- else:
- who_begins = 1
- play_game()
- else:
- print("The Result: %d %s -- %d %s -- %d %s"
- % (player_won, "Win" if player_won == 1 else "Wins", computer_won,
- "Loss" if computer_won == 1 else "Losses", draws, "Draw" if draws == 1 else "Draws"))
- play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement