MCPcopy Create free account
hub / github.com/ndleah/python-mini-project / legal_moves

Method legal_moves

Othello-Reversi-Game/Board.py:34–108  ·  view source on GitHub ↗

Return all legal moves for the cell at the given position

(self, row: int, col: int)

Source from the content-addressed store, hash-verified

32 return all_legal_moves
33
34 def legal_moves(self, row: int, col: int) -> list:
35 '''Return all legal moves for the cell at the given position'''
36 PLAYER = self.board[row, col]
37 OPPONENT = Board.WHITE if PLAYER == Board.BLACK else Board.BLACK
38 legal_moves = list()
39
40 # check for legal moves along the row of the cell
41 if col >= 2:
42 i = col - 1
43 while i >= 0 and self.board[row, i] == OPPONENT:
44 i -= 1
45 if (i != col - 1 and i >= 0) and self.board[row, i] == Board.EMPTY:
46 legal_moves.append((row, i))
47
48 if col <= 5:
49 i = col + 1
50 while i < 8 and self.board[row, i] == OPPONENT:
51 i += 1
52 if (i != col + 1 and i < 8) and self.board[row, i] == Board.EMPTY:
53 legal_moves.append((row, i))
54
55 # check for legal moves along the column of the cell
56 if row >= 2:
57 i = row - 1
58 while i >= 0 and self.board[i, col] == OPPONENT:
59 i -= 1
60 if (i != row - 1 and i >= 0) and self.board[i, col] == Board.EMPTY:
61 legal_moves.append((i, col))
62
63 if row <= 5:
64 i = row + 1
65 while i < 8 and self.board[i, col] == OPPONENT:
66 i += 1
67 if (i != row + 1 and i < 8) and self.board[i,col] == Board.EMPTY:
68 legal_moves.append((i, col))
69
70 # check for legal moves along diagonals on which the cell lies
71 if row >= 2 and col >= 2: # diagonal from the cell towards top left
72 r = row - 1
73 c = col - 1
74 while (r >= 0 and c >= 0) and self.board[r, c] == OPPONENT:
75 r -= 1
76 c -=1
77 if (r != row - 1 and c != col - 1) and (r >= 0 and c >= 0) and self.board[r, c] == Board.EMPTY:
78 legal_moves.append((r, c))
79
80 if row >= 2 and col <= 5: # diagonal from the cell towards top right
81 r = row - 1
82 c = col + 1
83 while (r >= 0 and c < 8) and self.board[r, c] == OPPONENT:
84 r -= 1
85 c +=1
86 if (r != row - 1 and c != col + 1) and (r >= 0 and c < 8) and self.board[r, c] == Board.EMPTY:
87 legal_moves.append((r, c))
88
89 if row <= 5 and col <= 5: # diagonal from the cell towards bottom right
90 r = row + 1
91 c = col + 1

Callers 1

all_legal_movesMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected