Jump to content

Java uzdevums ar cikliem


Recommended Posts

Sveiki bootisti!

Mācos java no grāmatas Addison Wesley Absolute Java 5th Edition 2012. Katras nodaļas beigās ir uzdevumi, taču atbildes pieejamas internetā, par cik grāmata ir nokačāta, nav pieejas šiem resursiem. Vēlos lūgt padomus kā uzlabot algoritmu šim uzdevumam un jautāt vai vispār mans risinājums ir pareizs -

 

In the game of craps, a pass line bet proceeds as follows: Two six-sided dice are

rolled; the first roll of the dice in a craps round is called the “come out roll.”

A come out roll of 7 or 11 automatically wins, and a come out roll of 2, 3, or 12

automatically loses. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number

becomes “the point.” The player keeps rolling the dice until either 7 or the point is

rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first,

then the player loses.

Write a program that simulates a game of craps using these rules without human

input. Instead of asking for a wager, the program should calculate whether the

player would win or lose. The program should simulate rolling the two dice and

calculate the sum. Add a loop so that the program plays 10,000 games. Add

c ounters that count how many times the player wins, and how many times the

player loses. At the end of the 10,000 games, compute the probability of winning

[i.e.,Wins / (Wins + Losses)] and output this value. Over the long run, who

is going to win the most games, you or the house?

 

Mans risinājums

import java.util.Random;
public class Project2 {


	public static void main(String[] args) {
		int dice1,dice2,diceSum,count,win=0,loss=0,pointCounter=0,point;
		double probability;
		Random Randomgenerator=new Random();
		
		
		for (count=0;count<10000;count++)
		{
			dice1=Randomgenerator.nextInt(6)+1;
			dice2=Randomgenerator.nextInt(6)+1;
			diceSum=dice1+dice2;
			//System.out.println("dice:"+diceSum);
			if (diceSum==7 || diceSum==11)
				win++;
			else if (diceSum==2 || diceSum==3 || diceSum==12)
				loss++;
			else
			{
				point=diceSum;
				pointCounter++;
				while (diceSum!=7||diceSum!=point)
				{
					dice1=Randomgenerator.nextInt(6)+1;
					dice2=Randomgenerator.nextInt(6)+1;
					diceSum=dice1+dice2;
					//System.out.println("dice point:"+diceSum);
					if (point==diceSum)
					{
						win++;
						break;
					}
					else if (diceSum==7)
					{
						loss++;
						break;
					}
					else;
					
				}
			}	
		}
		probability=(win/(double)(count))*100;
		System.out.println("Wins:"+win);
		System.out.println("Losses:"+loss);
		System.out.println("Point:"+pointCounter);
		System.out.println("Probability:"+probability+"%");
	}

}

Izvade-

 

Wins:4917

Losses:5083

Point:6657

Probability:49.17%

 

Link to post
Share on other sites

korektumā neiedziļinājos, bet daudz smukāk būtu pārbaudi iznest atsevišķā funkcijā.

 

 

 if ( checkTurn( dice1, dice2 ) ){ 
   win++; 
 }  else {
   loss++;
 } 

 

boolean checkTurn(int myDice, int yourDice){
   boolean iWon = true;  // būsim optimisti 
   .. 
   .. 
   return iWon;
} 
Link to post
Share on other sites

Boolean'u es nevaru izmatot, jo ārējā ir 3 pārbaudes - ja uzkrīt 7 vai 11; ja uzkrīt 2,3 vai 12; un visi parējie.

Savukārt iekšējā arī ir 3 pārbaudes - ja uzkrīt 7, ja uzkrīt "point" vai uzkrīt jebkurš cits.

Vai tad ne tā?

Edit: Kāpēc tauta tik neatsaucīga?

Edited by eddyboi
Link to post
Share on other sites

usver ieteikumam nepievienojos (naf slēpt tik elementāru loģiku?), bet īstenot to varētu ar pārslogotām (overloaded) metodēm. Tas ir, metodēm ar vienādu nosaukumu, bet atšķirīgu parametru listi. Piemēram,

boolean resolveRoll(int a, int b)
{
    return (a == 7 || b == 11);
}

boolean resolveRoll(int a, int b, int c)
{
    return (a == 2 || b == 3 || c == 12);
}

 

Kodā kļūdas nesaskatīju, un rezultāts arī atbilst gaidītajam. Vienīgais, ko pamanītu ir,

while (diceSum!=7||diceSum!=point)
{
    /* šo... */
}

while (true)
{
    /* uz šo */
}

jo šī pārbaude ir bezjēdzīga, ja visa loģika jau ir iekš while bloka. Tas ļenganais else, while bloka beigās, arī naf nav vajadzīgs.

Link to post
Share on other sites

Nekad.

 

while bloks tiks izpildīts, kamēr nebūs patiess viens no diviem nosacījumiem, kas noved pie break izpildes, kas pārtrauc while, for un do-while ciklu, kura iekšienē tas atrodas. Tātad, kauliņi tiks mesti, kamēr to summa nebūs vienāda ar 7, un tiks ieskaitīts zaudējums, vai to summa būs vienāda ar mērķa punktu sakaitu, un tiks ieskaitīta uzvara.

 

Šādi bezgalīgi while cikli, no kuriem izlaužas ar break palīdzību, kurš izpildās pie nosacījumiem, kas ir pārāk sarežģīti, lai tos sakaidri aprakstītu iekš while galvenes, ir klasiska un bieži sastopama programēšanas idioma.

Edited by valka
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...