Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**I\'m creating a Minesweeper game and I don\'t know how to connect user input (

ID: 3837542 • Letter: #

Question

**I'm creating a Minesweeper game and I don't know how to connect user input (make_move) to the Board. (I commented out some of my ideas but I'm not sure)**


----------

import random
import time # new module

# creating two classes for indivadual cells and board properties

class Cell(object):
"""a class to deal with indivadual cells"""
def __init__(self, can_see,flagged, is_mine): #initializing Cell class
self.can_see = not can_see # whether cell is visible or not
self.flagged = not flagged #whether cell is flagged or not
self.is_mine = is_mine #whether cell is a mine or not

def show(self):
self.can_see = True # if you want user to see cell

def flag(self):
self.flagged = not self.flagged # not self bc it starts as False

def put_mine(self):
self.is_mine = True #if it is a mine place it in grid

class Board(object):
"""class that creates the playing board"""

def __init__(self,size, mines):
self.playing = True
self.size = size
self.make_board(size, mines)
self.mines = mines

def __str__(self):
boardstr = ("MINES: " + str(self.mines) + " ")
for (row_coord,row) in enumerate(self.grid):
boardstr += " " + str(row_coord+1) #writing coordinates on bottom
for (col_coord,cell) in enumerate(row):
if not cell.can_see: # .can_see is initalized as FALSE
if cell.is_mine:
boardstr += "*"
else:
boardstr += str(self.count_surroundings(row_coord,col_coord)) #number indicators
elif cell.flagged:
boardstr += "f"
else:
boardstr += "X"
boardstr += " "
boardstr += " " + "".join([str(i) for i in range(len(self.grid)+1)]) #writing numbers on bottom
return boardstr

def make_board(self, mines, size):
"""function that uses my created Board and Cell methods to create the playing board"""
self.grid = [[Cell(False , True ,False) for i in range(size)]
for j in range(size)]
#row = 0
#col = 0
#trying to connect user input and grid???? AHHH
#cell_at_row_col = self.grid[row][col]
open_pos = list(range(size-1)*(size-1))
for i in range(mines):
new_pos = random.choice(open_pos) #randomly sel]ect from open positions
open_pos.remove(new_pos) # take that new position out of the open one
(row_coord,col_coord) = (new_pos % 9, new_pos // 9) # mod and floor div
self.grid[row_coord][col_coord].put_mine()

def mine_rep(self,row_coord,col_coord): #row_coord = row coordinate and same for collumn
#figuring out whats a mine
cell = self.grids[row_coord][col_coord]
if cell.can_see:
if cell.is_mine:
return "M"
else:
show_surroundings = self.count_surroundings(row_coord,col_coord)
return str(show_surroundings)
elif cell.flagged:
return "F"
else:
return "X"

def show(self,row_coord,col_coord):
#figuring out what to show
cell = self.grid[row_coord][col_coord]
if not cell.can_see:
cell.show()
if (cell.is_mine and not cell.flagged):
self.playing = False #stop playing, user loses
elif self.count_surroundings(row_coord,col_coord) == 0:
for (surr_row,surr_col) in self.find_neighbors(row_coord,col_coord):
if self.is_in_range(surr_row,surr_col):
self.show(surr_row,surr_col) #show the number indicators


def count_surroundings(self,row_coord,col_coord):
count = 0
for (surr_row,surr_col) in self.find_neighbors(row_coord,col_coord):
if (self.is_in_range(surr_row,surr_col) and self.grid[surr_row][surr_col].is_mine):
count += 1
return count

def is_solved(self):
for row in self.grid:
for cell in row:
if not cell.can_see or not cell.flagged:
return False
return True


def put_mine(self, row_coord,col_coord):
self.grid[row_coord][col_coord].put_mine()


def find_neighbors(self, row_coord,col_coord):
return (self.cells[row + x][col + y] for x, y in product((-1, 0, 1), repeat=2) if (x, y) != (0, 0) and 0 <= row + x <= self.width and 0 <= col + y <= self.height)

def flag(self, row_coord, col_coord):
if not self.grid[row_coord][col_coord].can_see:
self.grid[row_coord][col_coord].flag()
else:
print("CANNOT FLAG")

def is_in_range(self,row_coord,col_coord):
return 0 <= row_coord < len(str(self.size % 9)) and 0 <= col_coord < len(str(self.size // 9))

#begin creating FUNCTIONS

def make_move(board):
"""This function gets the players next move """
move = raw_input("Pick a Cell: ")
while not is_valid(move,board):
move = raw_input("ERROR: Invalid move. Pick a cell:")
m = tuple(move) # makes input into a list
return m

def is_valid(move,size):
""" This function is a helper function for make_move and checks if the users move is a valid move or not"""
try: #error handling
if move[0].isalpha() or move[1].isalpha(): #if move is a letter
return False
if (len(move) == 3 and move[2] != "f"): #if user tries to flag with anything other than "f"
return False
except IndexError:
return True
return True

**Main is a work in progress, conditionals aren't ready to be set up set until I sort out everything else**

def main():
""" puts all functions together"""
print
print ("MINESWEEPER")
print
print ("To pick a cell enter the collumn number then the row number.")
print ("To flag a cell enter f after coordinates.")
print

size = 9
mines = 10
grid = Board(size=size, mines= mines)
print grid
print
move = make_move(grid)
print move
start_t = time.time()
if not move.flagged:
grid.show(row_coord,column_coord)
else:
grid.flag(row_coord,column_coord)
if not grid.playing and not grid.is_solved or win_count == 0:
end_t = time.time()
elapsed_t = end_t - start_t
print "YOU LOSE"
print "TIME:" , elapsed_t
if not grid.playing and grid.is_solved:
end_t = time.time()
elapsed_t = end_t - start_t
print "YOU WIN"
print "TIME:", elapsed_t


if __name__ == "__main__": #runs automatically
main()

Explanation / Answer

import random
import time # new module

# creating two classes for indivadual cells and board properties

class Cell(object):
"""a class to deal with indivadual cells"""
def __init__(self, can_see,flagged, is_mine): #initializing Cell class
self.can_see = not can_see # whether cell is visible or not
self.flagged = not flagged #whether cell is flagged or not
self.is_mine = is_mine #whether cell is a mine or not

def show(self):
self.can_see = True # if you want user to see cell

def flag(self):
self.flagged = not self.flagged # not self bc it starts as False

def put_mine(self):
self.is_mine = True #if it is a mine place it in grid

class Board(object):
"""class that creates the playing board"""

def __init__(self,size, mines):
self.playing = True
self.size = size
self.make_board(size, mines)
self.mines = mines

def __str__(self):
boardstr = ("MINES: " + str(self.mines) + " ")
for (row_coord,row) in enumerate(self.grid):
boardstr += " " + str(row_coord+1)
for (col_coord,cell) in enumerate(row):
if not cell.can_see: # .can_see is initalized as FALSE
if cell.is_mine:
boardstr += "*"
else:
boardstr += str(self.count_surroundings(row_coord,col_coord))
elif cell.flagged:
boardstr += "f"
else:
boardstr += "X"
boardstr += " "
boardstr += " " + "".join([str(i) for i in range(len(self.grid)+1)])

def make_board(self, mines, size):
"""function that uses my created Board and Cell methods to create the playing board"""
self.grid = [[Cell(False , True ,False) for i in range(size)]
for j in range(size)]

open_pos = list(range(size-1)*(size-1))
for i in range(mines):
new_pos = random.choice(open_pos)
open_pos.remove(new_pos)
(row_coord,col_coord) = (new_pos % 9, new_pos
self.grid[row_coord][col_coord].put_mine()

def mine_rep(self,row_coord,col_coord): #row_coord = row coordinate and same for collumn
#figuring out whats a mine
cell = self.grids[row_coord][col_coord]
if cell.can_see:
if cell.is_mine:
return "M"
else:
show_surroundings = self.count_surroundings(row_coord,col_coord)
return str(show_surroundings)
elif cell.flagged:
return "F"
else:
return "X"

def show(self,row_coord,col_coord):
#figuring out what to show
cell = self.grid[row_coord][col_coord]
if not cell.can_see:
cell.show()
if (cell.is_mine and not cell.flagged):
self.playing = False #stop playing, user loses
elif self.count_surroundings(row_coord,col_coord) == 0:
for (surr_row,surr_col) in self.find_neighbors(row_coord,col_coord):
if self.is_in_range(surr_row,surr_col):
self.show(surr_row,surr_col) #show the number indicators


def count_surroundings(self,row_coord,col_coord):
count = 0
for (surr_row,surr_col) in self.find_neighbors(row_coord,col_coord):
if (self.is_in_range(surr_row,surr_col) and self.grid[surr_row][surr_col].is_mine):
count += 1
return count

def is_solved(self):
for row in self.grid:
for cell in row:
if not cell.can_see or not cell.flagged:
return False
return True


def put_mine(self, row_coord,col_coord):
self.grid[row_coord][col_coord].put_mine()


def find_neighbors(self, row_coord,col_coord):
return (self.cells[row + x][col + y] for x, y in product((-1, 0, 1), repeat=2) if (x, y) != (0, 0) and 0 <= row + x <= self.width and 0 <= col + y <= self.height)

def flag(self, row_coord, col_coord):
if not self.grid[row_coord][col_coord].can_see:
self.grid[row_coord][col_coord].flag()
else:
print("CANNOT FLAG")

def is_in_range(self,row_coord,col_coord):
return 0 <= row_coord < len(str(self.size % 9)) and 0 <= col_coord < len(str(self.size // 9))

def make_move(board):
"""This function gets the players next move """
move = raw_input("Pick a Cell: ")
while not is_valid(move,board):
move = raw_input("ERROR: Invalid move. Pick a cell:")
m = tuple(move) # makes input into a list
return m

try: #error handling
if move[0].isalpha() or move[1].isalpha(): #if move is a letter
return False
if (len(move) == 3 and move[2] != "f"):
except IndexError:
return True
return True

def main():
""" puts all functions together"""
print
print ("MINESWEEPER")
print
print ("To pick a cell enter the collumn number then the row number.")
print ("To flag a cell enter f after coordinates.")
print

size = 9
mines = 10
grid = Board(size=size, mines= mines)
print grid
print
move = make_move(grid)
print move
start_t = time.time()
if not move.flagged:
grid.show(row_coord,column_coord)
else:
grid.flag(row_coord,column_coord)
if not grid.playing and not grid.is_solved or win_count == 0:
end_t = time.time()
elapsed_t = end_t - start_t
print "YOU LOSE"
print "TIME:" , elapsed_t
if not grid.playing and grid.is_solved:
end_t = time.time()
elapsed_t = end_t - start_t
print "YOU WIN"
print "TIME:", elapsed_t