-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
executable file
·166 lines (146 loc) · 7.08 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
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
from Board import Board
from Player import Player
import logging
logger = logging.getLogger(__name__)
# formatter = logging.Formatter('{%(pathname)s:%(lineno)d}')
FORMAT = '[%(filename)s:%(lineno)d] %(message)s'
# FORMAT = '%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s'
# DATEFMT = '%Y-%m-%d:%H:%M:%S'
LEVEL = logging.INFO
logging.basicConfig(format=FORMAT, level=LEVEL)
# logger = logging.getLogger(__name__)
# logger.debug("This is a debug log")
# logger.info("This is an info log")
# logger.critical("This is critical")
# logger.error("An error occurred")
class Game():
def __init__(self):
self._turn = 'White'
self.white_player = None
self.black_player = None
self._board = None
self._game_over = False
@property
def board(self):
return self._board
@board.setter
def board(self, new_board):
self._board = new_board
@property
def turn(self):
return self._turn
@turn.setter
def turn(self, next_turn):
self._turn = next_turn
def finish_turn(self):
self.turn = 'Black' if self.turn == 'White' else 'White'
def create_new_game(self):
'''this method should create and set a new board
- should prompt users for player names
- prompt users for color choice
- initiate the game
'''
self.board = Board()
self.board.create_board()
self.board.create_pieces_for_new_game()
self.board.set_pieces_for_new_game()
player_1_name = input('Enter a name for player 1: ').title()
player_2_name = input('Enter a name for player 2: ').title()
white_player_number = None
while white_player_number not in ['1','2']:
white_player_number = input('Which player wants to use white pieces? Enter 1 or 2: ')
if white_player_number == '1':
self.white_player = Player(player_1_name, 'White')
self.black_player = Player(player_2_name, 'Black')
else:
self.black_player = Player(player_1_name, 'Black')
self.white_player = Player(player_2_name, 'White')
print(f'''
{self.white_player} vs {self.black_player}''')
def start_turn_sequence(self):
print(self.board)
player = self.white_player if self.turn == 'White' else self.black_player
opponent = self.black_player if self.turn =='White' else self.white_player
file_rank = '--'
piece_pos = None
piece = None
valid_move_pos_list = None
while piece_pos is None or piece is None:
file_rank = input(f'''Player {player.name}
Enter a piece to move by file and rank: ''')
piece_pos = self.board.translate_file_rank_to_col_row(file_rank)
piece = self.board.is_valid_piece_selection(piece_pos, self.turn)
if piece is not None:
valid_move_pos_list = self.board.get_valid_moves(piece_pos, self.turn)
if len(valid_move_pos_list) == 0:
piece = None
print('No available moves for that piece.')
continue
else:
continue # if piece IS none, then is_valid_piece_selection returned none
# that means we want to give them another chance to enter a piece
move_file_rank = None
valid_file_ranks = [self.board.translate_col_row_to_file_rank(pos)
for pos in valid_move_pos_list]
valid_file_ranks_str = ''
for file_rank_opt in valid_file_ranks:
valid_file_ranks_str += file_rank_opt + ' '
while True:
move_file_rank = input(f'''{player.name}'s turn
Enter the file and rank of where you want to move your {piece.name}.
Piece currently at: {file_rank}
Possible moves are: {valid_file_ranks_str}
Selected move: ''')
if move_file_rank in valid_file_ranks:
move_pos = self.board.translate_file_rank_to_col_row(move_file_rank)
# check that self not put in check with selected move
# logging.info(self.board.king_in_check(player.color, ))
king_pos = move_pos if piece.name == 'King' else None
if self.board.king_in_check(player.color, king_pos=king_pos):
print(''' You cannot put your own King in check - try again.
''')
piece = None
break
# move_piece method moves piece at piece_pos to move_pos
# if space was previously occupied it returns the captured piece;
# otherwise it returns none
captured_piece = self.board.move_piece(piece_pos, move_pos)
if captured_piece is not None:
print(f''' You captured {opponent.name}\'s {captured_piece.name}''')
# before we finish player's turn, we check if the opponents king is in check
if self.board.king_in_check(opponent.color):
print(f''' Heads up {opponent.name}! Your King is in check.
You next move must get your King out of check or you LOSE.''')
break
else:
print('''That is not a valid move - try again.
''')
continue
# after every time a piece is moved, we need to check that moved piece's valid moves
# if we check after every successful piece move, we will never miss it
# variable move_pos holds the current position of the piece that was just moved.
# make a method on board class called king_put_in_check -
# this method will call get_valid_moves for the piece that just moved
# this method will also locate the opponents king position
# if the kings position is in valid_moves, then king_put_in_check should return True.
# king_in_check - returns boolean if king in check or not
# if a selected move puts player's king in check - move not allowed
# if a selected move puts opponents king in check - opponent is alerted
# opponent must select a move that moves king out of check
# if for all valid moves of all opponent's remaining pieces the king is still in check, then check_mate - game over
# if any of that piece's valid moves includes the opponent's king, we need to tell the opponent that they are in check
# we need to save all moves that result in capturing the king
# we need to make sure that the opponents next move takes the king out of check
# if none of those moves exists - CHECK MATE!
self.finish_turn()
if self._game_over:
pass
# do something to end game, display winner, etc
else:
self.start_turn_sequence()
# square = self.board.get_square(move_pos)
# print(square)
game = Game()
game.create_new_game()
# print(game.board)
game.start_turn_sequence()