I am coding a utility app for chess and I have some repetitive code pattern that keeps repeating across different methods.
Basically, I am tracking the color of the user {white, black}
. If the user is white
then the moves they do follow the pattern moves[i] % 2 ==0
(since a white player always starts the game).
if (userColor === "white") {
if(length % 2 === 0) {
max = white = time - timeA;
black = time - timeB;
} else {
max= black = time - timeA;
white = time - timeB;
}
} else {
if(length % 2 === 0) {
max = black = time - timeA;
white = time - timeB;
} else {
max = white = time - timeA;
black = time - timeB;
}
}
This is one example where I use the pattern for player color mentioned above. Does anyone see a way where I could elegantly reduce this code?
How could I simplify this snippet of code? There must be a way since there is a symmetry pattern here
Things I have tried
I have tried writing a method that takes in the color
of the user along with max
white
and black
, but I always seem to go back to the same code that I have written, doing no progress.