-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add theory, fix first several lessons * Finish with fixes for warm up * Fix detekt * Fix tests
- Loading branch information
Showing
231 changed files
with
5,511 additions
and
775 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1 @@ | ||
id: 722717686 | ||
id: 468192544 |
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 |
---|---|---|
@@ -1 +1 @@ | ||
id: 1229098139 | ||
id: 692295510 |
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 |
---|---|---|
@@ -1 +1 @@ | ||
id: 643359210 | ||
id: 619115895 |
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 |
---|---|---|
@@ -1 +1 @@ | ||
id: 843781288 | ||
id: 949232906 |
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 |
---|---|---|
@@ -1 +1 @@ | ||
id: 1874291146 | ||
id: 734671394 |
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,4 @@ | ||
type: framework | ||
is_template_based: false | ||
content: | ||
- welcome |
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 @@ | ||
id: 350072480 |
5 changes: 5 additions & 0 deletions
5
Introduction/welcome/src/main/kotlin/jetbrains/kotlin/course/welcome/Main.kt
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,5 @@ | ||
package jetbrains.kotlin.course.welcome | ||
|
||
fun main() { | ||
// Write your solution here | ||
} |
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,5 @@ | ||
type: theory | ||
custom_name: Kotlin Onboarding - Welcome! | ||
files: | ||
- name: src/main/kotlin/jetbrains/kotlin/course/welcome/Main.kt | ||
visible: true |
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 @@ | ||
id: 309464317 |
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,20 @@ | ||
Hello! Welcome to the Kotlin course. | ||
This course is designed for novices in Kotlin | ||
and focuses on the basic concepts of the Kotlin language. | ||
|
||
Each lesson of the course is built in the form of a project: | ||
step by step, by completing different small tasks, | ||
you will get a finished small project in the end. | ||
At the end of each lesson, an additional similar project will be offered: | ||
it includes all the topics of the lesson but is not divided into small tasks. | ||
|
||
All topics will be accompanied by links to [the official Kotlin documentation](https://kotlinlang.org/docs/home.html), | ||
which you can read later. | ||
|
||
Note, this course **does not provide the detailed explanation** of the basic concepts | ||
like variables, it only shows how to use the concepts in Kotlin and may remind you of their definitions. | ||
|
||
Please join the course chat on Discord using [the link](https://discord.gg/pN3kfttB). | ||
There, you can ask questions, interact with instructors, and connect with your fellow students. | ||
|
||
Let's go! |
91 changes: 91 additions & 0 deletions
91
...ed/CompleteTheProject/src/main/kotlin/jetbrains/kotlin/course/mastermind/advanced/Main.kt
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,91 @@ | ||
package jetbrains.kotlin.course.mastermind.advanced | ||
|
||
fun getGameRules(wordLength: Int, maxAttemptsCount: Int, secretExample: String) = | ||
"Welcome to the game! $newLineSymbol" + | ||
newLineSymbol + | ||
"Two people play this game: one chooses a word (a sequence of letters), " + | ||
"the other guesses it. In this version, the computer chooses the word: " + | ||
"a sequence of $wordLength letters (for example, $secretExample). " + | ||
"The user has several attempts to guess it (the max number is $maxAttemptsCount). " + | ||
"For each attempt, the number of complete matches (letter and position) " + | ||
"and partial matches (letter only) is reported. $newLineSymbol" + | ||
newLineSymbol + | ||
"For example, with $secretExample as the hidden word, the BCDF guess will " + | ||
"give 1 full match (C) and 1 partial match (B)." | ||
|
||
fun countPartialMatches(secret: String, guess: String): Int { | ||
val matches = minOf( | ||
secret.filter { it in guess }.length, | ||
guess.filter { it in secret }.length, | ||
) | ||
return matches - countExactMatches(guess, secret) | ||
} | ||
|
||
fun countExactMatches(secret: String, guess: String): Int = | ||
guess.filterIndexed { index, letter -> letter == secret[index] }.length | ||
|
||
fun generateSecret(wordLength: Int, alphabet: String) = | ||
List(wordLength) { alphabet.random() }.joinToString("") | ||
|
||
fun isComplete(secret: String, guess: String) = secret == guess | ||
|
||
fun printRoundResults(secret: String, guess: String) { | ||
val fullMatches = countExactMatches(secret, guess) | ||
val partialMatches = countPartialMatches(secret, guess) | ||
println("Your guess has $fullMatches full matches and $partialMatches partial matches.") | ||
} | ||
|
||
fun isWon(complete: Boolean, attempts: Int, maxAttemptsCount: Int) = complete && attempts <= maxAttemptsCount | ||
|
||
fun isLost(complete: Boolean, attempts: Int, maxAttemptsCount: Int) = !complete && attempts > maxAttemptsCount | ||
|
||
fun isCorrectInput(userInput: String, wordLength: Int, alphabet: String): Boolean { | ||
if (userInput.length != wordLength) { | ||
println("The length of your guess should be $wordLength! Try again!") | ||
return false | ||
} | ||
val notAlphabetSymbols = userInput.filter { it !in alphabet } | ||
if (notAlphabetSymbols.isNotEmpty()) { | ||
println("All symbols in your guess should be from the alphabet: $alphabet! Try again!") | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
fun safeUserInput(wordLength: Int, alphabet: String): String { | ||
var guess: String | ||
var isCorrect: Boolean | ||
do { | ||
println("Please input your guess. It should be of length $wordLength, and each symbol should be from the alphabet: $alphabet.") | ||
guess = safeReadLine() | ||
isCorrect = isCorrectInput(guess, wordLength, alphabet) | ||
} while(!isCorrect) | ||
return guess | ||
} | ||
|
||
fun playGame(secret: String, wordLength: Int, maxAttemptsCount: Int, alphabet: String) { | ||
var complete: Boolean | ||
var attempts = 0 | ||
do { | ||
println("Please input your guess. It should be of length $wordLength.") | ||
val guess = safeUserInput(wordLength, alphabet) | ||
printRoundResults(secret, guess) | ||
complete = isComplete(secret, guess) | ||
attempts++ | ||
if (isLost(complete, attempts, maxAttemptsCount)) { | ||
println("Sorry, you lost! :( My word is $secret") | ||
break | ||
} else if (isWon(complete, attempts, maxAttemptsCount)) { | ||
println("Congratulations! You guessed it!") | ||
} | ||
} while (!complete) | ||
} | ||
|
||
fun main() { | ||
val wordLength = 4 | ||
val maxAttemptsCount = 3 | ||
val secretExample = "ACEB" | ||
val alphabet = "ABCDEFGH" | ||
println(getGameRules(wordLength, maxAttemptsCount, secretExample)) | ||
playGame(generateSecret(wordLength, alphabet), wordLength, maxAttemptsCount, alphabet) | ||
} |
5 changes: 5 additions & 0 deletions
5
...ed/CompleteTheProject/src/main/kotlin/jetbrains/kotlin/course/mastermind/advanced/Util.kt
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,5 @@ | ||
package jetbrains.kotlin.course.mastermind.advanced | ||
|
||
fun safeReadLine() = readlnOrNull() ?: error("Your input is incorrect, sorry") | ||
|
||
val newLineSymbol: String = System.lineSeparator() |
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,15 @@ | ||
type: edu | ||
custom_name: Mastermind Advanced - Finish The Game | ||
files: | ||
- name: src/main/kotlin/jetbrains/kotlin/course/mastermind/advanced/Main.kt | ||
visible: true | ||
- name: src/main/kotlin/jetbrains/kotlin/course/mastermind/advanced/Util.kt | ||
visible: false | ||
- name: test/MainClass.kt | ||
visible: false | ||
- name: test/MastermindTestUtil.kt | ||
visible: false | ||
- name: test/Methods.kt | ||
visible: false | ||
- name: test/Tests.kt | ||
visible: false |
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 @@ | ||
id: 2109256460 |
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,26 @@ | ||
It's time to apply implemented functions and finish the game! | ||
|
||
### Task | ||
|
||
Replace `safeReadLine` function inside the `playGame` function with implemented | ||
on the previous step `safeUserInput` function. | ||
Since the `safeUserInput` function requires `alphabet: String` argument, don't forget update the signature of | ||
the `playGame` function. | ||
|
||
<div class="hint" title="Push me to see the new signature of the playGame function"> | ||
|
||
The signature of the function is: | ||
```kotlin | ||
fun playGame(secret: String, wordLength: Int, maxAttemptsCount: Int, alphabet: String): Unit | ||
``` | ||
</div> | ||
|
||
Finally, don't forget to use the `alphabet` argument inside the main function when you call the `playGame` function. | ||
|
||
Good luck! | ||
|
||
<div class="hint" title="Push me to see the final version of the game"> | ||
|
||
![The game's example](../../utils/src/main/resources/images/part1/warmup/game.gif "The game's example") | ||
|
||
</div> |
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,18 @@ | ||
import org.jetbrains.academy.test.system.core.models.classes.TestClass | ||
|
||
internal val mainClass = TestClass( | ||
classPackage = "jetbrains.kotlin.course.mastermind.advanced", | ||
customMethods = listOf( | ||
isCompleteMethod, | ||
countExactMatchesMethod, | ||
countPartialMatchesMethod, | ||
isWinMethod, | ||
isLostMethod, | ||
generateSecretMethod, | ||
getGameRulesMethod, | ||
printRoundResultsMethod, | ||
isCorrectInputMethod, | ||
safeUserInputMethod, | ||
playGameMethod | ||
), | ||
) |
23 changes: 23 additions & 0 deletions
23
MastermindAdvanced/CompleteTheProject/test/MastermindTestUtil.kt
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,23 @@ | ||
import util.Util | ||
|
||
internal const val SECRET = "ABCD" | ||
internal const val WORD_LENGTH = 4 | ||
internal const val ALPHABET = "ABCDEFG" | ||
|
||
data class GameStep( | ||
val attempt: String, | ||
val positions: Int, | ||
val letters: Int | ||
) { | ||
companion object { | ||
private const val welcomeMessage = "Please input your guess. It should be of length $WORD_LENGTH." | ||
} | ||
|
||
fun imitateGameProcess() = "$welcomeMessage${Util.newLineSeparator}Your guess has $positions full matches, and $letters partial matches." | ||
} | ||
|
||
enum class UserInputCorrectness(val message: String, val isCorrect: Boolean) { | ||
CORRECT("", true), | ||
INCORRECT_LENGTH("The length of your guess should be $WORD_LENGTH! Try again!", false), | ||
INCORRECT_ALPHABET("All symbols in your guess should be from the alphabet: $ALPHABET! Try again!", false) | ||
} |
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,110 @@ | ||
import org.jetbrains.academy.test.system.core.models.TestKotlinType | ||
import org.jetbrains.academy.test.system.core.models.method.TestMethod | ||
import org.jetbrains.academy.test.system.core.models.variable.TestVariable | ||
|
||
internal val isCompleteMethod = TestMethod( | ||
"isComplete", | ||
TestKotlinType("Boolean"), | ||
listOf( | ||
TestVariable("secret", "String"), | ||
TestVariable("guess", "String"), | ||
), | ||
) | ||
|
||
internal val isCorrectInputMethod = TestMethod( | ||
"isCorrectInput", | ||
TestKotlinType("Boolean"), | ||
listOf( | ||
TestVariable("userInput", "String"), | ||
TestVariable("wordLength", "Int"), | ||
TestVariable("alphabet", "String"), | ||
), | ||
) | ||
|
||
internal val safeUserInputMethod = TestMethod( | ||
"safeUserInput", | ||
TestKotlinType("String"), | ||
listOf( | ||
TestVariable("wordLength", "Int"), | ||
TestVariable("alphabet", "String"), | ||
), | ||
) | ||
|
||
internal val countExactMatchesMethod = TestMethod( | ||
"countExactMatches", | ||
TestKotlinType("Int"), | ||
listOf( | ||
TestVariable("secret", "String"), | ||
TestVariable("guess", "String"), | ||
), | ||
) | ||
|
||
internal val countPartialMatchesMethod = TestMethod( | ||
"countPartialMatches", | ||
TestKotlinType("Int"), | ||
listOf( | ||
TestVariable("secret", "String"), | ||
TestVariable("guess", "String"), | ||
), | ||
) | ||
|
||
internal val isWinMethod = TestMethod( | ||
"isWon", | ||
TestKotlinType("Boolean"), | ||
listOf( | ||
TestVariable("complete", "Boolean"), | ||
TestVariable("attempts", "Int"), | ||
TestVariable("maxAttemptsCount", "Int"), | ||
), | ||
) | ||
|
||
internal val isLostMethod = TestMethod( | ||
"isLost", | ||
TestKotlinType("Boolean"), | ||
listOf( | ||
TestVariable("complete", "Boolean"), | ||
TestVariable("attempts", "Int"), | ||
TestVariable("maxAttemptsCount", "Int"), | ||
), | ||
) | ||
|
||
internal val generateSecretMethod = TestMethod( | ||
"generateSecret", | ||
TestKotlinType("String"), | ||
listOf( | ||
TestVariable("wordLength", "Int"), | ||
TestVariable("alphabet", "String"), | ||
), | ||
) | ||
|
||
internal val playGameMethod = TestMethod( | ||
"playGame", | ||
TestKotlinType("Unit"), | ||
listOf( | ||
TestVariable("secret", "String"), | ||
TestVariable("wordLength", "Int"), | ||
TestVariable("maxAttemptsCount", "Int"), | ||
TestVariable("alphabet", "String"), | ||
), | ||
"Void", | ||
) | ||
|
||
internal val getGameRulesMethod = TestMethod( | ||
"getGameRules", | ||
TestKotlinType("String"), | ||
listOf( | ||
TestVariable("wordLength", "Int"), | ||
TestVariable("maxAttemptsCount", "Int"), | ||
TestVariable("secretExample", "String"), | ||
), | ||
) | ||
|
||
internal val printRoundResultsMethod = TestMethod( | ||
"printRoundResults", | ||
TestKotlinType("Unit"), | ||
listOf( | ||
TestVariable("secret", "String"), | ||
TestVariable("guess", "String"), | ||
), | ||
"Void", | ||
) |
Oops, something went wrong.