-3

This is a Numerical Tic Tac Toe game. I try to make the winning condition which is a row, a column or a diagonal add up to 15. However, the ' ' cannot be changed to integer. What can I do to avoid this?

Here is the main problem part:

local_board = [[ '','0','1','2'],
               ['0',' ',' ',' '],
               ['1',' ',' ',' '],
               ['2',' ',' ',' ']]

def isWinner(self):
    if int(local_board[1][1]) + int(local_board[1][2]) + int(local_board[1][3]) == 15\
    or int(local_board[2][1]) + int(local_board[2][2]) + int(local_board[2][3]) == 15\
    or int(local_board[3][1]) + int(local_board[3][2]) + int(local_board[3][3]) == 15\
    or int(local_board[1][1]) + int(local_board[2][1]) + int(local_board[3][1]) == 15\
    or int(local_board[1][2]) + int(local_board[2][2]) + int(local_board[3][2]) == 15\
    or int(local_board[1][3]) + int(local_board[2][3]) + int(local_board[3][3]) == 15\
    or int(local_board[1][1]) + int(local_board[2][2]) + int(local_board[3][3]) == 15\
    or int(local_board[1][3]) + int(local_board[2][2]) + int(local_board[3][1]) == 15:
        print('Player '+ self.player +' wins. Congrats!')
        self.continue_game = False
martineau
  • 119,623
  • 25
  • 170
  • 301
Hu Jack
  • 65
  • 6
  • _However, the ‘’ cannot be changed to integer. What can I do to avoid this?_ Aren’t you in the best position to answer that? You’re the one designing the program, after all. By the way, that if statement is painful, you should really use loops! – AMC Feb 29 '20 at 02:10
  • Write your own function that accepts a string argument which checks it for `' '` and returns whatever value you would like for that, or otherwise returns `int(arg)`. It not clear what value should be returned when the position contains `' '`, however… – martineau Feb 29 '20 at 02:18

2 Answers2

0

Instead of a 2D list, create a 2D numpy array. The empty spaces you can replace with a NaN. Everything else input as an integer. Then you can use the numpy.nansum function to compute the sums you are interested in computing.

Here is some sample code. I did not test it.

import numpy as np

local_board = [[np.nan,0,1,2],
               [0,np.nan,np.nan,np.nan],
               [1,np.nan,np.nan,np.nan],
               [2,np.nan,np.nan,np.nan]]

local_board = np.array(local_board)

def isWinner(self):
    if np.nansum(local_board[1][1] + local_board[1][2] + local_board[1][3]) == 15\
    or np.nansum(local_board[2][1] + local_board[2][2] + local_board[2][3]) == 15\
    or np.nansum(local_board[3][1] + local_board[3][2] + local_board[3][3]) == 15\
    or np.nansum(local_board[1][1] + local_board[2][1] + local_board[3][1]) == 15\
    or np.nansum(local_board[1][2] + local_board[2][2] + local_board[3][2]) == 15\
    or np.nansum(local_board[1][3] + local_board[2][3] + local_board[3][3]) == 15\
    or np.nansum(local_board[1][1] + local_board[2][2] + local_board[3][3]) == 15\
    or np.nansum(local_board[1][3] + local_board[2][2] + local_board[3][1]) == 15:
        pr'Player '+ self.player +' wins. Congrats!')
        self.continue_game = False

Sohrab T
  • 625
  • 4
  • 14
0

You don't want empty positions to count as zero so you'll have to turn them into an integer that assures that the sum won't be 15 (for example -15). This can be done using the a if condition else b construct:

n = int(p) if p.strip() else -15 # where p is the string value at a given position

To check the sums on lines rows and diagonals, you could prepare a list of indexes (in a flattened list of the positions) and use it as indirection to add up the values at the corresponding board positions:

local_board = [['','0','1','2'],['0',' ',' ',' '],['1',' ',' ',' '],['2',' ',' ',' ']]

lines = [ [0,1,2,3],  [4,5,6,7],  [8,9,10,11], [12,13,14,15],
          [0,4,8,12], [2,5,9,13], [2,6,10,14], [3,7,11,15],
          [1,5,10,15],[3,6,9,12] ]
nums  = [ int(n) if n.strip() else -15 for row in local_board for n in row ]
win   = 15 in (sum(nums[p] for p in line) for line in lines)

print(win) # False
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • So I just replace the def part with this? – Hu Jack Feb 29 '20 at 03:18
  • No this is to give you an idea of the technique. Looking back at your code I realized that you are using row and column zero as kinds of headers so my suggestion won't work as is because I was assuming your board was 4x4. Analyze my suggestion and adapt it to your needs. – Alain T. Feb 29 '20 at 03:21