Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- from random import randrange
- X = "X"
- O = "O"
- E = " "
- game = [[0,0,0],[0,0,0],[0,0,0]]
- avaible_moves = [["a0", "b0", "c0"], ["a1", "b1", "c1"], ["a2", "b2", "c2"]]
- move_was_valid = True
- winning_player = 1
- ai_moves = ["a0", "b0", "c0", "a1", "b1", "c1", "a2", "b2", "c2"]
- welcome_message = """
- ####################################
- # #
- # TIC - TAC - TOE #
- # by Pawelek #
- # v 0.02 #
- ####################################
- proper input example: b2, or a1
- """
- def DisplayGameMap():
- print(" A B C")
- for count, row in enumerate(game):
- print(count, row)
- def PurgeGameMap():
- for x in range(len(game)):
- for y in range(len(game[x])):
- game[x][y] = E
- global ai_moves
- ai_moves = ["a0", "b0", "c0", "a1", "b1", "c1", "a2", "b2", "c2"]
- def Player_1_move():
- DisplayGameMap()
- chosen_row = 0
- chosen_column = 0
- move_available = False
- global winning_player
- winning_player = 1
- players_move = input("Player 1 Choose your move: ")
- for x in range(len(avaible_moves)):
- for y in range(len(avaible_moves[x])):
- if avaible_moves[x][y] == players_move:
- chosen_row = x
- chosen_column = y
- move_available = True
- if move_available == True and game[chosen_row][chosen_column] == E:
- game[chosen_row][chosen_column] = X
- move_was_valid = True
- ai_moves.remove(players_move)
- game_loop(2)
- elif move_available == True and game[chosen_row][chosen_column] != E:
- print("WARNING Field already taken")
- move_was_valid = False
- game_loop(1)
- elif move_available != True:
- print("WARNING Invalid move")
- move_was_valid = False
- game_loop(1)
- def Player_2_move():
- DisplayGameMap()
- chosen_row = 0
- chosen_column = 0
- move_available = False
- global winning_player
- winning_player = 2
- players_move = ai_moves[randrange(0, len(ai_moves)-1)]
- print("Player 2 move: ", players_move)
- for x in range(len(avaible_moves)):
- for y in range(len(avaible_moves[x])):
- if avaible_moves[x][y] == players_move:
- chosen_row = x
- chosen_column = y
- move_available = True
- if move_available == True and game[chosen_row][chosen_column] == E:
- game[chosen_row][chosen_column] = O
- move_was_valid = True
- game_loop(1)
- if move_available == True and game[chosen_row][chosen_column] != E:
- print("WARNING Field already taken")
- move_was_valid = False
- game_loop(2)
- elif move_available != True:
- print("WARNING Invalid move")
- move_was_valid = False
- game_loop(2)
- def game_loop(player_no):
- check_for_winners()
- if player_no == 1:
- Player_1_move()
- elif player_no == 2:
- Player_2_move()
- def ask_for_replay():
- player_answer = input("Do you want to play again? [Y/N]: ")
- if player_answer == "Y" or player_answer == "y":
- PurgeGameMap()
- game_loop(1)
- elif player_answer == "N" or player_answer == "n":
- sys.exit()
- else:
- print("Invalid Input")
- ask_for_replay()
- def check_for_winners():
- if check_for_horizontal_winner() == True or chceck_for_vertical_winner() == True or check_for_diagonal_winner() == True:
- DisplayGameMap()
- print("Congratulations, Player ", winning_player, " has won!")
- ask_for_replay()
- elif check_for_tie() == True:
- DisplayGameMap()
- print("There is a tie")
- ask_for_replay()
- def check_for_horizontal_winner():
- there_is_winner = False
- for row in game:
- if row.count(row[0]) == len(row) and row[0] != E:
- #print("WINNER!"")
- there_is_winner = True
- return there_is_winner
- def chceck_for_vertical_winner():
- there_is_winner = False
- for i in range(len(game[0])):
- check = []
- for row in game:
- check.append(row[i])
- if check.count(check[0]) == len(check) and check[0] != E:
- #print("WInner!")
- there_is_winner = True
- return there_is_winner
- def check_for_diagonal_winner():
- there_is_winner = False
- diags = []
- for ix in range(len(game)):
- diags.append(game[ix][ix])
- if diags.count(diags[0]) == len(diags) and diags[0] != E:
- there_is_winner = True
- diags = []
- for col, row, in enumerate(reversed(range(len(game)))):
- diags.append(game[row][col])
- if diags.count(diags[0]) == len(diags) and diags[0] != E:
- there_is_winner = True
- return there_is_winner
- def check_for_tie():
- there_is_tie = True
- for row in range(len(game)):
- for column in range(len(game[row])):
- if game[row][column] == E:
- there_is_tie = False
- return there_is_tie
- print(welcome_message)
- PurgeGameMap()
- game_loop(1)
Advertisement
Add Comment
Please, Sign In to add comment