綜合練習——洗牌發牌Easy版

dapan發表於2021-09-09

Card程式碼,裡面是卡牌的屬性,重寫了equals方法,並且實現了Comparable介面。這裡因為要求只是比較大小,所以我在屬性裡多新增了一條,用多加的這條屬性來進行比較,由數字10,20,30,40,50,60,70,80,90,91,92,93,94還有顏色用A-D表示,組合而成的一個字串,這樣去排序看大小的話會非常方便。
因為一般的時候2是最大的牌。2用94,A93,K92,Q91,J90,10用80……3用10。後面跟著如果數字相同則比較花色,如黑紅梅方,用DCBA來表示,比如說當都是2時,前兩位都是94,方片2是94A,黑桃2就是94D。這樣升序會排在對方後面。直接排序然後取最大的值就可以了。再用最大的值和當時的手牌去比較,相同的那位玩家獲勝。

遊戲執行的類

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by Administrator on 2017/4/4.
 */
public class PokerGameTest {
    public static void main(String[] args) {
        System.out.println("--------歡迎來到撲克牌遊戲--------");
        System.out.println("------------建立撲克牌------------");
        System.out.println("----------撲克牌建立成功----------");
        System.out.print("撲克牌為:");
        //建立卡牌集合類,例項化,呼叫方法建立52張的卡組
        PokerCardsList pcl=new PokerCardsList();
        pcl.pokerCardsListAdd();
        System.out.println("------------開始洗牌-------------");
        System.out.println("------------洗牌結束!------------");
        System.out.println("------------建立玩家--------------");
        PokerPlayerList pm=new PokerPlayerList();
        //新建一個PokerPlayerList集合把這兩個玩家儲存進來
        System.out.println("請輸入第1位玩家的ID和姓名");
        pm.playerIdNameAdd();
        Player player1=pm.playerList.get(0);
        System.out.println("請輸入第2位玩家的ID和姓名");
        pm.playerIdNameAdd();
        Player player2=pm.playerList.get(1);
        System.out.println("歡迎玩家:"+player1.playerName);
        System.out.println("歡迎玩家:"+player2.playerName);
        System.out.println("------------開始發牌-------------");
        int[] k=pcl.randomTest();
        Card player1Card1=new Card(pcl.pokerCardsList.get(k[0]).colorNum,
                pcl.pokerCardsList.get(k[0]).colorSum);
        Card player1Card2=new Card(pcl.pokerCardsList.get(k[1]).colorNum,
                pcl.pokerCardsList.get(k[1]).colorSum);
        Card player2Card1=new Card(pcl.pokerCardsList.get(k[2]).colorNum,
                pcl.pokerCardsList.get(k[2]).colorSum);
        Card player2Card2=new Card(pcl.pokerCardsList.get(k[3]).colorNum,
                pcl.pokerCardsList.get(k[3]).colorSum);
        //建立兩個玩家的手牌List,例項化,否則會無法呼叫,空指標異常
        player1.playerCardsList=new ArrayList();
        player2.playerCardsList=new ArrayList();
        player1.playerCardsList.add(player1Card1);
        System.out.println("-----玩家:"+player1.playerName+"----拿牌");
        player2.playerCardsList.add(player2Card1);
        System.out.println("-----玩家:"+player2.playerName+"----拿牌");
        player1.playerCardsList.add(player1Card2);
        System.out.println("-----玩家:"+player1.playerName+"----拿牌");
        player2.playerCardsList.add(player2Card2);
        System.out.println("-----玩家:"+player2.playerName+"----拿牌");
        System.out.println("------------發牌結束-------------");
        System.out.println("------------開始遊戲-------------");
        Collections.sort(player1.playerCardsList);
        System.out.println("玩家"+player1.playerName+"最大的手牌是:"+
                player1.playerCardsList.get(1).colorNum);
        Collections.sort(player2.playerCardsList);
        System.out.println("玩家"+player2.playerName+"最大的手牌是:"+
                player2.playerCardsList.get(1).colorNum);
        Player nullPlayer=new Player();
        nullPlayer.playerCardsList=new ArrayList();
        nullPlayer.playerCardsList.add(player1.playerCardsList.get(1));
        nullPlayer.playerCardsList.add(player2.playerCardsList.get(1));
        Collections.sort(nullPlayer.playerCardsList);
        if(nullPlayer.playerCardsList.get(1).equals(player1.playerCardsList.get(1))){
            System.out.println("----------"+"玩家"+player1.playerName+"獲勝"+"----------");
        }else{
            System.out.println("----------"+"玩家"+player2.playerName+"獲勝"+"-----------");
        }
        System.out.println("玩家各自的手牌為:");
        System.out.println("玩家"+player1.playerName+":"+player1.playerCardsList.get(0).colorNum+
                " "+player1.playerCardsList.get(1).colorNum);
        System.out.println("玩家"+player2.playerName+":"+player2.playerCardsList.get(0).colorNum+
                " "+player2.playerCardsList.get(1).colorNum);
    }
}

