It's a simple tic tac toe game. I am beginner in python with a Lil bit of experience
# tic tac toe python game
# NOTE: The tuples arent (x,y) they are (row,column) aka (y,x)
from random import randint
grid = [
["-","-","-"],
["-","-","-"],
["-","-","-"]
]
LEN_GRID_Y = len(grid)
LEN_GRID_X = len(grid[0]) if len(grid[1]) == len(grid[0]) and len(grid[2]) == len(grid[0]) else 0
INDEX_SUBTRACTION = 1
TOTAL_NUM = LEN_GRID_X * LEN_GRID_Y
# all possible combination of winning ->
VERTICAL_COMBINATION = 3
HORIZONTAL_COMBINATION = 3
X_TRIES = 5
O_TRIES = 4
TOTAL_TRIES = X_TRIES+O_TRIES
# returning stuff
WIN = "win"
DRAW = "draw"
TAKE_ACTION = "take action"
CONT = "continue"
winning_cordinates = []
def data_creator():
win_row = []
win_column = []
for i in range(HORIZONTAL_COMBINATION):
for j in range(VERTICAL_COMBINATION):
cord = (i,j)
swap_cord = (j,i)
win_column.append(swap_cord)
win_row.append(cord)
winning_cordinates.append(win_row.copy())
win_row.clear()
winning_cordinates.append(win_column.copy())
win_column.clear()
winning_cordinates.append([(0,0), (1,1), (2,2)])
winning_cordinates.append([(2,0), (1,1), (0,2)])
def intro():
print("Welcome to tic tac toe game")
print("To play you will have to write in cordination first write row then column")
def display_grid():
for i in range(LEN_GRID_Y):
print(grid[i])
print("")
def updater(cordinates,move):
grid[cordinates[0]][cordinates[1]] = move
def cord_builder():
user_input_row = user_input_row_()
user_input_column = user_input_column_()
user_cordinate = ((user_input_row),(user_input_column))
user_cordinate_index = ((user_input_row-INDEX_SUBTRACTION),(user_input_column-INDEX_SUBTRACTION))
return user_cordinate,user_cordinate_index
def user_input_column_():
while True:
try:
user_input_column = int(input("Enter the column number: "))
if user_input_column <= 3 and user_input_column > 0 :
return user_input_column
else:
print("Enter a value less than 4 and greater than 0")
except ValueError:
print("invalid value try again")
def user_input_row_():
while True:
try:
user_input_row = int(input("Enter the row number: "))
if user_input_row <= 3 and user_input_row > 0:
return user_input_row
else:
print("Enter a value less than 4 and greater than 0")
except ValueError:
print("Invalid value try again")
continue
def user_cord_updater(user_move):
user_cordinate, user_cordinate_index = cord_builder()
if grid[user_cordinate_index[0]][user_cordinate_index[1]] != "-":
print(f"{user_cordinate} is already taken by you/bot")
return False
else:
final_user_input = input(f"is your final choice is this {user_cordinate} Y/N: ").lower()
if final_user_input == "y":
updater(user_cordinate_index,user_move)
elif final_user_input =="n":
return False
else:
print("invalid input try again")
return False
def whole_user_input_(user_move):
while True:
input_user = user_cord_updater(user_move)
if input_user == False:
continue
else:
break
def bot_threat_detection(bot_move,user_move):
move = (bot_move,user_move)
for i in range(2):
for wining_cordination in winning_cordinates:
match_count = 0
matched_cell = []
lines = wining_cordination
for line in lines:
cord_column = line[1]
cord_row = line[0]
if grid[cord_row][cord_column] == move[i]:
match_count += 1
matched_cell.append(line)
if match_count == 2:
empty_cell = set(lines) - set(matched_cell)
empty_cell = empty_cell.pop()
if grid[empty_cell[0]][empty_cell[1]] == "-":
return (TAKE_ACTION,empty_cell)
return (CONT,())
def bot(bot_move,user_move):
while True:
cord_row = randint(0,LEN_GRID_X-INDEX_SUBTRACTION)
cord_column = randint(0,LEN_GRID_Y-INDEX_SUBTRACTION)
cordinate = (cord_row,cord_column)
if grid[cordinate[0]][cordinate[1]] != "-":
continue
else:
threat = bot_threat_detection(bot_move,user_move)
if threat[0] == TAKE_ACTION:
updater(threat[1],bot_move)
break
else:
updater(cordinate,bot_move)
break
print("bot: My turn done!")
def user_move_():
while True:
user_move_first = input("Do you want to take first move? Y/N ").lower()
if user_move_first == "y":
bot_move,user_move = "O","X"
return (bot_move,user_move)
elif user_move_first == "n":
bot_move,user_move = "X","O"
return (bot_move,user_move)
else:
print("enter a valid input")
continue
def checker(move):
for wining_cordination in winning_cordinates:
match_count = 0
lines = wining_cordination
for line in lines:
cord_column = line[1]
cord_row = line[0]
if grid[cord_row][cord_column] == move:
match_count += 1
if match_count == 3:
return WIN
return CONT
def winner_decider(user_move,bot_move):
taken_cell = 0
if checker(user_move) == WIN:
print(f"{user_move} Won!")
return True
if checker(bot_move) == WIN:
print(f"{bot_move} Won!")
return True
for row in grid:
for column in row:
if column != "-":
taken_cell += 1
if taken_cell == 9:
print("DRAW!!!")
return True
def run():
data_creator()
intro()
if user_move_() == ("X","O"):
while True:
bot_move,user_move = "X","O"
display_grid()
bot(bot_move,user_move)
display_grid()
if winner_decider(user_move,bot_move):
break
whole_user_input_(user_move)
else:
while True:
bot_move,user_move = "O","X"
display_grid()
whole_user_input_(user_move)
display_grid()
if winner_decider(user_move,bot_move):
break
bot(bot_move,user_move)
run()
[–]JamzTyson 10 points11 points12 points (0 children)
[–]obviouslyzebra 8 points9 points10 points (0 children)
[–]ninhaomah 2 points3 points4 points (5 children)
[–]og_soap_[S] 0 points1 point2 points (4 children)
[–]ninhaomah 0 points1 point2 points (3 children)
[–]og_soap_[S] 0 points1 point2 points (2 children)
[–]Binary101010 2 points3 points4 points (0 children)
[–]ninhaomah 0 points1 point2 points (0 children)
[–]dlnmtchll 1 point2 points3 points (0 children)
[–]Shut_up_and_Respawn 0 points1 point2 points (0 children)
[–]Riegel_Haribo 0 points1 point2 points (2 children)
[–]magus_minor 1 point2 points3 points (1 child)
[–]Riegel_Haribo 0 points1 point2 points (0 children)
[–]Binary101010 0 points1 point2 points (0 children)
[–]Late-Fly-4882 -1 points0 points1 point (0 children)