patterntypescriptMinor
Check for Scrabble double letter bonus
Viewed 0 times
bonusscrabbledoubleforlettercheck
Problem
This function checks if the cell of a Scrabble board is a double letter bonus. It has a 23 cyclomatic complexity, which is higher than the threshold of 20. I don't know how to do it another way, I think that this is the only way to do it. Here is my function :
checkDoubleLetterCell(row: number, column: number): boolean
{
if((row === middle- 1 || row === middle + 1)
&& (column === middle - 1 || column === middle + 1)
|| (row === 0 || row == SCRABBLE_SIZE - 1 || row === middle)
&& (column === middle + Math.round(middle/2) || column === middle - Math.round(middle/2))
|| (column === 0 || column === SCRABBLE_SIZE - 1 || column === middle)
&& (row === middle + Math.round(middle/2) || row === middle - Math.round(middle/2))
|| (column === middle + 1 || column === middle - 1)
&& (row === middle + Math.round(middle/2) + 1 || row === middle - Math.round(middle/2) - 1)
|| (row === middle + 1 || row === middle - 1)
&& (column === middle + Math.round(middle/2) + 1 || column === middle - Math.round(middle/2) - 1))
{
return true;
}
return false;
}Solution
So, I am going to answer you javascript question using python code because I think the concepts are identical and I was too lazy to make sure my javascript syntax was right.
A couple of ways to simplify your expression include:
Use a lookup table:
I find this implementation more intuitive and much easier to debug.
Build some intermediate products:
If you really need the stacked logic, you can produce some intermediate products which are then much easier to verify and understand.
Test Code:
And for completeness, here is the code I used to test the above.
A couple of ways to simplify your expression include:
Use a lookup table:
I find this implementation more intuitive and much easier to debug.
double_letter_cells = [
#ABCDEFGHIJKLMNO
'...1.......1...', # 1
'...............', # 2
'......1.1......', # 3
'1......1......1', # 4
'...............', # 5
'...............', # 6
'..1...1.1...1..', # 7
'...1.......1...', # 8
'..1...1.1...1..', # 9
'...............', # 10
'...............', # 11
'1......1......1', # 12
'......1.1......', # 12
'...............', # 14
'...1.......1...', # 15
]
def checkDoubleLetterCell(row, col):
return double_letter_cells[row][col] == '1'
Build some intermediate products:
If you really need the stacked logic, you can produce some intermediate products which are then much easier to verify and understand.
SCRABBLE_SIZE = 15
middle = int(SCRABBLE_SIZE / 2)
def checkDoubleLetterCell(row, col):
one_off_middle_row = (row == middle - 1) or (row == middle + 1)
one_off_middle_col = (col == middle - 1) or (col == middle + 1)
outer_middle_row = (
row == 0 or row == SCRABBLE_SIZE - 1 or row == middle)
outer_middle_col = (
col == 0 or col == SCRABBLE_SIZE - 1 or col == middle)
middle_middle_row = row == middle + int(middle / 2) + 1 or \
row == middle - int(middle / 2) - 1
middle_middle_col = col == middle + int(middle / 2) + 1 or \
col == middle - int(middle / 2) - 1
middle_middle_plus_row = row == middle + int(middle / 2) + 2 or \
row == middle - int(middle / 2) - 2
middle_middle_plus_col = col == middle + int(middle / 2) + 2 or \
col == middle - int(middle / 2) - 2
return (
one_off_middle_row and one_off_middle_col or
outer_middle_row and middle_middle_col or
outer_middle_col and middle_middle_row or
one_off_middle_col and middle_middle_plus_row or
one_off_middle_row and middle_middle_plus_col
)
Test Code:
And for completeness, here is the code I used to test the above.
for i in range(SCRABBLE_SIZE):
for j in range(SCRABBLE_SIZE):
assert (double_letter_cells[i][j] == '1') \
== checkDoubleLetterCell(i, j)
Context
StackExchange Code Review Q#158195, answer score: 3
Revisions (0)
No revisions yet.