/ scala

Scala 006 Rock Paper Scissors

The code in this post is possibly hugely inefficent and could have been done way better

The task was to create rock paper scissors in scala. The idea was kept simple that the other player would just simply select a choice at random instead of acting intelligent.

rockPaperScissors.scala

object game  {
  class player (var id:Int, var state:Boolean, var wins:Int)  {
    def win (amount:Int)  {
      this.wins = this.wins + 1
    }
  }

  def main (args: Array[String])  {
    var input = ""
    var ongoing = 0
    var player = new player (1, false, 0)
    while (input != "q")  {
      println("\n\n\n\n\n\n\n\n\n\n\n\n\n==================================================")
      println("\nRock, Paper, Scissors.\nWins: "+ player.wins +"\n==================================================\n\n[p] Play\n[q] Quit")
      input = readLine("> ")

      if (input == "p")
      {
        ongoing = 0
        playgame
      }

    }
    def playgame ()  {
      //player win 1

      var state = 0
      while (ongoing != 1)  {
        println("\n\n\n\n\n\n\n\n\n\n\n\n\n==================================================")
        println("\nWhich hand do you wish to play?\n\n[r] Rock\n[p] Paper\n[s] Scissors\n\n To quit enter [q]")
        println("\n==================================================")
        input = readLine ("> ")

        if (input == "r" || input == "rock")  {
          state = 1
          oppTurn(0)
        }
        else if (input == "p" || input == "paper")  {
          state = 1
          oppTurn(1)
        }
        else if (input == "s" || input == "scissors")  {
          state = 1
          oppTurn(2)
        }
        else if (input == "q")  {
          ongoing = 1
        }  else  {
          println("Invalid choice\n")
        }
      }
    }
    def oppTurn(myHand:Int)  {
      val rnd = new scala.util.Random
      val oppHand = rnd.nextInt(3)
      logic (myHand, oppHand)
    }

    def logic(myHand:Int, oppHand:Int)
    {
      var message = ""
      if (myHand == 0 && oppHand == 2)  {
        player win 1
        ongoing = 1
        message = "Win"
      }
      else if (myHand == 1 && oppHand == 0)  {
        player win 1
        ongoing = 1
        message = "Win"
      }
      else if (myHand == 2 && oppHand == 1)   {
        player win 1
        ongoing = 1
        message = "Win"
      }
      else if (myHand == 0 && oppHand == 0)  {
        message = "Draw"
      }
      else if (myHand == 1 && oppHand == 1)  {
        message = "Draw"
      }
      else if (myHand == 2 && oppHand == 2)  {
        message = "Draw"
      }
      else   {
        message = "Lost"
      }
      println("\n\n\n\n==================================================")
      if (message == "Win")  {
        println("You have won this game")
      }  else if (message == "Lost") {
        println("You have lost this game")
      }  else if (message == "Draw")  {
        println("It was a tie!")
      }

      print("\nYour hand was ")
      if (myHand == 0)  {
        print("Rock")
      } else if (myHand == 1)  {
        print("Paper")
      } else if (myHand == 2)  {
        print("Scissors")
      } else {
        print("Error")
      }
      print(" and opponents hand was ")
      if (oppHand == 0)  {
        print("Rock")
      } else if (oppHand == 1)  {
        print("Paper")
      } else if (oppHand == 2)  {
        print("Scissors")
      } else {
        print("Error")
      }
      println("\n==================================================")
    }
  }


}