5
\$\begingroup\$

I've recently reached a milestone for my Othello clone. This is my first Python project and the farthest I've come to a full program beyond small scripts and little automations.

I'd love for you to critique and help me to write better code.

Link: https://github.com/Mcclujdd/othello

Most of my code here: board.py

import tkinter as tkr
master = tkr.Tk()
master.geometry("800x800")
frame = tkr.Frame(master)
frame.pack()
canvas = tkr.Canvas(master, width=800, height=800)
canvas.pack(expand=tkr.YES, fill=tkr.BOTH)
class Tile:
 is_empty = True
 def __init__(self, coordinate, is_black, tkr_coordinates):
 self.coordinate = coordinate
 self.color = 'grey'
 self.tkr_coordinates = tkr_coordinates
 # self.tkr_coordinates = [40, 40, 130, 130] #pixel coordinates for x1, y1, x2, y2
board_matrix = [0,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]
def create_tkr_coordinates(coordinates): # coordinates must be [x1, y1, x2, y2] format
 y1 = coordinates[1]
 y2 = coordinates[3]
 tkr_values = []
 for row in range(8):
 x1 = coordinates[0]
 x2 = coordinates[2]
 for col in range(8):
 tkr_values.append([x1, y1, x2, y2])
 x1 += 90
 x2 += 90
 y1 += 90
 y2 += 90
 return tkr_values
def generateBoardValues():
 board_height = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h')
 board_width = ('1', '2', '3', '4', '5', '6', '7', '8')
 board_matrix_assignments = {} # a1 - h8 to be assigned to tile objects
 index = 0
 tkr_values = create_tkr_coordinates([40, 40, 130, 130])
 for letter in board_height:
 for number in board_width:
 board_matrix_assignments[board_matrix[index]] = Tile(f'{letter+number}', False, tkr_values[index])
 index +=1
 return board_matrix_assignments
def modifyColor(tile, color):
 canvas.itemconfig(tile, fill=color)
def onClick(event):
 tile = canvas.find_closest(event.x, event.y)
 current_color = canvas.itemcget(tile, 'fill')
 if current_color == 'grey':
 color = 'red'
 elif current_color == 'red':
 color = 'blue'
 else:
 color = 'grey'
 modifyColor(tile, color)
def drawBoard(board):
 for tile in board:
 x1 = board.get(tile).tkr_coordinates[0]
 y1 = board.get(tile).tkr_coordinates[1]
 x2 = board.get(tile).tkr_coordinates[2]
 y2 = board.get(tile).tkr_coordinates[3]
 canvas.create_rectangle(x1, y1, x2, y2, fill=board.get(tile).color)
 canvas.bind('<Button-1>', onClick)
 tkr.mainloop()
toolic
14.6k5 gold badges29 silver badges204 bronze badges
asked Aug 10, 2021 at 0:38
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Overview

You've done an excellent job:

  • You did a good job partitioning code into functions
  • You leveraged code written by others with the import
  • Used meaningful names for functions and variables

Here are some adjustments for you to consider, mainly for coding style.

Layout

I recommend moving the class and functions to the top, after the import statements. Having them in the middle of the code interrupts the natural flow of the code (from a human readability standpoint).

You have inconsistent vertical whitespace. I recommend removing many of the blank lines, except those separating the class and the functions.

Commented-out code

Commented-out code just adds clutter, and it should be removed:

 # self.tkr_coordinates = [40, 40, 130, 130] #pixel coordinates for x1, y1, x2, y2

Unused code

Unused code should be removed.

In the class:

is_empty = True

pylint identified a few more.

In the class: is_black.

In the create_tkr_coordinates function, variables row and col can be replaced with _:

for _ in range(8):
 for _ in range(8):

Simpler

Since board_matrix is a list of integers 0 to 63, it is simpler to generate these values with range than it is to list all 64:

board_matrix = range(64)

Documentation

You should add documentation to the top of your file to state the purpose of the code. The PEP 8 style guide recommends that you add docstrings for your methods.

Enum

Consider making the color variable an enum type.

answered Mar 15, 2024 at 16:08
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.