-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgameLogic.py
140 lines (127 loc) · 5.48 KB
/
gameLogic.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
import numpy as np
import train
from random import randint as rand
from keras.models import model_from_json
from keras.utils.vis_utils import plot_model
class Game:
def __init__(self, row_size=6, column_size=7):
self.board = np.zeros((row_size, column_size), int)
self.turn = 1 if rand(0, 1) == 1 else 2
self.amountOfCheckers = 42 #comes with 21 yellow checkers and 21 red checkers for a total of 42 :)
self.inputColumn = -1 #just for initilization
self.winner = None
self.game_over = False
self.model = self.getAnExistModel()
self.twoPlayer = False
def predict (self, board):
Y = self.model.predict(board.flatten().reshape((1, 42)))
return Y[0][self.turn]
def getAnExistModel(self) :
try:
model_file = open('Data/Model/model.json', 'r')
model = model_file.read()
model_file.close()
model = model_from_json(model)
model.load_weights('Data/Model/weights.h5')
return model
except:
train.main()
return self.getAnExistModel()
def getTurn (self):
if self.turn == 1 :
return 'Player1'
else:
if self.twoPlayer:
return 'Player2'
else:
return 'Computer'
def changeTurn (self):
if self.turn == 1:
self.turn = 2
else:
self.turn = 1
def move(self, pos, play_yourself=False):
if (self.twoPlayer == True or self.turn == 1) and not play_yourself:
column = (pos[0]-50)//100
if column <0 :
column=0
elif column >6:
column=6
for row in range(5, -1, -1):
if self.board[row][column] == 0 :
self.board[row][column] = self.turn
self.amountOfCheckers -= 1
return row, column
return -1 , -1
else :
#computer's input play_yourself:
possibleMoves = []
for column in range (7):
copy_of_board = np.matrix.copy(pos)
for row in range (5,-1,-1):
if copy_of_board[0][column] != 0 : continue
if copy_of_board[row][column] == 0:
copy_of_board[row][column] = self.turn
possibleMoves.append((row,column,self.predict(copy_of_board)))
break
row, col, prob = possibleMoves[0]
for i in range (len(possibleMoves)) :
if possibleMoves[i][2] > prob:
row,col ,prob = possibleMoves[i]
self.board[row][col] = self.turn
self.amountOfCheckers -= 1
return row, col
def moveWithRandomInputs (self) :
column = rand(0,6)
for row in range(5, -1, -1):
if self.board[row][column] == 0:
self.board[row][column] = self.turn
self.amountOfCheckers -= 1
return
self.moveWithRandomInputs()
def isGameFinished (self):
for r in range(3): # test diagonally ()
for c in range(4):
if self.board[r][c] == self.turn and self.board[r][c] == self.board[r + 1][c + 1] and self.board[r + 1][c + 1] == self.board[r + 2][
c + 2] and self.board[r + 2][c + 2] == self.board[r + 3][c + 3] :
self.game_over = True
self.winner = self.getTurn()
if not self.game_over :
for r in range(5,2,-1): # test diagonally (ters birim matris)
for c in range(4):
if self.board[r][c] == self.turn and self.board[r][c] == self.board[r - 1][c + 1] and self.board[r - 1][c + 1] == self.board[r - 2][
c + 2] and self.board[r - 2][c + 2] == self.board[r - 3][c + 3]:
self.game_over = True
self.winner = self.getTurn()
if not self.game_over : # test horizontally
for r in range(6):
for c in range(4):
if self.board[r][c] == self.turn and self.board[r][c] == self.board[r][c + 1] and self.board[r][c + 1] == self.board[r][
c + 2] and self.board[r][c + 2] == self.board[r][c + 3]:
self.game_over = True
self.winner = self.getTurn()
if not self.game_over :
for r in range(3): # test vertically
for c in range(7):
if self.board[r][c] == self.turn and self.board[r][c] == self.board[r + 1][c] and self.board[r + 1][c] == self.board[r + 2][
c] and self.board[r + 2][c] == self.board[r + 3][c]:
self.game_over = True
self.winner = self.getTurn()
if not self.game_over :
if self.amountOfCheckers == 0 :
self.winner = 'Draw'
self.game_over = True
else :
self.changeTurn()
if self.game_over :
with open("Data/dataset.csv", "a+") as f:
for row in self.board:
for element in row :
f.write(str(element)+',')
if self.winner == 'Player1':
f.write(str(1))
elif self.winner == 'Player2' or 'Computer':
f.write(str(2))
else:
f.write(str(0))
f.write('\n')