• Register
1 vote
19 views

Problem:

I am a python newbee. I have an assginement on python where I have to make a 2 player game called tic tac toe. I wrote some code but that does not seem to work:

from __future__ import print_function

choices = []

for x in range (0, 9) :
    choices.append(str(x + 1))

playerOneTurn = True
winner = False

def printBoard() :
    print( '\n -----')
    print( '|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
    print( ' -----')
    print( '|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
    print( ' -----')
    print( '|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
    print( ' -----\n')



    for x in range (0, 3) :
        y = x * 3
        if (choices[y] == choices[(y + 1)] and choices[y] == choices[(y + 2)]) :
            winner = True
            printBoard()
        if (choices[x] == choices[(x + 3)] and choices[x] == choices[(x + 6)]) :
            winner = True
            printBoard()

    if((choices[0] == choices[4] and choices[0] == choices[8]) or 
       (choices[2] == choices[4] and choices[4] == choices[6])) :
        winner = True
        printBoard()

print ("Player " + str(int(playerOneTurn + 1)) + " wins!\n")

Please help me to complete this thing 

10 7 2
6,060 points

1 Answer

1 vote

Answer:

Please use this code to complete your assginement:

from __future__ import print_function

choices = []

for x in range (0, 9) :
    choices.append(str(x + 1))

playerOneTurn = True
winner = False

def printBoard() :
    print( '\n -----')
    print( '|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|')
    print( ' -----')
    print( '|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|')
    print( ' -----')
    print( '|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|')
    print( ' -----\n')

while not winner :
    printBoard()

    if playerOneTurn :
        print( "Player 1:")
    else :
        print( "Player 2:")

    try:
        choice = int(input(">> "))
    except:
        print("please enter a valid field")
        continue
    if choices[choice - 1] == 'X' or choices [choice-1] == 'O':
        print("illegal move, plase try again")
        continue

    if playerOneTurn :
        choices[choice - 1] = 'X'
    else :
        choices[choice - 1] = 'O'

    playerOneTurn = not playerOneTurn

    for x in range (0, 3) :
        y = x * 3
        if (choices[y] == choices[(y + 1)] and choices[y] == choices[(y + 2)]) :
            winner = True
            printBoard()
        if (choices[x] == choices[(x + 3)] and choices[x] == choices[(x + 6)]) :
            winner = True
            printBoard()

    if((choices[0] == choices[4] and choices[0] == choices[8]) or 
       (choices[2] == choices[4] and choices[4] == choices[6])) :
        winner = True
        printBoard()

print ("Player " + str(int(playerOneTurn + 1)) + " wins!\n")

Hope this helps you.

11 6 4
34,950 points

Related questions

0 votes
1 answer 13 views
13 views
How to implement the Tik-tac-toe game in C++?
asked Oct 10 sikandar 2.4k points
2 votes
1 answer 29 views
29 views
Problem: Hello, Kodlogs I am looking for a code that makes a tic-tac-toe game board of two-player. I am searching for the codes for a long time over the internet but didn’t get codes that I understand easily. Thanks for your effort guys. I must recognize your help. Thanks again
asked Mar 31 Gavin 15.3k points
1 vote
1 answer 22 views
22 views
Problem: I’ve completed my basic java course yesterday. My instructor provided me a problem to solve as an assignment for the final test. The problem is to make a tic tac toe java code using a 2d array. I know how to use a 2d array and handle it. But I ... not looking for the whole solution, but yeah if you can provide it might help me a lot to understand the process better. Thanks, fellow!
asked Mar 31 Gavin 15.3k points
0 votes
0 answers 5 views
5 views
I have Python 2.7 install on Windows machine. I am getting bash: pip: command not found error. Please help
asked 20 hours ago hashq 260 points
0 votes
0 answers 6 views
6 views
This line: sift = cv2.xfeatures2d.SIFT_create() return error: Traceback (most recent call last): File "C:/Python27/openCVskrypty/GUI/SOLUTION2.py", line 11, in <module> sift = cv2.xfeatures2d.SIFT_create() AttributeError: 'module' object has no attribute 'xfeatures2d' I ... ) and I haven't got xfeatures2d module. Does anyone know why? Can I download it separately? Thanks for help how fix this.
asked 5 days ago psandprop 2.2k points