Skip to content

add ConsoleSnake and ConsoleMinesweeper #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ConsoleMinesweeper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/venv
__pycache__
.main.py.swp
674 changes: 674 additions & 0 deletions ConsoleMinesweeper/LICENSE

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions ConsoleMinesweeper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ConsoleMinesweeper

python script to play the classic minesweeper game in your console.

## How to play

input the row and column in the following form:
``` row,col ```
this will dig in the specified place.

if you want to mark a spot you know there is a bomb,
put an 'm' following the row and column, like this:
``` row,colm ```

press 'h' for help in-game and 'q' to quit.
199 changes: 199 additions & 0 deletions ConsoleMinesweeper/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
from random import randint, choice
import os
import re


class MineField:
def __init__(self, size, n_bombs):
self.size = size
self.digged = 0
self.n_bombs = n_bombs
self.grid = [[0 for j in range(size)] for i in range(size)]
self.grid_mask = [['·' for j in range(size)] for i in range(size)]
self.plant_bombs()
# self.assign_numbers()
self.first_dig()

def __str__(self):

_str = ' ';

for i in range(1, self.size+1):
_str += str(i) + ' '
if i < 10:
_str += ' '
_str += '\n'

_str += ' ' + '―' * (self.size * 3 + 3) + '\n'

for i, row in enumerate(self.grid_mask, 1):
if i < 10:
_str += ' '
_str += str(i) + '| '
for char in row:
_str += str(char) + ' '
_str += '\n'
return _str

def plant_bombs(self):
bombs_planted = 0
while bombs_planted < self.n_bombs:
row = randint(0, self.size - 1)
column = randint(0, self.size - 1)

if self.grid[row][column] == '*':
continue

self.grid[row][column] = '*'

self.fill_neighbors(row, column)

bombs_planted += 1

def fill_neighbors(self, row, column):
if row != 0:
if column != 0:
if self.grid[row-1][column-1] != '*':
self.grid[row-1][column-1] += 1

if self.grid[row-1][column] != '*':
self.grid[row-1][column] += 1

if column != self.size-1:
if self.grid[row-1][column+1] != '*':
self.grid[row-1][column+1] += 1

if column != 0:
if self.grid[row][column-1] != '*':
self.grid[row][column-1] += 1

if column != self.size-1:
if self.grid[row][column+1] != '*':
self.grid[row][column+1] += 1

if row != self.size-1:
if column != 0:
if self.grid[row+1][column-1] != '*':
self.grid[row+1][column-1] += 1

if self.grid[row+1][column] != '*':
self.grid[row+1][column] += 1

if column != self.size-1:
if self.grid[row+1][column+1] != '*':
self.grid[row+1][column+1] += 1

def first_dig(self):
zeros = []
for row in range(self.size):
for column in range(self.size):
if self.grid[row][column] == 0:
zeros.append((row, column))

spot_to_dig = choice(zeros)
self.dig(spot_to_dig[0], spot_to_dig[1])

def dig(self, row, col):
spot = self.grid[row][col]
if spot == '*':
return False
elif spot != self.grid_mask[row][col]:
self.grid_mask[row][col] = spot
self.digged += 1
if (spot == 0):
self.clear_zeros(row, col)
return True

def clear_zeros(self, row, col):
if row != 0:
if col != 0:
self.dig(row-1, col-1)

self.dig(row-1, col)

if col != self.size-1:
self.dig(row-1, col+1)

if col != 0:
self.dig(row, col-1)

if col != self.size-1:
self.dig(row, col+1)

if row != self.size-1:
if col != 0:
self.dig(row+1, col-1)

self.dig(row+1, col)

if col != self.size-1:
self.dig(row+1, col+1)

def mark_spot(self, row, col):
spot_mask = self.grid_mask[row][col]
if spot_mask == 'X':
self.grid_mask[row][col] = '·'
elif spot_mask == '·':
self.grid_mask[row][col] = 'X'

def show_bombs(self):
for row in range(self.size):
for column in range(self.size):
if self.grid[row][column] == '*':
self.grid_mask[row][column] = '*'


def print_help():
print('――――― Console Minesweeper ―――――')
print('To dig in a spot, input the row\nand the column you want to dig in.')
print('For example, if I wanted to dig\nin the row 2, column 4, I would\ntype: 2,4')
print('―――――――――――――――――――――――――――――――――')
print('If you just want to mark a spot\nwhere you know is a bomb, type the\ncoordinates followed with an "m".\nExample: 1,4m')
print('\n')


def play(field):
pattern = re.compile(r"^[0-9]+,[0-9]+m?$")
os.system('cls' if os.name == 'nt' else 'clear')
print(field)
move = 'h'
alive = True
while alive:

move = input("Your move ('h' for help, 'q' to quit): ")
os.system('cls' if os.name == 'nt' else 'clear')

if move == 'h':
print_help()
elif move == 'q':
print('Bye!\n')
alive = 'bye'
break
elif re.fullmatch(pattern, move):
row, column= [int(i)-1 for i in move.strip('m').split(',')]
if (row > field.size) |( column > field.size):
print('Invalid coordinates\n')
elif move[-1] == 'm':
field.mark_spot(row, column)
else:
alive = field.dig(row, column)
if field.digged == (field.size**2 - field.n_bombs):
break
else:
print('Invalid input\n')

print(field)

if alive:
if alive == True:
print(field)
print("You won, congratulations!!!")
else:
os.system('cls' if os.name == 'nt' else 'clear')
field.show_bombs()
print(field)
print("You lost :(")


field = MineField(10, 10)
play(field)
125 changes: 125 additions & 0 deletions ConsoleSnake/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
21 changes: 21 additions & 0 deletions ConsoleSnake/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 tomimara52

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions ConsoleSnake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ConsoleSnake
Snake game in your windows, linux or macOS console
Loading