Dice game simulator kotlin
I'm writing a code where I am versing the computer. A player throws a six-sided die and scores as many points as the total shown on the die providing the die doesn’t roll a 1. The player may continue rolling and accumulating points (but risk rolling a 1) or end his turn. If the player rolls a 1 his turn is over, he loses all points he accumulated that turn, and he passes the die to the next player.
If the player ends without throwing a 1, the turn total is then added to the player's grand total.
Play passes from player to player until a winner is determined.
We are playing against the computer. The computer will roll the die using this rule:
A random number between 1 and 2 is CALCULATED. IF the number is a 1, the computer will roll. IF the number is a 2 it will stop rolling and give the dice back to the player. This loops until the calculated random number is a 2.
If a 1 is rolled, the turn is over and the computer loses all points from that turn. The sum of all the rolls of the dice during this turn are added to computer's grand total
The human player can choose from a menu whether to roll again or "hold" and allow the computer to have a turn.
The issue is when I press hold (2) to pass it to the computer (ai) or when I roll a 1 it does nothing. the program stops. I am trying to git the program to only stop if all conditions are met can someone please help this is what I have so far.
var player = 0
var turntotal = 0
var computerpoints = 0
var grandtotal = 0
var roll = (1..6).random()
var ai = (1..2).random()
println("1. Roll Dice")
println("2. Hold and pass to computer")
println("3. Quit")
println()
println("Please select a menu Item")
player = readLine()!!.toInt()
while (turntotal < 50 && computerpoints < 50 && player != 3)
if (player == 1) {
roll = (1..6).random()
println("You rolled a $roll")
turntotal += roll
println("Turn total: $turntotal")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
while (roll != 1) {
println("1. Roll Again")
println("2. Hold")
player = readLine()!!.toInt()
if (player == 2) break
roll = (1..6).random()
println("You rolled a $roll")
turntotal += roll
println("Turn total: $turntotal")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
}
if (roll == 1) {
turntotal = 0
println("You rolled a $roll")
println("You lose all your turn points and turn ")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
println()
println("Computer's turn to roll")
while ( ai != 1)
roll = (1..2).random()
if (roll == 1)
roll =(1..6).random()
println("Computer decides to roll: $roll")
computerpoints += roll
println("Computer's points: $computerpoints")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
if (roll== 1)
computerpoints = 0
println("Computer rolled a $roll")
println("Computer's points is $computerpoints")
println("***********************************")
println("* Grand Total - You: $turntotal Computer:$computerpoints *")
println("***********************************")
if (roll == 2 )
println("1. Roll Dice")
println("2. Hold and pass to computer")
println("3. Quit")
println()
println("Please select a menu Item")
player = readLine()!!.toInt()
}
}
}
Comments
Post a Comment