-
Notifications
You must be signed in to change notification settings - Fork 0
/
testScores.py
122 lines (99 loc) · 3.49 KB
/
testScores.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
import pygame
import os
import random
from time import sleep
from pygame.locals import *
from geneticCalcScore import MachineLearning
from weights import Weight
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)
#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 = []
#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)
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))
#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))
#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()
display.fill(pygame.Color("black"))
addBorder()
grrid=[
[1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
[4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
[4,-1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,4,-1,-1,-1],
[4,-1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
[4,-1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
[4,4,4,4,4,4,4,4,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
[1,-1,-1,-1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
[1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
]
instance.setGrid(grrid)
pMain()
instance.setNext(0)
print(instance.getPureScore())
instance.nextMove()
print(instance.getPureScore())
rune =True
while rune:
update()
pygame.display.update()
#if game is over, quit game
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
run=False
if __name__ == "__main__":
view()