Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment04 Матвеева Виктория #124

Open
wants to merge 2 commits into
base: assignment04
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assignments/04/artifact/BullsAndCowsGame.jar
Binary file not shown.
14 changes: 13 additions & 1 deletion assignments/04/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

group = "io.rybalkinsd"
version = "1.0-SNAPSHOT"

Expand All @@ -24,6 +23,19 @@ dependencies {
ktlint("com.github.shyiko", "ktlint", "0.28.0")
}

val fatJar = task("fatJar", type = Jar::class) {
baseName = "${project.name}-fat"
manifest {
attributes["Main-Class"] = "io.matveyeva.kotlinbootcamp.bullsandcows.MainKt"
}
from(
configurations.runtime.map {
if (it.isDirectory) it else zipTree(it)
}
)
with(tasks["jar"] as CopySpec)
}

tasks {
val ktlint by creating(JavaExec::class) {
group = "verification"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.matveyeva.kotlinbootcamp.bullsandcows

import java.io.FileNotFoundException
import java.util.Random

object BullsAndCowsApplication {
private const val welcomeText = "Welcome to \"Bulls and Cows\" game!"
private val wordList = getWordsList()

fun execute() {
println(welcomeText)
while (true) {
val hiddenWord = extractWordFromDictionary()
println("I offered a ${hiddenWord.length}-letter word, your guess?")
Game(hiddenWord).start()
var answer: String
do {
println("Wanna play again? Y/N")
answer = readLine()?.toLowerCase() ?: ""
} while (answer != "n" && answer != "y")
if (answer == "n") return
if (answer == "y") continue
}
}

private fun getWordsList(): List<String> = try {
BullsAndCowsApplication::class.java.getResource("/dictionary.txt").readText().split("\n")
} catch (e: FileNotFoundException) {
e.printStackTrace()
emptyList()
}

private fun extractWordFromDictionary(): String {
val indexOfWord = Random().nextInt(wordList.size)
return wordList[indexOfWord]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.matveyeva.kotlinbootcamp.bullsandcows

class Game(private val hiddenWord: String) {
var attempts = 1
var isWon = false

fun start() {
while (attempts <= 10 && !isWon) {
print("$attempts attempt > ")
val inputWord = readLine() ?: ""
if (inputWord.length != hiddenWord.length) {
println("Length of your word is not equals ${hiddenWord.length}")
continue
}
attempts++
val (bulls, cows) = checkWord(inputWord.toLowerCase())
if (bulls == hiddenWord.length) isWon = true
println("Bulls: $bulls")
println("Cows: $cows")
}

if (attempts > 10) println("Your 10 attempts are spent...:( The hidden word is \"$hiddenWord\"")
if (isWon) println("You win!")
}

fun checkWord(inputWord: String): Result {
var bulls = 0
var cows = 0
var droppedInputWord = inputWord
val droppedWord = hiddenWord.filterIndexed { index, c ->
if (c == inputWord[index]) {
bulls++
droppedInputWord = droppedInputWord.replaceFirst(c.toString(), "")
false
} else true
}

droppedWord.forEach { if (droppedInputWord.contains(it)) { cows++
droppedInputWord = droppedInputWord.replaceFirst(it.toString(), "") } }

return Result(bulls, cows)
}

data class Result(val bulls: Int, val cows: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.matveyeva.kotlinbootcamp.bullsandcows

fun main(args: Array<String>) {
BullsAndCowsApplication.execute()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.matveyeva.kotlinbootcamp.bullsandcows

import junit.framework.TestCase.assertEquals
import org.junit.Test
class Test {
@Test
fun `same letters in word`() {
val result = Game("cetes").checkWord("katee")
assertEquals(Game.Result(2, 1), result)
}
}