forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [Fácil]/go/qwik-zgheib.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
type WordScorer interface { | ||
Score(word string) int | ||
} | ||
|
||
type SpanishWordScorer struct { | ||
letterValues map[rune]int | ||
} | ||
|
||
func NewSpanishWordScorer() *SpanishWordScorer { | ||
letterValues := make(map[rune]int) | ||
for i, letter := range "abcdefghijklmnñopqrstuvwxyz" { | ||
letterValues[letter] = i + 1 | ||
} | ||
return &SpanishWordScorer{letterValues: letterValues} | ||
} | ||
|
||
func (s *SpanishWordScorer) Score(word string) int { | ||
total := 0 | ||
for _, letter := range word { | ||
if value, exists := s.letterValues[letter]; exists { | ||
total += value | ||
} else if unicode.IsLetter(letter) { | ||
fmt.Printf("unknown character'%c' ignored.\n", letter) | ||
} | ||
} | ||
return total | ||
} | ||
|
||
type UserInteractor interface { | ||
GetInput(prompt string) string | ||
PrintOutput(output string) | ||
} | ||
|
||
type ConsoleInteractor struct{} | ||
|
||
func (ci *ConsoleInteractor) GetInput(prompt string) string { | ||
fmt.Print(prompt) | ||
scanner := bufio.NewScanner(os.Stdin) | ||
scanner.Scan() | ||
return scanner.Text() | ||
} | ||
|
||
func (ci *ConsoleInteractor) PrintOutput(output string) { | ||
fmt.Println(output) | ||
} | ||
|
||
type WordGame struct { | ||
scorer WordScorer | ||
interactor UserInteractor | ||
targetScore int | ||
} | ||
|
||
func NewWordGame(scorer WordScorer, interactor UserInteractor, targetScore int) *WordGame { | ||
return &WordGame{ | ||
scorer: scorer, | ||
interactor: interactor, | ||
targetScore: targetScore, | ||
} | ||
} | ||
|
||
func (wg *WordGame) Play() { | ||
for { | ||
word := wg.interactor.GetInput("enter a word: ") | ||
word = strings.ToLower(strings.TrimSpace(word)) | ||
score := wg.scorer.Score(word) | ||
wg.interactor.PrintOutput(fmt.Sprintf("the word insert has %d points.", score)) | ||
if score >= wg.targetScore { | ||
wg.interactor.PrintOutput("you have reached or exceeded 100 points! The game is over.") | ||
break | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
scorer := NewSpanishWordScorer() | ||
interactor := &ConsoleInteractor{} | ||
game := NewWordGame(scorer, interactor, 100) | ||
game.Play() | ||
} |