Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Really important assignment need checking please :)

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Really important assignment need checking please :)

    The point of the game is that there are 3 players player one has 25 accuracy player 2 has 50 accuracy and player 3 has one hundred accuracy who will win in a 10000 round shootoff. thank you so much for helping me out i just really want to make sure that this is correct.


    I get outputs like this

    Player 1 won 4944.0 times
    Player 2 won 7606.0 times
    Player 3 won 3725.0 times
    Player 1 probablity is 49.44%
    Player 2 probability is 76.06%
    Player 2 probability is 37.25%

    I do not know if this is right because player 2 always seems to win.

    Have a great day ツ ☻



    import java.io.PrintStream;
    import java.util.Random;
    import java.util.Scanner;
     
     
    public class SkillGame {
     
    	public static void main(String[] args) {
    		int rnum, life = 1, life2 = 1, life3 = 1, player, p1 = 0, p2 = 0, p3 = 0, acc1 = 0, acc2 = 0, acc3 = 0, rnum2, rnum3, rnum4, num;
    		double cnt1 = 0, cnt2 = 0, cnt3 = 0,  fin, fin2, fin3;
    		Scanner in = new Scanner(System.in);
    		PrintStream out = new PrintStream(System.out);
    		Random rnd = new Random();
    		acc1 = 25;
    		acc2 = 50;
    		acc3 = 100;
    		num = 1;
    		for(int i = 0; i < 10000; i++){
    			while(true){
    		if(life == 1 )
    		{
    			rnum4 = rnd.nextInt(4);
    			if(life2 == 1 && life3 == 1)
    			{
    				if(acc3 > acc2)
    				{
    					if (rnum4 == 1)
    					{
     
    						life3 = 0;
    						cnt1 = cnt1 + 1;
    					}
    				if(life2 == 1)
    				{
    					if(rnum4 == 1)
    					{
    						life2 = 0;
    						cnt1 = cnt1 +1;
    					}
    					if(life2 == 1);
    					{
    						rnum3 = rnd.nextInt(2);
    						if(life == 1 && life3 == 1)
    						{
    							if(acc3 > acc1)
    							{
    								if(rnum3 == 1)
    								{
    								life3 = 0;
    								cnt2 = cnt2 +1;
    								}
    								if(life == 1){
    									if(rnum3 == 1){
    										life = 0;
    										cnt2 = cnt2 +1;
    									}
    								}
     
    								if(life3 == 1)
    								{
    									if(life2 == 1){
    										life2 = 0;
    										cnt3 = cnt3 +1;
    									}
     
    								}
    							}
    						}
    					}
    				}
    			    }
    			}
    		}  
    		if(i < 10000)
    			break;
    		}
    		life = 1; 
    		life2 = 1; 
    		life3 = 1;
    		}
    		 out.println("Player 1 won " + cnt1 + " times" + "\nPlayer 2 won " + cnt2 + " times" + "\nPlayer 3 won " + cnt3 + " times");
    		 fin = cnt1/10000 * 100;
    		 Math.round(fin);
    		 fin2 = cnt2/10000 * 100;
    		 Math.round(fin2);
    		 fin3 = cnt3/10000 * 100;
    		 Math.floor(fin3);
    		out.println("Player 1 probablity is " + fin + "%\n" + "Player 2 probability is " + fin2 + "%\n" + "Player 2 probability is " + fin3 + "%");
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Really important assignment need checking please :)

    Define the meaning of 25/50/100 accuracy. Is 100 accuracy better or worse than 25 accuracy? How or why?

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    ClarmonkGaming (July 30th, 2014)

  4. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Really important assignment need checking please :)

    the shooter wants to shoot the guy with the highest accuracy

    so 25 accu will try to shoot 100 accu
    but he may miss because his accu is 25

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Really important assignment need checking please :)

    Ya, that doesn't really help. If those numbers were percentages, 25% accuracy, 50% accuracy, and 100% accuracy, then a program could easily be written to show each shooter's hits out of 10,000. That doesn't seem quite right, because I wouldn't expect a shooter to have 100% accuracy, the results would always be the same, and shooters wouldn't be shooting each other as you've suggested.

    Sorry, I'm not sure how to help with the info you've posted.

  6. #5
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Really important assignment need checking please :)

    Using random number generation is always going to give you
    well, random numbers. Unless you hard code the seed of the
    number generator to have say a 50% chance of either result,
    it's going to be hit or miss what numbers you get.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  7. #6
    Junior Member
    Join Date
    Jul 2014
    Location
    Canada
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 5 Times in 4 Posts

    Default Re: Really important assignment need checking please :)

    I think the most important factors to note in the example stats you gave is that 4944 + 7606 + 3725 > 10000, and 49.44 + 76.06 + 37.25 > 100%... There's clearly an issue there; it probably has to do with the nested loops you have.

    In terms of the stats you want to be getting:

    This is an example of a truel - the way you've written it p2 will generally come out on top because p1 is presumably forced to shoot (in this scenario, p1 should waste his shot).

    p1's 1/4 + p2's 1/2 gives them a 62.5% chance to kill p3 before he can shoot. If p3 is killed, then it's p1's turn again, and he shoots at p2 with a 1/4 chance. He will miss 3/4 times, allowing p2 a 1/2 chance to shoot. Even if p2 misses, p1's overall chances after his next shot are only 43.75%, where p2's overall chances are 75% if the turn passes back to him. If p3 is not killed, he will kill p2 with 100% accuracy, and p3 will have a 1/4 chance to win, so p3's 37.5% survival chance drops down to only 28.125% (this is his win percentage).

    p1's win percentage can be summed from these scenarios - his 9.375% chance when p3 survives, and his win percentage when p3 is killed; sparing the details (you can find a good explanation here), p1 ends up with a win percentage of approximately 23.7%.

    Simple math (100 - 23.7 - 28.125) leaves p2 with an approximately 48.2% chance of winning. If you average near these percentages (once you rewrite the body of your code to work properly) then you should be golden.

  8. The Following User Says Thank You to DuncanS For This Useful Post:

    GregBrannon (August 1st, 2014)

  9. #7
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Really important assignment need checking please :)

    Quote Originally Posted by Ada Lovelace View Post
    Using random number generation is always going to give you
    well, random numbers. Unless you hard code the seed of the
    number generator to have say a 50% chance of either result,
    it's going to be hit or miss what numbers you get.

    Wishes Ada xx
    Even more to the point, random numbers on computers are pseudo-random. So what you get is the same series of random values for every seed value. So if you run the same sequence of operations with the same seed, you will always get the same results.

Similar Threads

  1. Important
    By cseknowledge in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 3rd, 2011, 01:43 PM
  2. Blocking format (important)
    By benglish in forum Java Theory & Questions
    Replies: 3
    Last Post: April 11th, 2011, 06:34 PM

Tags for this Thread