Prijavi se z GoogleID

» »

[java] cho han težave

[java] cho han težave

kretze ::

dober dan...obrnil sem se na ta forum ker imam težave...kot je razvidno od zgoraj delam "igro" choHan..in tudi sem jo naredil compalier ne javi nobene napake ampak program ne deluje pravilno..napka je ta da mi ne povečuje točko(points v kodi ker je napisana v angleščini in verjetno imam nekaj slovničnih napak noter ampak to nima veze)....vlgavnem imam 2 kocki če je recimo na obeh kockah vrednost 3 je to skupaj 6 se pravi to je cho(ali soda) ampak točk mi ne sešteva sploh in na koncu je vedno enako rezultat imam 2 playerja(igralca) in na koncu je vedno rezulta 0:0..mi lahko kdo pove kje imam napako? oz kaj je narobe..koda je malo daljša zato bom probal spodaj nekako spacat da bo vse skup kar se da pregledno;

class DIe:
public class DIe {
import java.util.Random;
    private int value;
    private int sides;
    
    public DIe(int numsides)
    {
        sides=numsides;
        roll();
    }
    
    public void roll()
    {
        Random r=new Random();
        value=r.nextInt(sides)+1;
        
        
    }
    
    public int getValue()
    {
        return value;
    }
    public int getSides()
    {
        return sides;
    }
}


class dealer:
public class dealer {
    private int die1Value;
    private int die2Value;
    
    public dealer()
    {
        die1Value=0;
        die2Value=0;
    }
    public void RollDice()
    {
        int sides=6;
        DIe die1=new DIe(sides);
        DIe die2=new DIe(sides);
        
        die1Value=die1.getValue();
        die2Value=die2.getValue();
    }
    
    public String ChoHan()
    {
        int sum=die1Value+die2Value;
        String result;
        if(sum%2==0)
            result=" cho(even)";
        else
            result=" han(odd)";
        
        return result;
        
    }
    
    public int getDie1val()
    {
        return die1Value;
    }
    public int getDie2Val()
    {
        return die2Value;
    }
}



class player:
import java.util.Random;


public class player {
   private String name;
    private String guess;
    private int points;
    
    public player(String n)
    {
        name=n;
        guess="";
        points=0;
    }
    
    public void makeGuess()
    {
        Random r=new Random();
        int guessNumber=r.nextInt(2);
        
        if(guessNumber==0)
            guess=" cho(even) ";
        else
            guess=" han(odd) ";
        
    }
    public void addPoints(int newPoints)
    {
        points+=newPoints;
    }
    
    public String GetName()
    {
        return name;
    }
    public String getGuess()
    {
        return guess;
    }
    public int getPoint()
    {
        return points;
    }
    
}


glavni program z tremi funkcijami:

import java.util.Scanner;

public class JavaP25Chohanremeake {

 
    public static void main(String[] args) {
        int max=5;
        String p1name;
        String p2name;
        Scanner input=new Scanner(System.in);
        
        System.out.print("enter name for the first player: ");
        p1name=input.nextLine();
        System.out.print("enter name for the second player; ");
        p2name=input.nextLine();
        
        dealer d=new dealer();
        player p1=new player(p1name);
        player p2=new player(p2name);
        for(int i=0;i<max;i++)
        {
            System.out.println("----------------");
            System.out.println("now playing "+(i+1) +" round");
            d.RollDice();
            p1.makeGuess();
            p2.makeGuess();
            RoundResult(d,p1,p2);
           
            
        }
        
         display(p1,p2);
    }
    
    public static void CheckGuess(player p,dealer d)
    {
       final int PTA = 1; 
      String guess = p.getGuess();
      String choHanResult = d.ChoHan(); 
      
      
      System.out.printf("The player %s guessed %s.\n", 
                        p.GetName(), p.getGuess());
      
      
      if (guess.equalsIgnoreCase(choHanResult))
      {
         p.addPoints(PTA);
         System.out.printf("Awarding %d point(s) to %s.\n",
                           PTA, p.GetName());
      }
   }
    
    public static void RoundResult(dealer d,player p1,player p2)
    {
        System.out.println("the dealer rolled "+d.getDie1val()+d.getDie2Val());
        System.out.println("result: "+d.ChoHan());
        
        CheckGuess(p1,d);
        CheckGuess(p2,d);
        
    }
    
    public static void display(player p1,player p2)
    {
        System.out.print("------------------------");
        System.out.print("GAME OVER here are the result : ");
        System.out.println("points\n"+p1.GetName()+p1.getPoint());
        System.out.println("points\n"+p2.GetName()+p2.getPoint());
        
        if(p1.getPoint()>p2.getPoint())
            System.out.println(p1.GetName()+" is the grand winner!!");
        else if(p2.getPoint()>p1.getPoint())
            System.out.println(p2.GetName()+" is the gran winner!!!");
        else
            System.out.println("score are tied");
        
        
    }
}


