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
Pull Request — develop (#14)
by Theo
01:51
created

ChessBoard   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Test Coverage

Coverage 92.98%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 228
ccs 159
cts 171
cp 0.9298
rs 4.8387
wmc 58

17 Methods

Rating   Name   Duplication   Size   Complexity  
A get_piece_moves() 0 3 1
B generate_fen() 0 29 6
B initialize_board() 0 25 5
A __setitem__() 0 2 1
A __getitem__() 0 2 1
D export() 0 35 8
A clear_board() 0 3 2
A load_json() 0 7 2
A is_valid_move() 0 5 2
A __iter__() 0 3 2
B import_fen_board() 0 21 6
B __init__() 0 22 4
A board() 0 3 1
C end_locations_for_piece_at_location() 0 21 8
A valid_moves() 0 2 1
C move() 0 25 7
A __len__() 0 2 1

How to fix   Complexity   

Complex Class

Complex classes like ChessBoard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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 1
        if existing_board and existing_board['players']['current'] == "Player 1":
39 1
            self.current_players_turn = "w"
40
        else:
41
            self.current_players_turn = "b"
42
43
        # FEN data I should take into account
44 1
        self.castling_opportunities = "KQkq"
45 1
        self.en_passant_target_square = "-"
46 1
        self.half_move_clock = 0
47 1
        self.full_move_number = 1
48
49 1
    def __getitem__(self, key):
50 1
        return self.board[key]
51
52 1
    def __setitem__(self, key, value):
53 1
        self.board[key] = value
54
55 1
    def __iter__(self):
56 1
        for key in self.board:
57 1
            yield key
58
59 1
    def __len__(self):
60 1
        return len(self.board)
61
62 1
    def generate_fen(self):
63
        """Generates a FEN representation of the board."""
64 1
        board = ""
65
        # FEN notation starts in the top left
66 1
        for row in range(self.rows - 1, -1, -1):
67 1
            num_missing = 0
68 1
            for column in range(0, self.columns):
69 1
                key = (row, column)
70 1
                piece = self[key]
71
72 1
                if piece:
73 1
                    prepend = ''
74 1
                    if num_missing:
75 1
                        prepend = str(num_missing)
76 1
                    board += prepend + repr(piece)
77 1
                    num_missing = 0
78
                else:
79 1
                    num_missing += 1
80 1
            if num_missing:
81 1
                board += str(num_missing)
82 1
            board += "/"
83
84 1
        other_info = " {cpt} {co} {epts} {hmc} {fmn}".format(cpt=self.current_players_turn,
85
                                                             co=self.castling_opportunities,
86
                                                             epts=self.en_passant_target_square,
87
                                                             hmc=self.half_move_clock,
88
                                                             fmn=self.full_move_number)
89 1
        fen = board[0:-1] + other_info
90 1
        return fen
91
92 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...
93 1
        json_data = self.load_json()
94 1
        board_data, self.current_players_turn, self.castling_opportunities,\
95
            self.en_passant_target_square, self.half_move_clock,\
96
            self.full_move_number = fen_board.split(' ')
97 1
        rows = board_data.split('/')
98 1
        for row in range(self.rows - 1, -1, -1):
99 1
            fen_row = rows[7 - row]
100 1
            actual_column = 0
101 1
            for column in range(0, len(fen_row)):
102 1
                try:
103 1
                    num_missing = int(fen_row[actual_column])
104 1
                    for column in range(0, num_missing):
105 1
                        self[(row, actual_column)] = None
106 1
                        actual_column += 1
107 1
                except ValueError:
108 1
                    name = map_fen_to_piece_name[fen_row[actual_column].lower()]
109 1
                    color = "black" if fen_row[actual_column].islower() else "white"
110 1
                    moves = self.get_piece_moves(name, json_data)
111 1
                    self[(row, column)] = Piece(name, color, moves)
112 1
                    actual_column += 1
113
114 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...
115 1
        json_data = {}
116 1
        json_data['pieces'] = {}
117 1
        for piece in self.pieces:
118 1
            json_data['pieces'][piece.kind] = {'moves': piece.moves}
119
120 1
        json_data['players'] = self.players
121 1
        if self.current_players_turn == 'w':
122 1
            json_data['players']['current'] = "Player 1"
123
        else:
124
            json_data['players']['current'] = "Player 2"
125
126 1
        map_color_to_name = {}
127 1
        json_board = {}
128 1
        for player in self.players:
129 1
            if player != 'current':
130 1
                map_color_to_name[self.players[player]['color']] = player
131 1
                json_board[player] = {}
132
133 1
        for location in self:
134 1
            piece = self[location]
135 1
            if piece:
136 1
                player = map_color_to_name[piece.color]
137 1
                if piece.kind in json_board[player]:
138 1
                    json_board[player][piece.kind].append(list(location))
139
                else:
140 1
                    json_board[player][piece.kind] = [list(location)]
141
142 1
        json_data['board'] = json_board
143
144 1
        json_data['end_game'] = self.end_game
145
146 1
        print("export data is:")
147 1
        print(json_data)
148 1
        return json_data
149
150 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...
151 1
        json_board = json_data['board']
152
153 1
        self.end_game = json_data['end_game']
154 1
        for player in ['Player 1', 'Player 2']:
155 1
            players_data = json_data['players']
156 1
            color = players_data[player]['color']
157
158 1
            self.players[player] = players_data[player]
159
160 1
            player_pieces = json_board[player]
161 1
            for piece in player_pieces:
162 1
                name = piece
163 1
                moves = self.get_piece_moves(name, json_data)
164 1
                a_piece = Piece(name, color, moves)
165
166 1
                self.pieces.append(a_piece)
167
168 1
                for location in player_pieces[piece]:
169 1
                    self[tuple(location)] = a_piece
170
171 1
        if json_data['players']['current'] == "Player 1":
172 1
            self.current_players_turn = 'w'
173
        else:
174
            self.current_players_turn = 'b'
175
176 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...
177 1
        for location in self:
178 1
            self[location] = None
179
180 1
    @staticmethod
181
    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...
182 1
        return json_data['pieces'][name]['moves']
183
184 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...
185 1
        filename = self.standard_chess
186 1
        data = None
187 1
        with open(filename) as data_file:
188 1
            data = json.load(data_file)
189
190 1
        return data
191
192 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...
193 1
        piece = self[start_location]
194 1
        if not piece:
195
            return []
196 1
        player_direction = None
197 1
        for player in self.players:
198 1
            if piece.color == self.players[player]['color']:
199 1
                player_direction = self.players[player]['direction']
200 1
                break
201
202 1
        all_end_points = []
203 1
        for move in piece.moves:
204 1
            directions = move['directions']
205 1
            conditions = [getattr(movement, condition) for condition in move['conditions'] if hasattr(movement, condition)]
206 1
            ends = get_all_potential_end_locations(start_location, directions, self)
207 1
            for condition in conditions:
208
                # print("ends before condition: {} are: {}".format(condition, ends))
209 1
                ends = condition(self, start_location, directions, ends, player_direction)
210
                # print("ends after condition: {} are: {}".format(condition, ends))
211 1
            all_end_points += ends
212 1
        return all_end_points
213
214 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...
215 1
        if self.is_valid_move(start_location, end_location):
216 1
            if self.current_players_turn == 'w':
217 1
                if self[start_location].color == 'black':
218
                    print('black trying to move, but whites turn')
219
                    return False
220 1
                self.current_players_turn = 'b'
221
            else:
222
                if self[start_location].color == 'white':
223
                    print('white trying to move, but blacks turn')
224
                    return False
225
                self.full_move_number += 1
226
                self.current_players_turn = 'w'
227 1
            print("is valid move")
228 1
            is_capture = self[end_location] is not None
229 1
            self[end_location] = self[start_location]
230 1
            self[start_location] = None
231 1
            self[end_location].move_count += 1
232 1
            if self[end_location].kind != "pawn" and not is_capture:
233 1
                self.half_move_clock += 1
234
            else:
235
                self.half_move_clock = 0
236 1
            return True
237 1
        print('is not valid move')
238 1
        return False
239
240 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...
241 1
        possible_moves = self.valid_moves(start_location)
242 1
        if end_location in possible_moves:
243 1
            return True
244 1
        return False
245
246 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...
247 1
        return self.end_locations_for_piece_at_location(start_location)
248
249 1
    @property
250
    def board(self):
251
        return self._board
252