Skip to content

Commit

Permalink
Primeira versao com rotate funcional
Browse files Browse the repository at this point in the history
  • Loading branch information
brunosilva16 committed Nov 23, 2021
1 parent a0d8fd2 commit 5bfc6c0
Showing 1 changed file with 203 additions and 63 deletions.
266 changes: 203 additions & 63 deletions student.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
from copy import deepcopy

# Next 4 lines are not needed for AI agents, please remove them from your code!
import pygame
#import pygame

pygame.init()
program_icon = pygame.image.load("data/icon2.png")
pygame.display.set_icon(program_icon)
# 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"):
async with websockets.connect(f"ws://{server_address}/player") as websocket:
Expand All @@ -23,9 +23,9 @@ async def agent_loop(server_address="localhost:8000", agent_name="student"):
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))
# SCREEN = pygame.display.set_mode((299, 123))
# SPRITES = pygame.image.load("data/pad.png").convert_alpha()
# SCREEN.blit(SPRITES, (0, 0))

# state = json.loads(
# await websocket.recv()
Expand Down Expand Up @@ -55,29 +55,36 @@ async def agent_loop(server_address="localhost:8000", agent_name="student"):
) # receive game update, this must be called timely or your game will get out of sync with the server
piece = None
# game=[]
#print(state)
print(state['score'])
if 'piece' in state: # and "game" in state:
piece=state['piece']
game=state['game']

flag = False

while(piece == None):
if(piece == None):
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
piece=state['piece']
game=state['game']
print(piece)
print(get_rows(game))

ideal_pos_piece = simulate_all_possibilities(piece,game)
print(ideal_pos_piece)
translate = compare_pieces(piece, ideal_pos_piece)
keys = get_keys(translate)
print(keys)
print("\n")
flag = True
if 'piece' in state: # and "game" in state:
piece=state['piece']
game=state['game']
type_piece = get_piece(piece)
#print("Piece: ")
#print(piece)
#print(get_piece(piece))
#print(get_rows(game))
if piece != None:
ideal_pos_rot_piece = simulate_all_possibilities(piece,game, type_piece)
#print("Ideal Piece: ")
#print(ideal_pos_rot_piece[0])
#print(ideal_pos_rot_piece[1])
#if ideal_pos_rot_piece != None:
translate = compare_pieces(rotate(piece, type_piece, ideal_pos_rot_piece[1]), ideal_pos_rot_piece[0])
keys = get_keys(translate, ideal_pos_rot_piece[1])
#print(keys)
#print("\n")
flag = True

if(flag):
for key in keys:
Expand All @@ -93,7 +100,7 @@ async def agent_loop(server_address="localhost:8000", agent_name="student"):
return

# Next line is not needed for AI agent
pygame.display.flip()
#pygame.display.flip()


