DEV Community

Cover image for Python Terminal Game: Minesweeper
Fausto Tapia
Fausto Tapia

Posted on

Python Terminal Game: Minesweeper

As a part of the codecademy course on Computer Science I came up with this approach to code the game "Minesweeper" as a game interactible through the terminal.

The Game class

`class Game:
alive = 1
board_back = [[" " for i in range(9)] for j in range(9)]
board_front = [["#" for i in range(9)] for j in range(9)]
bomb_coor = []
def init(self):
x_coor = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8]
y_coor = [0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8]
for i in range(10):
temp = []
temp.append(x_coor.pop(random.choice(x_coor)))
temp.append(y_coor.pop(random.choice(y_coor)))
self.bomb_coor.append(temp)
self.board_back[temp[0]][temp[1]] = "B"
for i in range(9):
for j in range(9):
if self.board_back[i][j] == "B":
continue
else:
self.board_back[i][j] = str(self.count_bombs(i, j))

def count_bombs(self, x, y):
    count = 0
    for i in range(x-1,x+2):
        for j in range(y-1,y+2):
            if i == x and j == y:
                continue
            elif i < 0 or i > 8 or j < 0 or j > 8:
                continue
            elif self.board_back[i][j] == "B":
                count += 1
            else:
                continue
    return count

def print_board(self, board):
    for i in range(9):
        print(board[i])

def set_flag(self):
    x = int(input("Type horizontal coordinate to flag: "))
    y = int(input("Type vertical coordinate to flag: "))
    self.board_front[y][x] = "F"

def uncover(self):
    x = int(input("Type horizontal coordinate to uncover: "))
    y = int(input("Type vertical coordinate to uncover: "))
    if self.board_back[y][x] == "0":
        self.clear_empty(y, x)
    else:
        self.board_front[y][x] = self.board_back[y][x]

    if self.board_back[y][x] == "B":
        print("You lost")
        self.alive = 0

def clear_empty(self, x, y):
    if self.board_front[x][y] != " " and self.board_back[x][y] == "0":
        self.board_front[x][y] = " "
        for i in range(y-1, y+2, 2):
            if i < 0 or i > 8:
                continue
            self.clear_empty(x, i)
        for j in range(x-1, x+ 2, 2):
            if j < 0 or j > 8:
                continue
            self.clear_empty(j, y)
def verify_game(self):
    if self.alive == 0:
        print("Game Over")
        return 0
    else:
        user_wins = True
        for i in self.bomb_coor:
            if self.board_front[i[0]][i[1]] != "F":
                user_wins = False
                break
        for i in self.board_front:
            if "#" in i:
                user_wins = False
                break
        if user_wins:
            print("You Won!")
            return 0
        else:
            return 1`
Enter fullscreen mode Exit fullscreen mode

This class contains 4 attributes and 7 methods

THE ATTRIBUTES

  1. alive: This value indicates whether the player is alive or dead
  2. board_back: this isn't visible to the player, it is a 8x8 matrix (list of 8 lists of 8 values) or board containig all the info of the board including bomb locations and the number of bombs in surrounding squares.
  3. board_front: this is visible to the player, "#" means the squared hasn't been uncovered.
  4. bomb_coor: a list of pair values indicating the coordinates of all bombs

THE METHODS

  1. init: the constructor of the class defines the bomb_coor array and sets up the board_back matrix.
  2. count_bombs: this method is used in the constructor to count the number of bumbs surrounding a certain square assuming it doesn't have a bomb itself.
  3. print_board: prints a board in the terminal.
  4. set_flag: used to "flag" a certain square when we believe it to have a bomb in board_front.
  5. uncover: used to uncover a obscured square in board_front and reveal its corresponding value in board_back.
  6. clear_empty: used to clear all empty contiguous squares in board_front that correspond to a "0" in board_back.
  7. verify_game: used to check if the game is still going or if it has ended in a Win or a Loss

Example of object usage:
game = Game()
while game.alive:
game.print_board(game.board_back)
print()
game.print_board(game.board_front)
if game.verify_game() == 0:
break
choice = int(input("""
Select one optione:
1.- Flag
2.- Uncover
"""))
if choice == 1:
game.set_flag()
else:
game.uncover()

Code on github: https://github.com/ftapiaa27/MineSweeper.git

CONCLUSION:
This little project serves as a general practice of OOP in python, it's not much but it's hones work

Top comments (0)