The objective of the game is to guess a five-lettered word in five guesses or less. When the user (player) enters a word, the application will inform the user whether their guess was correct or not. If the user guesses the correct word the application will simply "move to the next level" and prompt the user to guess another word. If the user incorrectly guesses the word, the application will remove an attempt from the user and will give a hint based on which letters they guessed correctly. The letters the user have guessed correctly can be displayed in two ways: (Green - correct letter, correct index) (Yellow - correct letter, incorrect index)
I want to find out whether my logic is efficient or not for the application. I'd appreciate any feedback. Thank you.
fun main(args: Array<String>) {
Menu()
}
// Game Menu
fun Menu() {
println("******************************\n" +
"Welcome to WordGame!\n" +
"******************************")
println("Guess the correct word in\n" +
"five attempts or less\n" +
"******************************")
println("Type your name to play")
val player = readln()
WordGame(player)
}
// Game Function
fun WordGame(player : String) {
var level : Int = 1
var attempts : Int = 5
// Array to hold random words
val words: Array<String> = arrayOf("mayor", "train", "watch", "movie", "photo");
var word : String = words.random() // Gets random word in array words
// Loops around the game
while (true) {
println("******************************")
println(player + " - Level " + level + " - Attempts " + attempts)
println("******************************")
println("Guess the word:")
val answer : String = readln()
// Checks for correct word
if(CheckIndex(word, answer).size == 5) {
level = level + 1 // adds level
println("Correct!")
attempts = 5 // resets the players attempts
word = words.random()
// Ends game when level 6 is reached
if (level == 6) {
println("Well done! You have beaten WordGame.\n" +
"Press enter to return to menu")
readln()
Menu()
}
}
else {
attempts = attempts - 1 // Removes an attempt
// Ends game when attempts reach 0
if (attempts == 0) {
println("You've Lost at WordGame!\n" +
"Press enter to return to menu")
readln()
Menu()
}
var index = CheckIndex(word, answer).joinToString()
var letters = CheckLetters(word, answer).joinToString()
println("******************************\n" +
"Green: " + index + "\n" +
"Yellow: " + letters + "\n" +
"******************************")
println("Remaing attempts: " + attempts)
println("Press enter to try again")
readln()
}
}
}
// Function to check index
fun CheckIndex(word: String, answer: String) : ArrayList<Char> {
val index = ArrayList<Char>()
var i = 0
// Loops around the length of the guessed word
while (i < answer.length) {
// Checks against the char of guessed word
// and correct word
if(answer.get(i) == word.get(i)) {
index.add(answer.get(i)) // if a char is equal, adds to an arraylist
}
i++
}
return index
}
// Function to check correct letters
fun CheckLetters(word: String, answer: String) : ArrayList<Char> {
val letters = ArrayList<Char>()
var i = 0
while (i < answer.length) {
// Ensures the 'Green' letters are
// not seen as 'Yellow'
if(answer.get(i) != word.get(i)) {
// Checks against each letter in the correct word
// I feel that this is a bit clunky as I would prefer
// to not hard-code indexe
when (answer.get(i)) {
word.get(0) -> letters.add(answer.get(i))
word.get(1) -> letters.add(answer.get(i))
word.get(2) -> letters.add(answer.get(i))
word.get(3) -> letters.add(answer.get(i))
word.get(4) -> letters.add(answer.get(i))
}
}
i++
}
return letters
}