Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- botin1 = random.randint(1, 9) - 1
- botin2 = random.randint(1, 9) - 1
- botin3 = random.randint(1, 9) - 1
- botin4 = random.randint(1, 9) - 1
- botin5 = random.randint(1, 9) - 1
- #--------GLobal Variables--------
- Board = ["-","-","-",
- "-","-","-",
- "-","-","-",]
- Winner = None
- game_running = True
- current_player = "X"
- #--------Display board--------
- def show_board():
- print(Board[0] + "|" + Board[1] + "|" + Board[2])
- print(Board[3] + "|" + Board[4] + "|" + Board[5])
- print(Board[6] + "|" + Board[7] + "|" + Board[8])
- #--------running the game-------
- def play_game():
- show_board()
- while game_running:
- user_input_turn(current_player)
- checking_if_game_over()
- switch_player()
- if Winner == "X" or Winner == "O":
- print(Winner + " won.")
- elif Winner == None:
- print("tie")
- #--------Win or Tie---------
- def checking_if_game_over():
- check_for_winner()
- check_if_tie()
- def check_for_winner():
- global Winner
- #check rows
- row_winner = check_rows()
- #check columns
- column_winner = check_columns()
- #check diags
- diag_winner = check_diags()
- #check the winner
- if row_winner:
- Winner = row_winner
- elif column_winner:
- Winner = column_winner
- elif diag_winner:
- Winner = diag_winner
- return
- def check_rows():
- global game_running
- row_1 = Board[0] == Board[1] == Board[2] != "-"
- row_2 = Board[3] == Board[4] == Board[5] != "-"
- row_3 = Board[6] == Board[7] == Board[8] != "-"
- if row_1 or row_2 or row_3:
- game_running = False
- if row_1:
- return Board[0]
- if row_2:
- return Board[3]
- if row_3:
- return Board[7]
- return
- def check_columns():
- global game_running
- column_1 = Board[0] == Board[3] == Board[6] != "-"
- column_2 = Board[1] == Board[4] == Board[7] != "-"
- column_3 = Board[2] == Board[5] == Board[8] != "-"
- if column_1 or column_2 or column_3:
- game_running = False
- if column_1:
- return Board[0]
- if column_2:
- return Board[1]
- if column_3:
- return Board[2]
- return
- def check_diags():
- global game_running
- diag_1 = Board[0] == Board[4] == Board[8] != "-"
- diag_2 = Board[2] == Board[4] == Board[6] != "-"
- if diag_1 or diag_2:
- game_running = False
- if diag_1:
- return Board[0]
- if diag_2:
- return Board[2]
- return
- #------checking tie------
- def check_if_tie():
- global game_running
- if "-" not in Board:
- game_running = False
- return
- #------User move----------
- def user_input_turn(player):
- print(player + "'s turn.")
- if current_player == "O":
- if Board.count("X") == 1:
- position = botin1
- elif Board.count("X") == 2:
- position = botin2
- elif Board.count("X") == 3:
- position = botin3
- elif Board.count("X") == 4:
- position = botin4
- elif Board.count("X") == 5:
- position = botin5
- valid = False
- while not valid:
- if Board.count("X") == 5:
- position = botin1
- elif Board.count("X") == 4:
- position = botin2
- elif Board.count("X") == 1:
- position = botin3
- elif Board.count("X") == 2:
- position = botin4
- elif Board.count("X") == 3:
- position = botin5
- if Board[position] == "-":
- valid = True
- else:
- valid = False
- Board[position] = player
- else:
- position = input("Choose a position from 1 to 9: ")
- valid = False
- while not valid:
- while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
- print("Please put an integer between 1 and 9 :(")
- position = input("Choose a position from 1 to 9: ")
- position = int(position) - 1
- if Board[position] == "-":
- valid = True
- else:
- print("You can't go there,choose an empty position")
- Board[position] = player
- show_board()
- #------Switching current player-------
- def switch_player():
- global current_player
- if current_player == "X":
- current_player = "O"
- else:
- current_player = "X"
- return
- play_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement