Skip to main content
added 101 characters in body
Source Link
Justin
  • 2.6k
  • 3
  • 21
  • 59

Therefore, these statements could simplyalternatively be written as -

ifdef is_winner(self.board["TL", andplayer):
 "TM" and "TR"] ==player_type = player.type 
 or   runs = [
    self.board["ML" and "MM" and "MR"]# ==horizontal
 player.type or      ["TL", "TM", "TR"],
    self.board["BL" and "BM" and ["ML", "MM", "MR"],
        ["BL", "BM", "BR"],
 == player.type or     # vertical
    self.board["TL" and "ML" and ["TL", "ML", "BL"],
 == player.type or     ["TM", "MM", "BM"],
    self.board["TM" and "MM" and "BM"]["TR", =="MR", player.type"BR"],
 or       # diagonal
    self.board["TR" and "MR" and ["TL", "MM", "BR"],
 == player.type or     ["BL", "MM", "TR"]
    self.board["TL"]
 and "MM" and "BR"]for ==a, player.typeb, orc in runs:
        if self.board["BL"board[a] and== "MM"self.board[b] and== "TR"]self.board[c] == player.type)player_type:
            return True
    return False

Therefore, these statements could simply be written as -

if (self.board["TL" and "TM" and "TR"] == player.type or 
    self.board["ML" and "MM" and "MR"] == player.type or 
    self.board["BL" and "BM" and "BR"] == player.type or 
    self.board["TL" and "ML" and "BL"] == player.type or 
    self.board["TM" and "MM" and "BM"] == player.type or 
    self.board["TR" and "MR" and "BR"] == player.type or 
    self.board["TL" and "MM" and "BR"] == player.type or 
    self.board["BL" and "MM" and "TR"] == player.type):

Therefore, these statements could alternatively be written as -

def is_winner(self, player):
    player_type = player.type 
    runs = [
        # horizontal
        ["TL", "TM", "TR"],
        ["ML", "MM", "MR"],
        ["BL", "BM", "BR"],
        # vertical
        ["TL", "ML", "BL"],
        ["TM", "MM", "BM"],
        ["TR", "MR", "BR"],
        # diagonal
        ["TL", "MM", "BR"],
        ["BL", "MM", "TR"]
    ]
    for a, b, c in runs:
        if self.board[a] == self.board[b] == self.board[c] == player_type:
            return True
    return False
deleted 184 characters in body
Source Link
Justin
  • 2.6k
  • 3
  • 21
  • 59

The PEP changed at hg.python.org/peps/rev/7a48207aaab6https://hg.python.org/peps/rev/7a48207aaab6 to explicitly discourage backslashes.

if (self.board["TL"]board["TL" and self.board["TM"]"TM" and self.board["TR"]"TR"] == player.type or 
    self.board["ML"]board["ML" and self.board["MM"]"MM" and self.board["MR"]"MR"] == player.type or 
    self.board["BL"]board["BL" and self.board["BM"]"BM" and self.board["BR"]"BR"] == player.type or 
    self.board["TL"]board["TL" and self.board["ML"]"ML" and self.board["BL"]"BL"] == player.type or 
    self.board["TM"]board["TM" and self.board["MM"]"MM" and self.board["BM"]"BM"] == player.type or 
    self.board["TR"]board["TR" and self.board["MR"]"MR" and self.board["BR"]"BR"] == player.type or 
    self.board["TL"]board["TL" and self.board["MM"]"MM" and self.board["BR"]"BR"] == player.type or 
    self.board["BL"]board["BL" and self.board["MM"]"MM" and self.board["TR"]"TR"] == player.type):

The PEP changed at hg.python.org/peps/rev/7a48207aaab6 to explicitly discourage backslashes.

if (self.board["TL"] and self.board["TM"] and self.board["TR"] == player.type or 
    self.board["ML"] and self.board["MM"] and self.board["MR"] == player.type or 
    self.board["BL"] and self.board["BM"] and self.board["BR"] == player.type or 
    self.board["TL"] and self.board["ML"] and self.board["BL"] == player.type or 
    self.board["TM"] and self.board["MM"] and self.board["BM"] == player.type or 
    self.board["TR"] and self.board["MR"] and self.board["BR"] == player.type or 
    self.board["TL"] and self.board["MM"] and self.board["BR"] == player.type or 
    self.board["BL"] and self.board["MM"] and self.board["TR"] == player.type):

The PEP changed at https://hg.python.org/peps/rev/7a48207aaab6 to explicitly discourage backslashes.

if (self.board["TL" and "TM" and "TR"] == player.type or 
    self.board["ML" and "MM" and "MR"] == player.type or 
    self.board["BL" and "BM" and "BR"] == player.type or 
    self.board["TL" and "ML" and "BL"] == player.type or 
    self.board["TM" and "MM" and "BM"] == player.type or 
    self.board["TR" and "MR" and "BR"] == player.type or 
    self.board["TL" and "MM" and "BR"] == player.type or 
    self.board["BL" and "MM" and "TR"] == player.type):
added 2521 characters in body
Source Link
Justin
  • 2.6k
  • 3
  • 21
  • 59

Note that the trailing \ solutions are not recommended by PEP 8. One reason is that if space is added by mistake after a \ it might not show in your editor, and the code becomes syntactically incorrect.

The PEP changed at hg.python.org/peps/rev/7a48207aaab6 to explicitly discourage backslashes.

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets, and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

Another thing is that, here (for example) -

if self.board["TL"] == player.type and self.board["TM"] == player.type and self.board["TR"] == player.type or \
self.board["ML"] == player.type and self.board["MM"] == player.type and self.board["MR"] == player.type or \
self.board["BL"] == player.type and self.board["BM"] == player.type and self.board["BR"] == player.type or \
self.board["TL"] == player.type and self.board["ML"] == player.type and self.board["BL"] == player.type or \
self.board["TM"] == player.type and self.board["MM"] == player.type and self.board["BM"] == player.type or \
self.board["TR"] == player.type and self.board["MR"] == player.type and self.board["BR"] == player.type or \
self.board["TL"] == player.type and self.board["MM"] == player.type and self.board["BR"] == player.type or \
self.board["BL"] == player.type and self.board["MM"] == player.type and self.board["TR"] == player.type:

the lines are too long. According to PEP 8 -

Limit all lines to a maximum of 79 characters.

Therefore, these statements could simply be written as -

if (self.board["TL"] and self.board["TM"] and self.board["TR"] == player.type or 
    self.board["ML"] and self.board["MM"] and self.board["MR"] == player.type or 
    self.board["BL"] and self.board["BM"] and self.board["BR"] == player.type or 
    self.board["TL"] and self.board["ML"] and self.board["BL"] == player.type or 
    self.board["TM"] and self.board["MM"] and self.board["BM"] == player.type or 
    self.board["TR"] and self.board["MR"] and self.board["BR"] == player.type or 
    self.board["TL"] and self.board["MM"] and self.board["BR"] == player.type or 
    self.board["BL"] and self.board["MM"] and self.board["TR"] == player.type):

Overall, in terms of code readability and style, this is what you need to improve. You should make your code more PEP 8 compliant.

Overall, in terms of code readability and style, this is what you need to improve. You should make your code more PEP 8 compliant.

Note that the trailing \ solutions are not recommended by PEP 8. One reason is that if space is added by mistake after a \ it might not show in your editor, and the code becomes syntactically incorrect.

The PEP changed at hg.python.org/peps/rev/7a48207aaab6 to explicitly discourage backslashes.

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets, and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

Another thing is that, here (for example) -

if self.board["TL"] == player.type and self.board["TM"] == player.type and self.board["TR"] == player.type or \
self.board["ML"] == player.type and self.board["MM"] == player.type and self.board["MR"] == player.type or \
self.board["BL"] == player.type and self.board["BM"] == player.type and self.board["BR"] == player.type or \
self.board["TL"] == player.type and self.board["ML"] == player.type and self.board["BL"] == player.type or \
self.board["TM"] == player.type and self.board["MM"] == player.type and self.board["BM"] == player.type or \
self.board["TR"] == player.type and self.board["MR"] == player.type and self.board["BR"] == player.type or \
self.board["TL"] == player.type and self.board["MM"] == player.type and self.board["BR"] == player.type or \
self.board["BL"] == player.type and self.board["MM"] == player.type and self.board["TR"] == player.type:

the lines are too long. According to PEP 8 -

Limit all lines to a maximum of 79 characters.

Therefore, these statements could simply be written as -

if (self.board["TL"] and self.board["TM"] and self.board["TR"] == player.type or 
    self.board["ML"] and self.board["MM"] and self.board["MR"] == player.type or 
    self.board["BL"] and self.board["BM"] and self.board["BR"] == player.type or 
    self.board["TL"] and self.board["ML"] and self.board["BL"] == player.type or 
    self.board["TM"] and self.board["MM"] and self.board["BM"] == player.type or 
    self.board["TR"] and self.board["MR"] and self.board["BR"] == player.type or 
    self.board["TL"] and self.board["MM"] and self.board["BR"] == player.type or 
    self.board["BL"] and self.board["MM"] and self.board["TR"] == player.type):

Overall, in terms of code readability and style, this is what you need to improve. You should make your code more PEP 8 compliant.

added 280 characters in body
Source Link
Justin
  • 2.6k
  • 3
  • 21
  • 59
Loading
added 177 characters in body
Source Link
Justin
  • 2.6k
  • 3
  • 21
  • 59
Loading
Source Link
Justin
  • 2.6k
  • 3
  • 21
  • 59
Loading