I have this program, is working good but i got error when the rows are full because stop and not keep going...can some one help me please..:)


import random
class Rack(object):
def __init__(self, cols, rows):
self.rows = rows
self.cols = cols

grid = Rack(7,6)

def returnStartRack(xLen,yLen, blank):
grid = []
for y in range(yLen):
row = []
for x in range(xLen):
row.append(blank)
grid.append(row)
return grid


def returnUpdatedRack(rowChoice,grid,ch,blank):
for i in range(5,-1,-1):
if grid[rowChoice] == blank:
grid[rowChoice] = ch
return grid

def checkGameWin(grid):
whoWon = False
#Row horizontal
for row in grid:
rowStr = ''
for i in row:
rowStr += i
if 'RRRR' in rowStr:
whoWon = 'player1'
if 'BBBB' in rowStr:
whoWon = 'player2'

#Row verticle
col = 0
while col < len(grid[0]):
colStr = ''
for row in grid:
colStr += row[col]
if 'RRRR' in colStr:
whoWon = 'player1'
if 'BBBB' in colStr:
whoWon = 'player2'
col+=1

#Row diagonal moving up right
for y in range(3,6):
x = 0
diag = ''
while y >= 0 and x <= 6:
diag += grid[y][x]
x += 1
y -= 1
if 'RRRR' in diag:
whoWon = 'player1'
if 'BBBB' in diag:
whoWon = 'player2'

for x in range(1,4):
y = 5
diag = ''
while y >= 0 and x <= 6:
diag += grid[y][x]
x+=1
y-=1
if 'RRRR' in diag:
whoWon = 'player1'
if 'BBBB' in diag:
whoWon = 'player2'


#Row diagonal moving up left

for y in range(3,6):
x = 6
diag = ''
while y <= 5 and x >= 0:
diag += grid[y][x]
x -= 1
y -= 1
if 'RRRR' in diag:
whoWon = 'player1'
if 'BBBB' in diag:
whoWon = 'player2'

for x in range(5,-1,-1):
y = 5
diag = ''
while y >= 0 and x >= 0:
diag += grid[y][x]
x-=1
y-=1
if 'RRRR' in diag:
whoWon = 'player1'
if 'BBBB' in diag:
whoWon = 'player2'

return whoWon


p1 = 'R'
p2 = 'B'
blank = '-'

grid=returnStartRack(7,6,blank)

player1Win = False
player2Win = False

print '\n1 2 3 4 5 6 7\n'
for y in grid:
row = ''
for x in y:
row += x + ' '
print row
print '1 2 3 4 5 6 7\n'
a1 = int(raw_input('Enter 2 for player vs computer or 1 for player1 vs player2:'))
if (a1==1):

while player1Win != True and player2Win != True:

p1Choice = int(raw_input('Enter Red Player move,choose 1-7: ')) - 1
grid = returnUpdatedRack(p1Choice,grid,p1,blank)

print '\n1 2 3 4 5 6 7\n'
for y in grid:
row = ''
for x in y:
row += x + ' '
print row
print '\n1 2 3 4 5 6 7\n'

didGameEnd = checkGameWin(grid)
if didGameEnd == 'player1':
print '\n Red wins Yayyy!!!'
player1Win = True
if didGameEnd == 'player2':
print '\n Black wins Yuppi!!!'
player2Win = True

if player1Win != True and player2Win != True:
p2Choice = int(raw_input('Enter Black Player move(B) choose 1-7: ')) - 1
grid = returnUpdatedRack(p2Choice,grid,p2,blank)

print '\n1 2 3 4 5 6 7\n'
for y in grid:
row = ''
for x in y:
row += x + ' '
print row
print '\n1 2 3 4 5 6 7\n'

didGameEnd = checkGameWin(grid)
if didGameEnd == 'player1':
print '\nRed wins Yayyy!!!'
player1Win = True
if didGameEnd == 'player2':
print '\nBlack wins Yuppi!!!'
player2Win = True
else:
while player1Win != True and player2Win != True:

p1Choice = int(raw_input('Enter Red Player move,choose 1-7: ')) - 1
grid = returnUpdatedRack(p1Choice,grid,p1,blank)

print '\n1 2 3 4 5 6 7\n'
for y in grid:
row = ''
for x in y:
row += x + ' '
print row
print '\n1 2 3 4 5 6 7\n'

didGameEnd = checkGameWin(grid)
if didGameEnd == 'player1':
print '\n Red wins Yayyy!!!'
player1Win = True
if didGameEnd == 'player2':
print '\n Black wins Yuppi!!!'
player2Win = True

if player1Win != True and player2Win != True:

num = random.randint(0,7)
p2Choice = num - 1
grid = returnUpdatedRack(p2Choice,grid,p2,blank)

print '\n1 2 3 4 5 6 7\n'
for y in grid:
row = ''
for x in y:
row += x + ' '
print row
print '\n1 2 3 4 5 6 7\n'

didGameEnd = checkGameWin(grid)
if didGameEnd == 'player1':
print '\nRed wins Yayyy!!!'
player1Win = True
if didGameEnd == 'player2':
print '\nBlack wins Yuppi!!!'
player2Win = True

Recommended Answers

All 2 Replies

Don't forget code tags please (now the # sign in tool bar)

I have this program, is working good but i got error when the rows are full because stop and not keep going...can some one help me please..:)

import random
class Rack(object):
    def __init__(self, cols, rows):
        self.rows = rows
        self.cols = cols  
                
grid = Rack(7,6)

def returnStartRack(xLen,yLen, blank):
    grid = []
    for y in range(yLen):
        row = []
        for x in range(xLen):
            row.append(blank)
        grid.append(row)
    return grid