Card類

package pokergame;

/**
 * Created by Administrator on 2017/4/5.
 * 定義一個卡牌類
 */
public class Card implements Comparable{
    public String colorNum;
    public String colorSum;
    public Card(String colorNum,String colorSum) {
        this.colorSum = colorSum;
        this.colorNum=colorNum;
        }

    @Override
    public int compareTo(Card o) {
        return this.colorSum.compareTo(o.colorSum);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Card)) return false;

        Card card = (Card) o;

        return colorNum.equals(card.colorNum);
    }
}

遊戲玩家類

import java.util.*;

/**
 * Created by Administrator on 2017/4/4.
 */
public class Player{
    public Integer playerId;
    public String playerName;
    public List playerCardsList;
    public Player(Integer playerId,String playerName){
        this.playerId=playerId;
        this.playerName=playerName;
        List playerCardsList=new ArrayList();
    }
    public Player(){
    }
}

建立了整副牌的List類

import java.util.*;

/**
 * Created by Administrator on 2017/4/5.
 * 建立一整副卡牌的集合,由於要打亂卡牌的順序
 * 可以建立四個不重複的隨機數,分別取牌發給玩家
 */
public class PokerCardsList {
    public List pokerCardsList;
    public PokerCardsList() {
        this.pokerCardsList = new ArrayList();
    }
    /**
     * 往pokerCardsList中新增卡牌的方法
     */
    public void pokerCardsListAdd() {
        final String[] Color = {"黑桃", "紅桃", "梅花", "方片"};
        final String[] Num = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        final String[] colorTwo={"D","C","A","B"};
        final int[] sum={93,94,10,20,30,40,50,60,70,80,90,91,92};
        for(int i = 0;i47;j--){
            testSet.add(num.nextInt(j));
            if(testSet.size()>i){
                i++;
            }else{
                continue;
            }
        }
        //定義一個陣列接收這四個數
        int[] k=new int[4];
        int i=0;
        for(Object x:testSet) {
            int y=(int)x;
            k[i]=y;
            i++;
        }
        return k;
    }
}

玩家資訊儲存的類

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;

/**
 * Created by Administrator on 2017/4/5.
 */
public class PokerPlayerList {
    //建立一個List集合存放兩名玩家的資訊
    public List playerList;
    public PokerPlayerList(){
        this.playerList=new ArrayList();
    }
    /**
     * 存放玩家資訊
     */
    public void playerIdNameAdd() {
        while(true) {
            try {
                Scanner sc=new Scanner(System.in);
                System.out.println("輸入ID:");
                Integer playerid = sc.nextInt();
                System.out.println("輸入姓名:");
                String playername = sc.next();
                Player p = new Player(playerid, playername);
                this.playerList.add(p);
                break;
            } catch (InputMismatchException e) {
                System.out.println("您輸入的ID非數字");
                continue;
            }
        }
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/758/viewspace-2807648/,如需轉載,請註明出處,否則將追究法律責任。

相關文章