commit 7fdc9c991cecf7d4b817e951794c877c9103bde9
parent 4527ddada7a28cf9106f6fa5d430ae596c8df236
Author: Agastya Chandrakant <me@hanabi.in>
Date:   Thu, 17 Feb 2022 19:14:12 +0530
Tinsy-bit code refactoring, sensible if-else branch, eleminate special case (chance counter -- for error), make use of general case instead + show coloured-keyboard even when user guess
is wrong.
Diffstat:
2 files changed, 20 insertions(+), 19 deletions(-)
diff --git a/src/algos/arr-algo.go b/src/algos/arr-algo.go
@@ -22,10 +22,12 @@ func BinarySearch(arr []string, val string) int {
 		mid := (low + high) >> 1
 		if arr[mid] == val {
 			return mid
-		} else if arr[mid] > val {
-			high = mid - 1
 		} else {
-			low = mid + 1
+			if arr[mid] > val {
+				high = mid - 1
+			} else {
+				low = mid + 1
+			}
 		}
 	}
 	return -1
diff --git a/src/main.go b/src/main.go
@@ -26,26 +26,25 @@ func startGuessing(answer string) []string {
 	var colouredChoices = []string{}
 	fmt.Printf("Guess a %d-letter word.  You have %d tries.\n", word_size, chances)
 
-	for i := 0; i < chances; i++ { // not range, because i is being modified.
-		utils.GuessPrompt(i + 1) // 1-based indexing
+	for cur_chance := 1; cur_chance <= chances; {
+		utils.GuessPrompt(cur_chance)
 		guess, err := utils.GetValidGuess(word_size)
 		if err != nil {
-			i-- // Invalid input, reset current chance counter
 			fmt.Printf("%v", err)
-			continue
-		}
-		if guess != answer {
-			colour_string := algos.GetColours(answer, guess, anslookup, alphabet)
-			colouredChoices = append(colouredChoices, colour_string)
-			utils.PrintColouredGuess(colour_string, guess)
-			utils.PrintColouredAlpha(alphabet)
-		}
-		if guess == answer {
-			colour_string := "GGGGG" // If the answer was correct, GetColours is not called, hence hard-coding.
-			colouredChoices = append(colouredChoices, colour_string)
-			fmt.Println("Correct guess!")
-			return colouredChoices
+		} else {
+			cur_chance++
+			if guess != answer {
+				colour_string := algos.GetColours(answer, guess, anslookup, alphabet)
+				colouredChoices = append(colouredChoices, colour_string)
+				utils.PrintColouredGuess(colour_string, guess)
+			} else if guess == answer {
+				colour_string := "GGGGG" // If the answer was correct, GetColours is not called, hence hard-coding.
+				colouredChoices = append(colouredChoices, colour_string)
+				fmt.Println("Correct guess!")
+				break
+			}
 		}
+		utils.PrintColouredAlpha(alphabet)
 	}
 	return colouredChoices
 }