-1

Creating a function guess_index that will tell where number is on board. So if I have a 4x4 board, the the numbers will be

[
[2,False], [3,False], [4, False], [1, False]
[ Same line as above
[same line as above
[same line as above

]

So, first I need to convert that into a list like this below

[
[0,1,2,3],
[4,5,6,7],
[8,9,10,11],
[12,13,14,15]
]

So guess_index(board, 3) should return [0,3] meaning that the number 3 is located on the 0th row & the 3rd column. Likewise guess_index(board, 14) should return [3,2]. The tricky part is getting it to work for a 2x4 vs a 4x2 matrix etc

My code is

def guess(board, guess)
     count = 0
  for i in board:
    for j in i:
      board[i][j] == [count]
      count += 1
  for a in board:
    for b in a:
      if b == guess:
        return [a,b]

However I'm just getting None when I try the examples I wrote above??

user3555502
  • 339
  • 1
  • 4
  • 7
  • Maybe you should debug your code to see why it's returning None. The only time it will return anything is if `b == guess` so presumably that's never happening. – MxLDevs Apr 26 '14 at 05:32

1 Answers1

0

Starting with:

board = [[[5, False], [5, False], [3, False], [4, False]],
         [[1, False], [0, False], [6, False], [7, False]],
         [[2, False], [2, False], [3, False], [0, False]],
         [[7, False], [6, False], [1, False], [4, False]]]

You can make:

def guess_index(board, guess):
    """Calculate coordinates for guess index within board."""
    row_len = len(board[0])
    return guess // row_len, guess % row_len

Example:

>>> guess_index(board, 3)
(0, 3)
>>> guess_index(board, 14)
(3 ,2)

You can then use this to get the integer and Boolean values from the board:

row, col = guess_index(board, guess)
int_value, bool_value = board[row][col]

In the case guess == 3 that would be int_value == 4 and bool_value == False.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437