Posted by: Sourav | January 9, 2017

2 Player TIC TAC TOE Game,Python Teacher Sourav,Kolkata 09748184075


import sys

BOARDNUMS = [1, 2, 3, 4, 5, 6, 7, 8, 9]
availablepos = []
for i in BOARDNUMS:
    availablepos.append(str(i))


def createboard(BOARDNUMS):
    sys.stdout.write('\t')
    for i in range(0, len(BOARDNUMS)):

        sys.stdout.write(str(BOARDNUMS[i]))
        sys.stdout.write(' | ')
        if (i % 3) == 2:

            print
            sys.stdout.write('\t')
        else:
            pass


def checkresult(BOARDNUMS):
    if BOARDNUMS[0] == 'x' and BOARDNUMS[1] == 'x' and BOARDNUMS[2] == 'x':

        return 'player 1 wins'
    elif BOARDNUMS[3] == 'x' and BOARDNUMS[4] == 'x' and BOARDNUMS[5] == 'x':
        return 'player 1 wins'
    elif BOARDNUMS[6] == 'x' and BOARDNUMS[7] == 'x' and BOARDNUMS[8] == 'x':
        return 'player 1 wins '
    elif BOARDNUMS[0] == 'x' and BOARDNUMS[3] == 'x' and BOARDNUMS[6] == 'x':
        return 'player 1 wins'

    elif BOARDNUMS[1] == 'x' and BOARDNUMS[4] == 'x' and BOARDNUMS[7] == 'x':
        return 'player 1 wins'
    elif BOARDNUMS[2] == 'x' and BOARDNUMS[5] == 'x' and BOARDNUMS[8] == 'x':
        return 'player 1 wins'

    elif BOARDNUMS[0] == 'x' and BOARDNUMS[4] == 'x' and BOARDNUMS[8] == 'x':
        return 'player 1 wins'
    elif BOARDNUMS[2] == 'x' and BOARDNUMS[4] == 'x' and BOARDNUMS[6] == 'x':
        return 'player 1 wins'
    else:
        pass

    if BOARDNUMS[0] == 'o' and BOARDNUMS[1] == 'o' and BOARDNUMS[2] == 'o':

        return 'player 2 wins'
    elif BOARDNUMS[3] == 'o' and BOARDNUMS[4] == 'o' and BOARDNUMS[5] == 'o':
        return 'player 2 wins'
    elif BOARDNUMS[6] == 'o' and BOARDNUMS[7] == 'o' and BOARDNUMS[8] == 'o':
        return 'player 2 wins '
    elif BOARDNUMS[0] == 'o' and BOARDNUMS[3] == 'o' and BOARDNUMS[6] == 'o':
        return 'player 2 wins'

    elif BOARDNUMS[1] == 'o' and BOARDNUMS[4] == 'o' and BOARDNUMS[7] == 'o':
        return 'player 2 wins'
    elif BOARDNUMS[2] == 'o' and BOARDNUMS[5] == 'o' and BOARDNUMS[8] == 'o':
        return 'player 2 wins'

    elif BOARDNUMS[0] == 'o' and BOARDNUMS[4] == 'o' and BOARDNUMS[8] == 'o':
        return 'player 2 wins'
    elif BOARDNUMS[2] == 'o' and BOARDNUMS[4] == 'o' and BOARDNUMS[6] == 'o':
        return 'player 2 wins'
    else:
        pass


createboard(BOARDNUMS)
result=''
# print
wanttoplay = 'y'
while (len([x for x in availablepos if x.isdigit()])> 0):


    #createboard(BOARDNUMS)
    if len([x for x in availablepos if x.isdigit()])!=0:
        print '\n Number of available positions for player 1 is ',len([x for x in availablepos if x.isdigit()])
    if len([x for x in availablepos if x.isdigit()])!=0:
        print ('available positions are ', [x for x in availablepos if x.isdigit()])
    else:
        break
    player1pos = int(raw_input('\nPlayer One please choose position\n'))
    if str(player1pos) in availablepos:

        BOARDNUMS[player1pos - 1] = 'x'
        createboard(BOARDNUMS)
        result=checkresult(BOARDNUMS)
        if result=='player 1 wins':
            print 'Player 1 wins'
            break

    else:
        print '\nsorry player one can not choose that position ', player1pos
        continue

    availablepos[player1pos - 1] = 'x'
    if len([x for x in availablepos if x.isdigit()])!=0:
        print '\n Number of available positions for player 2 is ', len([x for x in availablepos if x.isdigit()])
    if len([x for x in availablepos if x.isdigit()])!=0:
        print ('available positions are ', [x for x in availablepos if x.isdigit()])
    else:
        break
    player2pos = int(raw_input('\nPlayer Two please choose position\n'))
    if str(player2pos) in availablepos:

        BOARDNUMS[player2pos - 1] = 'o'
        createboard(BOARDNUMS)
        result = checkresult(BOARDNUMS)
        if result == 'player 2 wins':
            print 'Player 2 wins'
            break


    else:

        print '\nsorry player two can not choose that position ', player2pos
        continue
    availablepos[player2pos - 1] = 'o'
#print 'the result is ',result
if result is None:
    print 'it\'s a draw'
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: