/
train.py
executable file
·157 lines (111 loc) · 3.16 KB
/
train.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
#! /usr/bin/python
import argparse
from core.board import board
from core.opponent import opponent
from core.move import move
from core.seed import seed
toLog=None
def log(statement):
if toLog:
print(statement)
def checkForCaptures(pieceList):
to_return = pieceList
captureList = []
for piece in pieceList:
for _move in piece.moves:
if _move.moveType == 2:
captureList.append(piece)
continue
if captureList != []:
to_return = captureList
return to_return
def checkForMoves(pieceList):
to_return = False
for piece in pieceList:
if piece.moves != []:
to_return = True
break
return to_return
def score(red, black, winStatus):
redScore = 10*len(red)
blackScore = 15*len(black)
winScore=0
if winStatus == 1:
winScore=100
else:
winScore=-100
score = redScore-blackScore+winScore
return score
def turns(turn):
if turn == 'Red':
turn = 'Black'
pieces = gameboard.Black
else:
turn = 'Red'
pieces = gameboard.Red
log('It is '+turn+'\'s turn.')
return turn, pieces
def progMove(pieces, prog):
to_return = False
pieceList = []
for piece in pieces:
hasValidMoves = piece.getPieceMoves()
if hasValidMoves == True:
pieceList.append(piece)
if checkForMoves(pieceList) == True:
pieceList = checkForCaptures(pieceList)
_move = prog.move(pieceList)
gameboard.updatePiece(_move)
to_return = True
return to_return
def trainGame():
to_return = 0
moveCount = 0
winStatus = None
gameboard.printBoard()
gameLoop = True
turn = 'Red'
pieces = gameboard.Red
log('It is Red\'s turn.')
while gameLoop:
gameboard.printBoard()
if turn == 'Red':
validTurn = progMove(pieces, trainee)
else:
validTurn = progMove(pieces, trainer)
if validTurn == False:
log(turn + ' has no moves left. Game over.')
if turn == 'Red':
to_return = 0
else:
to_return = 1
break
gameboard.moveCount +=1
turn, pieces = turns(turn)
if gameboard.moveCount == 60:
log('Draw')
to_return = 0
gameLoop = gameboard.win()
return to_return
parser = argparse.ArgumentParser(description='Input commands.')
parser.add_argument('-log', default='n', choices=['y','n'], help='Display output of the game?')
parser.add_argument('-seed', default='', help='Seed value to generate the opponent')
args = vars(parser.parse_args())
if args['log']=='y':
toLog=True
else:
toLog=False
if args['seed'] == "":
val = None
elif args['seed'] != "":
val = args['seed']
gameboard = board("Red", toLog)
trainee = opponent('Red', seed(None), toLog)
trainer = opponent('Black', seed(val), toLog)
trainer.genPieceProgram()
trainer.genMoveProgram()
trainee.genPieceProgram()
trainee.genMoveProgram()
winStatus =trainGame()
points = score(gameboard.Red, gameboard.Black, winStatus)
print points