Java學習--撲克牌比大小的小遊戲

有稜角的圓發表於2016-07-15

實現:生成五十四張牌,然後進行洗牌,輸入兩名玩家,然後分別發兩張牌,進行比較大小,兩張牌中擁有較大一張牌的玩家獲勝

Card.java程式碼如下:

package com.cardgame;

public class Card {

    String suit= new String();
    String num = new String();
    public Card(String suit ,String num){
    
        this.suit=suit;
        this.num=num;
        
    }
}

Player.java程式碼如下:

package com.cardgame;

import java.awt.List;
import java.util.ArrayList;

public class Player {

    String name= new String();
    String id = new String();
    java.util.List<Card> cards= new ArrayList<Card>();
    
    
    public Player(String name,String id){
        this.id=id;
        this.name=name;
    }
    
}

 

MainClass.java程式碼如下:

package com.cardgame;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class MainClass {

    public List<Card> cards= new ArrayList<Card>();
    public List<String> suitList= new ArrayList<String>();
    public List<Card> upsetCards= new ArrayList<Card>();
    
    /*
     * 
     * 新增54張撲克牌
     * 
     */
    public void add54Cards() {
        for (int i = 0; i < suitList.size(); i++) {
        
            String suit=suitList.get(i);
            String num;
            for (int j = 0; j < 13; j++) {
                
                if (j==0) {
                    num="A";    
                }
                else if (j==10) {
                    num="J";
                }
                else if (j==11) {
                    num="Q";
                }
                else if (j==12) {
                    num="K";
                }else {
                    Integer jInteger=(Integer)j+1;
                    num=jInteger.toString();
                }
                
                cards.add(new Card(suit, num));
            }
        }
        cards.add(new Card(null, "小王"));

        cards.add(new Card(null, "大王"));
        System.out.println("*洗牌前*");
        for (int i = 0; i < cards.size(); i++) {
            Card card= cards.get(i);
            
            System.out.print(card.suit+card.num+",");

        }
        System.out.println();

    }
    /*
     * 
     * 打亂順序
     * 
     */
    public void breakOrder() {
        int total = cards.size();
        Random random = new Random();
        int index;
        for (int i = 0; i < total; i++) {
            index= random.nextInt(cards.size());
            upsetCards.add(cards.get(index));
            cards.remove(cards.get(index));
        }
        System.out.println("*開始洗牌*");

        for (int i = 0; i < upsetCards.size(); i++) {
            Card card= upsetCards.get(i);
            
            System.out.print(card.suit+card.num+",");

            
        }
        System.out.println();
        System.out.println("*洗牌完成*");

    }
    /*
     * 
     * 建立玩家,併發牌
     * 
     */
    public void createPlayer(){
        Scanner scanner  = new Scanner(System.in); 
        
        System.out.println("請輸入一號玩家ID:");
        String id1= scanner.next();
        System.out.println("請輸入一號玩家暱稱:");
        String name1= scanner.next();
        Player first  = new Player(name1, id1);
        System.out.println("請輸入二號玩家ID:");
        String id2= scanner.next();
        System.out.println("請輸入二號玩家暱稱:");
        String name2= scanner.next();
        Player second  = new Player(name2, id2);
        //發牌
        for (int i = 0; i <2; i++) {
            Random random = new Random();
            //
            int index1=random.nextInt(upsetCards.size());
            first.cards.add(upsetCards.get(index1));
            upsetCards.remove(index1);
            //
            int index2=random.nextInt(upsetCards.size());
            second.cards.add(upsetCards.get(index2));
            upsetCards.remove(index2);
            
        }
        System.out.println("*發牌成功*");
        System.out.println("玩家:"+first.name+" 獲得的牌為:");
        for (Card card : first.cards) {
            System.out.print(card.suit+card.num+" ");
        }
        System.out.println();
        System.out.println("玩家:"+second.name+"獲得的牌為:");
        for (Card card : second.cards) {
            System.out.print(card.suit+card.num+" ");
        }
        System.out.println();
        int card11=this.getNum(first.cards.get(0));
        int card12=this.getNum(first.cards.get(1));    
        int card21=this.getNum(second.cards.get(0));
        int card22=this.getNum(second.cards.get(1));
        if ((card11>card12?card11:card12)>(card21>card22?card21:card22)) {
            System.out.println("一號玩家獲勝");
        }else if ((card11>card12?card11:card12)<(card21>card22?card21:card22)) {
            System.out.println("二號玩家獲勝");
        }else {
            System.out.println("平局!");
        }
    }
    
    /*
     * 
     * 獲得牌權值:
     * 
     */
    public int getNum(Card card) {
        int num;
        if (card.num=="A") {
            num=14;
        }else if (card.num=="J") {
            num=11;
        }else if (card.num=="Q") {
            num=12;
        }else if(card.num=="K") {
            num=13;
        }else if (card.num=="大王") {
            num=1000;
                    
        }else if (card.num=="小王") {
            num=888;
        }else {
            num=Integer.valueOf(card.num);
        }
        
        /*
         * 
         * 獲取花色權值
         * 
         */
        int suitC=0;
        
        if (card.suit=="黑桃") {
            suitC=4;
        }else if (card.suit=="紅桃") {
            suitC=3;
        }else if (card.suit=="梅花") {
            suitC=2;
        }else if (card.suit=="方塊") {
            suitC=1;
        }
        
        int result = num*10+suitC;
        return result;
    }
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        MainClass mClass=new MainClass();
        mClass.suitList.add("黑桃");
        mClass.suitList.add("紅桃");
        mClass.suitList.add("梅花");
        mClass.suitList.add("方塊");

        mClass.add54Cards();
        mClass.breakOrder();
        mClass.createPlayer();
    }

}

執行結果

相關文章