4

I have a grid that looks like this. I place one "b" randomly in the grid and put the number 1 surround the letter "b". This seems to work everywhere except when a 1 is supposed to be placed on the bottom row and the column all the way to the right. For example, it would look something like this

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 b
0 0 0 0 0 0 0 0 0 0

Where it should look like

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 1 b
0 0 0 0 0 0 0 0 1 1

Here is the code I am using and I can't figure out why those 1's arent being placed there.

from random import*
mat1 = []
mat2 = []

def makemat(x):
    for y in range(x):
        list1 = []
        list2 = []
        for z in range(x):
            list1.append(0)
            list2.append("-")
        mat1.append(list1)
        mat2.append(list2)
makemat(10)


def printmat(mat):
    for a in range(len(mat)):
        for b in range(len(mat)):
            print(str(mat[a][b]) + "\t",end="")
        print("\t")



def addmines(z):
    count = 0
    while (count < z):
        x = randrange(0,len(mat1))       
        y = randrange(0,len(mat1))      
        if mat1[y][x] == "b":
            count -= 1
        else:
            mat1[y][x] = "b"
        count += 1
addmines(1)


    

def addscores():
    for x in range(len(mat1)):
        for y in range(len(mat1)):
            if ((y < len(mat1)-1) and (x < len(mat1)-1)) and ((y >= 0) and (x >= 0))):
                if mat1[y+1][x] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x] == "b":
                    mat1[y][x] = 1
                if mat1[y][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y][x-1] == "b":
                    mat1[y][x] = 1
                if mat1[y+1][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y+1][x-1] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x-1] == "b":
                    mat1[y][x] = 1
    printmat(mat1)
addscores()
bad_coder
  • 11,289
  • 20
  • 44
  • 72
DHulse94
  • 67
  • 5
  • What is that `-1` doing there? `x < len(mat1)-1` – avishayp Nov 17 '12 at 00:19
  • 1
    The OP is ignoring a border of 1 square as he is checking for the presence of bombs at the `x+1` coordinate – inspectorG4dget Nov 17 '12 at 00:21
  • 1
    In your previous question, you were getting out of bounds errors, because when `x` reached `9`, your code tested for values at `x + 1`, which is `10`, which is out of bounds. Your attempt at a solution here was to never allow `x` to reach `9`. But if `x` never reaches `9`, how will you ever place a `1` in column `9`? You need to find a different solution to your original problem. – senderle Nov 17 '12 at 00:21

3 Answers3

2

Your nested loop checks each square to see if it should have a 1 in it. However, in your first if clause in addscores(), you omit every square that lies on an edge of the square. A nice way to solve this would be to omit the if cluase and instead add a function to check a square that automatically checks bounds. For example:

def checksqu(y, x):
    if y < 0 or y >= len(mat1) or x < 0 or x >= len(mat1):
        return False
    return mat1[y][x] == 'b'

Then instead of if mat1[y - 1][x - 1]:, you could do if checksqu(y - 1, x - 1): (and etcetera).

jma127
  • 1,432
  • 9
  • 13
0

You can simplify this part:

def addscores():
    for x in range(len(mat1)):
        for y in range(len(mat1)):
            if ((y < len(mat1)-1) and (x < len(mat1)-1)) and ((y >= 0) and (x >= 0))):
                if mat1[y+1][x] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x] == "b":
                    mat1[y][x] = 1
                if mat1[y][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y][x-1] == "b":
                    mat1[y][x] = 1
                if mat1[y+1][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y+1][x-1] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x+1] == "b":
                    mat1[y][x] = 1
                if mat1[y-1][x-1] == "b":
                    mat1[y][x] = 1

By using this code:

def addscores():
    size = len(mat1)
    directions = [(dx, dy) for dx in [-1,0,1] for dy in [-1,0,1] if (dy!=0 or dx!=0)]
    for x in range(size):
        for y in range(size):
            for dx, dy in directions:
                try:
                    if mat1[y+dy][x+dx] == "b":
                        mat1[y][x] = 1
                except:
                    pass
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
0

This seems to do the trick:

def addscores(mat):
    for y in range(len(mat)):
        for x in range(len(mat[y])):
            if mat[y][x] == 'b':
                mat = pad(mat, x, y, '1')
    return mat

def pad(mat, x, y, n):
    for i, (x,y) in enumerate(itertools.product(range(x-1, x+2), range(y-1, y+2))):
        if i != 4: # the coordinate at index 4 is where the bomb is
            if 0<=y<len(mat) and 0<=x<len(mat[y]):
                mat[y][x] = n
    return mat

Testing:

In [127]: mat
Out[127]: 
[['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '1', '1', '1'],
 ['0', '0', '0', '0', '0', '0', '0', '1', '0', '1']]

In [129]: addscores(mat)
Out[129]: 
[['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '1', '1'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '1', 'b'],
 ['0', '0', '0', '0', '0', '0', '0', '0', '1', '1']]
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241