vectors=[[0,1], [0,2], [0,3], [0,4],
Expand Down Expand Up @@ -142,12 +149,12 @@ def get_all_positions(piece):
return possible_positions

I = get_all_positions([[2,2],[3,2],[4,2],[5,2]])
L = get_all_positions([[2,1],[3,1],[2,2],[2,3]])
S = get_all_positions([[2,1],[1,2],[2,2],[1,3]])
L = get_all_positions([[2,1],[2,2],[2,3],[3,3]])
S = get_all_positions([[2,1],[2,2],[3,2],[3,3]])
T = get_all_positions([[4,2],[4,3],[5,3],[4,4]])
J = get_all_positions([[2,1],[2,2],[2,3],[3,3]])
J = get_all_positions([[2,1],[3,1],[2,2],[2,3]])
O = get_all_positions([[3,3],[4,3],[3,4],[4,4]])
Z = get_all_positions([[2,1],[2,2],[3,2],[3,3]])
Z = get_all_positions([[2,1],[1,2],[2,2],[1,3]])
PIECES=[I,L,S,T,J,O,Z]

def get_piece(piece):
Expand All @@ -165,7 +172,7 @@ def get_piece(piece):
return 'J'
elif p == O:
return 'O'
elif p == 'Z':
elif p == Z:
return 'Z'

return 'Not found'
Expand All @@ -183,12 +190,108 @@ def get_rows(game):

return rows

def get_height(game):
temp = 30
for p in game:
if p[1] < temp:
temp = p[1]
return 30 - temp
def get_center(piece, type_piece):
return [0, 0]

def rotate(piece, type_piece, numOfrotations):
#type_piece = get_piece(piece)
#print(type_piece)
piece = deepcopy(piece)
center = [0,0]
if type_piece == 'O':
return piece
elif type_piece == 'T':
#center = get_center(piece, type_piece)
center = piece[1]
for i in range(numOfrotations):
for block in piece:
if block != center:
if block[0] == center[0] and block[1] < center[1]:
block[0] += 1
block[1] += 1
elif block[0] > center[0] and block[1] == center[1]:
block[0] -= 1
block[1] += 1
elif block[0] == center[0] and block[1] > center[1]:
block[0] -= 1
block[1] -= 1
elif block[0] < center[0] and block[1] == center[1]:
block[0] += 1
block[1] -= 1
return piece
elif type_piece == 'S':
#center = get_center(piece, type_piece)
center = piece[1]
if numOfrotations % 2 != 0:
for block in piece:
if block != center: # atençao à ordem das peças
if block[0] == center[0] and block[1] == center[1] - 1:
block[0] -= 1
block[1] += 2
if block[0] == center[0] + 1 and block[1] == center[1] + 1:
block[0] -= 1
return piece
elif type_piece == 'Z':
#center = get_center(piece, type_piece)
center = piece[2]
if numOfrotations % 2 != 0:
for block in piece:
if block != center: # atençao à ordem das peças
if block[0] == center[0] and block[1] == center[1] - 1:
block[0] += 1
block[1] += 2
if block[0] == center[0] - 1 and block[1] == center[1] + 1:
block[0] += 1
return piece
elif type_piece == 'L' or type_piece == 'J':
#center = get_center(piece, type_piece)
if type_piece == 'L':
center = piece[1]
elif type_piece == 'J':
center = piece[2]

for i in range(numOfrotations):
for block in piece:
if block != center:
if block[0] == center[0] and block[1] < center[1]:
block[0] += 1
block[1] += 1
elif block[0] > center[0] and block[1] == center[1]:
block[0] -= 1
block[1] += 1
elif block[0] == center[0] and block[1] > center[1]:
block[0] -= 1
block[1] -= 1
elif block[0] < center[0] and block[1] == center[1]:
block[0] += 1
block[1] -= 1
else:
if block[0] > center[0] and block[1] < center[1]: # trata se do bloco "cauda", o unico que nao está diretamente conectado ao centro
block[1] += 2
elif block[0] > center[0] and block[1] > center[1]:
block[0] -= 2
elif block[0] < center[0] and block[1] > center[1]:
block[1] -= 2
elif block[0] < center[0] and block[1] < center[1]:
block[0] += 2
return piece
elif type_piece == 'I':
#center = get_center(piece, type_piece)
center = piece[2]
if numOfrotations % 2 != 0:
for block in piece:
if block != center:
if block[0] == center[0] - 2:
block[0] += 2
block[1] -= 2
elif block[0] == center[0] - 1:
block[0] += 1
block[1] -= 1
else:
block[0] -= 1
block[1] += 1
return piece


def get_aggregate_height(game):
rows = get_rows(game)
Expand Down Expand Up @@ -277,36 +380,69 @@ def simulate_fall(piece, game):
sim_game += piece_clone
return sim_game

def simulate_all_possibilities(piece, game):
x_min = 8
x_max = 0
for block in piece: #check the minimum and maximum x coordenate of the blocks
if block[0] < x_min:
x_min = block[0]
if block[0] > x_max:
x_max = block[0]

#width = x_max - x_min
ini_translate = 1 - x_min
piece = translate(piece, ini_translate - 1)
original_piece = translate(piece,1)
def simulate_all_possibilities(piece, game, type_piece):
if piece == None:
return None
original_piece_real = piece
#type_piece = get_piece(piece)
numbOfrotations=0
iterations = 1
original_piece = []
scores=[]
while piece[0][0] < 8 and piece[1][0] < 8 and piece[2][0] < 8 and piece[3][0] < 8:
piece = translate(piece, 1)
game1 = simulate_fall(piece, game)
total_height = get_aggregate_height(game1)
completeLines = complete_lines(game1)
numberOfHoles = get_numberOfHoles(game1)
bumpiness = get_bumpiness(game1)
score = calculateHeuristic(total_height, completeLines, numberOfHoles, bumpiness)
scores.append(score)

pos = scores.index(max(scores))
print(scores)
piece = translate(original_piece, pos)
return piece


total_numberOfpositions=[]
if type_piece == 'O':
numbOfrotations = 0
elif type_piece == 'I' or type_piece == 'S' or type_piece == 'Z':
numbOfrotations = 1
else:
numbOfrotations = 3

iterations = numbOfrotations + 1

for i in range(iterations):
if i > 0:
piece = rotate(original_piece_real, type_piece, i)

x_min = 8
x_max = 0
for block in piece: #check the minimum and maximum x coordenate of the blocks
if block[0] < x_min:
x_min = block[0]
if block[0] > x_max:
x_max = block[0]

#width = x_max - x_min
ini_translate = 1 - x_min
piece = translate(piece, ini_translate - 1)
original_piece.append(translate(piece,1))
numberOfpositions = 0
while piece[0][0] < 8 and piece[1][0] < 8 and piece[2][0] < 8 and piece[3][0] < 8:
piece = translate(piece, 1)
game1 = simulate_fall(piece, game)
total_height = get_aggregate_height(game1)
completeLines = complete_lines(game1)
numberOfHoles = get_numberOfHoles(game1)
bumpiness = get_bumpiness(game1)
score = calculateHeuristic(total_height, completeLines, numberOfHoles, bumpiness)
scores.append(score)
numberOfpositions += 1
total_numberOfpositions.append(numberOfpositions)

ind = scores.index(max(scores))
numberOfpossibilities = len(scores)
if ind < total_numberOfpositions[0]:
piece = translate(original_piece[0], ind)
return [piece, 0] # Retorna a peça e o numero de rotaçoes a executar
elif ind < total_numberOfpositions[0] + total_numberOfpositions[1]:
piece = translate(original_piece[1], ind - total_numberOfpositions[0])
return [piece, 1]
elif ind < total_numberOfpositions[0] + total_numberOfpositions[1] + total_numberOfpositions[2]:
piece = translate(original_piece[2], ind - (total_numberOfpositions[0] + total_numberOfpositions[1]))
return [piece, 2]
else:
piece = translate(original_piece[3], ind - (total_numberOfpositions[0] + total_numberOfpositions[1] + total_numberOfpositions[2]))
return [piece, 3]


def calculateHeuristic(total_height, completeLines, numberOfHoles, bumpiness):
a = -0.51006
Expand All @@ -322,11 +458,15 @@ def translate(piece, value):
return piece_clone

def compare_pieces(original_piece, new_piece):
if original_piece == None or new_piece == None:
return 0
translate = new_piece[0][0] - original_piece[0][0]
return translate

def get_keys(translate):
def get_keys(translate, numberOfrotations):
keys = []
for i in range(numberOfrotations):
keys.append("w")
if translate < 0:
for i in range(abs(translate)):
keys.append("a")
Expand Down

0 comments on commit 5bfc6c0

Please sign in to comment.