Python Forum
Need help please, mostly done, not working.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help please, mostly done, not working.
#6
I seriously have no idea why this is not working, could anybody please help me out, I would greatly appreciate it, it is not displaying the board correctly or showing the changes... :/

import random

GRIDSIZE = 9

def initMines(): #working
    grid = []
    #creates the inner lists
    for i in range(GRIDSIZE):
        grid.append([])
        for j in range(GRIDSIZE):
            minechance = random.randint(1,101)
            if minechance <= 10:
                grid[i].append('x')
            else:
                grid[i].append('')
    return grid
#initMines()

def initBoard(): #working
    pass
    grid = []
    for i in range(GRIDSIZE):
        grid.append([])
        for j in range(GRIDSIZE):
            grid[i].append('#')
    return grid
#initBoard()

def createBoard():
    output=''
    output+=" |"
    for n in range(GRIDSIZE):
        output+=str(n)
    output+='\n'
    for o in range(GRIDSIZE):
        output+="--"
    output+='\n'
    for row in range(GRIDSIZE):
        output+=str(row) +"|"
        for col in range(GRIDSIZE):
            output+='#'
        output+='\n'
    return output

#takes the board previously created in the createBoard funtion and displays it to the user
def displayBoard(board): #working
    print(board)
displayBoard(createBoard())

def countHiddenCells(grid): #working
    hiddenCells = 0
    for i in range(GRIDSIZE):
        for j in range(GRIDSIZE):
            if grid[i][j] == '#':
                hiddenCells+= 1
    #print(hiddenCells)
    return hiddenCells
countHiddenCells(initBoard())

#this funtion counts how many mines are present in the grid by going throught the nested list
def countMines(grid): #working
    mines = 0
    for i in range(GRIDSIZE):
        for j in range(GRIDSIZE):
            if grid[i][j] == 'x':
                mines+= 1
    #print(mines)
    return mines
countMines(initMines())

def isMineAt(grid, row, column): #working
    mine = False
    if grid[row][column] == 'x':
        mine = True
    #print(mine)
    return mine
isMineAt(initMines(), 2, 2)

def countMinesAround(grid, row, column): #working
    minesAround = 0
    if grid[row-1][column-1] == 'x':
        minesAround+= 1
    if grid[row-1][column] == 'x':
        minesAround+= 1
    if grid[row-1][column+1] == 'x':
        minesAround+= 1
    if grid[row][column-1] == 'x':
        minesAround+= 1
    if grid[row][column] == 'x':
        minesAround+= 1
    if grid[row+1][column+1] == 'x':
        minesAround+= 1
    if grid[row+1][column] == 'x':
        minesAround+= 1
    if grid[row-1][column+1] == 'x':
        minesAround+= 1
    #print(mines)
    return minesAround
countMinesAround(initMines(), 3, 3)

def main():
    grid = initMines()
    #true represents that there is no mine
    result = False
    mineField = initMines()
    playerboard = initBoard()
    while result == False:
        row,column = input("Select a cell (row,column) ").split(',')
        row,column = int(row), int(column)
        if row>0 and row<GRIDSIZE and column>0 and column<GRIDSIZE:
            if grid[row][column] != 'x':
                displayBoard(createBoard())
                continue
            elif grid[row][column] == 'x':
                print("Game Over!")
                result = True
                displayBoard(createBoard())
                continue
            else:
                print("You Win!")
                result = True
                displayBoard(createBoard())
                break
        else:
            print("Invalid input")

def reveal(grid,mineGrid,row,column):
    '''userChoice'''
    #row,column=userChoice
    row,column = int(row), int(column)
    num =countMinesAround(initMines(),row,column)
    if mineGrid[row][column]=='x':
        for i in range(GRIDSIZE):
            for j in range(GRIDSIZE):
                if mineGrid[i][j]=='x':
                    grid[i][j]='x'
main()