def returnUpdatedRack(rowChoice,grid,ch,blank):
    for i in range(5,-1,-1):
        if grid[i][rowChoice] == blank:
            grid[i][rowChoice] = ch
            return grid

def checkGameWin(grid):
    whoWon = False
    #Row horizontal
    for row in grid:
        rowStr = ''
        for i in row:
            rowStr += i
        if 'RRRR' in rowStr:
            whoWon = 'player1'
        if 'BBBB' in rowStr:
            whoWon = 'player2'

    #Row verticle
    col = 0
    while col < len(grid[0]):
        colStr = ''
        for row in grid:
            colStr += row[col]
        if 'RRRR' in colStr:
            whoWon = 'player1'
        if 'BBBB' in colStr:
            whoWon = 'player2'
        col+=1

     #Row diagonal moving up right
    for y in range(3,6):
        x = 0
        diag = ''
        while y >= 0 and x <= 6:
            diag += grid[y][x]
            x += 1
            y -= 1
        if 'RRRR' in diag:
            whoWon = 'player1'
        if 'BBBB' in diag:
            whoWon = 'player2'
   
    for x in range(1,4):
        y = 5
        diag = ''
        while y >= 0 and x <= 6:
            diag += grid[y][x]
            x+=1
            y-=1
        if 'RRRR' in diag:
            whoWon = 'player1'
        if 'BBBB' in diag:
            whoWon = 'player2'


    #Row diagonal moving up left
   
    for y in range(3,6):
        x = 6
        diag = ''
        while y <= 5 and x >= 0:
            diag += grid[y][x]
            x -= 1
            y -= 1
        if 'RRRR' in diag:
            whoWon = 'player1'
        if 'BBBB' in diag:
            whoWon = 'player2'
           
    for x in range(5,-1,-1):
        y = 5
        diag = ''
        while y >= 0 and x >= 0:
            diag += grid[y][x]
            x-=1
            y-=1
        if 'RRRR' in diag:
            whoWon = 'player1'
        if 'BBBB' in diag:
            whoWon = 'player2'
           
    return whoWon


p1 = 'R' 
p2 = 'B' 
blank = '-' 

grid=returnStartRack(7,6,blank)
 
player1Win = False
player2Win = False

print '\n1 2 3 4 5 6 7\n'
for y in grid:
    row = ''
    for x in y:
        row += x + ' '
    print row
print '1 2 3 4 5 6 7\n'
a1 = int(raw_input('Enter 2 for player vs computer or 1 for player1 vs player2:'))
if (a1==1):

    while player1Win != True and player2Win != True:
           
        p1Choice = int(raw_input('Enter Red Player move,choose 1-7: ')) - 1
        grid = returnUpdatedRack(p1Choice,grid,p1,blank)
       
        print '\n1 2 3 4 5 6 7\n'
        for y in grid:
            row = ''
            for x in y:
                row += x + ' '
            print row
        print '\n1 2 3 4 5 6 7\n'
           
        didGameEnd = checkGameWin(grid)
        if didGameEnd == 'player1':
            print '\n Red wins Yayyy!!!'
            player1Win = True
        if didGameEnd == 'player2':
            print '\n Black wins Yuppi!!!'
            player2Win = True
           
        if player1Win != True and player2Win != True:
            p2Choice = int(raw_input('Enter Black Player move(B) choose 1-7: ')) - 1
            grid = returnUpdatedRack(p2Choice,grid,p2,blank)
           
            print '\n1 2 3 4 5 6 7\n'
            for y in grid:
                row = ''
                for x in y:
                    row += x + ' '
                print row
            print '\n1 2 3 4 5 6 7\n'
           
            didGameEnd = checkGameWin(grid)
            if didGameEnd == 'player1':
                print '\nRed wins Yayyy!!!'
                player1Win = True
            if didGameEnd == 'player2':
                print '\nBlack wins Yuppi!!!'
                player2Win = True
else:
    while player1Win != True and player2Win != True:
           
        p1Choice = int(raw_input('Enter Red Player move,choose 1-7: ')) - 1
        grid = returnUpdatedRack(p1Choice,grid,p1,blank)
       
        print '\n1 2 3 4 5 6 7\n'
        for y in grid:
            row = ''
            for x in y:
                row += x + ' '
            print row
        print '\n1 2 3 4 5 6 7\n'
           
        didGameEnd = checkGameWin(grid)
        if didGameEnd == 'player1':
            print '\n Red wins Yayyy!!!'
            player1Win = True
        if didGameEnd == 'player2':
            print '\n Black wins Yuppi!!!'
            player2Win = True
           
        if player1Win != True and player2Win != True:

            
            


                num = random.randint(0,7)
                p2Choice = num - 1
                grid = returnUpdatedRack(p2Choice,grid,p2,blank)
           
                print '\n1 2 3 4 5 6 7\n'
                for y in grid:
                    row = ''
                    for x in y:
                        row += x + ' '
                    print row
                print '\n1 2 3 4 5 6 7\n'
           
                didGameEnd = checkGameWin(grid)
                if didGameEnd == 'player1':
                    print '\nRed wins Yayyy!!!'
                    player1Win = True
                if didGameEnd == 'player2':
                    print '\nBlack wins Yuppi!!!'
                    player2Win = True

You mean this error?

1 2 3 4 5 6 7

- B - - - - - 
- B - - - - - 
R R - - - - - 
R R - - - - - 
B B - - - - - 
R B R - - - - 

1 2 3 4 5 6 7

Enter Black Player move(B) choose 1-7: 2

1 2 3 4 5 6 7


Traceback (most recent call last):
  File "D:\Tony\game.py", line 149, in <module>
    for y in grid:
TypeError: 'NoneType' object is not iterable
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.