Advertisement
Guest User

My first Scala program, the number guessing game.

a guest
Apr 14th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.60 KB | None | 0 0
  1.  
  2. import scala.swing._
  3. import scala.util.Random
  4. import event._
  5. import scala.swing.BorderPanel.Position._
  6.  
  7. object GuessingGame extends SimpleSwingApplication {
  8.  
  9.   val r = new Random
  10.  
  11.   def newRand(max: Int): Int = {
  12.     r.nextInt(max)
  13.   }
  14.  
  15.   var maxTries = 7
  16.   var tries = 0
  17.   var sekretNum = newRand(100)
  18.  
  19.   def top = new MainFrame {
  20.     title = "Guessing Game"
  21.     preferredSize = new Dimension(700, 400)
  22.     contents = new BorderPanel {
  23.       val hundred = new Button {
  24.         text = "1-100 range"
  25.       }
  26.       val thousand = new Button {
  27.         text = "1-1000 range"
  28.       }
  29.       val guess = new TextField {
  30.         columns = 15
  31.         text = "Take a wild guess between 0 and 100!"
  32.       }
  33.       val buttonBox = new BoxPanel(Orientation.Vertical) {
  34.         contents += hundred
  35.         contents += thousand
  36.         contents += guess
  37.         border = Swing.EmptyBorder(30, 30, 10, 30)
  38.       }
  39.  
  40.       val feedback = new TextArea {
  41.         text = ""
  42.       }
  43.       val feedbackBox = new BoxPanel(Orientation.Vertical) {
  44.         contents += feedback
  45.         border = Swing.EmptyBorder(30, 30, 10, 30)
  46.       }
  47.  
  48.       layout(buttonBox) = West
  49.       //layout(thousand) = West
  50.       layout(feedback) = Center
  51.  
  52.  
  53.       listenTo(hundred)
  54.       listenTo(thousand)
  55.       listenTo(guess)
  56.       reactions += {
  57.         case ButtonClicked(component) if component == hundred => {
  58.           sekretNum = newRand(100)
  59.           maxTries = 7
  60.           tries = 0
  61.           guess.text = "Take a wild guess between 0 and 100"
  62.         }
  63.         case ButtonClicked(component) if component == thousand => {
  64.           sekretNum = newRand(1000)
  65.           maxTries = 20
  66.           tries = 0
  67.           guess.text = "Take a wild guess between 0 and 1000"
  68.         }
  69.         case MouseClicked(component, _, _, _, _ )  if component == guess => {
  70.           guess.text = ""
  71.         }
  72.         case EditDone(component) if component == guess => {
  73.  
  74.           if (tries == maxTries) {
  75.             feedback.text += "You lose!\n"
  76.             tries = 0
  77.           }
  78.           else if (guess.text.forall(! _.isDigit)){
  79.             guess.text = ""
  80.           }
  81.           else {
  82.             if ( guess.text.toInt < sekretNum) {
  83.               feedback.text += "Higher\n"
  84.               tries += 1
  85.             }
  86.             else if (guess.text.toInt > sekretNum) {
  87.               feedback.text += "Lower!\n"
  88.               tries += 1
  89.             }
  90.             else {
  91.               feedback.text += "You win!\n"
  92.               tries = 0
  93.             }
  94.           }
  95.           guess.text = ""
  96.         }
  97.       }
  98.     }
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement