Dziwne zachowanie if'a. Nic nie robi pomimo spełniania warunku.

0

Witam.
Tworzę "grę" kościanego pokera. Mam dwóch graczy każdy dostaje 5 kości ( każda gość wartość 1-6 ). I mam problem przy ustelniu tego kto wygrał.


public Winner (Player ... player){
        System.out.println("Player 1 " + player[0].getResult());
         System.out.println("Player 2 " + player[1].getResult());
        if ((player[0].getResult()) < (player[1].getResult())){
            System.out.println("Wygral 1");
        }
        else if ((player[0].getResult()) > (player[1].getResult())) {
            System.out.println ("Wygral 2");
        }
        else if ((player[0].getResult()) == (player[1].getResult())){
            equalRanks(player[0].getResult());
        }
        
    }

mam wydruk rezultatu jaki uzyskał każdy z graczy.

Dice for player number : 1
[1, 3, 5, 5, 5]
Dice for player number : 2
[1, 3, 3, 4, 4]
Player 1 5
Player 2 6

player 1 ma wartość 5 ( czyli powinien wygrać, a mimo to nie ma wydruku, że wygrał ) [kod wyżej ]
Już nawet nie chodzi o to, że źle wydrukował, tylko o to, że w ogóle nie drukuje. Ale spełniony jest 1szy warunek w ifie i powinien się pojawić napis "Wygral 1", ale jednak to sie nie pojawia.

Cały program :

PokerKosci.java

package pokerkosci;

public class PokerKosci {
 // 1 - poker
    // 2- czworka
    //3 - full itp..
    public static void main(String[] args) {
        Player player1 = new Player();
        player1.getDice();
       // player1.getResult();
        
        Player player2 = new Player();
        player2.getDice();
        //player2.getResult();
        
        Winner who = new Winner(player1, player2);
    }
    
}

package pokerkosci;

Player.java

import java.util.Arrays;
import java.util.Random;

public class Player extends Result{
    private int[] dice = new int[5];
    Random gene = new Random();
    private static int nrPlayer =0;
    
    private int playerResult;
    public Player(){
        for (int i=0; i < 5; i++){
           dice[i] = 1 + gene.nextInt(6);
        //System.out.println("Number dice " + i + " : " + dice[i]); 
        }
        nrPlayer++;
            
        Arrays.sort(dice);
        
        for (int i=0;i <5 ;i++)
            ++appearance[dice[i]];
    }
    
    public void getDice (){
        System.out.println("Dice for player number : " + nrPlayer + "\n" + Arrays.toString(dice));
    }
    public int getResult(){
        return setResult(appearance);
    }

   /* public int getAppearance(int i){
        return result.appearance[i];
    }*/
    
Result.java

package pokerkosci;

import java.util.Arrays;

public class Result{
    public int[] appearance = new int[7];
    private int counterThree =0;
    private int counterTwo =0;
    private int counterOne = 0;
    private int result =0;
    public Result(){
        
            
    }
    
    /*1 - poker
    2 - fourOfKind
    3 - fullHouse
    4 - Straight
    5 - threeOfKind
    6- twoPairs
    7 - pair
    8 - high
    */
    public int setResult(int appearance[]){
        for ( int i =1; i < 7;i++){
            //System.out.println("Number " + i + " " + appearance[i]);
            if (appearance[i] == 5) result = 1;
            else if (appearance[i] == 4) result =2;
            else if (appearance[i] == 3) counterThree++;
            else if (appearance[i] == 2) counterTwo++;
            else if (appearance[i] == 1) counterOne++;
        }
        if (result !=0) return result;
        else if (counterThree == 1 && counterTwo == 1 ) return 3;
        else if (counterThree == 1 && counterTwo == 0 ) return 5;
        else if (counterTwo == 2) return 6;
        else if (counterTwo == 1) return 7;
        
        else if (appearance[1] == 0 || appearance [6] == 0) return 4;
        
        
        return 8;
    } 
    
}


}


Winner.java

package pokerkosci;

public class Winner {
    private boolean[] first = new boolean[9];
    private boolean[] second = new boolean[9];
    
