Javaは集合で簡単な闘地主発札を実現

15088 ワード

配列、コレクションの作成、データの保存
public class FightAgainstLandlords {
    /**
     * poker  ,  54  
     */
    private ArrayList poker;

    /**
     * colors        
     */
    private String[] colors;

    /**
     * numbers       
     */
    private String[] numbers;
}

構造方法
public class FightAgainstLandlords {
    public FightAgainstLandlords(ArrayList poker, String[] colors, String[] numbers) {
        this.poker = poker;
        this.colors = colors;
        this.numbers = numbers;
    }
}

トランプの順序を乱す方法の定義
public class FightAgainstLandlords {
    /**
     *   54  
     */
    public ArrayList fiftyFive() {
        for (String color: colors) {
            for (String number: numbers) {
                poker.add(color + number);
            }
        }
        poker.add("  ");
        poker.add("  ");
        //   ,  Collections      shuffle(),               
        Collections.shuffle(poker);
        return poker;
    }
}

トランプを配る
public class FightAgainstLandlords {
    /**
     *   
     *          
     * j = 1, 2, 3      
     * j =          
     */
    public ArrayList licensing(int j, ArrayList pokers) {
        //     
        ArrayList people1 = new ArrayList<>();
        ArrayList people2 = new ArrayList<>();
        ArrayList people3 = new ArrayList<>();
        //   
        ArrayList basePoker = new ArrayList<>();

        for (int i = 0; i < pokers.size(); i++) {
            String p = pokers.get(i);
            if ( i < 51) {
                if (i % 3 == 0) {
                    people1.add(p);
                } else if (i % 3 == 1) {
                    people2.add(p);
                } else {
                    people3.add(p);
                }
            } else {
                basePoker.add(p);
            }
        }

        //
        if (j == 1) {
            return people1;
        } else if (j == 2) {
            return people2;
        } else if (j == 3) {
            return people3;
        } else {
            return basePoker;
        }
    }
}

FightAgainstLandlordsクラスのテスト
import java.util.ArrayList;

public class DemoFightAgainstLandlords {
    public static void main(String[] args) {

        ArrayList poker = new ArrayList<>();
        String[] colors = {"  ", "  ", "  ", "  "};
        String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};

        // new     
        FightAgainstLandlords fightAgainstLandlords = new FightAgainstLandlords(poker, colors, numbers);
        // 54  
        ArrayList pokers = fightAgainstLandlords.fiftyFive();

        //
        ArrayList people1 = fightAgainstLandlords.licensing(1, pokers);
        ArrayList people2 = fightAgainstLandlords.licensing(2, pokers);
        ArrayList people3 = fightAgainstLandlords.licensing(3, pokers);
        ArrayList basePoker = fightAgainstLandlords.licensing(4, pokers);

        //
        System.out.println("people1:" + people1);
        System.out.println("people2:" + people2);
        System.out.println("people3:" + people3);
        System.out.println("basePoker:" + basePoker);
    }
}
    (     ,        ):
people1:[  3,   J,   K,   J,   K,   10,   6,   9,   Q,   Q,   4,   A,   2,   8,   4,   8,   K]
people2:[  A,   3,   ,   J,   7,   5,   9,   10,   8,   Q,   6,   6,   10,   Q,   5,   2,   A]
people3:[  5,   8,   7,   4,   9,   9,   K,   7,   6,   3,   10,   4,   3,   5,   ,   J,   A]
basePoker:[  2,   2,   7]

FightAgainstLandlordsクラスのすべてのコード
import java.util.ArrayList;
import java.util.Collections;

public class FightAgainstLandlords {
    /**
     * poker  ,  54  
     *           52  (     、   )
     */
    private ArrayList poker;

    /**
     * colors        
     */
    private String[] colors;

    /**
     * numbers       
     */
    private String[] numbers;

    public FightAgainstLandlords(ArrayList poker, String[] colors, String[] numbers) {
        this.poker = poker;
        this.colors = colors;
        this.numbers = numbers;
    }

    /**
     *   54  
     */
    public ArrayList fiftyFive() {
        for (String color: colors) {
            for (String number: numbers) {
                poker.add(color + number);
            }
        }
        poker.add("  ");
        poker.add("  ");
        //   ,  Collections      shuffle(),               
        Collections.shuffle(poker);
        return poker;
    }

    /**
     *   
     *          
     * j = 1, 2, 3      
     * j =          
     */
    public ArrayList licensing(int j, ArrayList pokers) {
        //     
        ArrayList people1 = new ArrayList<>();
        ArrayList people2 = new ArrayList<>();
        ArrayList people3 = new ArrayList<>();
        //   
        ArrayList basePoker = new ArrayList<>();

        for (int i = 0; i < pokers.size(); i++) {
            String p = pokers.get(i);
            if ( i < 51) {
                if (i % 3 == 0) {
                    people1.add(p);
                } else if (i % 3 == 1) {
                    people2.add(p);
                } else {
                    people3.add(p);
                }
            } else {
                basePoker.add(p);
            }
        }

        //
        if (j == 1) {
            return people1;
        } else if (j == 2) {
            return people2;
        } else if (j == 3) {
            return people3;
        } else {
            return basePoker;
        }
    }
}