rosettacode: pig the dice game
authorRomain Chanoir <romain.chanoir@viacesi.fr>
Tue, 23 Jun 2015 00:04:11 +0000 (20:04 -0400)
committerJean Privat <jean@pryen.org>
Tue, 23 Jun 2015 00:12:20 +0000 (20:12 -0400)
Close #1485

Signed-off-by: BlackMinou <romain.chanoir@viacesi.fr>

examples/rosettacode/pig_the_dice_game.nit [new file with mode: 0644]

diff --git a/examples/rosettacode/pig_the_dice_game.nit b/examples/rosettacode/pig_the_dice_game.nit
new file mode 100644 (file)
index 0000000..da4c218
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/env nit
+#
+# This file is part of NIT ( http://www.nitlanguage.org ).
+# This program is public domain
+
+# Task: Pig the dice game
+# SEE: http://rosettacode.org/wiki/Pig_the_dice_game
+
+var player = 0
+var score = [0, 0]
+var turnscore = 0
+var max_score = 100
+loop
+       if (score[player] + turnscore) >= max_score then
+                       print "Congratulation Player {player + 1}! You won with {score[player]} points! "
+                       return
+       end
+       print "Player {player +1}"
+       print "Total score : {score[player]}"
+       print "What do you want to do? (\"1\" to roll, \"0\" to hold)"
+       if stdin.eof then break
+       var input = gets
+       if input == "1" then
+               var roll = 6.rand + 1
+               if roll == 1 then
+                       turnscore = 0
+                       print "You rolled 1! all points lost for this turn"
+                       print ""
+                       player = 1 - player
+               else
+                       turnscore += roll
+                       print "rolled: {roll}"
+                       print "score this turn: {turnscore}"
+                       print ""
+               end
+       else if input == "0" then
+               score[player] += turnscore
+               print "Scored: {turnscore}"
+               print "New Score: {score[player]}"
+               print ""
+               player = 1 - player
+               turnscore = 0
+       end
+end