Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python tic tac toe

board = ['-', '-', '-',
         '-', '-', '-',
         '-', '-', '-']

gameplay = [1, 0, 1, 0, 1, 0, 1, 0, 1]
player_dict = {1: 'X', 0: 'O'}
play_history_dict = {}
def display_board():
    print(board[0] + '|' + board[1] + '|' + board[2])
    print(board[3] + '|' + board[4] + '|' + board[5])
    print(board[6] + '|' + board[7] + '|' + board[8])

winner = None
class Win_Determine:
    def row(player):
        global winner
        previous = 0
        for i in range(3, 12, 3):
            arr = list(board[previous:i]); previous = i
            if '-' not in arr:
                if all([x == arr[0] for x in arr]):
                    winner = player_dict[player]
                    print('Player {} win'.format(player))
    
    def column(player):
        global winner
        for index in range(3):
            arr = [board[index], board[index+3], board[index+6]]
            if '-' not in arr:
                if all([x == arr[0] for x in arr]):
                    winner = player_dict[player]
                    print('Player {} win'.format(player))

    def slash(player):
        global winner
        arr_1 = [board[0], board[4], board[8]]
        arr_2 = [board[2], board[4], board[6]]
        for i in [arr_1, arr_2]:
            if '-' not in i:
                if all([x == i[0] for x in i]):
                    winner = player_dict[player]
                    print('Player {} win'.format(player))

def play_move(player):
    move = int(input('Player {}, Select a location: '.format(player_dict[player])))
    if board[move] == '-':
        board[move] = player_dict[player]
    else:
        return play_move(player)

def clear_board():
    for index in range(len(board)):
        if index != '-':
            board[index] = '-'

def tic_tac_toe():
    while True:
        display_board()
        for player in gameplay:
            play_move(player)
            display_board()
            Win_Determine.row(player)
            Win_Determine.column(player)
            Win_Determine.slash(player)
            if winner != None:
                clear_board()
                break
        if winner == None:
            clear_board()
            print('Draw')
        print('-----------------------')
tic_tac_toe()
Comment

PREVIOUS NEXT
Code Example
Python :: how to append items to a list in python 
Python :: matplotlib styles attr 
Python :: droping Duplicates 
Python :: create pytorch zeros 
Python :: catalan number 
Python :: np.random.RandomState 
Python :: how to find highest number in list without using max function python 
Python :: Auto-removal of grids by pcolor() and pcolormesh() is deprecated since 3.5 and will be removed two minor releases later; please call grid(False) first. 
Python :: python write text file on the next line 
Python :: python script to copy files to remote server 
Python :: Python NumPy split Function Example 
Python :: how to check if item is in the variable python 
Python :: numpy aray map values with dictionary 
Python :: flask cookies 
Python :: python sns lable axes 
Python :: tokenizer in keras 
Python :: remove new line character from string python 
Python :: Django custome login 
Python :: import get user model django 
Python :: python look up how many rows in dataframe 
Python :: python selenium click element 
Python :: subtract current date from pandas date column 
Python :: how to open cmd at specific size using python 
Python :: how print 2 decimal in python 
Python :: Write a Python program to sum all the items in a dictionary. 
Python :: open csv from url python 
Python :: remove space characters from string in python 
Python :: how to dump a database using manage.py 
Python :: Write a program that prints #pythoniscool, followed by a new line, in the standard output. Your program should be maximum 2 lines long You are not allowed to use print or eval or open or import sys in your file 
Python :: python formdata requests 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =