Skip to content

Commit

Permalink
Funcoes get_height, get_numberOfholes, get_bumpiness
Browse files Browse the repository at this point in the history
  • Loading branch information
brunosilva16 committed Nov 18, 2021
1 parent d65bac8 commit a39b778
Showing 1 changed file with 121 additions and 64 deletions.
185 changes: 121 additions & 64 deletions student.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,6 @@
program_icon = pygame.image.load("data/icon2.png")
pygame.display.set_icon(program_icon)

class Event():
type = None
key = None

def __init__(self,type, key):
self.type = type
self.key = key

counter = 0
def run_ai(game_field, game_figure, game_width, game_heigth):
global counter
counter += 1
if counter < 3:
return []
counter = 0
e = Event(pygame.KEYDOWN, pygame.K_UP)
return [e]


async def agent_loop(server_address="localhost:8000", agent_name="student"):
async with websockets.connect(f"ws://{server_address}/player") as websocket:

Expand All @@ -51,42 +32,44 @@ async def agent_loop(server_address="localhost:8000", agent_name="student"):
await websocket.recv()
) # receive game update, this must be called timely or your game will get out of sync with the server

# Next lines are only for the Human Agent, the key values are nonetheless the correct ones!
key = ""
# for event in list(pygame.event.get()) + run_ai():
# 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_d:
# import pprint

# pprint.pprint(state)



piece=state['piece']
game=state['game']
print(piece)
print(game)
#print(game)
#print(get_piece(piece))
#print(get_rows(game))
print(get_height(game))
print(get_rows(game))
#print(get_aggregate_height(game))
print(numberOfHoles(game))
#print(numberOfHoles(game))
#print(get_all_positions(piece))
# print(len(get_all_positions(piece))==(len(vectors)*2))

# 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_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
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:
Expand Down Expand Up @@ -172,27 +155,101 @@ def get_piece(piece):
return 'Not found'

def get_rows(game):
rows=[[0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0]]

rows=[[0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0]]
for p in game:
rows[p[1]][p[0]] = 1
return rows
rows[p[1]][p[0]] = 1

for i in range(len(rows)):
del rows[i][0]

def numberOfHoles(row):
count = 0
for pos in row:
if pos == 0:
count += 1
return count
return rows

def get_height(game):
height = 30
temp = 30
for p in game:
if p[1] < height:
height = p[1]
return 30 - height
if p[1] < temp:
temp = p[1]
return 30 - temp

def get_aggregate_height(game):
rows = get_rows(game)
#altura_colunas=[0,0,0,0,0,0,0,0]
column_height = 0
aggregate_height = 0
for x in range(8):
for y in range(30):
if rows[29-y][x] == 1:
column_height = y + 1
#altura_colunas[x] = column_height
aggregate_height += column_height
return aggregate_height

def get_columns_height(game):
rows = get_rows(game)
heightPerColumn=[0,0,0,0,0,0,0,0]
column_height = 0
for x in range(8):
for y in range(30):
if rows[29-y][x] == 1:
column_height = y + 1
heightPerColumn[x] = column_height
return heightPerColumn

def get_bumpiness(game):
columns_heigths = get_columns_height(game)
total_bumpiness = 0
for i in range(7):
total_bumpiness += abs(columns_heigths[i] - columns_heigths[i+1])
return total_bumpiness

# def numberOfHoles(game):
# rows = get_rows(game)
# holesColumn = 0
# totalHoles = 0
# holesPerColumn = [0,0,0,0,0,0,0,0]
# for x in range(8):
# for y in range(30):
# if rows[29-y][x] == 0: #and rows[29-y-1][x] == 1:
# for y2 in range(y,30):
# if rows[29-y2][x] == 1:
# holesColumn = y2 - y -1
# holesPerColumn[x] = holesColumn
# totalHoles += holesColumn
# return holesPerColumn

def numberOfHoles(game):
rows = get_rows(game)
columns_heigths = get_columns_height(game)
for x in range(8):
height = columns_heigths[x]
for y in range(height, 30):
rows[29-y][x] = -1

countHoles = 0
for x in range(8):
for y in range(30):
if rows[29-y][x] == 0:
countHoles += 1
return countHoles

def complete_lines(game):
rows = get_rows(game)
numberOflines = 0
for r in rows:
if sum(r) == 8:
numberOflines += 1
return numberOflines


def simulate_fall(piece, game):
sim_game = game
for block in piece:
a = 0
return 0


# DO NOT CHANGE THE LINES BELLOW
# You can change the default values using the command line, example:
Expand Down

0 comments on commit a39b778

Please sign in to comment.