bordle/src/game.cr
2022-08-21 13:43:39 +02:00

69 lines
1.5 KiB
Crystal

require "colorize"
require "./types"
require "./dictionary"
require "./target_word"
class Bordle
class Game
def initialize
@length = 5_u8
@dict = Dictionary.new(@length)
@target = TargetWord.new(@dict.choose_word)
end
def display(diff : Array(LetterScore), try)
printf("#{try}. ")
diff.each do |ls|
colors = case ls[1]
when Score::NotOk then {:white, :black}
when Score::WrongPlace then {:white, :yellow}
when Score::RightPlace then {:white, :green}
else {:white, :black}
end
str = ("%s" % ls[0]).colorize.fore(colors[0]).back(colors[1])
printf("%s", str)
end
puts ""
end
def input_word(try)
word = ""
loop do
printf "#{try}. "
word = gets()
word = "" if word.nil?
word.tr(TR_DIACRITICS, TR_ASCII).downcase
break if word.size == @length && @dict.includes? word
printf("\x1B[A\x1B[2K")
end
word
end
def run
printf " .....\n"
try = 0
while true
try += 1
word = input_word(try)
printf("\x1B[A\x1B[2K")
diff = @target.diff(word)
display(diff, try)
if @target.equals?(word)
puts "-- GAGNÉ ! --".colorize.fore(:green)
return
end
if try >= 5
puts "-- PERDU ! --".colorize.fore(:red)
return
end
end
end
end
end