From 209fdc7f0581f4f2d940cc2166626fa46c0e47d6 Mon Sep 17 00:00:00 2001 From: oloturia <5429234+oloturia@users.noreply.github.com> Date: Sat, 30 Apr 2022 04:19:26 +0200 Subject: [PATCH] connect4 engine working --- .gitignore | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4_engine.py | 108 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+) create mode 100644 .gitignore create mode 100644 4_engine.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..28fb9d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,132 @@ +TOKEN +DAMA.SECRET + +# 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 + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# 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/ diff --git a/4_engine.py b/4_engine.py new file mode 100644 index 0000000..2a48e72 --- /dev/null +++ b/4_engine.py @@ -0,0 +1,108 @@ +#!/usr/bin/python3 + +def drawChequerboard(status,players=[],toprow="1234567"): + print(toprow) + for row in status: + for cell in row: + if cell == 0: + print("░",end="") + else: + try: + print(players[cell],end="") + except IndexError: + print(str(cell),end="") + print(" ") + +def initChequerboard(cols=7,rows=6): + board = list() + for row in range(rows): + board.append([0]*7) + return board + +def dropChip(board,move_str,player): + failure = (False,0) + try: + move = int(move_str)-1 + except ValueError: + return failure + if move < 0 or move > 6: + return failure + free_space = -1 + for row in board: + if row[move] != 0: + break + else: + free_space += 1 + if free_space == -1: + return failure + board[free_space][move] = player + return board, checkFour(board,free_space,move) + +def checkFour(board,row,col): + offset_pairs = ( ((0,1),(0,-1)) , ((1,0),(-1,0)) , ((1,1),(-1,-1)) , ((-1,1),(1,-1)) ) + points = 0 + for offset in offset_pairs: + count = 0 + count += checkSide(board,row,col,offset[0]) + count += checkSide(board,row,col,offset[1]) + if count >= 4: + points += 1 + return points + + +def checkSide(board,row,col,incs): + incY,incX = incs + count = 0 + player = board[row][col] + offsetX = 0 + offsetY = 0 + while count < 4: + try: + if board[row+offsetX][col+offsetY] == player: + count += 1 + offsetX += incX + offsetY += incY + else: + break + except IndexError: + break + return count + + +if __name__ == "__main__": + board = initChequerboard() + match = True + player = 1 + points_1 = 0 + points_2 = 0 + while match: + drawChequerboard(board) + move = input("Player "+str(player)+" turn:") + if move == "q": + print("quitting") + quit() + valid, point = dropChip(board,move,player) + if not(valid): + print("Invalid move") + else: + board = valid + if player == 2: + points_2 += point + player = 1 + else: + points_1 += point + player = 2 + match = False + for row in board: + for cell in row: + if cell == 0: + match = True + print("Match over") + print("Player 1 scored "+str(points_1)+" points") + print("Player 2 scored "+str(points_2)+" points") + if points_1 > points_2: + print("Player 1 won!") + elif points_2 > points_2: + print("Player 2 won!") + else: + print("Draw!")