Posts: 7
Threads: 2
Joined: Nov 2018
For some reason, I cannot get this to work and am fed up! :(
What I am really having trouble with is part 2 and 3. There are the instructions of the game I need to create, https://pastebin.com/teyRTi2p
This is what I got so far.
import random
GRIDSIZE=7
def initMines():
grid=[]
for i in range(GRIDSIZE):
grid.append([])
for j in range(GRIDSIZE):
minechance=random.randint(1,11)
if minechance==1:
grid[i].append('x')
else:
grid[i].append('')
return grid
def initBoard():
gridBoard=[]
for i in range(GRIDSIZE):
gridBoard.append([])
for j in range(GRIDSIZE):
gridBoard[i].append('#')
return gridBoard
def displayBoard(grid):
output=''
output+=" |"
for n in range(GRIDSIZE):
output+=str(n)
output+='\n'
for o in range(GRIDSIZE):
output+="--"
output+='\n'
count=0
for lis in grid:
output+=str(count) +"|"
for e in lis:
output+=str(e)
count+=1
output+='\n'
print(output)
return output
def countHiddenCells(grid): #working
hiddenCells = 0
for i in range(GRIDSIZE):
for j in range(GRIDSIZE):
if grid[i][j] == '#':
hiddenCells+= 1
return hiddenCells
def countMInes(grid): #working
hiddenMIn = 0
for i in range(GRIDSIZE):
for j in range(GRIDSIZE):
if grid[i][j] == 'x':
hiddenMIn+= 1
return hiddenMIn
def isMineAt(grid, row, column):
mineat = False
if grid[row][column] == 'x':
mineat = True
return mineat
def countMinesAround(Mgrid,row,column):
mines=0
if Mgrid[row-1][column-1] == 'x':
mines+=1
elif Mgrid[row-1][column] == 'x':
mines+=1
elif Mgrid[row-1][column+1] == 'x':
mines+=1
elif Mgrid[row][column-1] == 'x':
mines+=1
elif Mgrid[row][column+1] == 'x':
mines+=1
elif Mgrid[row+1][column-1] == 'x':
mines+=1
elif Mgrid[row+1][column] == 'x':
mines+=1
elif Mgrid[row+1][column+1] == 'x':
mines+=1
return mines
def testdoc():
userChoice= 2,2
displayBoard(initBoard())
countHiddenCells(initBoard())
countMInes(initMines())
isMineAt(initMines(), userChoice)
countMinesAround(initMines(),userChoice)
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 column>GRIDSIZE:
if grid[row][column] != 'x':
continue
elif grid[row][column] == 'x':
print("Game Over!")
result = True
continue
else:
print("You Win!")
result = True
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'
elif mineGrid[row][column]==' ' and num ==0:
for i in range(GRIDSIZE):
for j in range(GRIDSIZE):
if mineGrid[i][j]==' ':
grid[i][j]='/'
else:
for i in range(GRIDSIZE):
for j in range(GRIDSIZE):
if mineGrid[i][j]==' ':
grid[i][j]=str(num)
return grid
'''-blank " " spaces
-reveals the amount of bombs around where the user picked
-if user selects a bomb, reveals all other bombs
-search the mine grid for 'x' and replace every 'x' with the
same position in the grid'''
x= random.randint(1,GRIDSIZE-2)
y= random.randint(1,GRIDSIZE-2)
#print(reveal(initBoard(),initMines(),x,y))
displayBoard(reveal(initBoard(),initMines(),x,y))
Posts: 3,458
Threads: 101
Joined: Sep 2016
Quote:The rules for the game are as follows:
The computer generates a 2D grid, and places several secret mines into it.
The player then guesses locations on the grid, trying to guess every location except the ones with mines.
If the player guesses a location that's already revealed, nothing happens.
If the player guesses a location that has a mine, the game is over and the player loses.
If the player guesses a location that does not have a mine, the computer reveals how many mines are in adjacent locations on the grid. This number is displayed on the grid at the location the user guessed.
If there are zero mines in adjacent locations the computer reveals a blank space, and then subsequently reveals all adjacent cells according to these rules as well. (I.e., If any of those locations are also adjacent to zero mines, they trigger a similar reaction to their adjacent cells.)
Once the user has revealed all locations except for the mines, they win!
Below, the game development is divided into parts to aid your process. Read through all of it before beginning. You are free to build the program in any order you see fit.
Part 1: Helper functions
It is recommended that you begin your game by writing and testing the following functions. You can create any additional helper functions you might need, but the functions listed here are required components in your solution for full marks. Your solution should not depend on any imported modules other than random (and optionally math).
initMines() - This function takes zero arguments and returns a grid (i.e. a 2D list) with randomly placed mines. The size of the grid should be a global constant called GRIDSIZE (where both the number of rows and columns equal GRIDSIZE). The grid will be filled with either empty strings ("") or mines ("x"). For each cell in the grid there should be a 1 in 10 chance of placing a mine. (If you would like, this too can be a global constant making it a 1 in MINECHANCE chance of placing a mine. For submission, please set this constant to 10).
initBoard() - This function takes zero arguments and returns a grid (ie. a 2D list) representing the player's game board. Initially all cells in the game board should be unrevealed ("#"). The size of the game board should be the same as the size of the grid of mines.
displayBoard() - This function takes a 2D grid representing the player's game board as argument and prints it to the screen. The printout of the board should include row and column labels (see examples below). You may assume that GRIDSIZE will always be a number in the range [2-10].
countHiddenCells() - This function takes a 2D grid representing the player's game board as argument and returns the number of cells in the board that are unrevealed ("#"). Initially this will be all of them, but as the game progresses this value should get smaller and smaller with each turn.
countMines() - This function takes a 2D grid representing the minefield as argument and returns the number of cells in the grid that contain mines. This value should not change once the minefield is initialized.
isMineAt() - This function takes as arguments, a 2D grid representing the minefield and two integers representing a row and column respectively, and returns True if the cell indicated by the row and column contains a mine in the grid, and False otherwise. This function should validate the inputted row and column values to ensure that they are on the gameboard, and return False if they are not.
countMinesAround() - This function takes as arguments, a 2D grid representing the minefield and two integers representing a row and column respectively, and returns the number of mines in adjacent cells. Adjacent cells are the 8 cells surrounding the provided cell, including diagonals, as illustrated in the figure to the right.
Part 2: Game loop
Next build the main() function and game loop.
The main() function should initialize two grids representing the minefield and player's gameboard respectively.
The game loop should repeat the following actions each round, until the user wins or loses:
Get the user's move
Check if they lost
If not, reveal the cell (see part 3)
Display the updated game board
The user should be prompted to input two integers in a single prompt, separated by a comma. (e.g. > 3,4)
You may not assume that the user will enter good input. Any input that does not consist of two integers separated by a comma should result in the prompt repeating. Integers that are too big or small for the gameboard, can either be rejected as bad input, or accepted as valid input, but then subsequently have no effect on the game.
If the user loses, the location of all mines should be revealed and displayed, accompanied by a "Game Over!" message.
If the user wins, a congratulatory "You Win!!" message should be displayed.
Part 3: Reveal()
Write a recursive method called reveal() that takes as arguments: the player's gameboard, the grid of mines, and the user's choice (row & column values). This function should reveal the selected cell on the user's gameboard according to the rules of the game as laid out in the instructions above. Some additional notes:
Cells that are revealed as being blank should be displayed as empty spaces (" ").
The function should do input validation on the provided row and column values to verify that they indicate a valid location on the board. (If not, the function should do nothing).
The function should not need to return anything since its work is done in mutating the player's gameboard.
I don't like pastebins lol. Haven't read it yet, so I don't have an otherwise meaningful reply.
Posts: 3,458
Threads: 101
Joined: Sep 2016
For future reference, please share the error message you're getting. If there isn't one, share your input/output, and what your expected output is. Personally, I don't like having to guess where you currently need help.
That said, part 2 is about having a main loop. Which you wrote ( main() , line 94), but you never call it. So the very last line of your file should be main() , immediately after you displayBoard() .
For reference to others, here's the main function: 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 column>GRIDSIZE:
if grid[row][column] != 'x':
continue
elif grid[row][column] == 'x':
print("Game Over!")
result = True
continue
else:
print("You Win!")
result = True
break
else:
print("Invalid input") Once you start using it, though, you'll quickly find another issue: your input validation is off. You don't allow for row to be zero, so the user could never pick the first row (or, if you want to use 1-indexing, you should relabel the grid since it starts at 0). Also, requiring column to be greater than the GRIDSIZE , means that it'd need to be at least 8, which means every value you'd consider valid will always throw and IndexError.
Posts: 7
Threads: 2
Joined: Nov 2018
I don't know how to do the reveal function.
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Dec-03-2018, 01:35 AM)pythonlearnery Wrote: 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'
I don't know what output you're getting, or how that's different from what you're expecting, so all I can do is guess. It looks like the beginning of this function is checking if the user picked a mine, and then it shows all mines on the board. Is that what it's supposed to do?
Posts: 7
Threads: 2
Joined: Nov 2018
Dec-04-2018, 07:48 PM
(This post was last modified: Dec-04-2018, 08:01 PM by pythonlearnery.)
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()
Posts: 3,458
Threads: 101
Joined: Sep 2016
(Dec-04-2018, 07:48 PM)pythonlearnery Wrote: else:
print("You Win!")
result = True
displayBoard(createBoard())
I think there's a fundamental issue with the way you're organizing things. If initBoard() creates a new board everytime it's called, using random, then why would you call it more than one time in your code? createBoard() looks like it's being used as if it takes a board, and returns something that can be displayed, and yet it doesn't take a board, it just returns a blank board every time, without regard to what the user did previously.
Posts: 7
Threads: 2
Joined: Nov 2018
(Dec-04-2018, 09:05 PM)nilamo Wrote: (Dec-04-2018, 07:48 PM)pythonlearnery Wrote: else:
print("You Win!")
result = True
displayBoard(createBoard())
I think there's a fundamental issue with the way you're organizing things. If initBoard() creates a new board everytime it's called, using random, then why would you call it more than one time in your code? createBoard() looks like it's being used as if it takes a board, and returns something that can be displayed, and yet it doesn't take a board, it just returns a blank board every time, without regard to what the user did previously.
I totally get what you are saying, kind of. But have no idea how to fix it, I barely even get this assignment, useless game!
|