-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
executable file
·97 lines (82 loc) · 3.18 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
## DeepYellowJ - Version 0.1 (c) 2016 Ahmad Nazeri
# Game - game.py
#
# This file creates everything needed
# for the chess game.
# It creates:
# 1) Board
# 2) Players
# 3) LegalMoveChecker
# And it keeps track of the moves to
# look after the game is over.
# Import classes from other files
from board import Board
from legalmovechecker import LegalMoveChecker
from player import Player
from deepYellowJ import DeepYellowJ
# Import a sleep, to pause between moves
from time import sleep
# Used to determine the size of board
SIZE = 8
# Game class is the main part of DeepYellowJ.
# It creates the board, players, and keeps track of
# whose move, what the last move, 50 moves, and previous
# 3 positions.
class Game():
# The Constructor for Game class
def __init__(self):
self._gameNotation = []
self._board = Board()
self._player = [Player("w", True, self._board), Player("b", True, self._board)]
self._whoseMove = 0
self._mvChecker = LegalMoveChecker(self._board)
self._lastMove= "" # not implemented yet
self._move50 = 0 # not implemented yet
self._previousPositions = [None for i in range(6)] # not implemented yet
# Starts the Game
def startGame(self):
# Creates the chessboard and draws the board and pieces
self._board.initialize()
while not self.isGameOver():
sleep(1) # used to pause between moves
player = self._player[self._whoseMove]
moves = player.getMove() # receives move from player/DeepYellowJ
# Checks if it is a legal move
if self._mvChecker.isLegalMove(moves[0], moves[1]):
# Makes the move
self._board.move(moves[0], moves[1])
else:
print(moves[0] + ", " + moves[1] + " Not Legal Move")
# Keeps track of moves
self._gameNotation.append((moves[0], moves[1]))
# Updates whose move
self._whoseMove = (self._whoseMove + 1)%2
# Determines if the game is over
# Currently, see if the King is taken
#
# Returns:
# gameOver - Boolean
def isGameOver(self):
# Used to go through each square
columns = ["A", "B", "C", "D", "E", "F", "G", "H"]
gameOver = True
# Goes through to find the King
for i in range(SIZE):
for j in range(SIZE):
square = columns[j]+str(SIZE-i)
piece = self._board.getPiece(square)
player = self._player[self._whoseMove]
# Determines if the King doesn't exist
if piece != None and piece.getName() == "K" and piece.getColor() == player.color():
gameOver = False
return gameOver
# Restarts the Game
def restartGame(self):
self._gameNotation = []
self._board.resetBoard()
self._player = [Player("w", True, self._board), Player("b", True, self._board)]
self._mvChecker = LegalMoveChecker(self._board)
self.startGame()
# Starts the game!
g = Game()
g.startGame()