Close
Faqja 0 prej 2 FillimFillim 12 FunditFundit
Duke shfaqur rezultatin -9 deri 0 prej 15
  1. #1
    Student
    Anėtarėsuar
    16-04-2004
    Vendndodhja
    USA
    Postime
    35

    Java - Lojė me zare

    Me falni po te jete hapur njehere kjo teme!
    Kam nje detyre te vogel ne Java qe ka te beje me klasat. Duhet te ndertoj vete nje klase te thjeshte. Tani programi eshte nje simulim zarash. Nese e dini lojen Craps. Per logjiken se kam problem po puna eshte te ndertoj klasen.
    p.sh.

    public class Zar {

    public int roll (int roll1, int roll2){
    roll1=Math.random()*6;
    roll2=Math.random()*6;
    return roll1;
    return roll2;
    }
    }

    Duhet te ngjaje pak a shume si kjo. Te tregoje dy numra sikur jane hedhur 2 zara. Pastaj keto numra te perdoren ne programin kryesor ku eshte gjithe logjika. Programin duhet ta shkruaj ne Java 1.5 po s'po gjej as ndonje material dhe ai libri qe kam i kalon shume perciptash keto gjerat.
    Nese ndonje ka ndonje ide, le ta hedhe.
    Shendet

  2. #2
    Programues Softueresh Maska e edspace
    Anėtarėsuar
    04-04-2002
    Vendndodhja
    Filadelfia, SHBA
    Postime
    2,565
    Mė poshtė ke njė shėmbull qė i hedh zaret 5 herė dhe printon rezultatin e tyre.
    Pėrmirėsoje vetė kodin sipas rregullave tė Javas se dhe unė nuk e kam pėrdorur shumė.

    Loja.java
    Kodi PHP:
    public class Loja {
        
        private 
    Zare zare1 = new Zare();
        private 
    Zare zare2 = new Zare();
        
        public static 
    void main(String[] argsthrows Exception {
            
    Loja loja = new Loja();
            
    loja.hidhZaret();
            
    loja.hidhZaret();
            
    loja.hidhZaret();
            
    loja.hidhZaret();
            
    loja.hidhZaret();
        }
            
        public 
    void hidhZaret() {
            
    zare1.hidh();
            
    zare2.hidh();
            
    System.out.print(zare1.merr() + ", " zare2.merr() + "\n");
        }
        


    Zare.java
    Kodi PHP:
    public class Zare {
        
        public 
    void hidh() {
            
    numri = (int)(6*Math.random() + 1);  // nga 1 deri ne 6
        
    }
        
        public 
    int merr() {
            return 
    numri;
        }

        private 
    int numri;
        

    Rezultati
    6, 2
    4, 3
    3, 6
    1, 1
    2, 3
    Edi

  3. #3
    Student
    Anėtarėsuar
    16-04-2004
    Vendndodhja
    USA
    Postime
    35
    Faleminderit. Vetem pak modifikime do t'i bej dhe eshte e mbaruar.

  4. #4
    Programues Softueresh Maska e edspace
    Anėtarėsuar
    04-04-2002
    Vendndodhja
    Filadelfia, SHBA
    Postime
    2,565
    Tek main() hiqe "throws exception" se nuk duhet. Po pėrdorja njė funksion qė kishte exceptions por pastaj e fshiva dhe harrova tė ndryshoja main.
    Edi

  5. #5
    Student
    Anėtarėsuar
    16-04-2004
    Vendndodhja
    USA
    Postime
    35
    Pershendetje. Po u bie edhe njehere me qafe. Loja eshte me zara po tani pak me e komplikuar sepse duhet te llogaritesh nje probabilitet. Po e postoj pyetjen sic ma kane dhene. Do me merrte ca kohe ta perktheja ne shqip, keshtu qe po e le ne anglisht:

    WeightedDie.

    Part A. Create a new class named WeightedDie.

    Add two methods to the class Die to form a new
    WeightedDie class.

    The signatures and descriptions of the two new methods
    are given below.

    // Input: an int in the range 1-6 and a double in the
    // range [0-1)
    // Set a probability in the range [0-1) that a roll
    // of the die that results in a value _n_ will trigger
    // a 'do over.' For example, if the rejectProbability
    // of the number 3 is 0.05, then approximately 5% of
    // 3's result in a 'do over.' A 'do over' is second fair
    // roll of the die, resulting in a number in the range
    // 1-6. The rejectProbability may be set separately for
    // each value in the range 1-6 for each Die object.

    void setRejectProbability(int side, double rejectProb)



    // Input: an int in the range 1-6
    // Output: the rejectProbability of the given input
    // value for the given object
    double getRejectProbability(int side)



    Part B. Revise the playGame() method of Craps so that it has the following
    signature:

    public static boolean playGame(WeightedDie a,
    WeightedDie b)

    That is, playGame() is passed as parameters two
    WeightedDie's that it uses to play a game of Craps.
    playGame() returns the outcome of the game: true for a
    user win; false for a user loss.



    A sample output is below.

    100,000 games with fair dice a and b
    Side a rejectProb b rejectProb
    1 0.0 0.0 //kuptohet qe 0.0 jane nen a rejectProb dhe
    2 0.0 0.0 b rejectProb
    3 0.0 0.0
    4 0.0 0.0
    5 0.0 0.0
    6 0.0 0.0

    Won 49174 Lost 50826

    100,000 games with biased dice x and y
    Side x rejectProb y rejectProb
    1 0.0 0.0
    2 0.0 0.0
    3 0.0 0.0
    4 0.0 0.0
    5 0.0 0.0
    6 0.5 0.5

    Won 49079 Lost 50921


    Po mbashkangjis edhe 80% te codit qe e kam bere nje detyre me pare. Po ketij codi duhet t'i behen modifikimet.

    Kodi:
     
    
    public class Die {
        
        // simulate the roll of a fair six-sided die (1-6)
        public int roll(){ 
            return (int)( (Math.random()* 6) + 1);
        }
        
        /* 
         * Simulate the roll of a biased six-sided die (1-6).
         * Reduce probability of a 6 from 10/60 to 9/60, with
         * equal probability of values from 1-5
         */
        public int rollWeighted(){
            // pick a number, 1-6 with equal probability
            int rtnVal = roll();
            
            // but with probability 1/10 reject a 6 for a value 
            // in range 1-5
            if (rtnVal == 6){
                 if (Math.random() < 0.1){
                     rtnVal = (int)((Math.random()* 5) + 1);
                }
            }
            return rtnVal;
        }
    }
    Kodi:
    import java.util.*;
    //import java.io.*;
    
    public class Craps {
    
        public static void main(String[] args){
            final int MAX_GAMES = 25;
    
            boolean[] wonLostRecord = new boolean[MAX_GAMES]; // t = won; f = lost
            int game = 0;    // count the games played, beginning with 0
            Scanner sc = new Scanner(System.in);
            
            // play up to MAX_GAMES rounds of Craps, at user discretion
            for (String answer = "y"; answer.equals("y") && game < MAX_GAMES; game++){
                wonLostRecord[game] = playGame();    // play game and store the outcome
                System.out.print("Play again (y/n)? "); 
                answer = sc.next();
                answer = answer.substring(0,1).toLowerCase();
                System.out.println();
            }
            
            // print out the won/lost record
            System.out.println("Won/Lost Record:");
            for (int i = 0; i < game; ++i){
                if (wonLostRecord[i]){
                    System.out.print("W");
                }
                else {
                    System.out.print("L");
                }
            }
        }
        
        public static boolean playGame() {
            Die a = new Die();
            Die b = new Die();
            int c= b.rollWeighted();
    // first roll of the dice
            int rollA = a.roll();
            int rollB = b.roll();
            int rollAB = rollA + rollB;
            System.out.println("You rolled " + rollA + " + " + rollB + " = " + rollAB);
            System.out.println(c);
            // first roll -- game over on 2,3,7,11,12
            switch(rollAB){
                case 2:
                case 3:
                case 12: {
                    System.out.println("Craps. You lose!");
                    System.out.println();
                    return false;
                }
                case 7:
                case 11:{
                    System.out.println("Natural. You win!");
                    return true;
                }
                
                // first roll cases 4,5,6,8,9,10 -- keep rollin' til point or 7
                default:{
                    int point = rollAB;
                    System.out.println("Your point is " + point);
                    // second and subsequent rolls of the dice
                    do {
                        rollA = a.roll();
                        rollB = b.roll();
                        rollAB = rollA + rollB;
                        System.out.println("You rolled " + rollA + " + " + rollB + " = " + rollAB);
                        
                        if (rollAB == point){
                            System.out.println("Got your point. You win!");
                            System.out.println();
                            return true;
                        }
                        else if (rollAB == 7){
                            System.out.println("You lose.");
                            System.out.println();
                            return false;
                        }
                    } while (true);
                }
            }
        }
    }
    Gjithashtu na thane qe si udhezim mund te jete public int rollWeighted() tek klasa Die.
    Po te kete mundesi ndonje udhezim se nuk e kam idene cfare kerkon dhe per te na e shpjeguar harruan fare ne shkolle.
    Flm
    Ndryshuar pėr herė tė fundit nga Nocturnal : 20-04-2005 mė 21:29

  6. #6
    Programues Softueresh Maska e edspace
    Anėtarėsuar
    04-04-2002
    Vendndodhja
    Filadelfia, SHBA
    Postime
    2,565
    Me aq sa kuptova, profesori tė kėrkon qė probabiliteti i numrave 1-6 tė zareve tė mos jetė i barabartė por tė mund tė kontrollohet duke caktuar probabilitetin e pėrjashtimit (rejectProbability). Pra, zar me hile.

    Psh: E zėmė se kemi njė zare pa hile dhe njė zare me hile. Probabiliteti qė tė bier numri 1 nė zaren pa hile ėshtė 1/6 ose 0.167. Pėr zaren me hile vendosim qė probaliteti i pėrjashtimit ėshtė 1.0 ose 100%.

    Do hedhim nė fillim zarin me hile. Nėse bie 1, e pėrjashtojmė atė zare dhe hedhim zarin pa hile. Sa ėshtė probabiliteti qė rezultati ėshtė 1?

    Probaliteti(rezultati=1) = Probabilitetin(Zari1 = 1) * Probabliteti(Zari2 = 1)
    Probaliteti(rezultati=1) = (1/6) * (1/6)
    Probaliteti(rezultati=1) = 1/36 = 0.028

    Kjo ishte kėrkesa. Mė poshtė gjen edhe programin e modifikuar qė tė lejon tė caktosh probalitetin e pėrjashtimit pėr ēdo numėr tė zares. Krijojmė njė matricė me 6 elementė pėr tė ruajtur probabilitetin e pėrjashtimit dhe si fillim i caktojmė probalitetin 0, dmth nuk pėrjashtojmė asnjė numėr.

    Pastaj duke pėrdorur metodėn setRejectProbabality(N, P), caktojmė probalitetin e pėrjashtimit P pėr numrin N tė zarit. Duke vazhduar me shembullin mė lart, pėr tė pėrjashtuar numrin 1, thėrrasim metodėn setRejectProbability(1, 1.0). Pėr tė pėrjashtuar vetėm 50% tė hedhjeve tė numrit 5: setRejectProbability(5, 0.5).

    Kur hedhim zaren, marrim njė numėr tė rastėsishėm nga kompjuteri (math.random()) dhe nqs ky numėr ėshtė mė i vogėl ose i barabartė me probabilitetin qė kemi nė matricė, atėherė hedhim zaren pa hile.

    Ja kodi nė Java qė bėn kėto qė shpjegova mė lart:

    Kodi PHP:
    public class WeightedDie {
        
        private 
    double[] rejectProb = {0.00.00.00.00.00.0};

        
    // simulate the roll of a fair six-sided die (1-6)
        
    public int rollFair(){ 
            return (int)( (
    Math.random()* 6) + 1);
        }
        
        
    /* 
         * Simulate the roll of a biased six-sided die (1-6).
         * Reduce probability of a 6 from 10/60 to 9/60, with
         * equal probability of values from 1-5
         */
        
    public int rollWeighted(){
            
    // pick a number, 1-6 with equal probability
            
    int rtnVal rollFair();
            
            
    // but with probability 1/10 reject a 6 for a value 
            // in range 1-5

            
    if (Math.random() <= this.rejectProb[rtnVal 1]){
                 
    rtnVal rollFair();
            }

            return 
    rtnVal;
        }

        
    // Input: an int in the range 1-6 and a double in the
        // range [0-1)

        
    void setRejectProbability(int sidedouble rejectProb){
            if( 
    rejectProb >= 0.0f && rejectProb <= 1.0f )
                
    this.rejectProb[side 1] = rejectProb;
        }

        
    // Input: an int in the range 1-6
        // Output: the rejectProbability of the given input
        // value for the given object
        
    double getRejectProbability(int side){
            return 
    this.rejectProb[side 1];
        }



    Pėr tė parė nėse zari me hile punon siē duhet, e hedhim atė 100.000 herė, nė fillim me probabilitetin e pėrjashtimeve (0, 0, 0, 0, 0, 0) dhe pastaj me probabilitetin e pėrjashtimeve (1.0, 0.7, 0.4, 0, 0, 0). Dmth pėrjashtojmė 100% tė njėshave, 70% tė dyshave dhe 40% tė treshave.

    Ja kodi nė Java qė e bėn kėtė gjė.

    Kodi PHP:
    public class Prova {

        public static 
    void main(String[] args){
            
            
    WeightedDie zare = new WeightedDie();
            
    int[] statistikat = {000000};

            
    System.out.println("Zare te paanshme (100.000 hedhje)");

            for (
    int i 0100000; ++i){   // hedhim zaret
                
    statistikat[zare.rollWeighted() - 1]++;  // ruajme numrin
            
    }

            for (
    int i 06; ++i){  // nxjerrim probabilitetin
                
    System.out.println("P(" + (1) + ") = " + (statistikat[i] / 100000.0f) );
            }

            for (
    int i 06; ++i){  // shuajme statistikat e zareve te paanshme
                
    statistikat[i] = 0;
            }

            
    System.out.println("Zare te anshme (100.000 hedhje)");
            
    zare.setRejectProbability(11.0); // ndryshojme probalitetin e perjashtimeve
            
    zare.setRejectProbability(20.7); // per 1, 2, 3
            
    zare.setRejectProbability(30.4);

            for (
    int i 0100000; ++i){ // hedhim zaret
                
    statistikat[zare.rollWeighted() - 1]++;
            }

            for (
    int i 06; ++i){ // nxjerrim probabilitetin
                
    System.out.println("P(" + (1) + ") = " + (statistikat[i] / 100000.0f) );
            }
        }


    Rezultati nė ekran:

    Kodi:
    C:\>java Prova
    Zare pa hile (100.000 hedhje)
    P(1) = 0.16753
    P(2) = 0.16355
    P(3) = 0.16756
    P(4) = 0.16679
    P(5) = 0.16682
    P(6) = 0.16775
    Zare me hile (100.000 hedhje)
    P(1) = 0.05874
    P(2) = 0.10703
    P(3) = 0.15818
    P(4) = 0.22656
    P(5) = 0.22412
    P(6) = 0.22537
    Rezultatet tregojnė se probabiliteti i pėrjashtimit ka patur efekt nė probabilitetin e 1, 2, 3.

    P(rezultati=1) = P(1 perjashtuar)*P(zari1=1)*P(zari2=1) + P(2 perjashtuar)*P(zari1=2)*P(zari2=1) + P(perjashtuar 3)*P(zari1=3)*P(zari2=1)
    P(rezultati=1) = (1)*(1/6)*(1/6) + (0.7)*(1/6)*(1/6) + (0.4)*(1/6)*(1/6)
    P(rezultati=1) = (1/36)*(1 + 0.7 + 0.4)
    P(rezultati=1) = (0.278)*(2.1)
    P(rezultati=1) = 0.5838

    0.5838 ~= 0.5874

    Pra, programi punon siē duhet. Nė tė njėjtėn mėnyrė mund tė llogaritėsh edhe P(2), P(3), P(...), P(6).

    Kodin mė lart e gjen edhe nė skedarėt e bashkėngjitur mė poshtė.
    Skedarėt e Bashkėngjitur Skedarėt e Bashkėngjitur
    Ndryshuar pėr herė tė fundit nga edspace : 21-04-2005 mė 19:50
    Edi

  7. #7
    Student
    Anėtarėsuar
    16-04-2004
    Vendndodhja
    USA
    Postime
    35
    Shume flm per kohen Ed.
    Edhe mua po me merr shume kohe ta kuptoj si problem prandaj edhe po has veshtiresi. Ne ushtrim jepet nje shembull si output (postuar me pare). Dmth per fair die,per hedhjet pa hile, ne duhet te marrim prob. 0.0 sepse nuk ka reject probability eshte gjithmone fair ose nuk ka 'do over', e vetmja gje eshte numerimi qe ne duhet te bejme i won dhe lost ne 100000 loja. Ndersa Kur hasim reject Prob. atehere duhet te bejme roll again dhe kjo do ta ndryshoje numrin e won dhe lost. Outputi e tregon me mire. Te pakten keshtu e kuptoj une. Nese e kupton me mire nga output atehere po pati mundesi hidh ndonje ide.
    Flm

  8. #8
    Programues Softueresh Maska e edspace
    Anėtarėsuar
    04-04-2002
    Vendndodhja
    Filadelfia, SHBA
    Postime
    2,565
    Mė poshtė ke kodin pėr pjesėn B tė detyrės.

    Supozohet se PlayGame() jep rezultatin e saktė pėr fitoret dhe humbjet. Unė nuk i di rregullat e lojės, prandaj shikoje nqs punon siē duhet.

    Pėrdora tė njėjtat probabilitete qė ka pėrdorur edhe profesori yt nė shembullin e dhėnė dhe rezultatet janė tė pėrafėrta. Nuk besoj tė kesh paqartėsira por nqs ke, kėtu e kemi forumin.

    Herėn tjetėr shkruaj kėrkesėn nė anglisht por pėrktheje edhe nė shqip. Nuk ėshtė e thėnė ta pėrkthesh fjalė pėr fjalė, por njė pėrshkrim tė pėrgjithshėm qė ta kuptojnė edhe ata qė nuk dinė anglisht.

    Kodi PHP:
    // Craps.java

    public class Craps {

        public static 
    void main(String[] args){

            final 
    int NUM_LOJRAVE 100000;
            
    int fitore 0;
            
    int humbje 0;

            
    WeightedDie zareA = new WeightedDie(); // pa hile
            
    WeightedDie zareB = new WeightedDie(); // pa hile
            
            // =================== Loja me zare pa hile =========================

            
    System.out.println("100.000 lojra me zaret pa hile A dhe B");
            
    System.out.println("Numri\tZari A\tZari B");

            for (
    int i 1<= 6i++)
                
    System.out.println("\t" zareA.getRejectProbability(i
                        + 
    "\t" zareB.getRejectProbability(i));

            for (
    int i 0NUM_LOJRAVEi++)
                if (
    playGame(zareAzareB))
                    
    fitore++;
                else
                    
    humbje++;
            
            
    System.out.println("Fitore: " fitore "\tHumbje: " humbje);
            
    System.out.println();


            
    // =================== Loja me zare me hile =========================

            
    System.out.println("100.000 lojra me zaret me hile X dhe Y");
            
    System.out.println("Numri\tZari X\tZari Y");

            
    // ndryshojme probabilitetin e perjashtimit per numrin 6 
            
    zareA.setRejectProbability(60.5);     // ne zarin A
            
    zareB.setRejectProbability(60.5);     // ne zarin B

            
    fitore 0;
            
    humbje 0;

            for (
    int i 1<= i++)
                
    System.out.println("\t" zareA.getRejectProbability(i
                        + 
    "\t" zareB.getRejectProbability(i));

            for (
    int i 0NUM_LOJRAVEi++)
                if (
    playGame(zareAzareB))
                    
    fitore++;
                else
                    
    humbje++;
            
            
    System.out.println("Fitore: " fitore "\tHumbje: " humbje);
            
    System.out.println();

        }
        
        public static 
    boolean playGame(WeightedDie aWeightedDie b) {
            
    int cb.rollWeighted();
            
    // first roll of the dice
            
    int rollA a.rollWeighted();
            
    int rollB b.rollWeighted();
            
    int rollAB rollA rollB;

            
    // first roll -- game over on 2,3,7,11,12
            
    switch(rollAB){
                case 
    2:
                case 
    3:
                case 
    12:
                    return 
    false;
                case 
    7:
                case 
    11:
                    return 
    true;

                
    // first roll cases 4,5,6,8,9,10 -- keep rollin' til point or 7
                
    default:{
                    
    int point rollAB;

                    
    // second and subsequent rolls of the dice
                    
    do {
                        
    rollA a.rollWeighted();
                        
    rollB b.rollWeighted();
                        
    rollAB rollA rollB;
                        
                        if (
    rollAB == point)
                            return 
    true;
                        else if (
    rollAB == 7)
                            return 
    false;

                    } while (
    true);
                }
            }
        }

    Ja dhe rezultati nė ekran:

    Kodi:
    C:\>java Craps
    100.000 lojra me zaret pa hile A dhe B
    Numri   Zari A  Zari B
    1       0.0     0.0
    2       0.0     0.0
    3       0.0     0.0
    4       0.0     0.0
    5       0.0     0.0
    6       0.0     0.0
    Fitore: 49423   Humbje: 50577
    
    100.000 lojra me zaret me hile X dhe Y
    Numri   Zari X  Zari Y
    1       0.0     0.0
    2       0.0     0.0
    3       0.0     0.0
    4       0.0     0.0
    5       0.0     0.0
    6       0.5     0.5
    Fitore: 49053   Humbje: 50947

    Nė skedarin e bashkėngjitur gjen kodin pėr pjesėn e parė (WeightedDie) dhe pėr pjesėn e dytė (Craps).
    Skedarėt e Bashkėngjitur Skedarėt e Bashkėngjitur
    Edi

  9. #9
    Student
    Anėtarėsuar
    16-04-2004
    Vendndodhja
    USA
    Postime
    35
    Edspace, ti duhet te marresh urdherin Naim Frasheri i klasit te pare nga forumi Me bere shume pune. C'eshte e verteta programi duhet te ishte me i thelle po ai profesori na e pranoi. Keshtu qe flm shume. Per sa i perket te shkrojtures anglisht s'eshte problem do mundohem t'i sqaroj ne shqip ato problemat. Tani ne fakt edhe nje detyre kam dhe mbaron puna. Po se bera dot do hedh nje anonime ketej
    Shendet dhe $

  10. #10
    Student
    Anėtarėsuar
    16-04-2004
    Vendndodhja
    USA
    Postime
    35
    Nje gje e vogel fare:

    Tek programi i mesiperm (dmth ai qe ka shkruar Edspace) duhet te vendos nje
    int games=stdin.nextInt();
    Try{

    }
    Catch(Exceptions e)
    System.err.println("Numer shume i madh!");
    System.exit(0);
    }

    Dmth kur perdoruesi te jape nje numer shume te madh (sa loja do te luaje dmth games do jete ai numri), programi te nxjerre : Numer shume i madh!
    Problemi eshte cfare duhet te vendos tek try block? int games=stdin.nextInt();
    se vendos dot se variabla games nuk njihet nga programi me poshte!
    Shendet

Faqja 0 prej 2 FillimFillim 12 FunditFundit

Regullat e Postimit

  • Ju nuk mund tė hapni tema tė reja.
  • Ju nuk mund tė postoni nė tema.
  • Ju nuk mund tė bashkėngjitni skedarė.
  • Ju nuk mund tė ndryshoni postimet tuaja.
  •