HotBurek ::

Men zadeva dela. Normalno prikazuje cifre (points).



Kodo sem skopiral od tu. Ker sem dal vse v en fajl, sem moral dodat static class-om. To je to.

Pa spremenil sem output, kako izpisuje te pointse. Ker mi prvno "ni delalo". Sem vnesel uporabnika kot user1, pa uporabnik je dobil recimo 2 point-a. Na izpisu je potem bilo user12 (koda: p2.GetName()+p2.getPoint()). Me je zmedlo. Mogoče je to?

package com.company;

import java.util.Scanner;
import java.util.Random;

public class Main {

    public static class DIe {

        private int value;
        private int sides;

        public DIe(int numsides)
        {
            sides=numsides;
            roll();
        }

        public void roll()
        {
            Random r=new Random();
            value=r.nextInt(sides)+1;
        }

        public int getValue()
        {
            return value;
        }
        public int getSides()
        {
            return sides;
        }
    }

    public static class dealer {
        private int die1Value;
        private int die2Value;

        public dealer()
        {
            die1Value=0;
            die2Value=0;
        }
        public void RollDice()
        {
            int sides=6;
            DIe die1=new DIe(sides);
            DIe die2=new DIe(sides);

            die1Value=die1.getValue();
            die2Value=die2.getValue();
        }

        public String ChoHan()
        {
            int sum=die1Value+die2Value;
            String result;
            if(sum%2==0)
                result="cho(even)";
            else
                result="han(odd)";

            return result;

        }

        public int getDie1val()
        {
            return die1Value;
        }
        public int getDie2Val()
        {
            return die2Value;
        }
    }

    public static class player {
        private String name;
        private String guess;
        private int points;

        public player(String n)
        {
            name=n;
            guess="";
            points=0;
        }

        public void makeGuess()
        {
            Random r=new Random();
            int guessNumber=r.nextInt(2);

            if(guessNumber==0)
                guess="cho(even)";
            else
                guess="han(odd)";

        }
        public void addPoints(int newPoints)
        {
            points+=newPoints;
        }

        public String GetName()
        {
            return name;
        }
        public String getGuess()
        {
            return guess;
        }
        public int getPoint()
        {
            return points;
        }

    }

    public static void main(String[] args) {

        int max=5;
        String p1name;
        String p2name;
        Scanner input=new Scanner(System.in);

        System.out.print("enter name for the first player: ");
        p1name=input.nextLine();
        System.out.print("enter name for the second player: ");
        p2name=input.nextLine();

        dealer d = new dealer();
        player p1 = new player(p1name);
        player p2 = new player(p2name);
        for(int i=0;i<max;i++)
        {
            System.out.println("----------------");
            System.out.println("now playing "+(i+1) +" round");
            d.RollDice();
            p1.makeGuess();
            p2.makeGuess();
            RoundResult(d,p1,p2);


        }
        display(p1,p2);
    }

    public static void CheckGuess(player p,dealer d)
    {
        final int PTA = 1;
        String guess = p.getGuess();
        String choHanResult = d.ChoHan();

        System.out.printf("The player %s guessed %s.\n",  p.GetName(), p.getGuess());

        System.out.printf("\tguess=" + String.valueOf(guess) + "\n");
        System.out.printf("\tchoHanResult=" + String.valueOf(choHanResult) + "\n");

        if (guess.equalsIgnoreCase(choHanResult))
        {
            p.addPoints(PTA);
            System.out.printf("Awarding %d point(s) to %s.\n",  PTA, p.GetName());
        }
    }

    public static void RoundResult(dealer d,player p1,player p2)
    {
        System.out.println("the dealer rolled "+d.getDie1val()+d.getDie2Val());
        System.out.println("result: "+d.ChoHan());

        CheckGuess(p1,d);
        CheckGuess(p2,d);

    }

    public static void display(player p1,player p2)
    {
        System.out.print("------------------------\n");
        System.out.print("GAME OVER here are the result:\n");
        System.out.println(p1.GetName() + "\n" + " Points: ->" + p1.getPoint() + "<-");
        System.out.println(p2.GetName() + "\n" + " Points: ->" + p2.getPoint() + "<-");

        if(p1.getPoint()>p2.getPoint())
            System.out.println(p1.GetName()+" is the grand winner!!");
        else if(p2.getPoint()>p1.getPoint())
            System.out.println(p2.GetName()+" is the gran winner!!!");
        else
            System.out.println("score are tied");
    }
}
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window

kretze ::

sedaj mi pa več res ni ni nič jasno