lundi 18 mai 2020

Tic Tac Toe Game using python . not getting output for winner

so i was making a beginner level tic tac toe game using python but i was not able to make the player win, I mean the main loop wasn't ending and the program was just keeping on taking inputs.i have tried everything like using the win statement in the main loop or making the variables in the main loop as global variables but it's not working.

#make symbols
#take user input
#process it
#player turns
player1 = input("Player 1 Please enter your name: ")
player2 = input("Player 2 Please enter your name: ")

game_over = False
game_over1 = False

board = [
  "   ", "   ", "   ", "   ", "   ", "   ", "   ", "   ", "   ", 
]
def game_board():
  print("   |   |   ")
  print(board[0] + "|" + board[1] + "|" + board[2])
  print("   |   |   ")
  print("---|---|---")
  print("   |   |   ")
  print(board[3] + "|" + board[4] + "|" + board[5])
  print("   |   |   ")
  print("---|---|---")
  print("   |   |   ")
  print(board[6] + "|" + board[7] + "|" + board[8])
  print("   |   |   ")

game_board()


#for getting input and inserting x
while game_over == False:

  def user_input():
    global position
    position = input("Please enter the place for X from 1-9: ")
    position = int(position) - 1
    board[position] = " X "
    game_board()
    #for getting input and inserting O
    position1 = input("Please enter the place for O from 1-9 except where X was put: ")
    position1 = int(position1) - 1
    board[position1] = " O "
      #checking and SHOUTING if error
    if position1 == position:
      print("Choose another number, the value you entered is already reserved for  X (:-P)\nNOW START AGAIN")
      game_over = True

    else:
      game_board()




  user_input()
  if game_over == True:
    break

#checking win of X for rows
if board[0] == "X" and board[1] == "X" and board[2] == "X":
  game_over == True
  print(player1 +" won.")


#checking win of X for columns
def columns():
  global game_over
  columns_1 = board[0] == board[3] == board[6] == position
  columns_2 = board[1] == board[4] == board[7] == position
  columns_3 = board[2] == board[5] == board[8] == position
  if columns_1 or columns_2 or columns_3:
    game_over == True
  if columns_1:
    return board[0]
  if columns_2:
    return board[1]
  if columns_3:
    return board[2]
  return
#checking win of X for diagonols
def dia():
  global game_over
  dia_1 = board[0] == board[4] == board[8]
  dia_2 = board[2] == board[4] == board[6]
  if dia_1 or dia_2:
    game_over == True
  if dia_1:
    return board[0]
  if dia_2:
    return board[2]
  return
#checking win of O for rows
def rows_O():
  row_O_1 = board[0] == board[1] == board[2]
  row_O_2 = board[3] == board[4] == board[5]
  row_O_3 = board[6] == board[7] == board[8]
  if row_O_1 or row_O_2 or row_O_3:
    game_over1 == True
  if row_O_1:
    return board[0]
  if row_O_2:
    return board[3]
  if row_O_3:
    return board[6]
  return
#checking win of O for columnns
def columns_O():
  columns_O_1 = board[0] == board[3] == board[6]
  columns_O_2 = board[1] == board[4] == board[7]
  columns_O_3 = board[2] == board[5] == board[8]
  if columns_O_1 or row_2 or row_3:
    game_over1 == True
  if columns_O_1:
    return board[0]
  if columns_O_2:
    return board[3]
  if columns_O_3:
    return board[6]
  return
#checking win of O for diagonals
def dia_O():
  dia_O_1 = board[0] == board[4] == board[8]
  dia_O_2 = board[2] == board[4] == board[6]
  if dia_O_1 or dia_O_2:
    game_over1 == True
  if dia_O_1:
    return board[0]
  if dia_O_2:
    return board[2]
  return
#TIES LEFT. THINKINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG 
#def tie():
#  elif : 
#    print("It's a tie.")
#TIES LEFT. THINKINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG 

Aucun commentaire:

Enregistrer un commentaire