JAVA入門第三季——最後練習題撲克牌

welchang發表於2021-09-09

圖片描述
圖片描述

玩家類:

package playCards;

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

//建立物件玩家,玩家至少要有ID、姓名、手牌等屬性,手牌為撲克牌的集合

public class Players {
    public int id;
    public String name;
    public List handCards;
    public Players(Integer id,String name) {
        this.id = id;
        this.name = name;
        this.handCards = new ArrayList();
    }
}

卡牌類

package playCards;

//建立物件撲克牌,不考慮大小王
public class Cards {
    public String num;
    public String color;
    public Cards(String num, String color) {
        this.num=num;
        this.color=color;
    }
    public Cards() {

    }
    @Override
    public boolean equals(Object arg0) {
        // TODO 自動生成的方法存根
        return super.equals(arg0);
    }

}

遊戲類(含main方法)

package playCards;

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

public class PlayGame  {

    public static List cardsToSelect;
    public static List cardsToSelectNew;
    private Scanner console;
    public Players player1;
    public Players player2;

    public PlayGame() {
        cardsToSelect = new ArrayList();
        cardsToSelectNew = new ArrayList();
        console = new Scanner(System.in);
    }

//新增撲克牌
    public void addCards() {
        System.out.println("建立撲克牌");//預設花色大小:黑桃》紅桃》梅花》方片
        Cards[] cd = {new Cards("2","方片"),new Cards("2","梅花"),new Cards("2","紅桃"),new Cards("2","黑桃"),
                new Cards("3","方片"),new Cards("3","梅花"),new Cards("3","紅桃"),new Cards("3","黑桃"),
                new Cards("4","方片"),new Cards("4","梅花"),new Cards("4","紅桃"),new Cards("4","黑桃"),
                new Cards("5","方片"),new Cards("5","梅花"),new Cards("5","紅桃"),new Cards("5","黑桃"),
                new Cards("6","方片"),new Cards("6","梅花"),new Cards("6","紅桃"),new Cards("6","黑桃"),
                new Cards("7","方片"),new Cards("7","梅花"),new Cards("7","紅桃"),new Cards("7","黑桃"),
                new Cards("8","方片"),new Cards("8","梅花"),new Cards("8","紅桃"),new Cards("8","黑桃"),
                new Cards("9","方片"),new Cards("9","梅花"),new Cards("9","紅桃"),new Cards("9","黑桃"),
                new Cards("10","方片"),new Cards("10","梅花"),new Cards("10","紅桃"),new Cards("10","黑桃"),
                new Cards("J","方片"),new Cards("J","梅花"),new Cards("J","紅桃"),new Cards("J","黑桃"),
                new Cards("Q","方片"),new Cards("Q","梅花"),new Cards("Q","紅桃"),new Cards("Q","黑桃"),
                new Cards("K","方片"),new Cards("K","梅花"),new Cards("K","紅桃"),new Cards("K","黑桃"),
                new Cards("A","方片"),new Cards("A","梅花"),new Cards("A","紅桃"),new Cards("A","黑桃")
                ,};
        for (Cards cards : cd) {
            cardsToSelect.add(cards);
        }
        System.out.println("建立撲克牌成功!");
    }

    //遍歷所有撲克牌
    public void cardsForEach(List cardsToSelect) {
        System.out.print("所有的牌為:");
        for (Cards cards : cardsToSelect) {
            System.out.print(cards.color+cards.num+",");
        }
        System.out.println();
    }

    //洗牌
    public List shuffle(List cardsToSelect) {
        Cards cdNew = new Cards();
        Random random = new Random();
        List cardsToSelectNew = new ArrayList();
        for(int i=0;icardsToSelect.indexOf(max2)) {
            System.out.println("-----玩家"+player1.name+"獲勝-----");
            System.out.println("玩家各自的手牌為:");
            System.out.println(player1.name+":["+player1.handCards.get(0).color+player1.handCards.get(0).num
                    +player1.handCards.get(1).color+player1.handCards.get(1).num+"]");
            System.out.println(player2.name+":["+player2.handCards.get(0).color+player2.handCards.get(0).num
                    +player2.handCards.get(1).color+player2.handCards.get(1).num+"]");
        }
        else {
            System.out.println("-----玩家"+player1.name+"獲勝-----");
            System.out.println("玩家各自的手牌為:");
            System.out.println(player1.name+":["+player1.handCards.get(0).color+player1.handCards.get(0).num
                    +player1.handCards.get(1).color+player1.handCards.get(1).num+"]");
            System.out.println(player2.name+":["+player2.handCards.get(0).color+player2.handCards.get(0).num
                    +player2.handCards.get(1).color+player2.handCards.get(1).num+"]");
        }

    }

    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        PlayGame game = new PlayGame();
        game.addCards();
        game.cardsForEach(cardsToSelect);
        cardsToSelectNew=game.shuffle(cardsToSelect);
        System.out.println("洗牌成功!");
        game.cardsForEach(cardsToSelectNew);
        game.addPlayer();
        game.deal();
        game.gameStart();
    }

}

第一次寫手記,個人基礎也不好,高階的方法用的很少,比大小的時候也是隻有用原來按大小建立的卡牌的位置來比大小,希望各位大佬多多指教。

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

相關文章