I seriously have no idea why this is not working, could anybody please help me out, I would greatly appreciate it, it is not displaying the board correctly or showing the changes... :/

import random

GRIDSIZE = 9

def initMines(): #working
    grid = []
    #creates the inner lists
    for i in range(GRIDSIZE):
        grid.append([])
        for j in range(GRIDSIZE):
            minechance = random.randint(1,101)
            if minechance <= 10:
                grid[i].append('x')
            else:
                grid[i].append('')
    return grid
#initMines()

def initBoard(): #working
    pass
    grid = []
    for i in range(GRIDSIZE):
        grid.append([])
        for j in range(GRIDSIZE):
            grid[i].append('#')
    return grid
#initBoard()

def createBoard():
    output=''
    output+=" |"
    for n in range(GRIDSIZE):
        output+=str(n)
    output+='\n'
    for o in range(GRIDSIZE):
        output+="--"
    output+='\n'
    for row in range(GRIDSIZE):
        output+=str(row) +"|"
        for col in range(GRIDSIZE):
            output+='#'
        output+='\n'
    return output

#takes the board previously created in the createBoard funtion and displays it to the user
def displayBoard(board): #working
    print(board)
displayBoard(createBoard())

def countHiddenCells(grid): #working
    hiddenCells = 0
    for i in range(GRIDSIZE):
        for j in range(GRIDSIZE):
            if grid[i][j] == '#':
                hiddenCells+= 1
    #print(hiddenCells)
    return hiddenCells
countHiddenCells(initBoard())

#this funtion counts how many mines are present in the grid by going throught the nested list
def countMines(grid): #working
    mines = 0
    for i in range(GRIDSIZE):
        for j in range(GRIDSIZE):
            if grid[i][j] == 'x':
                mines+= 1
    #print(mines)
    return mines
countMines(initMines())

def isMineAt(grid, row, column): #working
    mine = False
    if grid[row][column] == 'x':
        mine = True
    #print(mine)
    return mine
isMineAt(initMines(), 2, 2)

def countMinesAround(grid, row, column): #working
    minesAround = 0
    if grid[row-1][column-1] == 'x':
        minesAround+= 1
    if grid[row-1][column] == 'x':
        minesAround+= 1
    if grid[row-1][column+1] == 'x':
        minesAround+= 1
    if grid[row][column-1] == 'x':
        minesAround+= 1
    if grid[row][column] == 'x':
        minesAround+= 1
    if grid[row+1][column+1] == 'x':
        minesAround+= 1
    if grid[row+1][column] == 'x':
        minesAround+= 1
    if grid[row-1][column+1] == 'x':
        minesAround+= 1
    #print(mines)
    return minesAround
countMinesAround(initMines(), 3, 3)

def main():
    grid = initMines()
    #true represents that there is no mine
    result = False
    mineField = initMines()
    playerboard = initBoard()
    while result == False:
        row,column = input("Select a cell (row,column) ").split(',')
        row,column = int(row), int(column)
        if row>0 and row<GRIDSIZE and column>0 and column<GRIDSIZE:
            if grid[row][column] != 'x':
                displayBoard(createBoard())
                continue
            elif grid[row][column] == 'x':
                print("Game Over!")
                result = True
                displayBoard(createBoard())
                continue
            else:
                print("You Win!")
                result = True
                displayBoard(createBoard())
                break
        else:
            print("Invalid input")

def reveal(grid,mineGrid,row,column):
    '''userChoice'''
    #row,column=userChoice
    row,column = int(row), int(column)
    num =countMinesAround(initMines(),row,column)
    if mineGrid[row][column]=='x':
        for i in range(GRIDSIZE):
            for j in range(GRIDSIZE):
                if mineGrid[i][j]=='x':
                    grid[i][j]='x'
main()
Reply


Messages In This Thread
RE: Need help please, mostly done, not working. - by pythonlearnery - Dec-04-2018, 07:48 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020