    public Winner (Player ... player){
        System.out.println("Player 1 " + player[0].getResult());
         System.out.println("Player 2 " + player[1].getResult());
        if ((player[0].getResult()) < (player[1].getResult())){
            System.out.println("Wygral 1");
        }
        else if ((player[0].getResult()) > (player[1].getResult())) {
            System.out.println ("Wygral 2");
        }
        else if ((player[0].getResult()) == (player[1].getResult())){
            equalRanks(player[0].getResult());
        }
        
    }
    
    public void equalRanks(int result, Player ... player){
       
       int i=6;
        if (result == 1){
            while (!first[result] && !second[result]){
               if(player[0].appearance[i] !=0 ) first[result]=true;
               if(player[1].appearance[i] !=0 ) second[result]=true;
               i--;
            }
            i =6;
        }
        
        else if (result == 2){
            while (!second[result] && !first[result]){
               if(player[0].appearance[i] == 4 ) first[result]=true;
               if(player[1].appearance[i] == 4 ) second[result]=true;
               i--;
            }
            i=6;
        }
        else if (result == 3){
            while (!second[result] && !first[result]){
               if(player[0].appearance[i] == 3 ) first[result]=true;
               if(player[1].appearance[i] == 3 ) second[result]=true;
               i--;
            }
             i=6;
            if (first[result]==true && second[result]==true){
                first[result]=false;
                second[result]=false;
                while (!second[result] && first[result]){
               if(player[0].appearance[i] == 2 ) first[result]=true;
               if(player[1].appearance[i] == 2 ) second[result]=true;
               i--;
            }
            }
            i=6;
        }
        else if (result == 4){
            i = 6;
            while (!second[result] && first[result]){
               if(player[0].appearance[i] == 1 ) first[result]=true;
               if(player[1].appearance[i] == 1 ) second[result]=true;
               i--;
            }
            i=0;
        } 
         else if (result == 5){
            while (!second[result] && !first[result]){
               if(player[0].appearance[i] == 3 ) first[result]=true;
               if(player[1].appearance[i] == 3 ) second[result]=true;
               i++;
            }
            i=0;
        }
        else if (result == 6){
            i=6;
            while (!second[result] && !first[result]){
               if(player[0].appearance[i] == 2 ) first[result]=true;
               if(player[1].appearance[i] == 2 ) second[result]=true;
               i--;
            }
            
            if (first[result]==true && second[result]==true){
                first[result]=false;
                second[result]=false;
                i=6;
                
            while (!second[result] && !first[result]){
               if(player[0].appearance[i] == 2 ) first[result]=true;
               if(player[1].appearance[i] == 2 ) second[result]=true;
               i--;
            }
            if (first[result]==true && second[result]==true){
                first[result]=false;
                second[result]=false;
                i=6;
                while (!second[result] && !first[result]){
               if(player[0].appearance[i] == 1 ) first[result]=true;
               if(player[1].appearance[i] == 1 ) second[result]=true;
               i--;
            }
            }
            }
            i=0;
        }
        
        
        for (int j=1;i<9;i++){
    if (first[j] == true)   System.out.println("Wygral 1.......");
    else if (first[j] == true)System.out.println("Wygral 2.....");
}
    }
     
}


Już nawet nie chodzi mi o 3 warunek, gdy rezultaty są równe, tylko o te 2 najprostsze, które programik dobrze identyfikuje. Czyli przypisuje odpowiednią wartość liczbą dla danych kości ( im mniejsza tym "wartość" kości większa.

1

Problem zapewne powoduje metoda setResult. Zmienne counterThree, counterTwo, counterOne, result, jeśli ich nie wykorzystujesz w innym miejscu powinny być wewnątrz metody. Jeśli jednak z nich korzystasz poza metodą setResult to albo na początku, albo na końcu powinny być przywrócone do wartości domyślnych.

W przykładzie, który podałeś wywołujesz metodę pierwszy raz do wyświetlenia wyniku, a później jeszcze raz do sprawdzenia warunku. W drugim przypadku wartości powyższych zmiennych nie mają domyślnych wartości, tylko te wyliczone z poprzedniego wywołania metody.

1 użytkowników online, w tym zalogowanych: 0, gości: 1