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.
Merge pull request mouredev#6042 from didacdev/main
Reto mouredev#47 - Swift
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
Retos/Reto #47 - LA PALABRA DE 100 PUNTOS [Fácil]/swift/didacdev.swift
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,45 @@ | ||
import Foundation | ||
|
||
let alphabet = Array("abcdefghijklmnñopqrstuvwxyz") | ||
var letterValues: [Character: Int] = [:] | ||
|
||
for (index, letter) in alphabet.enumerated() { | ||
letterValues[letter] = index + 1 | ||
} | ||
|
||
func getPoints(word: String) -> Int { | ||
var points = 0 | ||
|
||
for letter in word { | ||
if let number = letterValues[letter] { | ||
points += number | ||
} | ||
} | ||
|
||
return points | ||
} | ||
|
||
func play() { | ||
var finish = false | ||
|
||
while !finish { | ||
print() | ||
print("Escribe una palabra:") | ||
|
||
if let word = readLine(){ | ||
|
||
let points = getPoints(word: word) | ||
print() | ||
print("Puntuación: \(points)") | ||
|
||
if points == 100 { | ||
print("Has ganado!") | ||
finish = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
play() | ||
|
||
|