Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Apr 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The betting game Keno! ...small issue

    I've been working ahead in my intro class and i've almost finished my last project, Keno. its a betting game that rewards money according to how many numbers you matched with the dealer. I'm having issues on where to put the betting aspect, they start with 100$ and are asked to wage a certain amount of money. I don't know which method that would go under for it to still work because my methods aren't voids, so i wont be able to return more than one data value.

    my second issue, maybe the more important one, is that they need to be unique numbers. To do that i would need to search the array of numbers every time to see if they match, or use an array of booleans to keep track of the numbers. I don't know how i would do the second but i have a good idea of what i would do with the first. The issue is that im using a do while already, im not sure how i could add the for loop with a nested for loop in. Here is my code, sorry if its messy, i know my teacher hates my curly braces:
    Code:
    //Adam Musciano Keno
    
    package Keno;
    
    import cs1.Keyboard;
    
    public class Keno {
    	public static void main(String[]args){
    		int userArr[]=user();
    		int compArr[]=computer();
    		int howMany=matchNums(compArr,userArr);
    		int moneyGained=betting(howMany);
    		
    		System.out.println("You matched "+howMany+" numbers");
    		System.out.println("You have gained "+moneyGained+" dollars!");
    		
    	}
    	
    	public static int[] computer(){
    		int []compChoice=new int[20];
    		for(int x=0;x<compChoice.length;x++){
    			compChoice[x]=(int)(Math.random()*81);
    		}
    		return compChoice;
    	}
    	public static int[] user(){
    		int choice[]=new int[7];
    		System.out.println("Welcome to Keno!");
    		System.out.println("Choose 7 unique numbers ranging from 1-80");
    		System.out.println("*************************************************");
    		//assigns numbers to choice array
    		for(int x=0;x<choice.length;x++){
    			do{
    				int temp=x+1;
    				System.out.println("number "+temp+": ");
    				choice[x]=Keyboard.readInt();
    			}while(choice[x]<0||choice[x]>80);
    			
    		}
    		System.out.println("Thanks!");
    		System.out.println("*************************************************");
    		return choice;
    	
    	}
    	public static int matchNums(int arr1[], int arr2[]){
    		int count=0;
    		//checks each array slot individually to see if they match
    		for(int x=0;x<arr1.length;x++){
    			for(int y=0;y<arr2.length;y++){
    				if(arr1[x]==arr2[y]){
    					count++;
    				}
    			}
    		}
    		return count;
    	}
    	public static int betting(int matches){
    		int moneyGained=0;
    		if(matches==7){
    			moneyGained=12000;
    		}else if(matches==6){
    			moneyGained=200;
    		}else if(matches==5){
    			moneyGained=20;
    		}else if(moneyGained==4){
    			moneyGained=1;
    		}
    		return moneyGained;
    	}
    		
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Are you stuck with the signatures as you have them here? If so, betting will require you to store it within a class property if you want to be able to use it later in the betting() method. Otherwise, just modify the betting to accept the user's choice.
    I'm not that familiar with Keno. Is the user only allowed to bet an increase on the entire game, or on each item chosen? If its on each item chosen, that'll take a bit more work, but if its just for the game, than you can add that in main. You would then alter the betting signature so that it accepts the number of matches, as well as the amount to bet and calculate from there.
    For tracking already used, I would suggest the use of an iterative search based on what you have currently. Write a new method that returns a boolean and accepts an int[], int. Then simply run it through for each attempt to place in the array. This would be usable for both the user and computer selections.

    As an alternative selection which would require a pretty much entire rewrite (which also means you have signature control), without moving to a collection level you could always work with an boolean[80] array, for both the user and the computer. Then you make use of the key position as the selection instead of the value. Java's arrays are 0 based, so you'd need to select position - 1 for specific numbers, but comparisons wouldn't require that. So if a selection of number 28 is used, that would simply mean that arr[27] is set to true.

    Pros of using a boolean[80] to the int[20|7] are:
    O(1) search capability. You simply check if arr[chosen - 1] is true
    O(1) comparative *for each* of the items. That means you have only 80 loops versus the current 140 loops
    Cons of using boolean[80] are:
    A bit more memory to use. This would require 2 x 80 x 1 bytes or 160 bytes of memory versus 4b * 20 + 4b * 7 which is only 108 bytes
    Would require an entire rewrite of the existing code.

    The project at hand doesn't throw enough weight behind either to make it preferred (ie: no matter which you choose the performance difference would be nominal). I myself would look at the order of magnitude on the run of the script which refers to the amount of looping you will perform, and would choose the two boolean 80's since they are more efficient at the cost of memory. Programming is all about the trade offs, for every choice you make, you sacrifice something to get there. You simply want to choose the right balance between efficiency and logical. I don't do small embedded system development, so I typically consider larger chunks of memory to be expendable whilst opting for efficiency or readability.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •