GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( 86b96e...4422cd )
by Theo
01:44
created

ChessBoard.end_locations_for_piece_at_location()   B

Complexity

Conditions 7

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7
Metric Value
cc 7
dl 0
loc 19
rs 7.3333
ccs 18
cts 18
cp 1
crap 7
1
# pylint: disable=R0902
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
# disabling too many instance attributes for now.
3
# once I implement enpassant and castling,
4
# I shouldn't need some of the FEN attributes.
5 1
import json
6
7 1
from .. import standard_chess_json
8 1
from .base import Board
9 1
from ..piece import Piece
10
11 1
from ..movement import get_all_potential_end_locations
12 1
from .. import movement
13
14 1
map_fen_to_piece_name = {
15
    'k': "king",
16
    'q': "queen",
17
    'n': "knight",
18
    'b': "bishop",
19
    'r': "rook",
20
    'p': "pawn"
21
}
22
23
24 1
class ChessBoard(Board):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
25
26 1
    def __init__(self, existing_board=None):
27 1
        super().__init__(8, 8)
28 1
        self.pieces = []
29 1
        self.players = {}
30 1
        self.end_game = {}
31
32 1
        self.standard_chess = standard_chess_json
33
34 1
        if not existing_board:
35 1
            existing_board = self.load_json()
36
37 1
        self.initialize_board(existing_board)
38
39
        # FEN data I should take into account
40 1
        self.current_players_turn = "w"
41 1
        self.castling_opportunities = "KQkq"
42 1
        self.en_passant_target_square = "-"
43 1
        self.half_move_clock = 0
44 1
        self.full_move_number = 1
45
46 1
    def __getitem__(self, key):
47 1
        return self.board[key]
48
49 1
    def __setitem__(self, key, value):
50 1
        self.board[key] = value
51
52 1
    def __iter__(self):
53 1
        for key in self.board:
54 1
            yield key
55
56 1
    def __len__(self):
57 1
        return len(self.board)
58
59 1
    def generate_fen(self):
60
        """Generates a FEN representation of the board."""
61 1
        board = ""
62
        # FEN notation starts in the top left
63 1
        for row in range(self.rows - 1, -1, -1):
64 1
            num_missing = 0
65 1
            for column in range(0, self.columns):
66 1
                key = (row, column)
67 1
                piece = self[key]
68
69 1
                if piece:
70 1
                    prepend = ''
71 1
                    if num_missing:
72 1
                        prepend = str(num_missing)
73 1
                    board += prepend + repr(piece)
74 1
                    num_missing = 0
75
                else:
76 1
                    num_missing += 1
77 1
            if num_missing:
78 1
                board += str(num_missing)
79 1
            board += "/"
80
81 1
        other_info = " {cpt} {co} {epts} {hmc} {fmn}".format(cpt=self.current_players_turn,
82
                                                             co=self.castling_opportunities,
83
                                                             epts=self.en_passant_target_square,
84
                                                             hmc=self.half_move_clock,
85
                                                             fmn=self.full_move_number)
86 1
        fen = board[0:-1] + other_info
87 1
        return fen
88
89 1
    def import_fen_board(self, fen_board):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
90 1
        json_data = self.load_json()
91 1
        board_data, self.current_players_turn, self.castling_opportunities,\
92
            self.en_passant_target_square, self.half_move_clock,\
93
            self.full_move_number = fen_board.split(' ')
94 1
        rows = board_data.split('/')
95 1
        for row in range(self.rows - 1, -1, -1):
96 1
            fen_row = rows[7 - row]
97 1
            actual_column = 0
98 1
            for column in range(0, len(fen_row)):
99 1
                try:
100 1
                    num_missing = int(fen_row[actual_column])
101 1
                    for column in range(0, num_missing):
102 1
                        self[(row, actual_column)] = None
103 1
                        actual_column += 1
104 1
                except ValueError:
105 1
                    name = map_fen_to_piece_name[fen_row[actual_column].lower()]
106 1
                    color = "black" if fen_row[actual_column].islower() else "white"
107 1
                    moves = self.get_piece_moves(name, json_data)
108 1
                    self[(row, column)] = Piece(name, color, moves)
109 1
                    actual_column += 1
