• Register
1 vote
31 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

Please log in or register to answer this question.

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 19 views
19 views
Can anyone solve this problem?
asked Dec 25, 2020 TeamScript 13.5k points
0 votes
1 answer 20 views
20 views
Problem: Hello everyone, here I leave my code made in C ++ of the famous game Tic-Tac-Toe . The truth is that I am still learning this language on my own and I would like you to give me your opinions that I should edit or improve my code, also that I would like to achieve ... '7', '8', '9'}}; menu (numbers, Player, Name, Name_2); // Function where the menu and game options are located return 0; }
asked Dec 4, 2020 sasha 8.4k points
0 votes
1 answer 28 views
28 views
How to implement the Tik-tac-toe game in C++?
asked Oct 10, 2020 sikandar 2.4k points
2 votes
1 answer 55 views
55 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, 2020 Gavin 15.3k points
1 vote
1 answer 173 views
173 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, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
Problem: I have python 2 and python 3 on my system. But it always select python 2 but I want to switch it according to my needs. How can i switch in between them?
asked 1 day ago jyoti goyal 2.1k points
0 votes
1 answer 2 views
2 views
Problem: opencv3: does not support building both python 2 and 3 wrappers.
asked 4 days ago charles mathews 3.8k points
0 votes
1 answer 153 views
153 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 Oct 24, 2020 psandprop 2.4k points
0 votes
1 answer 43 views
43 views asked Oct 23, 2020 Shaad Sh 800 points
1 vote
1 answer 761 views
761 views
Problem: For this following two dictionaries: firstdictionary = {'x': 112, 'y': 224, 'z': 448} secoenddictionary = {'x': 56, 'y': 28, 'z': 14} I wrote a pyhton program to subtract their values: firstdictionary = {'x': 112, 'y': 224, 'z' ... - secoenddictionary} TypeError: unsupported operand type(s) for -: 'dict' and 'dict' Does any one have any solution for this error? Thanks in advance
asked May 14, 2020 samhaz 5k points