2

My coursework is to create Tic Tac Toe in Python, my tutor helped me get it working in 2.7 however It needs to be in 3.5.

Firstly in 2.7 the code below prints a 3x3 list, however in 3.5 it just prints the list downwards not 3x3. my tutor said maybe put end = ' ' at the end but that also doesn't work.

def printBoard( board ):
    counter = 0   
    for y in range(3):    
        for x in range(3):    
            print (board[counter]),    
            counter += 1    
        print    
    print

second problem is on 2.7 it allows me to continue to input numbers till the board is filled with X or O, on 3.5 it only allows to input once and then the program ends?

value = input("input number between 1 and 9")    
value = int(value)        
if value == 1:    
    alist[0] = player1    
    printBoard( alist )    
    value = input("input number between 1 and 9")    
if value == 2:    
    alist[1] = player1    
    printBoard( alist )    
    value = input("input number between 1 and 9")

etc.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Oscar Dolloway
  • 199
  • 3
  • 17
  • You have used `value = int(value)` only once, you should use it everytime when you want to take input as `int`. – ZdaR Nov 22 '15 at 11:55

2 Answers2

4
  1. print changed from statement to a function in Python 3.x. To print a statement without newline, you need to pass end=' ' parameter (You can use the print as a function in Python 2.7 if you put from __future__ import print_function at the beginning of the code):

    print(board[counter], end=' ')
    
  2. input returns a string in Python 3.x. (does not evaluate the input string). You need to convert the value into int every where you used input:

    value = input("input number between 1 and 9")
    value = int(value)
    

    Alternatively, instead of comparing the input with integer literal 1 or 2, compare the input string with strings: '1', '2' without converting the string into integer. (But this requires you to use raw_input in Python 2.7 instead of input)

  3. print should be called: print(). Otherwise, nothing is printed.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • changing 1 to '1' worked thanks a lot man, however the print(board[counter], end = '') only prints the list across rather then downwards now. – Oscar Dolloway Nov 22 '15 at 11:56
  • def printBoard( board ): counter = 0 for y in range(3): for x in range(3): print( board[counter], end='') counter += 1 print this is how the list printed, im working in 3.5, 1 2 3 4 5 6 7 8 9 input number between 1 and 9 – Oscar Dolloway Nov 22 '15 at 12:04
  • 2
    use `print()` in place of `print` to move text to new line. – furas Nov 22 '15 at 12:12
  • "thanks for your answer, but it's still not producing the right result" - "you should accept this answer." Uh... – TigerhawkT3 Nov 22 '15 at 12:18
0

I assume board is something like [['*', '*', '*'], ['*', '*', '*'], ['*', '*', '*']]. That means you have an easy way of printing this with a single print() call.

print(*(''.join(row) for row in board), sep='\n')

This joins each row into a new string, producing each row as part of a generator. This generator is unpacked with * and sent to print(), where each row is separated by a newline.

For your second issue, the problem is simple: you cast int() for the first value, but not for the subsequent ones. However, this is the sort of thing you should be doing with a loop. It'll prevent exactly this kind of bug. If you find yourself writing lots of code with Ctrl+V, you're doing something wrong. If each block is slightly different, with an incremented number, you would do that with something like for i in range(n):, which allows you to execute the same code with an incremented number on each iteration.

However, I'd recommend a simple while loop that checks if the game is complete:

while True:
    move = request_move()
    do_move('X', move)
    if game_complete():
        break
    request_move()
    do_move('O', move)
    if game_complete():
        break

You would then write appropriate functions to request move coordinates, input moves into the board, and check if the game is complete yet.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
  • I also recently posted [my own implementation of TTT](http://codereview.stackexchange.com/questions/111386/text-based-tic-tac-toe-with-dry-and-pep-8) on CR if you want to take a look. – TigerhawkT3 Nov 22 '15 at 12:09