Notice: add_option was called with an argument that is deprecated since version 2.3.0 with no alternative available. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 4088

Notice: The called constructor method for WP_Widget in Woo_AdWidget is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_BlogAuthorInfo is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_EmbedWidget is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_flickr is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_Search is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_Twitter is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_Subscribe is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_ContactUs is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_Widget_WooTabs is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_Widget_Feedback is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956

Notice: The called constructor method for WP_Widget in Woo_Widget_Component is deprecated since version 4.3.0! Use
__construct()
instead. in /home/xfivhjwectmq/public_html/olutolaobembe.com/wp-includes/functions.php on line 3956
The Python Learner: The Tic Tac Toe Game | Olutola Michael Obembe

The Python Learner: The Tic Tac Toe Game

Still continuing my Python and now I have to write the Tic Tac Toe game …  Still using my Learners brain, I wrote some crazy lazy program to achieve my assignment ( I so much know i can do better). But just in case it can help another lazy Python learner out there …

NOte: I didnt check for a game Tie, nor did i sort the functions in a ready to go manner ….

 

def greetings():
print ("Welcome to the TicTacToe v2.0, Designed by TekQuarters Inc, @2018")
print ("=================================================================")
print("")

##import random for player sequence randomization
import random
marker=[0,0]

##Function to determine player Marker
def player_input():
    player1_marker=''
    while (player1_marker!='X' and player1_marker!='O'):
        player1_marker=input('Player 1: Please pick your Marker X or O ')
    
    if player1_marker == 'X':
        marker[0]='X'
        marker[1]='O'
    else:
        marker[0]='O'
        marker[1]='X'
            
    return marker

## function to display the tic tac toe board anytime
def tac_board (board):
    
    print (board[7] + ' | ' + board[8] + ' | ' + board[9])
    print (board[4] + ' | ' + board[5] + ' | ' + board[6])
    print (board[1] + ' | ' + board[2] + ' | ' + board[3])

#function to check for winners on the tic tac toe board
def win_check(board,mark):

    win= ((board[7] ==  board[8] ==  board[9] == mark) or # across the top
    (board[4] ==  board[5] ==  board[6] == mark) or # across the middle
    (board[1] ==  board[2] ==  board[3] == mark) or # across the bottom
    (board[7] ==  board[4] ==  board[1] == mark) or # down the middle
    (board[8] ==  board[5] ==  board[2] == mark) or # down the middle
    (board[9] ==  board[6] ==  board[3] == mark) or # down the right side
    (board[7] ==  board[5] ==  board[3] == mark) or # diagonal
    (board[9] ==  board[5] ==  board[1] == mark)) # diagonal
    
    return win

##function to decide if to play again or not
def play_tac_again():
    play_again=''
    while (play_again!='Y' and play_again!='N'):
        play_again=input('Do you want to play again? Y=Yes, N=No ')
    
    if (play_again=='Y'):
        print ('')
        print ('Restarting TicTacToe v2.0')
        print ('Player1 marker is:' + marker[0])
        print ('Player2 marker is:' + marker[1])
        player_turn()
    else:
        print('Thank you for playing our Tic Tac Toe Game. Bye for Now')

#function to track inputs and decide player turns
def player_turn():
    i=1
    flip=random.randint(0,1)
    while i < len(board):
        if flip== 0:
            print("")
            play=int (input('Player 1: Your turn to play - Pick a number 1-9: '))
            board[play]=str (marker[0])
            tac_board(board)
            flip=1
            #check if plyer has won
            winner=win_check(board,marker[0])
            if (winner==True):
                print("Player 1 won the game")
                print("")
                play_tac_again()
                break;
          
        else:
            print("")
            play=int (input('Player 2: Your turn to play - Pick a number 1-9: '))
            board[play]=str (marker[1])
            tac_board(board)
            flip=0
            winner=win_check(board,marker[1])
            if (winner==True):
                print("Player 2 won the game")
                print("")
                play_tac_again()
                break;
        
        
        i += 1
            
#Lets PLAY!!!
#greetings
greetings()

##display initial board
board = ['#','A','A','A','A','A','A','A','A','A']
tac_board(board)

##Pick player marker
player_input()

##Play the Game
player_turn()

Tags: , , , ,