扑克牌游戏:
1.通过ArrayList实现一副扑克牌(不含鬼牌)
2.实现洗牌功能
3.三名玩家,每名玩家各自抓取5张牌
第一步:定义牌类(card),该类的每个对象表示一张牌
1.分值rank [1,13]
2.花色suit[四种] String
package card;
import java.util.Objects;
/**
* Program: Created with IntelliJ IDEA
* <p>
* Description:
* <p>
* Author: Nin9Wan9
* <p>
* Time:2021-03-08 10:18
**/
public class Card {
private final int rank;
private final String suit;
public Card(int rank,String suit){
this.rank = rank;
this.suit = suit;
}
//[♥ A]...
@Override
public String toString() {
return String.format("[%s %s]",suit,rankToString(rank));
}
private static String rankToString(int rank){
switch(rank){
case 1:
return"A";
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K";
default:
return String.valueOf(rank);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Card card = (Card) o;
return rank == card.rank &&
Objects.equals(suit, card.suit);
}
@Override
public int hashCode() {
return Objects.hash(rank, suit);
}
public static void main(String[] args) {
Card c1 = new Card(1,"♥");
Card c2 = new Card(1,"♠");
Card c3 = new Card(1,"♣");
Card c4 = new Card(1,"♦");
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
}
}
package card;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import static java.util.Collections.shuffle;
/**
* Program: Created with IntelliJ IDEA
* <p>
* Description:
* <p>
* Author: Nin9Wan9
* <p>
* Time:2021-03-08 10:32
**/
public class Game {
public static void main(String[] args) {
//1.初始化一副扑克牌
ArrayList<Card> cardlist = initCardList();
// System.out.println(cardlist);
//2.对一副牌进行洗牌
//Collections.shuffle(cardlist);
shuffle(cardlist);
// System.out.println(cardlist);
//3.h黑桃A在不在这副牌中? 在,期望打印true
//结果打印了false,为什么?
// Card spadeA = new Card(1,"♠");
// System.out.println(cardlist.contains(spadeA));
//3.准备三名玩家的手牌
ArrayList<Card> PlayerA = new ArrayList<>();
ArrayList<Card> PlayerB = new ArrayList<>();
ArrayList<Card> PlayerC = new ArrayList<>();
System.out.println("抓牌前:");
System.out.println(cardlist);
//4.按照A -> B -> C -> A -> B -> C 的顺序轮流抓牌,每人抓五张牌
for (int i = 0;i < 5;i++){
//抓牌 1.从牌堆(cardlist)中删除出删除出一张牌来(头删/尾删)这里选择头删
// 2.将该牌放入玩家的手牌
//A玩家抓牌
Card card = cardlist.remove(0);
PlayerA.add(card);
//B玩家抓牌
card = cardlist.remove(0);
PlayerB.add(card);
//C玩家抓牌
card = cardlist.remove(0);
PlayerC.add(card);
}
System.out.println("抓牌后:");
System.out.println(cardlist);
System.out.println(cardlist.size());
System.out.println("A:"+PlayerA);
System.out.println("B:"+PlayerB);
System.out.println("C:"+PlayerC);
}
private static void shuffle(ArrayList<Card> cardlist){
Random random = new Random();
for (int i =cardlist.size()-1;i > 0;i--){
//下标为【i】 为交换牌中的其中一张
//[0,i)位置中,随机生成一个下标
int randomidx = random.nextInt(i);
//下标为[randomidx] 为交换牌的另一张的下标
//交换
Card tempA = cardlist.get(i);
Card tempB = cardlist.get(randomidx);
cardlist.set(i,tempB);
cardlist.set(randomidx,tempA);
}
}
private static ArrayList<Card> initCardList() {
ArrayList<Card> cardlist = new ArrayList<>();
String[] suits ={"♥","♠","♣","♦"};
for (String suit: suits){
for (int rank = 1;rank <= 13;rank++){
Card card = new Card(rank,suit);
cardlist.add(card);
}
}
return cardlist;
}
}
最后的结果