Created
April 8, 2017 14:43
-
-
Save joemarchese/4dc2b5ac79f8f75545ab9746b92388f2 to your computer and use it in GitHub Desktop.
Command Line Minesweeper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random as r | |
| def print_grid(input_grid): | |
| for row in input_grid: | |
| print(row) | |
| print() | |
| def nearby_mines(x, y): | |
| """Calculates the number of nearby mines and returns it as an Int""" | |
| if grid[x][y] == 9: | |
| return 9 | |
| # Get a set of all the valid coordinates around a point | |
| else: | |
| index_set = set() | |
| for row in range(-1, 2): | |
| for column in range(-1, 2): | |
| index_set.add((min(5, abs(x + row)), min(5, abs(y + column)))) | |
| # Check the coordinates for mines | |
| count = 0 | |
| for coord in index_set: | |
| if grid[coord[0]][coord[1]] == 9: | |
| count += 1 | |
| return count | |
| # Command-Line Game Logic | |
| def choose_coordinate(): | |
| """Gets an X and Y input from the User and returns them as Ints""" | |
| print('Choose Coordinates') | |
| x, y = (None, None) | |
| while x not in range(6): | |
| x = int(input('X Coordinate (0-5):')) | |
| while y not in range(6): | |
| y = int(input('Y Coordinate (0-5):')) | |
| return x, y | |
| def select_coordinate(): | |
| """Runs choose_coordinate and handles the reveal logic.""" | |
| x, y = choose_coordinate() | |
| player_grid[x][y] = grid[x][y] | |
| if grid[x][y] == 9: | |
| print('Mine! You lose!') | |
| print_grid(grid) | |
| quit() | |
| # elif grid[x][y] == 0: | |
| # Need to add some kind of recursive logic that reveals all the contiguous | |
| # O spaces | |
| def place_flag(): | |
| x, y = choose_coordinate() | |
| player_grid[x][y] = 'F' | |
| return player_grid | |
| def win_check(): | |
| """Checks whether all mines are identified and if so prints a Victory Message.""" | |
| # This is broken somewhere. | |
| for row, col in mine_coordinates: | |
| if player_grid[row][col] != 'F': | |
| break | |
| else: | |
| continue | |
| print("You've identified all the mines. Congratulations, you win!") | |
| quit() | |
| def user_interface(): | |
| """Primary Game Engine""" | |
| print_grid(player_grid) | |
| print('Choose an option:') | |
| print('1: Select Coordinate') | |
| print('2: Place Flag') | |
| choice = None | |
| while choice not in [1, 2]: | |
| choice = int(input()) | |
| if choice == 1: | |
| select_coordinate() | |
| if choice == 2: | |
| place_flag() | |
| win_check() | |
| user_interface() | |
| # Setting the Grid like this for now. Was having aliasing issues with list comprehensions | |
| # Would need to set the first argument in the min() above to the size of the grid. | |
| grid = [[1, 1, 1, 1, 1, 1], | |
| [1, 1, 1, 1, 1, 1], | |
| [1, 1, 1, 1, 1, 1], | |
| [1, 1, 1, 1, 1, 1], | |
| [1, 1, 1, 1, 1, 1], | |
| [1, 1, 1, 1, 1, 1]] | |
| player_grid = [[9, 9, 9, 9, 9, 9], | |
| [9, 9, 9, 9, 9, 9], | |
| [9, 9, 9, 9, 9, 9], | |
| [9, 9, 9, 9, 9, 9], | |
| [9, 9, 9, 9, 9, 9], | |
| [9, 9, 9, 9, 9, 9]] | |
| # Set the Mines | |
| # Note, this can yield less than 6 mines if it selects the same indices. | |
| # Using an 9 as a Mine because the printing is prettier and because 8 is the max. | |
| mine_coordinates = set() | |
| for mine in range(6): | |
| x, y = (r.randint(0, 5), r.randint(0, 5)) | |
| mine_coordinates.add((x, y)) | |
| grid[x][y] = 9 | |
| # Test Print | |
| print_grid(grid) | |
| # Calculate the Nearby Mines with nearby_mines on each coordinate and set them. | |
| for row in range(6): | |
| for column in range(6): | |
| grid[row][column] = nearby_mines(row, column) | |
| # Test Prints | |
| print_grid(grid) | |
| # Test Mine Coordinates | |
| print(mine_coordinates) | |
| # Command Line Game Test | |
| user_interface() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment