Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python - TicTacToe Game
# Simple TicTacToe game in Python - EAO
import random
import sys
board=[i for i in range(0,9)]
player, computer = '',''
# Corners, Center and Others, respectively
# Winner combinations
# Table
def print_board():
for i in board:
end = ' | '
if x%3 == 0:
end = ' \n'
if i != 1: end+='---------\n';
char=' '
if i in ('X','O'): char=i;
def select_char():
if random.randint(0,1) == 0:
return chars[::-1]
return chars
def can_move(brd, player, move):
if move in tab and brd[move-1] == move-1:
return True
return False
def can_win(brd, player, move):
for i in brd:
if i == player: places.append(x);
for tup in winners:
for ix in tup:
if brd[ix] != player:
if win == True:
return win
def make_move(brd, player, move, undo=False):
if can_move(brd, player, move):
brd[move-1] = player
win=can_win(brd, player, move)
if undo:
brd[move-1] = move-1
return (True, win)
return (False, False)
# AI goes here
def computer_move():
# If I can win, others don't matter.
for i in range(1,10):
if make_move(board, computer, i, True)[1]:
if move == -1:
# If player can win, block him.
for i in range(1,10):
if make_move(board, player, i, True)[1]:
if move == -1:
# Otherwise, try to take one of desired places.
for tup in moves:
for mv in tup:
if move == -1 and can_move(board, computer, mv):
return make_move(board, computer, move)
def space_exist():
return board.count('X') + board.count('O') != 9
player, computer = select_char()
print('Player is [%s] and computer is [%s]' % (player, computer))
result='%%% Deuce ! %%%'
while space_exist():
print('# Make your move ! [1-9] : ', end='')
move = int(input())
moved, won = make_move(board, player, move)
if not moved:
print(' >> Invalid number ! Try again !')
if won:
result='*** Congratulations ! You won ! ***'
elif computer_move()[1]:
result='=== You lose ! =='
Copy link

rithvikgujjula commented Feb 3, 2019

hi where to download the software to program this.

thank you

Copy link

komalipriyaa commented Sep 14, 2019

hi where to download the software to program this.

thank you

For gaming in Python, According to my view, PyCharm is comfortable.
Here is the link to download it.

Copy link

LoekVV commented Oct 31, 2019

Hey, how can I make the program print an error message and then ask for an input again if the user inputs a letter or a character that isn't a number?

Copy link

88amit77 commented Nov 15, 2019

osm logic.

Copy link

tamaa019 commented Jan 22, 2020

Nice program!

Copy link

Calm-Mango commented Jan 28, 2020

amazing content to learn from for beginners like me.

Copy link

Avishekp6460 commented Apr 10, 2020

its showing error in move = int(input())
when ever we put any char value its shows error

Copy link

pd2399 commented Apr 12, 2020

Can you please help me with my code for tic-tac-toe (2 player). The output gets printed three times.
Here is the link to the code.

Copy link

PandaTobi commented Apr 24, 2020

@Avishekp6460 why are you inputting char values? if you wish to fix that (even there's no reason to), just implement a try-except statement.

Copy link

CaylusYT commented Aug 8, 2020

well this was useful!

Copy link

hariramn commented Sep 7, 2020

Hey can someone give me a logic walkthrough? or some resources that can be refered to?

Copy link

westernlabz commented Oct 21, 2020

Very helpful, thanks

Copy link

muneerbrohi commented Jun 14, 2021

can you use in this program that works restart the program use the if else condition or Y/N

Copy link

ErikVillarreal-bit commented Nov 12, 2021

Thanks 😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment