Example 1: tic tac toe python
def slant_check(matrix,P1,P2):
empty_lst1 = []
empty_lst2= []
lst1 = []
lst2 = []
x = len(matrix)
v = len(matrix) - 1
t = 0
g = 0
n = True
while n:
for i in range(x):
if matrix[i][i] == P1 or matrix[t][i] == P1:
empty_lst1.append(matrix[i][i])
if matrix[i][i] == P2 or matrix[t][i] == P2:
empty_lst2.append(matrix[i][i])
while v >= g:
if matrix[g][v] == P1:
lst1.append(matrix[g][v])
if matrix[g][v] == P2:
lst2.append(matrix[g][v])
t -= 1
v -= 1
g += 1
if len(empty_lst1) == x:
return True
if len(empty_lst2) == x:
return True
if len(lst1) == x:
return True
if len(lst2) == x:
return True
return False
def vertical_check(lst,P1,P2):
for i in range(len(lst) - 2):
for j in range(len(lst)):
if lst[i][j] == P1 and lst[i + 1][j] == P1 and lst[i + 2][j] == P1:
return True
if lst[i][j] == P2 and lst[i + 1][j] == P2 and lst[i + 2][j] == P2:
return True
return False
def horizontal_check(lst,P1,P2):
for i in range(len(lst)):
for j in range(len(lst) - 2):
if lst[i][j]== P1 and lst[i][j + 1]== P1 and lst[i][j + 2]== P1 :
return True
if lst[i][j]== P2 and lst[i][j + 1]== P2 and lst[i][j + 2]== P2 :
return True
return False
def find_grid2(place,lst):
for i in range(len(lst)):
for j in range(len(lst[i])):
if place == lst[i][j]:
return lst.index(lst[i])
def find_grid1(place,lst):
for i in range(len(lst)):
for j in range(len(lst[i])):
if place == lst[i][j]:
return lst[i].index(place)
def print_lst(lst):
for i in range(len(lst)):
for j in range(len(lst[i]) - 2):
print(lst[i][j],'|', lst[i][j + 1],'|', lst[i][j + 2])
print('----------')
def tic_tac_toe():
lst = [[1,2,3],
[4,5,6],
[7,8,9]]
P1 = 0
P2 = 0
counter_loop = 0
_ = 0
new_lst = [1,2]
while True:
P1 = input('Player1 select "x" or "o" ? (Type in x or o):\n').lower()
if P1 == 'x':
print('Player2 is now "o"!\n')
P2 = 'o'
break
if P1 == 'o':
print('Player2 is now "x"!\n')
P2 = 'x'
break
else:
print('Try Again\n')
print_lst(lst)
while _ < len(lst):
for i in range(len(lst[_])):
if counter_loop == 9:
print("Tie!")
break
place_grid1 = input('Where would Player 1 like to place? : ')
if int(place_grid1) >= 10 or int(place_grid1) <= 0:
print('Try Again')
place_grid1 = input('Where would Player 1 like to place? : ')
break
place_grid = int(place_grid1)
counter_loop += 1
inner_index1 = find_grid1(place_grid,lst)
outer_index1 = find_grid2(place_grid,lst)
lst[outer_index1][inner_index1] = P1
print_lst(lst)
if horizontal_check(lst,P1,P2) == True:
print("Player 1 wins!!")
counter_loop = 9
break
if vertical_check(lst,P1,P2) == True:
print("Player 1 wins!!")
counter_loop = 9
break
if slant_check(lst,P1,P2) == True:
print("Player 1 wins!!")
counter_loop = 9
break
if counter_loop == 9:
print("Tie!")
break
place_grid2 = input('Where would Player 2 like to place? : ')
if int(place_grid2) >= 10 or int(place_grid2) <=0:
print('Try Again')
place_grid2 = input('Where would Player 2 like to place? : ')
break
place_gridy = int(place_grid2)
counter_loop += 1
inner_index2 = find_grid1(place_gridy,lst)
outer_index2 = find_grid2(place_gridy,lst)
lst[outer_index2][inner_index2] = P2
print_lst(lst)
if horizontal_check(lst,P1,P2) == True:
print("Player 2 wins!!")
counter_loop = 9
break
if vertical_check(lst,P1,P2) == True:
print("Player 2 wins!!")
counter_loop = 9
break
if slant_check(lst,P1,P2) == True:
print("Player 2 wins!!")
counter_loop = 9
break
if counter_loop == 9:
print("Tie!")
break
if counter_loop == 9:
break
_ += 1
tic_tac_toe()
Example 2: tic tac toe python easy
def tic_tac_toe():
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
end = False
win_commbinations = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
def draw():
print(board[0], board[1], board[2])
print(board[3], board[4], board[5])
print(board[6], board[7], board[8])
print()
def p1():
n = choose_number()
if board[n] == "X" or board[n] == "O":
print("\nYou can't go there. Try again")
p1()
else:
board[n] = "X"
def p2():
n = choose_number()
if board[n] == "X" or board[n] == "O":
print("\nYou can't go there. Try again")
p2()
else:
board[n] = "O"
def choose_number():
while True:
while True:
a = input()
try:
a = int(a)
a -= 1
if a in range(0, 9):
return a
else:
print("\nThat's not on the board. Try again")
continue
except ValueError:
print("\nThat's not a number. Try again")
continue
def check_board():
count = 0
for a in win_commbinations:
if board[a[0]] == board[a[1]] == board[a[2]] == "X":
print("Player 1 Wins!\n")
print("Congratulations!\n")
return True
if board[a[0]] == board[a[1]] == board[a[2]] == "O":
print("Player 2 Wins!\n")
print("Congratulations!\n")
return True
for a in range(9):
if board[a] == "X" or board[a] == "O":
count += 1
if count == 9:
print("The game ends in a Tie\n")
return True
while not end:
draw()
end = check_board()
if end == True:
break
print("Player 1 choose where to place a cross")
p1()
print()
draw()
end = check_board()
if end == True:
break
print("Player 2 choose where to place a nought")
p2()
print()
if input("Play again (y/n)\n") == "y":
print()
tic_tac_toe()
tic_tac_toe()
Example 3: program for tic tac toe in python
board = ['-', '-', '-',
'-', '-', '-',
'-', '-', '-']
gameplay = [1, 0, 1, 0, 1, 0, 1, 0, 1]
def display_board():
print(board[0] + '|' + board[1] + '|' + board[2])
print(board[3] + '|' + board[4] + '|' + board[5])
print(board[6] + '|' + board[7] + '|' + board[8])
def win_check():
for col in range(7):
if board[col] is board[col+1] is board[col+2] == 'X':
print('You win')
return True
if board[col] is board[col+1] is board[col+2] == 'O':
print('You win')
return True
for row in range(3):
if board[row] is board[row+3] is board[row+6] == 'X':
print('You win')
return True
if board[row] is board[row+3] is board[row+6] == 'O':
print('You win')
return True
dia = 0
if board[dia] is board[dia+4] is board[dia+8] == 'X':
print('You win')
display_board()
return True
elif board[dia] is board[dia+4] is board[dia+8] == 'O':
print('You win')
display_board()
return True
dia = 2
if board[dia] is board[dia+2] is board[dia+4] == 'X':
print('You win')
display_board()
return True
elif board[dia] is board[dia+2] is board[dia+4] == 'O':
print('You win')
display_board()
return True
def play_game():
i = 0
if gameplay[i] == 1:
board[val] = 'X'
gameplay.pop(i)
res = win_check()
if res is True:
return True
else:
display_board()
inval()
else:
board[val] = 'O'
gameplay.pop(i)
res = win_check()
if res is True:
return True
else:
display_board()
inval()
def inval():
global val
val = int(input('Choose the values from 0 to 8'))
try:
if val<=8 and val>=0:
for item in range(9):
if item == val:
res = play_game()
if res is True:
break
break
else:
print('Enter Valid Input!!!!')
inval()
except TypeError:
print('Enter Valid Input!!!!')
inval()
display_board()
inval()
Example 4: tic tac toe in python
import time
import sys
TITLES = " A B C\n"
INIT_BOARD = "| / | / | / |\n"
A, B, C = 4, 8, 12
board = [f"{x} {INIT_BOARD}" for x in range(3)]
user_turn = ""
taken = True
winner = False
turn_number = 0
SYMBOLS = ["x", "o"]
winner_save = [list(x * 3) for x in SYMBOLS]
score = {symbol: 0 for symbol in SYMBOLS}
class logic:
def __init__(self, ctx, turn, win_template):
self.ctx = ctx
self.turn = turn
self.template = win_template
def winner_check(self):
win_check = [
[board[c][x] for x in range(4, len(board[c])) if x % 4 == 0]
for c in range(3)
]
for x in range(3):
win_check.append([win_check[c][x] for c in range(3)])
win_check.append([win_check[x][x] for x in range(3)])
win_check.append([win_check[x][c] for x, c in zip(range(3)[::-1], range(3))])
for x in win_check:
if x in self.template:
print(f"{self.turn} wins!")
keep = True
break
keep = False
return keep
def take_spot(self):
append_board = board[int(user[1])]
append_board = "".join(
[
append_board[x] if x != eval(user[0]) else self.turn
for x in range(len(append_board))
]
)
return append_board
def spot_taken(self):
board_ctx = board[int(self.ctx[1])][eval(self.ctx[0])]
check_spot = True if board_ctx in ["o", "x"] else False
if check_spot == True:
print("spot already taken :/ try again")
return check_spot
def input_check():
slow_print("location- \n")
ctx = input().upper()
all_input = [x + str(c) for x in ["A", "B", "C"] for c in range(3)]
if ctx in all_input:
pass
else:
while ctx not in all_input:
slow_print("invalid location, try again\n")
slow_print("location- \n")
ctx = input().upper()
return list(ctx)
def slow_print(inpt):
for x in inpt:
sys.stdout.write(x)
time.sleep(0.01)
slow_print(TITLES + "".join(board))
while True:
slow_print(f"{SYMBOLS[0]}'s or {SYMBOLS[1]}'s?- \n")
user_turn = input()
if user_turn in [SYMBOLS[0], SYMBOLS[1]]:
slow_print(f"{user_turn}'s first!\n")
break
else:
slow_print("incorrent input try again!")
while True:
outcome = "None"
while winner == False:
turn_number += 1
if turn_number == 10:
slow_print("Tie!\n")
outcome = None
break
while taken == True:
user = input_check()
init = logic(user, user_turn, winner_save)
taken = init.spot_taken()
ctx_board = init.take_spot()
board[int(user[1])] = ctx_board
slow_print(TITLES + "".join(board))
user_turn = SYMBOLS[0] if user_turn != SYMBOLS[0] else SYMBOLS[1]
taken = True
winner = init.winner_check()
if outcome == None:
pass
else:
score[SYMBOLS[0] if user_turn == SYMBOLS[1] else SYMBOLS[1]] += 1
slow_print(
f"Scores: {SYMBOLS[0]}-{score[SYMBOLS[0]]}, {SYMBOLS[1]}-{score[SYMBOLS[1]]}\n"
)
slow_print("Would you like to play another (Y/N)?- \n")
repeat = input().upper()
if repeat == "Y":
winner = False
board = [f"{x} {INIT_BOARD}" for x in range(3)]
turn_number = 0
continue
else:
break
Example 5: tic tac toe algorithm python
import random
def algoX(lisp):
'''my bacic stupid idiotic dunder-headded ape-brained algorithim'''
for i in range(len(lisp)):
if lisp[i] == 1:
lisp[i] = 'X'
elif lisp[i] == 2:
lisp[i] = 'O'
def zeros(list):
out = 0
for i in range(len(list)):
if list[i] == 0:
out += 1
return out
def count(list, simb):
out = 0
for i in range(len(list)):
if list[i] == simb:
out += 1
return out
if count(lisp[0:3], 'X') == 2 and zeros(lisp[0:3]) == 1:
lisp[0:3] = 'X','X','X'
elif count(lisp[3:6], 'X') == 2 and zeros(lisp[3:6]) == 1:
lisp[3:6] = 'X','X','X'
elif count(lisp[6:9], 'X') == 2 and zeros(lisp[6:9]) == 1:
lisp[6:9] = 'X','X','X'
elif count([lisp[0],
lisp[3],
lisp[6]],'X') == 2 and zeros([lisp[0],lisp[3],lisp[6]]) == 1:
for i in range(3):
lisp[i*3] = 'X'
elif count([lisp[1],
lisp[4],
lisp[7]],'X') == 2 and zeros([lisp[1],lisp[4],lisp[7]]) == 1:
for i in range(3):
lisp[(i*3)+1] = 'X'
elif count([lisp[2],
lisp[5],
lisp[8]],'X') == 2 and zeros([lisp[2],lisp[5],lisp[8]]) == 1:
for i in range(3):
lisp[(i*3)+2] = 'X'
elif count([lisp[0],
lisp[4],
lisp[8]],'X') == 2 and zeros([lisp[0],lisp[4],lisp[8]]) == 1:
for i in range(3):
lisp[(i*4)] = 'X'
elif count([lisp[2],
lisp[4],
lisp[6]],'X') == 2 and zeros([lisp[2],lisp[4],lisp[6]]) == 1:
lisp[2], lisp[4], lisp[6] = 'X','X','X'
else:
'''prevent loss'''
if count(lisp[0:3], 'O') == 2 and zeros(lisp[0:3]) == 1:
for i in range(3):
if lisp[i] == 0:
lisp[i] = 'X'
elif count(lisp[3:6], 'O') == 2 and zeros(lisp[3:6]) == 1:
for i in range(3,6):
if lisp[i] == 0:
lisp[i] = 'X'
elif count(lisp[6:9], 'O') == 2 and zeros(lisp[6:9]) == 1:
for i in range(6,9):
if lisp[i] == 0:
lisp[i] = 'X'
elif count([lisp[0],
lisp[3],
lisp[6]],'O') == 2 and zeros([lisp[0],lisp[3],lisp[6]]) == 1:
for i in range(3):
if lisp[i*3] == 0:
lisp[i*3] = 'X'
elif count([lisp[1],
lisp[4],
lisp[7]],'X') == 2 and zeros([lisp[1],lisp[4],lisp[7]]) == 1:
for i in range(3):
if lisp[(i*3)+1] == 0:
lisp[(i*3)+1] = 'X'
elif count([lisp[2],
lisp[5],
lisp[8]],'X') == 2 and zeros([lisp[2],lisp[5],lisp[8]]) == 1:
for i in range(3):
if lisp[(i*3)+2] == 0:
lisp[(i*3)+2] = 'X'
elif count([lisp[0],
lisp[4],
lisp[8]],'X') == 2 and zeros([lisp[0],lisp[4],lisp[8]]) == 1:
for i in range(3):
if lisp[i*4] == 0:
lisp[(i*4)] = 'X'
elif count([lisp[2],
lisp[4],
lisp[6]],'X') == 2 and zeros([lisp[2],lisp[4],lisp[6]]) == 1:
if lisp[2] == 0:
lisp[2] = 'X'
elif lisp[4] == 0:
lisp[4] = 'X'
elif lisp[6] == 0:
lisp[6] = 'X'
else:
'''regular options'''
if lisp[4] == 0:
lisp[4] = 'X'
else:
while True:
rand = random.randint(0,8)
if lisp[rand] == 'X' or lisp[rand] == 'O':
continue
else:
lisp[rand] = 'X'
break
return lisp