Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # USER: TCG
- # Tic Tac Toe (User vs Computer)
- import random
- def place(num,x):
- # Returns all the moves made
- for i in range(len(x)):
- if x[i] == num:
- pos = x.index(num)
- return x[(pos + 1)]
- return str(num)
- def print_grid(move,player,x):
- pos_list.extend([move,player])
- # Grid on which the player and computer play on
- grid = ["","-------------n",
- "|",place(1,pos_list),"|",place(2,pos_list),"|",place(3,pos_list),"|n",
- "|---+---+---|n",
- "|",place(4,pos_list),"|",place(5,pos_list),"|",place(6,pos_list),"|n",
- "|---+---+---|n",
- "|",place(7,pos_list),"|",place(8,pos_list),"|",place(9,pos_list),"|n",
- "-------------n"]
- if x == 2:
- # Only prints one the player has made a move
- print 'n', ' '.join(grid)
- def winner(x,player,xx):
- # Checks if there is a winner (Really messy, could do with code simplifying)
- if ((1 in x and 4 in x and 7 in x)or(1 in x and 2 in x and 3 in x)or(2 in x and 5 in x and 8 in x)or(3 in x and 6 in x and 9 in x)or
- (4 in x and 5 in x and 6 in x)or(7 in x and 8 in x and 9 in x)or(1 in x and 5 in x and 9 in x)or(3 in x and 5 in x and 7 in x)):
- # If prevents the A.I part from printing the statemnt
- if xx <> 1:
- print 'n'*5,"'%s'" %player, "HAS WON!"
- return 1 == 1
- def computer_AI_part(listx):
- global computer_move
- # Chceks all possible values which the player can and enter to win and blocks it
- for x in range(1,10):
- if x not in pos_list:
- listx.append(x)
- if (winner(listx,'Computer',1)) == True:
- del listx[-1]
- computer_move = x
- return 1
- del listx[-1]
- def computer_and_player():
- global computer_move,pos_list,player_list,computer_list
- replay,draw = 0,0
- while True:
- # Replay's the game
- if replay == 1:
- restart = raw_input("Would you like to replay?: ")
- if restart == "yes":
- pass
- else:
- return
- else:
- print "nTic Tac Toe - Computer vs You", 'n'*2,"Computer goes firstn"
- replay,computer_move,players_move,loop_count,pos_list,player_list,computer_list = 0,0,0,0,[],[],[]
- for each in "XXXXX":
- loop_count += 1
- # Computer's Move
- if computer_AI_part(computer_list) or computer_AI_part(player_list) == 1:
- pass
- else:
- while True:
- computer_move = random.randint(1,9)
- if computer_move in pos_list:
- continue
- break
- computer_list.append(computer_move)
- # Prints Grid
- print_grid(computer_move,'O',2)
- if loop_count == 5:
- if winner(player_list,'player',2) == True or winner(computer_list,'Computer',2) == True:
- pass
- else:
- print "Match Was a draw!"
- replay = 1
- break
- # Checks winner
- if winner(computer_list,'Computer',2) == True:
- replay = 1
- break
- # Player's Move
- while True:
- try:
- players_move = int(raw_input("n'%s' Enter a value from the grid to plot your move: " %each))
- if players_move in pos_list or players_move < 1 or players_move > 9:
- print "Enter an available number that's between 1-9"
- continue
- break
- except:
- print "Enter a number"
- player_list.append(players_move)
- # Sets player's move for printing
- print_grid(players_move,each,1)
- # Checks winner again
- if winner(player_list,'player',1) == True:
- print_grid(players_move,each,2)
- winner(player_list,'player',2)
- replay = 1
- break
- if __name__ == "__main__":
- computer_and_player()
- 'X' Enter a value from the grid to plot your move: exit
- Enter a number
- 'X' Enter a value from the grid to plot your move: leave
- Enter a number
- 'X' Enter a value from the grid to plot your move: quit
- Enter a number
- 'X' Enter a value from the grid to plot your move: ^CEnter a number
- def place(num,x):
- # Returns the value in x at position num
- for i, v in enumerate(x):
- if v == num:
- return x[(i + 1)]
- return str(num)
- template = """
- -------------
- | {} | {} | {} |
- |-----------|
- | {} | {} | {} |
- |-----------|
- | {} | {} | {} |
- --------------"""
- if x == 2:
- # Only prints if the player has made a move
- print template.format(*(place(num + 1, pos_list) for num in range(9)))
- def winner(x,player,xx):
- wins = ((1, 2, 3), (4, 5, 6), (7, 8, 9), # Horizontal
- (1, 4, 7), (2, 5, 8), (3, 6, 9), # Vertical
- (1, 5, 9), (3, 5, 7)) # Diagonal
- if any(all(pos in x for pos in win) for win in wins):
- if xx != 1:
- print 'n'*5, "'{}'".format(player), "HAS WON!"
- return True
- return False
- def computer_AI_part(listx):
- while True:
- # Replay's the game
- if replay == 1:
- restart = raw_input("Would you like to replay?: ")
- if restart == "yes":
- pass
- else:
- return
- else:
- print "nTic Tac Toe - Computer vs You", 'n'*2,"Computer goes firstn"
- while True:
- # Replay's the game
- if replay:
- restart = raw_input("Would you like to replay?: ").lower()
- if restart in ("y", "yes"):
- pass
- elif restart in ("n", "no"):
- return
- else:
- print "Say 'yes' or 'no'"
- continue
- else:
- print "nTic Tac Toe - Computer vs You", 'n'*2,"Computer goes firstn"
- replay,computer_move,players_move,loop_count,pos_list,player_list,computer_list = 0,0,0,0,[],[],[]
- replay, computer_move, players_move, loop_count = 0, 0, 0, 0
- pos_list, player_list, computer_list = [], [], []
- if computer_move in pos_list:
- continue
- break
- if computer_move not in pos_list:
- break
- if winner(computer_list,'Computer',2) == True:
- except:
- print "Enter a number"
- except ValueError:
- print "Enter a number"
- except (EOFError, KeyboardInterrupt):
- exit()
- if __name__ == "__main__":
- computer_and_player()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement