From 31dec710639d360536207692634a24b3e2bf108e Mon Sep 17 00:00:00 2001 From: Thomas Kwashnak Date: Thu, 6 Jan 2022 09:45:04 -0500 Subject: [PATCH] Simplified isGameFinished Unsure if I should simplify further --- 48_High_IQ/java/src/HighIQ.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/48_High_IQ/java/src/HighIQ.java b/48_High_IQ/java/src/HighIQ.java index 99d046fe..83cff254 100644 --- a/48_High_IQ/java/src/HighIQ.java +++ b/48_High_IQ/java/src/HighIQ.java @@ -96,18 +96,13 @@ public class HighIQ { for(Integer key : board.getKeySet()) { if(board.get(key)) { //Spacing is either 1 or 9 - for(int space = 1; space <= 9; space += 8) { - //Next val is the next spot, prev and next after are the two spots where the peg would go if a move was possible - Boolean nextVal = board.get(key + space); - Boolean prevAfter = board.get(key - space); - Boolean nextAfter = board.get(key + space * 2); - - if(nextVal != null && nextVal) { - if((prevAfter != null && !prevAfter) || (nextAfter != null && !nextAfter)) { - return false; - } + //Looking to the right and down from every point, checking for both directions of movement + for(int space : new int[] {1,9}) { + Boolean nextToPeg = board.getOrDefault(key + space,false); + Boolean hasMovableSpace = !board.getOrDefault(key - space,true) || !board.getOrDefault(key + space * 2, true); + if(nextToPeg && hasMovableSpace) { + return false; } - } }