Pig the dice game: Difference between revisions

Added Kotlin
(Added Kotlin)
Line 1,977:
Bob played 3 rolls and scored 8 points.
Bob won, scoring 102 points.
</pre>
 
=={{header|Kotlin}}==
<lang scala>// version 1.1.2
 
fun main(Args: Array<String>) {
print("Player 1 - Enter your name : ")
val name1 = readLine()!!.trim().let { if (it == "") "PLAYER 1" else it.toUpperCase() }
print("Player 2 - Enter your name : ")
val name2 = readLine()!!.trim().let { if (it == "") "PLAYER 2" else it.toUpperCase() }
val names = listOf(name1, name2)
val r = java.util.Random()
val totals = intArrayOf(0, 0)
var player = 0
while (true) {
println("\n${names[player]}")
println(" Your total score is currently ${totals[player]}")
var score = 0
while (true) {
print(" Roll or Hold r/h : ")
val rh = readLine()!![0].toLowerCase()
if (rh == 'h') {
totals[player] += score
println(" Your total score is now ${totals[player]}")
if (totals[player] >= 100) {
println(" So, ${names[player]}, YOU'VE WON!")
return
}
player = if (player == 0) 1 else 0
break
}
if (rh != 'r') {
println(" Must be 'r'or 'h', try again")
continue
}
val dice = 1 + r.nextInt(6)
println(" You have thrown a $dice")
if (dice == 1) {
println(" Sorry, your score for this round is now 0")
println(" Your total score remains at ${totals[player]}")
player = if (player == 0) 1 else 0
break
}
score += dice
println(" Your score for the round is now $score")
}
}
}</lang>
 
{{out}}
<pre>
Player 1 - Enter your name : Donald
Player 2 - Enter your name : Barack
 
DONALD
Your total score is currently 0
Roll or Hold r/h : r
You have thrown a 6
Your score for the round is now 6
Roll or Hold r/h : r
You have thrown a 2
Your score for the round is now 8
Roll or Hold r/h : r
You have thrown a 4
Your score for the round is now 12
Roll or Hold r/h : r
You have thrown a 2
Your score for the round is now 14
Roll or Hold r/h : r
You have thrown a 4
Your score for the round is now 18
Roll or Hold r/h : r
You have thrown a 6
Your score for the round is now 24
Roll or Hold r/h : h
Your total score is now 24
 
BARACK
Your total score is currently 0
Roll or Hold r/h : r
You have thrown a 5
Your score for the round is now 5
Roll or Hold r/h : r
You have thrown a 2
Your score for the round is now 7
Roll or Hold r/h : r
You have thrown a 3
Your score for the round is now 10
Roll or Hold r/h : r
You have thrown a 5
Your score for the round is now 15
Roll or Hold r/h : r
You have thrown a 4
Your score for the round is now 19
Roll or Hold r/h : r
You have thrown a 4
Your score for the round is now 23
Roll or Hold r/h : h
Your total score is now 23
 
DONALD
Your total score is currently 24
Roll or Hold r/h : r
You have thrown a 5
Your score for the round is now 5
Roll or Hold r/h : r
You have thrown a 5
Your score for the round is now 10
Roll or Hold r/h : r
You have thrown a 4
Your score for the round is now 14
Roll or Hold r/h : r
You have thrown a 1
Sorry, your score for this round is now 0
Your total score remains at 24
.........
.........
DONALD
Your total score is currently 81
Roll or Hold r/h : r
You have thrown a 1
Sorry, your score for this round is now 0
Your total score remains at 81
 
BARACK
Your total score is currently 85
Roll or Hold r/h : r
You have thrown a 5
Your score for the round is now 5
Roll or Hold r/h : r
You have thrown a 2
Your score for the round is now 7
Roll or Hold r/h : r
You have thrown a 3
Your score for the round is now 10
Roll or Hold r/h : r
You have thrown a 3
Your score for the round is now 13
Roll or Hold r/h : r
You have thrown a 5
Your score for the round is now 18
Roll or Hold r/h : h
Your total score is now 103
So, BARACK, YOU'VE WON!
</pre>
 
9,485

edits