generated from dgomes/ia-rush
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5963201
Showing
13 changed files
with
1,147 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
.DS_Store | ||
|
||
# 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/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Diogo Gomes | ||
|
||
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. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# ia-tetris | ||
Projecto de Inteligência Artificial 2022 - Rush Hour | ||
|
||
## How to install | ||
|
||
Make sure you are running Python 3.7 or higher | ||
|
||
`$ pip install -r requirements.txt` | ||
|
||
*Tip: you might want to create a virtualenv first* | ||
|
||
## How to play | ||
|
||
open 3 terminals: | ||
|
||
`$ python3 server.py` | ||
|
||
`$ python3 viewer.py` | ||
|
||
`$ python3 client.py` | ||
|
||
to play using the sample client make sure the client pygame window has focus | ||
|
||
### Keys | ||
|
||
Directions: arrows | ||
|
||
## Debug Installation | ||
|
||
Make sure pygame is properly installed: | ||
|
||
python -m pygame.examples.aliens | ||
|
||
# Tested on: | ||
- OSX Monterey 12.5.1 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
"""Example client.""" | ||
import asyncio | ||
import getpass | ||
import json | ||
import os | ||
|
||
# Next 4 lines are not needed for AI agents, please remove them from your code! | ||
import pygame | ||
import websockets | ||
|
||
pygame.init() | ||
program_icon = pygame.image.load("data/icon2.png") | ||
pygame.display.set_icon(program_icon) | ||
|
||
|
||
async def agent_loop(server_address="localhost:8000", agent_name="student"): | ||
"""Example client loop.""" | ||
async with websockets.connect(f"ws://{server_address}/player") as websocket: | ||
|
||
# Receive information about static game properties | ||
await websocket.send(json.dumps({"cmd": "join", "name": agent_name})) | ||
|
||
# Next 3 lines are not needed for AI agent | ||
SCREEN = pygame.display.set_mode((299, 123)) | ||
SPRITES = pygame.image.load("data/pad.png").convert_alpha() | ||
SCREEN.blit(SPRITES, (0, 0)) | ||
|
||
while True: | ||
try: | ||
state = json.loads( | ||
await websocket.recv() | ||
) # receive game update, this must be called timely or your game will get out of sync with the server | ||
|
||
print(state.get("cursor")) | ||
|
||
# Next lines are only for the Human Agent, the key values are nonetheless the correct ones! | ||
key = "" | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
pygame.quit() | ||
|
||
if event.type == pygame.KEYDOWN: | ||
if event.key == pygame.K_UP: | ||
key = "w" | ||
elif event.key == pygame.K_LEFT: | ||
key = "a" | ||
elif event.key == pygame.K_DOWN: | ||
key = "s" | ||
elif event.key == pygame.K_RIGHT: | ||
key = "d" | ||
elif event.key == pygame.K_SPACE: | ||
key = " " | ||
|
||
elif event.key == pygame.K_d: | ||
import pprint | ||
|
||
pprint.pprint(state) | ||
|
||
await websocket.send( | ||
json.dumps({"cmd": "key", "key": key}) | ||
) # send key command to server - you must implement this send in the AI agent | ||
break | ||
except websockets.exceptions.ConnectionClosedOK: | ||
print("Server has cleanly disconnected us") | ||
return | ||
|
||
# Next line is not needed for AI agent | ||
pygame.display.flip() | ||
|
||
|
||
# DO NOT CHANGE THE LINES BELLOW | ||
# You can change the default values using the command line, example: | ||
# $ NAME='arrumador' python3 client.py | ||
loop = asyncio.get_event_loop() | ||
SERVER = os.environ.get("SERVER", "localhost") | ||
PORT = os.environ.get("PORT", "8000") | ||
NAME = os.environ.get("NAME", getpass.getuser()) | ||
loop.run_until_complete(agent_loop(f"{SERVER}:{PORT}", NAME)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
"""Common data structures. Can be used by any module.""" | ||
from __future__ import annotations | ||
|
||
import math | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Coordinates: | ||
"""Coordinates data class.""" | ||
|
||
x: int | ||
y: int | ||
|
||
|
||
class MapException(Exception): | ||
"""Exception Moving Pieces.""" | ||
|
||
|
||
class Map: | ||
"""Representation of a map.""" | ||
|
||
empty_tile = "o" | ||
wall_tile = "x" | ||
player_car = "A" | ||
|
||
def __init__(self, txt: str): | ||
"""Initialize Map from string.""" | ||
pieces, grid, movements = txt.split(" ") | ||
self.pieces = int(pieces) | ||
self.movements = int(movements) | ||
self.grid_size = int(math.sqrt(len(grid))) | ||
self.grid = [] | ||
|
||
line = [] | ||
for i, pos in enumerate(grid): | ||
line.append(pos) | ||
if (i + 1) % self.grid_size == 0: | ||
self.grid.append(line) | ||
line = [] | ||
|
||
def __repr__(self): | ||
"""Revert map object to string.""" | ||
raw = "" | ||
for line in self.grid: | ||
for column in line: | ||
raw += column | ||
return f"{self.pieces} {raw} {self.movements}" | ||
|
||
@property | ||
def coordinates(self): | ||
"""Representation of ocupied map positions through tuples x,y,value.""" | ||
_coordinates = [] | ||
|
||
for y, line in enumerate(self.grid): | ||
for x, column in enumerate(line): | ||
if column != self.empty_tile: | ||
_coordinates.append((x, y, column)) | ||
|
||
return _coordinates | ||
|
||
def get(self, cursor: Coordinates): | ||
"""Return piece at cursor position.""" | ||
if 0 <= cursor.x < self.grid_size and 0 <= cursor.y < self.grid_size: | ||
return self.grid[int(cursor.y)][int(cursor.x)] | ||
raise MapException("Out of the grid") | ||
|
||
def piece_coordinates(self, piece: str): | ||
"""List coordinates holding a piece.""" | ||
return [Coordinates(x, y) for (x, y, p) in self.coordinates if p == piece] | ||
|
||
def move(self, piece: str, direction: Coordinates): | ||
"""Move piece in direction fiven by a vector.""" | ||
if piece == self.wall_tile: | ||
raise MapException("Blocked piece") | ||
|
||
piece_coord = self.piece_coordinates(piece) | ||
|
||
# Don't move vertical pieces sideways | ||
if direction.x != 0 and any([line.count(piece) == 1 for line in self.grid]): | ||
raise MapException("Can't move sideways") | ||
# Don't move horizontal pieces up-down | ||
if direction.y != 0 and any([line.count(piece) > 1 for line in self.grid]): | ||
raise MapException("Can't move up-down") | ||
|
||
def sum(a: Coordinates, b: Coordinates): | ||
return Coordinates(a.x + b.x, a.y + b.y) | ||
|
||
for pos in piece_coord: | ||
if not self.get(sum(pos, direction)) in [piece, self.empty_tile]: | ||
raise MapException("Blocked piece") | ||
|
||
for pos in piece_coord: | ||
self.grid[pos.y][pos.x] = self.empty_tile | ||
|
||
for pos in piece_coord: | ||
new_pos = sum(pos, direction) | ||
self.grid[new_pos.y][new_pos.x] = piece | ||
|
||
def test_win(self): | ||
"""Test if player_car has crossed the left most column.""" | ||
return any( | ||
[c.x == self.grid_size - 1 for c in self.piece_coordinates(self.player_car)] | ||
) | ||
|
||
|
||
""" TODO move to tests | ||
m = Map("02 ooooBoooooBoAAooBooooooooooooooooooo 14") | ||
print(m) | ||
print(m.get(Dimensions(4,0))) | ||
assert m.move("A", Coordinates(1, 0)) | ||
assert m.move("A", Coordinates(-1, 0)) | ||
assert not m.move("A", Coordinates(0, 1)) | ||
assert not m.move("A", Coordinates(0, -1)) | ||
assert m.move("B", Coordinates(0, 1)) | ||
assert m.move("B", Coordinates(0, -1)) | ||
assert not m.move("B", Coordinates(1,0)) | ||
assert not m.move("B", Coordinates(-1,0)) | ||
print(m) | ||
""" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.