Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/CogitoNTNU/TetrisAI into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
henrinha committed Mar 19, 2024
2 parents a0894ca + a7e0774 commit 612991a
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 46 deletions.
41 changes: 0 additions & 41 deletions .devcontainer/devcontainer.json

This file was deleted.

File renamed without changes.
25 changes: 25 additions & 0 deletions docs/guide/venv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Development Environment

We will be utilizing a virtual environment to develop the project. This will allow us to have a consistent development environment across all developers.In this case we will be using `venv` to create the virtual environment.

## Getting Started

* Have [Python](https://www.python.org/downloads/) installed
* Verify that python is installed by running `python --version`
* Pip install the virtual environment package
* Verify that pip is installed by running `pip --version`
* Install the virtual environment package by running `pip install virtualenv`
* In the root of the project, create a virtual environment by running `python -m venv .venv`
* Activate the virtual environment
* On Windows, run `.venv\Scripts\activate`
* On Mac/Linux, run `source .venv/bin/activate`
* Done! You should now have a fully functional development environment
* To deactivate the virtual environment, run `deactivate`

## Dependencies
Once you have entered venv you need to make sure the dependencies are installed by running `pip install -r requirements.txt`.
If you use a new pip dependency, make sure to add it to the `requirements.txt` file. This can be done by running:
```bash
pip freeze > requirements.txt
```
after you pip installed it locally, and then committing the changes.
14 changes: 14 additions & 0 deletions docs/planning/12.03.2024.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Cogito work night nr. 3

## Agenda

- Venv
- [Mer klarere plan for Tetris](#tetris)
- Progge
- Hva skjer neste uke?


## Tetris
- **Headless:** ingen grafikk ting skjer sekvensielt. En viss boardstate og en ny block -> velger presist hvor den skal plasseres (blant mulige plasser) uten tidsbegrensning.
For development purpuses har vi en to print outs av brettet. Første viser brettet med den uplasserte blokken (øverst i midten), andre viser brettet etter at blokken er plassert. Bruker de objektene som vi har blitt enige om. Tanken er at vi kan bruke dette til å teste ut forskjellige algoritmer for å plassere blokken (ai/algorytme velger posisjonen).
- **Grafisk:** pygame. Adapsjon av samme objekter som headless bare at vi nå styrer hvor blokken skal plasseres dynamisk. Blokken faller nedover med en viss hastighet og vi kan flytte den rundt med en viss hastighet (feks. et tastetrykk = forflytter blokken en rute). For å la agenten spille må vi lage et oversettelses lag mellom headless og grafisk, hvor vi kan sende input til grafisk for å manuvrere blokken til samme posisjon som headless ville plassert den.
122 changes: 117 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,122 @@
anyio==4.0.0
appdirs==1.4.4
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
arrow==1.3.0
asttokens==2.4.0
async-lru==2.0.4
attrs==23.1.0
Babel==2.13.0
backcall==0.2.0
beautifulsoup4==4.12.2
bleach==6.1.0
certifi==2023.7.22
cffi==1.16.0
charset-normalizer==3.3.1
click==8.1.7
cloudpickle==3.0.0
colorama==0.4.6
comm==0.1.4
contourpy==1.2.0
cycler==0.12.1
debugpy==1.8.0
decorator==5.1.1
defusedxml==0.7.1
docker-pycreds==0.4.0
executing==2.0.0
Farama-Notifications==0.0.4
fastjsonschema==2.18.1
fonttools==4.44.0
fqdn==1.5.1
gitdb==4.0.11
GitPython==3.1.41
iniconfig==2.0.0
gymnasium==0.29.1
idna==3.4
ipykernel==6.25.2
ipympl==0.9.3
ipython==8.16.1
ipython-genutils==0.2.0
ipywidgets==8.1.1
isoduration==20.11.0
jedi==0.19.1
Jinja2==3.1.2
json5==0.9.14
jsonpointer==2.4
jsonschema==4.19.1
jsonschema-specifications==2023.7.1
jupyter-events==0.8.0
jupyter-lsp==2.2.0
jupyter_client==8.4.0
jupyter_core==5.4.0
jupyter_server==2.8.0
jupyter_server_terminals==0.4.4
jupyterlab==4.0.7
jupyterlab-pygments==0.2.2
jupyterlab-widgets==3.0.9
jupyterlab_server==2.25.0
kiwisolver==1.4.5
MarkupSafe==2.1.3
matplotlib==3.8.1
matplotlib-inline==0.1.6
mistune==3.0.2
nbclient==0.8.0
nbconvert==7.9.2
nbformat==5.9.2
nest-asyncio==1.5.8
notebook==7.0.6
notebook_shim==0.2.3
numpy==1.26.2
overrides==7.4.0
packaging==23.2
pluggy==1.4.0
pandas==2.1.3
pandocfilters==1.5.0
parso==0.8.3
pickleshare==0.7.5
Pillow==10.1.0
platformdirs==3.11.0
prometheus-client==0.17.1
prompt-toolkit==3.0.39
protobuf==4.25.2
psutil==5.9.6
pure-eval==0.2.2
pybricks==3.2.0
pycparser==2.21
pygame==2.5.2
pytest==8.0.2
setuptools==69.0.3
Pygments==2.16.1
pynput==1.7.6
pyparsing==3.1.1
python-dateutil==2.8.2
python-json-logger==2.0.7
pytz==2023.3.post1
pywin32==306
pywinpty==2.0.12
PyYAML==6.0.1
pyzmq==25.1.1
referencing==0.30.2
requests==2.31.0
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rpds-py==0.10.6
Send2Trash==1.8.2
sentry-sdk==1.40.4
setproctitle==1.3.3
six==1.16.0
smmap==5.0.1
wheel==0.42.0
sniffio==1.3.0
soupsieve==2.5
stack-data==0.6.3
terminado==0.17.1
tinycss2==1.2.1
tornado==6.3.3
traitlets==5.11.2
types-python-dateutil==2.8.19.14
typing_extensions==4.9.0
tzdata==2023.3
uri-template==1.3.0
urllib3==2.0.7
wandb==0.16.3
wcwidth==0.2.8
webcolors==1.13
webencodings==0.5.1
websocket-client==1.6.4
widgetsnbextension==4.0.9
155 changes: 155 additions & 0 deletions src/game/TetrisGameManager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
from pynput.keyboard import Key, Listener
import time as t

baseScore = 100
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)

""" TODO: Timer for piece drop
keyboard input for piece movement
keyboard input for piece rotation
keyboard input for piece drop
keyboard input for game start
soft drop and hard drop implementation
"""


class TetrisGameManager:

currentPiece = None
nextPiece = None
score = 0
updateTimer = 1
streak = 1
currentTime = None

def __init__(self, board):
self.board = board
self.score = 0
self.currentTime = int(round(t.time() * 1000))

# while True:
# with Listener(on_press=self.on_press, on_release=self.on_release) as listener:
# listener.join()


def onPress(self, key):
switcher = {

}

def onRelease(self, key):
pass


def rotatePiece(self, direction):
self.currentPiece.rotate(direction)


def movePiece(self, direction):

if self.legalMove(direction):
self.currentPiece.move(direction)

def dropPiece(self, newPiece):
self.movePiece(DOWN)

def isGameOver(self):
return self.board.isGameOver()

def startGame(self):
self.currentPiece = self.newPiece()
self.nextPiece = self.newPiece()

while not self.isGameOver():
action = input("Enter action: ") ## valid commands: [moveLeft, moveRight, moveDown, softDrop, hardDrop, quitGame, rotateLeft, rotateRight, rotate180]
if action == "moveLeft" and self.legalMove(LEFT):
self.movePiece(LEFT)
elif action == "moveRight" and self.legalMove(RIGHT):
self.movePiece(RIGHT)
elif action == "moveDown" and self.legalMove(DOWN):
self.dropPiece(DOWN)
elif action == "softDrop":
self.softDrop()
elif action == "h":
self.hardDrop()
elif action == "rotateLeft":
self.rotatePiece(-1)
elif action == "rotateRight":
self.rotatePiece(1)
elif action == "rotate180":
self.rotatePiece(2)
elif action == "q":
self.stopGame()
break
else:
self.moveDown()




def newPiece(self):
return self.pieces.getNewPiece()

def legalMove(self, x, y):
return self.board.legalMove(x, y)

# def clearLines(self):
# linesCleared = self.board.checkGameState()
# if linesCleared == 4:
# self.streak += 1
# else:
# self.streak = 1


def updateScore(self, linesCleared):
self.score += self.streak*(baseScore**linesCleared)

def softDrop(self):
if self.legalMove(DOWN):
self.dropPiece()
else:
self.placePiece(DOWN)

def hardDrop(self):
while self.legalMove(DOWN):
self.dropPiece()
self.placePiece(DOWN)

def placePiece(self, direction):
x = direction[0]
y = direction[1]
if not self.legalMove(x, y):
self.board.placePiece(x, y, self.currentPiece)
self.currentPiece = self.nextPiece
self.next_piece = self.nextPiece()
else:
self.movePiece(DOWN)
return False
if self.isGameOver():
self.stopGame()
return True
clearLines = self.board.checkGameState()
if clearLines:
self.board.clearLines(clearLines)
self.updateScore(clearLines)
return True


def checkTimer(self):
checkTime = self.currentTime + 1000
newTime = int(round(t.time() * 1000))
if (checkTime > newTime):
self.movePiece(DOWN)


return True

def stopGame(self):
self.board.stop_game()


if __name__ == "__main__":
millisec = int(round(t.time() * 1000))
print(millisec)

0 comments on commit 612991a

Please sign in to comment.