├── assets └── example.png ├── README.md ├── LICENSE └── wordle.go /assets/example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AshishShenoy/wordle/HEAD/assets/example.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Wordle 2 | 3 | [![forthebadge](https://forthebadge.com/images/badges/made-with-go.svg)](https://forthebadge.com) 4 | [![forthebadge](https://forthebadge.com/images/badges/you-didnt-ask-for-this.svg)](https://forthebadge.com) 5 | [![forthebadge](https://forthebadge.com/images/badges/compatibility-emacs.svg)](https://forthebadge.com) 6 | 7 | A terminal Wordle written in Go. 8 | 9 | ![Example](assets/example.png) 10 | 11 | ## Running 12 | 13 | If you couldn't tell from the image, you need Go installed and then run: 14 | 15 | ``` 16 | go run wordle.go 17 | ``` 18 | 19 | ## Pros 20 | 21 | - Choose your wordlists 22 | - If you're good you can choose longer words 23 | - If you're bad you can increase number of guesses 24 | 25 | ## Cons 26 | 27 | - Sometimes you get weird words with the default wordlist 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Ashish Shenoy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /wordle.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io/ioutil" 7 | "log" 8 | "math/rand" 9 | "net/http" 10 | "os" 11 | "sort" 12 | "strings" 13 | "time" 14 | ) 15 | 16 | const WORDS_URL = "https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt" 17 | const WORD_LENGTH = 5 18 | const MAX_GUESSES = 6 19 | 20 | func get_filled_color_vector(color string) [WORD_LENGTH]string { 21 | color_vector := [WORD_LENGTH]string{} 22 | for i := range color_vector { 23 | color_vector[i] = color 24 | } 25 | return color_vector 26 | } 27 | 28 | func display_word(word string, color_vector [WORD_LENGTH]string) { 29 | for i, c := range word { 30 | switch color_vector[i] { 31 | case "Green": 32 | fmt.Print("\033[42m\033[1;30m") 33 | case "Yellow": 34 | fmt.Print("\033[43m\033[1;30m") 35 | case "Grey": 36 | fmt.Print("\033[40m\033[1;37m") 37 | } 38 | fmt.Printf(" %c ", c) 39 | fmt.Print("\033[m\033[m") 40 | } 41 | fmt.Println() 42 | } 43 | 44 | func main() { 45 | rand.Seed(time.Now().Unix()) 46 | 47 | res, err := http.Get(WORDS_URL) 48 | if err != nil { 49 | log.Fatalln(err) 50 | } 51 | 52 | body, _ := ioutil.ReadAll(res.Body) 53 | words := strings.Split(string(body), "\r\n") 54 | 55 | wordle_words := []string{} 56 | for _, word := range words { 57 | if len(word) == WORD_LENGTH { 58 | wordle_words = append(wordle_words, strings.ToUpper(word)) 59 | } 60 | } 61 | sort.Strings(wordle_words) 62 | 63 | selected_word := wordle_words[rand.Intn(len(wordle_words))] 64 | 65 | reader := bufio.NewReader(os.Stdin) 66 | guesses := []map[string][WORD_LENGTH]string{} 67 | var guess_count int 68 | for guess_count = 0; guess_count < MAX_GUESSES; guess_count++ { 69 | fmt.Printf("Enter your guess (%v/%v): ", guess_count+1, MAX_GUESSES) 70 | 71 | guess_word, err := reader.ReadString('\n') 72 | if err != nil { 73 | log.Fatalln(err) 74 | } 75 | guess_word = strings.ToUpper(guess_word[:len(guess_word)-1]) 76 | 77 | if guess_word == selected_word { 78 | fmt.Println("You guessed right!") 79 | color_vector := get_filled_color_vector("Green") 80 | 81 | guesses = append(guesses, map[string][WORD_LENGTH]string{guess_word: color_vector}) 82 | 83 | fmt.Println("Your wordle matrix is: ") 84 | for _, guess := range guesses { 85 | for guess_word, color_vector := range guess { 86 | display_word(guess_word, color_vector) 87 | } 88 | } 89 | break 90 | } else { 91 | i := sort.SearchStrings(wordle_words, guess_word) 92 | if i < len(wordle_words) && wordle_words[i] == guess_word { 93 | color_vector := get_filled_color_vector("Grey") 94 | 95 | // stores whether an index is allowed to cause another index to be yellow 96 | yellow_lock := [WORD_LENGTH]bool{} 97 | 98 | for j, guess_letter := range guess_word { 99 | for k, letter := range selected_word { 100 | if guess_letter == letter && j == k { 101 | color_vector[j] = "Green" 102 | // now the kth index can no longer cause another index to be yellow 103 | yellow_lock[k] = true 104 | break 105 | 106 | } 107 | } 108 | } 109 | for j, guess_letter := range guess_word { 110 | for k, letter := range selected_word { 111 | if guess_letter == letter && color_vector[j] != "Green" && yellow_lock[k] == false { 112 | color_vector[j] = "Yellow" 113 | yellow_lock[k] = true 114 | } 115 | } 116 | } 117 | guesses = append(guesses, map[string][WORD_LENGTH]string{guess_word: color_vector}) 118 | display_word(guess_word, color_vector) 119 | } else { 120 | guess_count-- 121 | fmt.Printf("Please guess a valid %v letter word from the wordlist", WORD_LENGTH) 122 | fmt.Println() 123 | } 124 | } 125 | } 126 | 127 | if guess_count == MAX_GUESSES { 128 | fmt.Println("Better luck next time!") 129 | color_vector := get_filled_color_vector("Green") 130 | fmt.Print("The correct word is : ") 131 | display_word(selected_word, color_vector) 132 | } 133 | } 134 | --------------------------------------------------------------------------------