-
Notifications
You must be signed in to change notification settings - Fork 0
/
geneticAlgoView.py
139 lines (116 loc) · 3.87 KB
/
geneticAlgoView.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import pygame
import os
import random
from time import sleep
from pygame.locals import *
from geneticCalcScore import MachineLearning
from weights import Weight
#rgb for all the colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
yellow = (255, 255, 0)
blue = (0, 0, 255)
purple = (128, 0, 128)
orange = (255, 165, 0)
green = (0, 255, 0)
aqua = (0, 255, 255)
#their id, index returns rgb value
colorID = [blue, orange, yellow, green, purple, red, aqua, white, black]
#size of the entire screen and each tile in tetris
fieldWidth, fieldHeight = 600, 700
boxWidth, boxHeight = 30, 30
#main grid and set up field
display = pygame.display.set_mode((fieldWidth, fieldHeight))
mainGrid = []
#score, holes, bump, tetrites
#imports the gene data from saved txt file
current = os.path.dirname(os.path.abspath(__file__))
#print(((os.path.join(current, 'gene.txt'))))
with open(((os.path.join(current, 'gene.txt')))) as f:
lines = f.readlines()
a, b, c = map(float,lines)
#creates instance of game as well as weights
tempWeight = Weight(a,b,c)
instance = MachineLearning(tempWeight)
#goes through every box to update their color (even ones that have already been updated)
def update():
# display the boxes
mainGrid = instance.getGrid()
for y in range(20):
for x in range(10):
pygame.draw.rect(display, colorID[mainGrid[x][19 - y]],
(50 + (boxWidth * x), 50 + (boxHeight * y), boxWidth, boxHeight))
#prints the grid (used to debug)
def pMain():
mainGrid = instance.getGrid()
for y in range(19, -1, -1):
s = ''
for x in range(10):
s += str(mainGrid[x][y]) + ' '
print(s)
#draws border of the game
def addBorder():
pygame.draw.rect(display, white, (49, 49, 302, 1))
pygame.draw.rect(display, white, (49, 49, 1, 602))
pygame.draw.rect(display, white, (351, 49, 1, 602))
pygame.draw.rect(display, white, (49, 651, 302, 1))
#main function, runs when file is ran
def view():
#initializing game
pygame.font.init()
pygame.mixer.init()
display = pygame.display.set_mode((fieldWidth, fieldHeight))
#creates display
pygame.display.set_caption("Tetris AI!")
pygame.key.set_repeat(500, 100)
sideFont = pygame.font.SysFont("monospace", 20)
pygame.display.update()
#runs game, sets game timer
run = instance.isRun()
clock = pygame.time.Clock()
pygame.key.set_repeat(500, 100)
#sets speed of the game
FPS = 5
saves=[]
scores = []
while run:
prev = instance.getResults()
clock.tick(FPS)
sideText = 'Hi'
# text on side
#settings side display, to show user game stats
score,level,lines = instance.getAll()
display.fill(pygame.Color("black"))
addBorder()
displayItems = ["Score:" + str(score),'Level:' + str(level),'Lines:' + str(lines)]
#sets side display
for x in range(len(displayItems)):
sideInfo = sideFont.render(displayItems[x], 1, (255, 255, 255))
display.blit(sideInfo, (375, 100+x*30))
#calculates next move
instance.nextMove()
run = instance.isRun()
#updates screen
update()
pygame.display.update()
now=instance.getResults()
situation = instance.getSit()
#holes, score,tetrites, bump
score = 0
if now[0]!=0:score += (prev[0]/now[0])-1
if now[1]!=0:score -= (prev[1]/now[1])-1
if now[2]!=0:score -= (prev[2]/now[2])-1
saves.append(situation)
scores.append(score)
#if game is over, quit game
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
run=False
#print final score
print("Final Score: " + str(instance.getAll()[0]))
#print(saves)
#print(scores)
if __name__ == "__main__":
view()