Oxygen for Java Version of Roulette
Pascal version by Ayotunde: Y9 Age ~14
Introduction
This Oxygene for Java version outputs Java bytecote in a .jar file. The bytecode runs in a Java Virtual Machine on a PC and on the Pi and should run on any platform with the Java Development Kit (JDK) installed. This screenshot shows the program running on the Pi.
Roulette on the PiWe used the command sudo apt-get install openjdk-6-jdk to install a version of the SDK. The java executable installs to the /usr/bin folder. We created the directory Oxygene with the command mkdir Oxygene then copied roulette.jar to that directory. Finally, the command /usr/bin/java -jar Oxygene/roulette.jar executed the program as shown.
In Oxygene for Java, regardless of the type of input, you must first use an array of Byte to store the keyed-in characters. We found it necessary to initialise the array of byte (to represent spaces) before each input, otherwise there could be corruption from additional characters remaining from previous longer inputs. The alternative is to store then use the number of characters entered for each input, as shown in our tutorial on keyboard input.
The Program
namespace roulette; { Copyright (c) 2012 Ayotunde Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License, as described at http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ Converted to Oxygene for Java by PPS. } interface type ConsoleApp = static class private endgame, number, redblack, bet, wheelroll, decide, bank, deposit, take, redblackchoose : Integer; total, wager1, wager2, maxbet, minbet : Double; input_chars : array[0 .. 6] of byte := [32, 32, 32, 32, 32, 32, 32]; str_temp : String; public class method Main(args : array of String); class method betS; class method betR; end; implementation uses java.util; class method ConsoleApp.betS; begin Thread.sleep(500); System.out.println('You can bet up to three quarters of your current total.'); Thread.sleep(500); System.out.println('How much money do you want to bet, in pounds?'); input_chars := [32, 32, 32, 32, 32, 32, 32]; System.in.read(input_chars); str_temp := new String(input_chars); wager1 := Float.valueOf(str_temp.trim); maxbet := total * 0.75; minbet := 6; if wager1 >= (maxbet + 1) then begin System.out.println('That is too much. You want to be able to buy food, no?'); end else if wager1 <= (minbet - 1) then begin System.out.println('That is too little. This is for-profit business, you know.'); end; end; class method ConsoleApp.betR; begin System.out.println('You can bet up to half of your current total.'); Thread.sleep(500); System.out.println('How much you want to bet?'); input_chars := [32, 32, 32, 32, 32, 32, 32]; System.in.read(input_chars); str_temp := new String(input_chars); wager2 := Float.valueOf(str_temp.trim); maxbet := total * 0.5; minbet := 6; if wager2 >= (maxbet + 1) then begin System.out.println('That is too much. You want to be able to buy food, no?'); end else if wager2 <= (minbet - 1) then begin System.out.println('That is too little. This is for-profit business, you know.'); end; end; class method ConsoleApp.Main(args : array of String); begin var random : Random := new Random; wager1 := 999; bank := 50; endgame := 5667; total := 100; System.out.println('Velcome. You vant to lose some cash?'); Thread.sleep(500); repeat System.out.println('You can place bet on either number or a colour, red or black.'); Thread.sleep(500); System.out.printf('You have %.2f pounds. Try not to lose it too fast!%n', total); Thread.sleep(500); System.out.println('1= Bet on a number.'); System.out.println('2= Bet on red or black.'); System.out.println('3= Vithdraw some cash.'); input_chars := [32, 32, 32, 32, 32, 32, 32]; System.in.read(input_chars); str_temp := new String(input_chars); bet := Integer.valueOf(str_temp.trim); if bet = 1 then begin repeat ConsoleApp.betS; until (wager1 < (maxbet + 1)) and (wager1 > (minbet - 1)); if (wager1 < (maxbet + 1)) and (wager1 > (minbet - 1)) then begin System.out.println('Bet on a number. It can be anything from 1 to 37.'); wheelroll := random.nextInt(37) + 1; input_chars := [32, 32, 32, 32, 32, 32, 32]; System.in.read(input_chars); str_temp := new String(input_chars); number := Integer.valueOf(str_temp.trim); if number = wheelroll then begin System.out.println('Hey, you hit the jackpot! You guessed right!'); total := total + 30 * wager1; end else begin System.out.println('Sorry, that is incorrect....hehehe...'); total := total - wager1; end; end; //if wager1 end; //bet = 1 if bet = 2 then begin repeat ConsoleApp.betR; until (wager2 < (maxbet + 1)) and (wager2 > (minbet - 1)); if (wager2 < (maxbet + 1)) and (wager2 > (minbet - 1)) then begin System.out.println('Bet on 1 (Red) or 2 (Black).'); redblack := random.nextInt(2) + 1; //Redblack can either be 2 or 1. input_chars := [32, 32, 32, 32, 32, 32, 32]; System.in.read(input_chars); str_temp := new String(input_chars); redblackchoose := Integer.valueOf(str_temp.trim); if (redblack = 1) and (redblackchoose = 1) then begin System.out.println('It came out red. You got lucky this time.'); total := total + wager2; end else if (redblack = 2) and (redblackchoose = 2) then begin System.out.println('It seems to be black. Fortune has smiled on you.'); total := total + wager2; end else begin System.out.println('Unlucky, you were wrong.'); total := total - wager2; end; end; end //bet = 2 else if bet = 3 then begin repeat System.out.println('Your current balance is ' + bank.toString + ' pounds.'); Thread.sleep(100); System.out.println('How much you want to vithdraw?'); input_chars := [32, 32, 32, 32, 32, 32, 32]; System.in.read(input_chars); str_temp := new String(input_chars); take := Integer.valueOf(str_temp.trim); if take > bank then System.out.println('Hehehe, nice try, kiddo.'); until take <= bank; bank := bank - take; total := total + take; System.out.println('Your balance is now ' + bank.toString + ' pounds.'); end; //bet = 3 System.out.println('Come on, play Roulette.'); System.out.println('Want to try again?'); System.out.println('1= Have another go.'); System.out.println('2= Quit the game.'); System.out.println('3= Continue and bank some cash.'); input_chars := [32, 32, 32, 32, 32, 32, 32]; System.in.read(input_chars); str_temp := new String(input_chars); decide := Integer.valueOf(str_temp.trim); if decide = 2 then begin System.out.println('........'); endgame := 1789; end else if decide = 3 then begin System.out.println('How much cash you vant to bank?'); input_chars := [32, 32, 32, 32, 32, 32, 32]; System.in.read(input_chars); str_temp := new String(input_chars); deposit := Integer.valueOf(str_temp.trim); total := total - deposit; bank := bank + deposit; System.out.println('Your current balance is ' + bank.toString + ' pounds.'); Thread.sleep(100); end; if decide = 1 then begin Thread.sleep(100); end; until (total < minbet * 2) or (total > 999) or (endgame = 1789); if total < minbet * 2 then begin System.out.printf('You have %.2f pounds remaining.%n', total); System.out.println('You have not got enough money to keep playing. Get out!'); Thread.sleep(2000); end; if endgame = 1789 then begin System.out.printf('You have %.2f pounds remaining.%n', total); if (total + bank) < 150 then begin System.out.println('Thank you for vasting money. Please show yourself out.'); end; Thread.sleep(2000); end; end; end.



dita powered.