• Register
1 vote
53 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 8 views
8 views
Problem: how to Solve this: error: tic tac toe game in python HELP! I need help understanding this: tic tac toe game in python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 13 Mashhoodch 13k points
0 votes
1 answer 10 views
10 views
Problem: I had a homework problem that was as follows:A 2-dimensional 3x3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of the first row are all equal. I was just wondering why both of these solutions are correct as it seems like his is redundant and should be wrong.
asked Apr 26 Humaira ahmed 50.7k points
0 votes
1 answer 49 views
49 views
Can anyone solve this problem?
asked Dec 25, 2020 TeamScript 13.5k points
1 vote
1 answer 8 views
8 views
Problem: Need help with tic tac toe program optimization. Tic tac toe program c++.
asked Mar 25 PkGuy 23.5k points
0 votes
1 answer 14 views
14 views
Problem: I just need someone to steer me in the right direction. Point out errors I have so I can try and fix them. I'm new to c++
asked Apr 6 neeraj 10.5k points
0 votes
1 answer 12 views
12 views
Problem: My code runs, but it doesn't print out anything on the board. I'm pretty sure I have done a couple of things wrong, and if someone could point me in the right direction, that would be great. Thanks for any help!
asked Apr 6 neeraj 10.5k points
0 votes
0 answers 14 views
14 views
Problem: anyone who know this problem please help? tic tac toe minimax java source code
asked Mar 25 Rohit kr 17.5k points
0 votes
1 answer 20 views
20 views
Problem: I'm having problems during my learning as I mentioned in my question above, I try to find on web search but I can't get proper information, can you help me to continue my project?
asked Mar 21 shamimkhan 15.1k points
1 vote
1 answer 15 views
15 views
Problem: How to make a java program to make a simple tic-tac-toe game ?
asked Mar 16 Raj Tyagi 15.6k points
0 votes
1 answer 33 views
33 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 16.2k points