110
111 1
    def export(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
112 1
        json_data = {}
113 1
        json_data['pieces'] = {}
114 1
        for piece in self.pieces:
115 1
            json_data['pieces'][piece.kind] = {'moves': piece.moves}
116
117 1
        json_data['players'] = self.players
118 1
        if self.current_players_turn == 'w':
119 1
            json_data['players']['current'] = "Player 1"
120
        else:
121
            json_data['players']['current'] = "Player 2"
122
123 1
        map_color_to_name = {}
124 1
        json_board = {}
125 1
        for player in self.players:
126 1
            if player != 'current':
127 1
                map_color_to_name[self.players[player]['color']] = player
128 1
                json_board[player] = {}
129
130 1
        for location in self:
131 1
            piece = self[location]
132 1
            if piece:
133 1
                player = map_color_to_name[piece.color]
134 1
                if piece.kind in json_board[player]:
135 1
                    json_board[player][piece.kind].append(list(location))
136
                else:
137 1
                    json_board[player][piece.kind] = [list(location)]
138
139 1
        json_data['board'] = json_board
140
141 1
        json_data['end_game'] = self.end_game
142
143 1
        print("export data is:")
144 1
        print(json_data)
145 1
        return json_data
146
147 1
    def initialize_board(self, json_data):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
148 1
        json_board = json_data['board']
149
150 1
        self.end_game = json_data['end_game']
151 1
        for player in ['Player 1', 'Player 2']:
152 1
            players_data = json_data['players']
153 1
            color = players_data[player]['color']
154
155 1
            self.players[player] = players_data[player]
156
157 1
            player_pieces = json_board[player]
158 1
            for piece in player_pieces:
159 1
                name = piece
160 1
                moves = self.get_piece_moves(name, json_data)
161 1
                a_piece = Piece(name, color, moves)
162
163 1
                self.pieces.append(a_piece)
164
165 1
                for location in player_pieces[piece]:
166 1
                    self[tuple(location)] = a_piece
167
168 1
        if json_data['players']['current'] == "Player 1":
169 1
            self.current_players_turn = 'w'
170
        else:
171
            self.current_players_turn = 'b'
172
173 1
    def clear_board(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
174 1
        for location in self:
175 1
            self[location] = None
176
177 1
    @staticmethod
178
    def get_piece_moves(name, json_data):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
179 1
        return json_data['pieces'][name]['moves']
180
181 1
    def load_json(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
182 1
        filename = self.standard_chess
183 1
        data = None
184 1
        with open(filename) as data_file:
185 1
            data = json.load(data_file)
186
187 1
        return data
188
189 1
    def end_locations_for_piece_at_location(self, start_location):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
190 1
        piece = self[start_location]
191 1
        player_direction = None
192 1
        for player in self.players:
193 1
            if piece.color == self.players[player]['color']:
194 1
                player_direction = self.players[player]['direction']
195 1
                break
196
197 1
        all_end_points = []
198 1
        for move in piece.moves:
199 1
            directions = move['directions']
200 1
            conditions = [getattr(movement, condition) for condition in move['conditions'] if hasattr(movement, condition)]
201 1
            ends = get_all_potential_end_locations(start_location, directions, self)
202 1
            for condition in conditions:
203 1
                print("ends before condition: {} are: {}".format(condition, ends))
204 1
                ends = condition(self, start_location, directions, ends, player_direction)
205 1
                print("ends after condition: {} are: {}".format(condition, ends))
206 1
            all_end_points += ends
207 1
        return all_end_points
208
209 1
    def move(self, start_location, end_location):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
210 1
        if self.is_valid_move(start_location, end_location):
211 1
            if self.current_players_turn == 'w':
212 1
                if self[start_location].color == 'black':
213
                    return False
214 1
                self.current_players_turn = 'b'
215
            else:
216
                if self[start_location].color == 'white':
217
                    return False
218
                self.full_move_number += 1
219
                self.current_players_turn = 'w'
220 1
            is_capture = self[end_location] is not None
221 1
            self[end_location] = self[start_location]
222 1
            self[start_location] = None
223 1
            self[end_location].move_count += 1
224 1
            if self[end_location].kind != "pawn" and not is_capture:
225 1
                self.half_move_clock += 1
226
            else:
227
                self.half_move_clock = 0
228 1
            return True
229 1
        return False
230
231 1
    def is_valid_move(self, start_location, end_location):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
232 1
        possible_moves = self.valid_moves(start_location)
233 1
        if end_location in possible_moves:
234 1
            return True
235 1
        return False
236
237 1
    def valid_moves(self, start_location):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
238 1
        return self.end_locations_for_piece_at_location(start_location)
239
240 1
    @property
241
    def board(self):
242
        return self._board
243