-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
81 lines (75 loc) · 3.06 KB
/
Game.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
import HexaPawn as hp
import os
from time import sleep
def clear():
if os.name == 'nt':
os.system('cls')
elif os.name == 'posix':
os.system('clear')
print(initMessage)
print(game.boardGame.printBoard())
print('Total of games played: %i' % game.gamesPlayed)
print('%s total wins: %i' % (game.humanName, game.humanWins))
print('%s total wins: %i' % (game.cpuName, game.machineWins))
initMessage = '''
#################################################################################
#################################################################################
## ##
## _ _ ____ ##
## | | | | _____ ____ _| _ \\ __ ___ ___ __ ##
## | |_| |/ _ \\ \\/ / _` | |_) / _` \\ \\ /\\ / / '_ \\ ##
## | _ | __/> < (_| | __/ (_| |\\ V V /| | | | ##
## |_| |_|\\___/_/\\_\\__,_|_| \\__,_| \\_/\\_/ |_| |_| ##
## ##
## ##
#################################################################################
#################################################################################
'''
humanName = input('What\'s your name, human? ')
cpuName = input('What machine would you like to play? ')
game = hp.HexaPawn(humanName,cpuName)
game.startNewGame()
while True:
clear()
if game.playerToMove == 0:
humanMove = input('Play your move, %s [format: row,col:row,col]: ' % game.humanName)
convertedMove = game.boardGame.convertMove(humanMove)
if not convertedMove:
print('Invalid move format. Try again!')
sleep(3)
continue
if game.makeMove(convertedMove[0], convertedMove[1]):
if game.checkWin():
game.humanWins += 1
game.machine.learn(False)
clear()
print('\n%s, you have won this game! %s has learned from its mistakes!' % (game.humanName, game.cpuName))
print('%s has %i victories.' % (game.humanName, game.humanWins))
print('%s has %i victories.' % (game.cpuName, game.machineWins))
sleep(5)
game.newGeneration()
else:
game.playerToMove = (game.playerToMove + 1) % 2
game.round += 1
else:
print('Invalid move! Try again!')
sleep(3)
continue
else:
machineMove = game.machine.chooseMove(game.boardGame.getBoardState(), game.round)
convertedMachineMove = game.boardGame.convertMove(machineMove)
game.boardGame.updateBoard(convertedMachineMove[0], convertedMachineMove[1])
print(convertedMachineMove)
sleep(3)
if game.checkWin():
game.machineWins += 1
game.machine.learn(True)
clear()
print('\n%s has won this game! It\'s learning!' % game.cpuName)
print('%s has %i victories.' % (game.humanName, game.humanWins))
print('%s has %i victories.' % (game.cpuName, game.machineWins))
sleep(5)
game.newGeneration()
else:
game.playerToMove = (game.playerToMove + 1) % 2
game.round += 1