-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtetris.py
915 lines (758 loc) · 27.4 KB
/
tetris.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
#################################################
# Hw6 - Tetris
# Your andrewID: akong
# Your section: F
#################################################
from tkinter import *
import random
import copy
import time
import os
##### My Functions
def makeButton(canvas,size,location,color,buttonText):
bounds = (location[0]-size,location[1]-size//3,
location[0]+size,location[1]+size//3)
canvas.create_rectangle(bounds,
fill=color)
canvas.create_text(location, text=buttonText)
return bounds
def drawStart(canvas,data):
butSize = 100
halfWid = data.width//2
quart = data.height//4
canvas.create_rectangle(0,0,data.width,data.height,fill="#40c2e2")
canvas.create_text(halfWid,data.height/10, text = "Tetris AI",font="Helvetica 30 bold")
canvas.create_text(halfWid,data.height/7, text = "112 Term Project by Andy Kong",font="Helvetica 15 bold")
data.spButton = makeButton(canvas,butSize,(halfWid,int(quart*1.5)),"#eeeeee","Single Player Tetris")
data.vsButton = makeButton(canvas,butSize,(halfWid,quart*2.25),"#eeeeee","Play against AI")
data.AIButton = makeButton(canvas,butSize,(halfWid,quart*3),"#eeeeee","Watch AI play")
# img = PhotoImage(file="tetrisbg.png")
# icon = PhotoImage(file="t.png")
# canvas.create_image(halfWid,data.height,anchor="s",image=img)
# canvas.create_image(0,0,"tetrisbg.png")
def drawSlider(canvas,data):
left = data.margin*4/10
top = data.topMargin*10
right = data.margin*6/10
bot = data.topMargin*15
gridSize = data.topMargin-10
canvas.create_text(data.margin/2,data.topMargin*9,text="Speed")
canvas.create_rectangle(left-3,top,right+3,bot,fill = "black")
for i in range(data.speed):
canvas.create_rectangle(left,bot-data.topMargin*i-2,right,bot-data.topMargin*(i+1),fill="blue",width=5)
def gameDimensions():
#(rows, cols, cellSize, side margin, top bottom margin)
return (20, 10, 30, 225, 40)
#Starts the game
def playTetris(maxPieces = -1,gameMode=0):
rows, cols, cellSize, sideMargin, topMargin = gameDimensions()
width = sideMargin*2+cellSize*cols
height = topMargin*2+cellSize*rows
no = run(width, height,maxPieces,gameMode)
return no
def genReadout(data):
totTime = time.time() - data.beginRun
print("TOTAL TIME: ", totTime)
print("TOTAL SCORE: ", data.score)
print("Score/Pieces ", data.score / data.numPlaced)
print("Score breakdown: ")
print("singles: ", data.scoring[0])
print("doubles: ", data.scoring[1])
print("trips: ", data.scoring[2])
print("tetrodes: ", data.scoring[3])
data.isGameOver = True
#Draws the board with drawCell at the corrent spots
def drawBoard(canvas, data):
for rowIndex in range(data.rows):
for colIndex in range(data.cols):
drawCell(canvas, data, rowIndex, colIndex, \
data.board[rowIndex][colIndex],data.cellSize,data.margin+data.offset,\
data.topMargin)
def drawHold(canvas, data):
top = data.topMargin*3
left = data.topMargin
right = data.margin - left
bot = top + right
canvas.create_rectangle(left-4,top-4,right+4,bot+4, fill = "black")
canvas.create_rectangle(left,top,right,bot, fill = data.emptyColor)
canvas.create_text((left+right)//2,top-4,text="Hold", fill="white",\
font = ("Arial " + str(data.cellSize)) + " bold", anchor = "s")
if data.heldPiece != None:
hold = data.tetrisPieces[data.heldPieceNum]
color = data.tetrisPieceColors[data.heldPieceNum]
height = len(hold)
width = len(hold[0])
startX = (left+right)/2-width*data.cellSize/2
startY = (top+bot)/2-height*data.cellSize/2
for row in range(height):
for col in range(width):
if hold[row][col]:
drawCell(canvas, data, row,col,color,data.cellSize,startX,startY)
def drawQueue(canvas,data):
top = data.topMargin
left = data.width - data.margin+data.topMargin
right = data.width - top
bot = top + (right-left)*4
height = right-left
canvas.create_text((left + right) // 2, top - 4, text="Up Next",\
fill="white", \
font=("Arial " + str(data.cellSize)) + " bold",\
anchor="s")
for i in range(4):
canvas.create_rectangle(left-4,top+i*height-4,right+4,top+height+i*height+4, fill = "black")
canvas.create_rectangle(left,top+i*height,right,top+(i+1)*height, fill = data.emptyColor)
pieceNum = (data.currentQueue + data.nextQueue)[i]
piece = data.tetrisPieces[pieceNum]
color = data.tetrisPieceColors[pieceNum]
tall = len(piece)
wide = len(piece[0])
startX = (left+right)/2-wide*data.cellSize/2
startY = (top+(i+0.5)*height)-tall*data.cellSize/2
for row in range(tall):
for col in range(wide):
if piece[row][col]:
drawCell(canvas,data, row, col, color, data.cellSize,startX, startY)
# canvas.create_rectangle(left, top, right, bot, fill="white")
def lighten(color):
change = 1.7
r = int(color[1:3], 16)
g = int(color[3:5], 16)
b = int(color[5:7], 16)
newColor = ""
for piece in (r,g,b):
if piece*change > 255:
piece = 255
else:
piece *= change
piece = int(piece)
piece = str(hex(piece))[2:]
if len(piece) == 1:
piece += piece
newColor += piece
return "#" + newColor
#Draws each block on the playing field
def drawCell(canvas, data, row, col, color,cellSize,margin, topMargin):
startX = margin + cellSize*col
startY = topMargin + cellSize*row
canvas.create_rectangle(startX,startY,startX+cellSize,startY+cellSize,\
width = '4', fill = color)
detail = cellSize//8
detailColor = lighten(color)
canvas.create_polygon((startX+2,startY+2,\
startX+detail+2,startY+detail+2,\
startX+8*detail, startY+detail+2,\
startX+8*detail, startY+8*detail,\
startX-2+cellSize,startY-2+cellSize,\
startX+cellSize-2,startY+2), fill=detailColor\
)
canvas.create_line(startX+detail+2,startY+detail*6,\
startX+detail+2,startY+8*detail,\
startX+4*detail+2,startY+8*detail,fill=lighten(detailColor))
#Spawns a new piece and resets all the piece data
def newFallingPiece(data,piece):
data.fallingPiece = data.tetrisPieces[piece]
data.fallingPieceColor = data.tetrisPieceColors[piece]
data.numFallingPieceCols = len(data.fallingPiece[0])
try:
data.lastHeight = data.rows-(data.fallingPieceRow+len(data.fallingPiece))
except:
pass
data.fallingPieceRow = 0
if piece == 0:
data.fallingPieceRow = 2
data.pieceNum = piece
data.pieceRotPos = 0
data.fallingPieceCol = data.cols//2-data.numFallingPieceCols//2
#Draws the piece based on starting position and data.piece
def drawFallingPiece(canvas, data):
piece = data.fallingPiece
pieceColor = data.fallingPieceColor
row = data.fallingPieceRow
col = data.fallingPieceCol
for rowIndex in range(len(piece)):
for colIndex in range(len(piece[0])):
if piece[rowIndex][colIndex]:
drawCell(canvas, data, rowIndex+row, colIndex+col, pieceColor,\
data.cellSize, data.margin, data.topMargin)
#Game over screen display
if data.isGameOver:
canvas.create_rectangle(data.margin,data.margin+data.cellSize,\
data.width-data.margin,data.margin+3*data.cellSize, fill="white")
canvas.create_text(data.width//2, data.margin+data.cellSize*2, \
text = "GAME HAS ENDED\npress r to restart!")
return data
#Moves, checks if move is legal, if not it undoes it
def moveFallingPieces(data, drow, dcol):
data.fallingPieceRow += drow
data.fallingPieceCol += dcol
if fallingPieceIsLegal(data):
return True
else:
data.fallingPieceRow -= drow
data.fallingPieceCol -= dcol
return False
def fallingPieceIsLegal(data):
#Making code more readable
piece = data.fallingPiece
pieceRow = data.fallingPieceRow
pieceCol = data.fallingPieceCol
pHeight = len(piece)
pWidth = len(piece[0])
#Check if each cell of each piece is legal i.e. doesn't overlap colored bloc
for i in range(pHeight):
for j in range(pWidth):
if piece[i][j]:
if pieceCol < 0 or pieceCol+pWidth > data.cols or\
pHeight+pieceRow-1 >= data.rows or \
data.board[i+pieceRow]\
[j+pieceCol] != data.emptyColor:
return False
return True
def rotateFallingPiece(data):
#Store old info
oldPiece = copy.deepcopy(data.fallingPiece)
oldRowPos = data.fallingPieceRow
oldColPos = data.fallingPieceCol
oldRows = len(oldPiece)
oldCols = len(oldPiece[0])
#Calculate new location, dimensions, and piece
newRows = oldCols
newCols = oldRows
newRowPos = oldRowPos + oldRows//2 - newRows//2
newColPos = oldColPos + oldCols//2 - newCols//2
#initialize newPiece and assign values from oldPiece
newPiece = []
for i in range(oldCols):
newPiece.append([])
for j in range(oldRows):
newPiece[i].append(None)
for i in range(oldRows):
for j in range(oldCols):
newPiece[newRows-1-j][i] = oldPiece[i][j]
#Assigning new variables
data.fallingPieceRow = newRowPos
data.fallingPieceCol = newColPos
data.fallingPiece = newPiece
if data.pieceNum == 5:
data.fallingPieceCol += 0
#Legality check! get ur lawyer!
if fallingPieceIsLegal(data):
data.pieceRotPos -= 1
data.pieceRotPos %= 4
return True
elif moveFallingPieces(data,0,-1):
if fallingPieceIsLegal(data):
data.pieceRotPos -= 1
data.pieceRotPos %= 4
return True
else:
data.fallingPieceRow = oldRowPos
data.fallingPieceCol = oldColPos
data.fallingPiece = oldPiece
return False
#Integrates a fallen piece into the data.board
def placeFallingPiece(data):
data.lastClear = 0
data.numPlaced += 1
piece = data.fallingPiece
pWidth = len(piece[0])
pHeight = len(piece)
startRow = data.fallingPieceRow
startCol = data.fallingPieceCol
#iterates over the piece, placing each piece at the right board spot
for i in range(pHeight):
for j in range(pWidth):
if piece[i][j]:
data.board[startRow+i][startCol+j] = data.fallingPieceColor
removeFullRows(data)
if len(data.currentQueue) == 0:
data.currentQueue = copy.copy(data.nextQueue)
random.shuffle(data.nextQueue)
newFallingPiece(data,data.currentQueue.pop(0))
data.canSwitch = True
#Removes full rows (sorry for the useless comment)
def removeFullRows(data):
oldBoard = data.board
newBoard = []
#makes a new board that doesn't have any full rows on it
for rowIndex in range(len(oldBoard)-1,-1,-1):
if oldBoard[rowIndex].count(data.emptyColor) != 0:
#Inserts new rows at the top because we're iterating from the bottom
newBoard.insert(0, oldBoard[rowIndex])
fullRows = len(oldBoard)-len(newBoard)
#However many rows were taken out, add new blank rows and add to score
for i in range(fullRows):
newBoard.insert(0,[data.emptyColor]*data.cols)
data.board = newBoard
if fullRows != 0:
data.scoring[fullRows-1] += 1
data.lastClear = fullRows
data.score += fullRows**2
def holdPiece(data):
if data.heldPieceNum == None:
data.heldPieceNum = data.pieceNum
data.heldPiece = data.fallingPiece
newFallingPiece(data,(data.currentQueue+data.nextQueue).pop(0))
elif data.canSwitch:
temp = data.heldPieceNum
data.heldPieceNum = data.pieceNum
data.heldPiece = data.fallingPiece
newFallingPiece(data,temp)
data.canSwitch = False
def drawScore(canvas, data):
canvas.create_text(data.width//2, data.topMargin//2, text="Score: " +\
str(data.score), font="bold")
def hardDrop(data):
while moveFallingPieces(data, +1, 0):
pass
placeFallingPiece(data)
################################################## Bot commands
# def moveRight():
####################################
# customize these functions
####################################
def init(data,offset=0):
data.beginRun = time.time()
data.rows, data.cols, data.cellSize, data.margin,data.topMargin\
= gameDimensions()
data.offset = offset
data.emptyColor = "#2d2d2d"
data.board = []
for i in range(data.rows):
data.board.append([])
for j in range(data.cols):
data.board[i].append(data.emptyColor)
iPiece = [[ True, True, True, True ]]
jPiece = [[ True, False, False ],[ True, True, True ]]
lPiece = [[ False, False, True ],[ True, True, True ]]
oPiece = [[ True, True ],[ True, True ]]
sPiece = [[ False, True, True ],[ True, True, False ]]
tPiece = [[ False, True, False ],[ True, True, True ]]
zPiece = [[ True, True, False ],[ False, True, True ]]
cyan = "#00bbbb"
blue = "#3159d1"
orange = "#ffa500"
yellow = "#ffcc55"
green = "#23ac00"
purple = "#663399"
red = "#a51404"
data.tetrisPieces = [iPiece, jPiece, lPiece, oPiece, sPiece, tPiece, zPiece]
data.tetrisPieceColors = [cyan,blue,orange,yellow,green,purple,red]
data.score = 0
data.timedOut = False
data.isGameOver = False
data.heldPieceNum = None
data.heldPiece = None
data.canSwitch = True
data.pieceNum = random.randint(0,len(data.tetrisPieces)-1)
data.lastHeight = 0
data.pieceRotPos = 0
data.lastClear = 0
data.qPos = 0
data.currentQueue = list(range(7))
data.nextQueue = list(range(7))
random.shuffle(data.currentQueue)
random.shuffle(data.nextQueue)
data.scoring = [0,0,0,0]
data.numPlaced = 0
data.timeCounter = 0
data.speed = 4
newFallingPiece(data,data.pieceNum)
# holdPiece(data)
def inBounds(click,bounds):
if bounds[0] < click[0] < bounds[2]:
if bounds[1] < click[1] < bounds[3]:
return True
def mousePressed(event, data):
click = (event.x, event.y)
if data.gameMode == 0:
if inBounds(click,data.spButton):
print("1")
data.gameMode = 1
elif inBounds(click,data.vsButton):
print("2")
data.gameMode = 2
elif inBounds(click, data.AIButton):
print("3")
data.gameMode = 3
if inBounds(click,(data.margin*4//10,data.topMargin*10,data.margin*6//10,data.topMargin*15)):
data.speed = 5-int((event.y-data.topMargin*10)/data.topMargin)
# use event.x and event.y
def keyPressed(event, data):
if event.keysym == "Escape":
data.isGameOver = True
data.gameMode = 0
#Controls restart
if event.keysym == 'r':
init(data)
#Controls arrow movements
if not (data.isGameOver):
if event.keysym == "Left":
moveFallingPieces(data, 0, -1)
if event.keysym == "Right":
moveFallingPieces(data, 0, 1)
if event.keysym == "Down":
moveFallingPieces(data, 1, 0)
if event.keysym == "Up":
rotateFallingPiece(data)
rotateFallingPiece(data)
rotateFallingPiece(data)
if event.keysym == "??":
holdPiece(data)
if event.keysym == "space":
hardDrop(data)
if event.keysym == "t":
time.sleep(60)
if event.keysym == "q":
genReadout(data)
# firstScore = rateBoard(data)
# thing = copy.deepcopy(data)
# hardDrop(thing)
# print((data.pieceRotPos, data.fallingPieceCol))
# print(rateBoard(thing) - firstScore)
# print(data.fallingPiece)
#Moves pieces, if it can't then it places it
def timerFired(data):
if not (data.isGameOver):
data.timeCounter += 1
if not moveFallingPieces(data,+1,0):
placeFallingPiece(data)
if not fallingPieceIsLegal(data):
data.isGameOver = True
if not data.timedOut and data.maxPieces != -1 and data.numPlaced == data.maxPieces:
data.isGameOver = True
data.timedOut = True
genReadout(data)
def redrawAll(canvas, data):
canvas.create_rectangle(data.offset,0,data.width+data.offset, data.height, fill="orange")
drawBoard(canvas, data)
drawFallingPiece(canvas, data)
drawScore(canvas, data)
drawHold(canvas,data)
drawQueue(canvas,data)
drawSlider(canvas,data)
####################################
# Run function from 15-112 course notes
####################################
def run(width=300, height=300, maxPieces = -1,gameMode = 0):
def redrawAllWrapper(canvas, data):
canvas.delete(ALL)
canvas.create_rectangle(0, 0, data.width, data.height,
fill='white', width=0)
if gameMode != 0 and gameMode !=1:
canvas.create_rectangle(data.offset, 0, data.width+data.offset, data.height,
fill='white', width=0)
redrawAll(canvas, data)
# try:
# redrawAll(canvas, secData)
# except:
# pass
canvas.update()
def mousePressedWrapper(event, canvas, data):
mousePressed(event, data)
redrawAllWrapper(canvas, data)
try:
pass# redrawAll(canvas, secData)
except:
pass
def keyPressedWrapper(event, canvas, data):
keyPressed(event, data)
redrawAllWrapper(canvas, data)
try:
pass# redrawAll(canvas, secData)
except:
pass
def timerFiredWrapper(canvas, data):
if data.gameMode == 0:
drawStart(canvas,data)
else:
timerFired(data)
redrawAllWrapper(canvas, data)
try:
pass
# redrawAll(canvas, secData)
except:
secData = Struct()
secData.width = width*2
secData.height = height
secData.maxPieces = maxPieces
init(secData,offset=width)
data.width = data.width*2
AI(canvas, data, redrawAllWrapper,data.speed)
# pause, then call timerFired again
canvas.after(data.timerDelay, timerFiredWrapper, canvas, data)
# if data.isGameOver:
# root.destroy()
# Set up data and call init
class Struct(object): pass
data = Struct()
data.width = width
data.height = height
data.timerDelay = 10 # milliseconds
data.maxPieces = maxPieces
data.gameMode = gameMode
init(data)
# create the root and the canvas
root = Tk()
root.resizable(width=False, height=False) # prevents resizing window
canvas = Canvas(root, width=width, height=data.height)
canvas.configure(bd=0, highlightthickness=0)
canvas.pack()
# set up events
root.bind("<Button-1>", lambda event:
mousePressedWrapper(event, canvas, data))
root.bind("<Key>", lambda event:
keyPressedWrapper(event, canvas, data))
timerFiredWrapper(canvas, data)
# and launch the app
root.mainloop() # blocks until window is closed
genReadout(data)
print("bye!")
return data
# 1. Make a set of methods that simulates moves and drops
# like move left and drop, or move right and drop, or rotate
# #bestSpot = (rotation state, position state)
# bestSpot = (0,0)
# for i in range(4):
# tempPiece = rotate(piece, i)
# for j in range (0,10):
# tempPoints = Testdrop(board, tempPiece, j)
# if tempPoints > curMaxPts:
# curMaxPts = tempPoints
# bestSpot = (i,j)
# moveTo(i,j)
# 2.
def cReader(file):
f = open(file,"r")
coeffs = f.read()
f.close()
coeffs.replace("\n","")
coeffs = coeffs.split(" ")
new = [float(x) for x in coeffs]
return new
def cWriter(file, coeffs):
f = open(file,"w")
newFile = ""
for num in coeffs:
newFile += str(num) + " "
newFile = newFile[:-1]
f.write(newFile)
def rateBoard(data):
#original coeffs:
# line: 1
# hole: 0.5
# comp: 0.1
# noLineC = 0
# oneLineC = 0
# twoLineC = 0
# threeLineC = 0
# fourLineC = 0
lineCoeff,holeCoeff,compCoeff = cReader("coeffs.txt")
heightCoeff = .1
# lineCoeff = 1
# holeCoeff = .5
# compCoeff = 0.1
score = 0
# temp = [noLineC,oneLineC,twoLineC, threeLineC,fourLineC]
# score += data.lastClear*temp[data.lastClear]
if data.lastClear == 1:
score -= .25
elif data.lastClear == 2:
score -= 0.1
else:
score += data.lastClear**4*lineCoeff
# score += data.lastClear**2*lineCoeff
holes = 0
for i in range(data.cols):
top=0
newHoles = 0
while top<data.rows and data.board[top][i] == data.emptyColor:
top += 1
while top < data.rows:
if data.board[top][i] == data.emptyColor:
newHoles += 1
top += 1
holes += newHoles
score -= holes*holeCoeff
# score -= data.lastHeight*heightCoeff #minimize height you place stuff at
colHeight = []
for j in range(data.cols):
for i in range(data.rows):
if i == data.rows:
break
if data.board[i][j] != data.emptyColor:
break
colHeight.append(i)
compareHeight = 0
for i in range(data.cols-1):
compareHeight += abs(colHeight[i]-colHeight[i+1])
score -= compareHeight*compCoeff
return score
def findBestMove(data,bestScore = -float("inf")):
origScore = rateBoard(data)
alreadyChecked = []
bestMove = []
for i in range(4):
data.fallingPieceCol = 5
rotateFallingPiece(data)
# if data.fallingPiece not in
# For each rotation, move to the left and rotate piece once if you
# pass an index not on the right wall
data.fallingPieceCol = 0
# oneRot = time.time()
if data.fallingPiece not in alreadyChecked:
for j in range(10):
if moveFallingPieces(data, 0, 1):
if j == 0:
moveFallingPieces(data, 0, -1)
thing = copy.deepcopy(data)
hardDrop(thing)
rating = rateBoard(thing) - origScore
# print(rating)
if rating > bestScore:
bestScore = rating
bestMove = [(data.pieceRotPos, j,0)]
elif rating == bestScore:
bestMove.append((data.pieceRotPos, j,0))
# allMove.append((data.pieceRotPos,j,0))
alreadyChecked.append(copy.copy(data.fallingPiece))
if data.canSwitch:
holding = copy.deepcopy(data)
holdPiece(holding)
newMoves, newScore = findBestMove(holding,bestScore)
if newScore > bestScore and len(newMoves) > 0:
bestScore = newScore
bestMove = newMoves
for i in range(len(bestMove)):
bestMove[i] = (bestMove[i][0], bestMove[i][1], 1)
return bestMove,bestScore
def makeMove(data,bestMove):
# time.sleep(.1)
if bestMove[2] == 1:
holdPiece(data)
moveFallingPieces(data, 0, -1)
while data.pieceRotPos != bestMove[0]:
rotateFallingPiece(data)
data.fallingPieceCol = bestMove[1]
def AI(canvas,data,redraw,speed=5):
if not fallingPieceIsLegal(data):
data.isGameOver = True
if data.isGameOver:
return
start = time.time()
# try:
# bestMove = data.nextMove
# bestScore = -float("inf")
# except:
bestMove,bestScore = findBestMove(data)
if len(bestMove) > 1:
newBestMoves = []
topNextScore = -float('inf')
for move in bestMove:
testHold = copy.deepcopy(data)
makeMove(testHold,move)
hardDrop(testHold)
newMoves,newScore = findBestMove(testHold,bestScore)
if newScore > topNextScore and len(newMoves) > 0:
topNextScore = newScore
newBestMoves = [move]
if newBestMoves != []:
bestMove = newBestMoves
try:
bestMove = random.choice(bestMove)
makeMove(data, bestMove)
except:
pass
# time.sleep((5-speed)/10)
redraw(canvas, data) #this function takes hella long
# time.sleep((5-speed)/10)
hardDrop(data)
# print("BEST: ", bestMove, bestScore, "Time: ", time.time()-start)
def testCoeffs(reps,maxPieces):
listRats = []
timedOut = True
for i in range(reps):
output = playTetris(maxPieces)
listRats.append(output.score/output.numPlaced)
if not timedOut:
timedOut = False
break
return sum(listRats)/reps, timedOut
def gradDescent():
alpha = .05
maxPieces = 500
reps = 1
cFile = "coeffs.txt"
coeffs = cReader(cFile)
# bestRatio = []
bestRatio = 0
bestCoeffs = []
for line in range(25,125,25):
for hole in range(25,125,25):
for side in range(1,6,1):
coeffs = [line/100,hole/100,side/10]
print(coeffs)
cWriter(cFile,coeffs)
rat, timedOut = testCoeffs(reps,maxPieces)
if not timedOut:
continue
else:
if rat-bestRatio > 0.1:
bestCoeffs = coeffs
print(bestCoeffs)
print(bestRatio)
# gradDescent()
#
#
#
#
# # Iterating through each feature
# for i in range(len(coeffs)):
# #Run the game, then run it again with a slightly changed coeff
# lastRat,timedOut1 = testCoeffs(reps,maxPieces)
#
# coeffs[i] += alpha
# cWriter(cFile,coeffs)
#
# newRat,timedOut2 = testCoeffs(reps, maxPieces)
# print(lastRat)
# print(newRat)
# #if the ratio goes up, change coeffs again
# while newRat-lastRat > 0.01:
# #if game just straight up lost after change, change coeffs immediately
# if not timedOut2:
# coeffs[i] += alpha
# cWriter(cFile, coeffs)
# lastRat = testCoeffs(reps, maxPieces)
# else:
# lastRat = newRat
#
# # either way, test again
#
# coeffs[i] += alpha
# cWriter(cFile, coeffs)
#
# newRat = testCoeffs(reps, maxPieces)
# gradDescent()
playTetris()
def tester():
maxPieces = 50
reps = 100
lst = []
for i in range(reps):
output = playTetris(maxPieces)
lst.append(output.score/output.numPlaced)
print(lst)
# tester()
# yah = playTetris(200)
# print(yah.numPlaced)
# AI should
# - take in board state,
# - find the best move and score for the current piece, returning them
# - do each best move (in a new data) and find bestmove and bestscore for next piece in each new data
# - only keep moves that return the highest future return ???
# - make new data where it does each bestmove and finds the next best score
# TODO: You commented out two redraws (692,458), and the BEST:
# fix lookahead
# have it auto-optimize variables
# New tetris pieces test