fix: show rules and explain mistakes

This commit is contained in:
Glenn Y. Rolland 2025-06-14 15:49:42 +02:00
parent 9de046b8ac
commit 3c19338971
6 changed files with 47 additions and 41 deletions

Binary file not shown.

View file

@ -1,32 +1,33 @@
class Bordle
class Dictionary
DICTIONARY_FILE = "/usr/share/dict/french"
DICTIONARY_PATH = Path.new("/usr/share/dict/")
property length : UInt8
property data : Array(String)
property language : String = "french"
def initialize(length : UInt8)
@length = length
@data = [] of String
load_data
end
def load_data
dictionary_file = DICTIONARY_PATH / @language
# use french dictionary from wfrench package
if ! File.exists? DICTIONARY_FILE
STDERR.puts "ERROR: dictionary file missing! (#{DICTIONARY_FILE})"
STDERR.puts " Please install then wfrench package on your system"
if !File.exists? dictionary_file
STDERR.puts "ERROR: dictionary file missing! (#{dictionary_file})"
STDERR.puts " Please install then w#{@language} package on your system"
exit 1
end
lines = File.read_lines(DICTIONARY_FILE)
lines = File.read_lines(dictionary_file)
@data =
lines
.select {|word| word.size == @length }
.map { |word| word.tr( TR_DIACRITICS, TR_ASCII ) }
.select { |word| word.size == @length }
.map { |word| word.tr(TR_DIACRITICS, TR_ASCII) }
end
def includes?(word)
@data.includes? word
end

View file

@ -1,4 +1,3 @@
require "colorize"
require "./types"
@ -37,14 +36,28 @@ class Bordle
word = "" if word.nil?
word.tr(TR_DIACRITICS, TR_ASCII).downcase
error = [] of String
error << "language" if !@dict.includes? word
error << "length" if word.size != @length
break if word.size == @length && @dict.includes? word
printf("\x1B[A\x1B[2K")
puts "-- #{word} : invalid #{error.join(" and ")} --"
end
word
end
def run
printf " .....\n"
puts "-- BORDLE (#{@target.inspect}) --".colorize.fore(:blue)
puts <<-MARK
1. You win when you find the secret word.
2. You have 5 attempts; you lose if you use them all.
3. The secret word is in #{@dict.language} and 5 characters long.
4. Language and length mistakes are not counted as attemps.
5. Diacritics (accents, etc.) are removed.
6. Use CTRL-C to exit.
MARK
puts ""
puts " ....."
try = 0
while true
try += 1
@ -65,4 +78,3 @@ class Bordle
end
end
end

View file

@ -1,14 +1,12 @@
require "option_parser"
require "./game"
class Bordle
class BordleCli
property options : String?
def initialize()
def initialize
@options = nil
end
@ -17,7 +15,7 @@ class Bordle
end
# FIXME: add --length LEN option (length of words)
# FIXME: add --lang LANG option (choose dictionnary)
# FIXME: add --language LANG option (choose dictionnary)
# FIXME: add --tries TRIES option (how many tries are allowed)
# FIXME: add --with-letters (show used/unused letters)
def self.run(args)
@ -26,11 +24,9 @@ class Bordle
app.options = options
game = Game.new
game.run()
game.run
end
end
end
Bordle::BordleCli.run(ARGV)

View file

@ -1,4 +1,3 @@
class Bordle
class TargetWord
def initialize(@target_word : String)
@ -8,7 +7,7 @@ class Bordle
@target_word == word
end
def to_h()
def to_h
hash = Hash(Char, Array(Int32)).new
@target_word.each_char_with_index do |char, index|
hash[char] = [] of Int32 unless hash.has_key? char
@ -25,27 +24,27 @@ class Bordle
# TEST= c e r e t
word.each_char_with_index do |char, index|
if ! hash.has_key? char
if !hash.has_key? char
result << {char, Score::NotOk}
next
end
char_values = hash[char]
char_misplaced = char_values.select {|pos| @target_word[pos] != word[pos] }
char_wellplaced = char_values.select {|pos| @target_word[pos] == word[pos] }
char_misplaced = char_values.select { |pos| @target_word[pos] != word[pos] }
char_wellplaced = char_values.select { |pos| @target_word[pos] == word[pos] }
# puts "values(#{char}) = #{char_values}"
# puts "misplaced(#{char}) = #{char_misplaced}"
# puts "wellplaced(#{char}) = #{char_wellplaced}"
if char_wellplaced.includes? index
result << {char, Score::RightPlace}
char_wellplaced.reject! {|pos| pos == index }
char_wellplaced.reject! { |pos| pos == index }
hash[char] = char_misplaced + char_wellplaced
hash.delete(char) if hash[char].empty?
next
end
if ! char_misplaced.empty?
if !char_misplaced.empty?
result << {char, Score::WrongPlace}
char_misplaced = char_misplaced.skip(1)
hash[char] = char_misplaced + char_wellplaced

View file

@ -1,9 +1,7 @@
class Bordle
TR_DIACRITICS = "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšȘșſŢţŤťŦŧȚțÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž"
TR_ASCII = "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSsSssTtTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz"
enum Score
RightPlace = 0
WrongPlace = 1