Life of the IT-Drummer

A web log from Tobias Rusås Olsen.

Lottery - Norwegian joker game in Groovy

By request, and for fun, I made the game “Joker” (I think that’s what it’s called?), best known for it’s parody “Ekstra Joker Nord” (Extra Joker North) from Team Antonsen:

It’s a simple game where you get five numbers from 1 to 9, and decide if you want to go up or down based on that number, then there’s generated a random number, and if you made the right decision, you climb up the prize ladder, if you made the wrong decision you climb down the prize ladder.

It’s made in Groovy, and is in fact my first Groovy program ever, therefore I have not used much of the cool features of Groovy, and made Groovy look not that groovy. For that I apologize. The code is pretty much Java in easy mode. I made two features, one for playing “manually”, and one where the machine automatically makes the best choice for you (up if you get 2, down if you get 6 and so on). There’s also a while loop you can uncomment to see some statistics.

The code:

/**
 * @author Tobias Rusås Olsen
 * 
 */
class EkstraTrekning{
	Random r
	Scanner scan = new Scanner(System.in)
	int[] gevinster
 
	EkstraTrekning() {
		r = new Random()
		gevinster = [124203, 240324, 506303,1204296,1920354]
	}
 
	static void main(String[] a) {
		EkstraTrekning ek = new EkstraTrekning()
		int j = 0
		int riktige = 0
		int sum = 0
		int toppgevinst = 0
		int gevinstplassering = 1
 
		//Repeat the process if you want to check statistics etc.
//		while(j<10000) {
		int[] str = [ek.r.nextInt(9)+1,ek.r.nextInt(9)+1,ek.r.nextInt(9)+1,ek.r.nextInt(9)+1,ek.r.nextInt(9)+1]
			boolean toppBool = true
			for(int i : str) {
				//Here you choose what method to call.
				if(ek.calulateValueManual(i)) {
					riktige++
					if (gevinstplassering!=4) gevinstplassering++
				}
				else {
					toppBool = false
					if(gevinstplassering!=1) gevinstplassering--
				}
				println "Gevinst: " + ek.gevinster[gevinstplassering-1]
				sum++
			}
			j++
			if(toppBool) toppgevinst++
			println "Gratulerer, du vant " + ek.gevinster[gevinstplassering-1] + " kroner! :)"
//		}
		print riktige + " av " + sum + ". Antall toppgevinster: " + toppgevinst
	}
 
	boolean calulateValue(int i) {
		boolean chosenOver = false
		boolean riktig = false
		int rand = r.nextInt(9)+1
 
		boolean over = (rand >= i)
		if(i < 6) {
			chosenOver = true
			println "Tallet er " + i + ". Du velger over"
		}
		else println "Tallet er " + i + ". Du velger under"
 
		if (rand == i) {
			println "Tallet ble " + rand + ", altså fikk du riktig"
			riktig = true
		}
		else if(over && chosenOver) {
			println "Tallet ble " + rand + ", altså fikk du riktig"
			riktig = true
		}
		else if (over && !chosenOver) println "3: Tallet ble " + rand + ", altså fikk du feil"
		else if (!over && !chosenOver){
			println "Tallet ble " + rand + ", altså fikk du riktig"
			riktig = true
		}
		else println "Tallet ble " + rand + ", altså fikk du feil"
		return riktig
	}
 
	boolean calulateValueManual(int i) {
		boolean riktig = false
		boolean chosenOver = false
		int rand = r.nextInt(9)+1
		boolean over = (rand >= i) 
 
		println "Tallet er " + i + ". Opp (1) eller ned(2)?"
		int inp = scan.nextInt()
		if(inp==1) chosenOver = true
		else chosenOver = false
 
		if (rand == i) {
			println "Tallet ble " + rand + ", altså fikk du riktig"
			riktig = true
		}
		else if(over && chosenOver) {
			println "Tallet ble " + rand + ", altså fikk du riktig"
			riktig = true
		}
		else if (over && !chosenOver) println "Tallet ble " + rand + ", altså fikk du feil"
		else if (!over && !chosenOver){
			println "Tallet ble " + rand + ", altså fikk du riktig"
			riktig = true
		}
		else println "Tallet ble " + rand + ", altså fikk du feil"
		return riktig
	}	
}
RSS 2.0 | Trackback | Comment

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">