目录
- 一、问题
- 二、创建一个Poker类
- 三、完成游戏内容
- 四、测试
- 五、总结
简单的洗牌算法是对ArrayList的具体使用
一、问题
我们需要一副完整的扑克牌,除去大小王一共52张牌,参与游戏的玩家共3名,在洗牌后分发每名玩家5张扑克牌。
二、创建一个Poker类
描述扑克牌的属性为花色和数字,我们对其进行封装,再重写 toString方法
public class Poker {
private String suit;//花色
private int rank;//数字
public Poker(String suit, int rank) {
this.suit = suit;
this.rank = rank;
}
@Override
public String toString() {
return "{"+suit+" "+rank+"}";
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
}
三、完成游戏内容
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Game {
public static final String[] suits = {"♥", "♣", "♦", "♠"};
public List<Poker> buyPokers() {
List<Poker> pokers = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 13; j++) {
Poker poker = new Poker(suits[i], j);
pokers.add(poker);
}
}
return pokers;
}
public void shuffle(List<Poker> pokers){
for (int i = pokers.size()-1; i > 0; i--) {
Random random=new Random();
int index=random.nextInt(i);
swap(pokers,i,index);
}
}
public void swap(List<Poker> pokers,int i,int index){
Poker tmp=pokers.get(i);
pokers.set(i,pokers.get(index));
pokers.set(index,tmp);
}
public List<List<Poker>> game(List<Poker> pokers){
List<List<Poker>> hand=new ArrayList<>();
List<Poker> hand1=new ArrayList<>();
List<Poker> hand2=new ArrayList<>();
List<Poker> hand3=new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
Poker removePoker=pokers.remove(0);
hand.get(j).add(removePoker);
}
}
return hand;
}
}
四、测试
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Game game=new Game();
List<Poker> pokers=game.buyPokers();
System.out.println(pokers);
//洗牌
game.shuffle(pokers);
System.out.println("洗牌");
System.out.println(pokers);
//揭牌
List<List<Poker>> List=game.game(pokers);
System.out.println("揭牌");
for (int i = 0; i < List.size(); i++) {
System.out.println(List.get(i));
}
}
}
最后结果
[{♥ 1}, {♥ 2}, {♥ 3}, {♥ 4}, {♥ 5}, {♥ 6}, {♥ 7}, {♥ 8}, {♥ 9}, {♥ 10}, {♥ 11}, {♥ 12}, {♣ 1}, {♣ 2}, {♣ 3}, {♣ 4}, {♣ 5}, {♣ 6}, {♣ 7}, {♣ 8}, {♣ 9}, {♣ 10}, {♣ 11}, {♣ 12}, {♦ 1}, {♦ 2}, {♦ 3}, {♦ 4}, {♦ 5}, {♦ 6}, {♦ 7}, {♦ 8}, {♦ 9}, {♦ 10}, {♦ 11}, {♦ 12}, {♠ 1}, {♠ 2}, {♠ 3}, {♠ 4}, {♠ 5}, {♠ 6}, {♠ 7}, {♠ 8}, {♠ 9}, {♠ 10}, {♠ 11}, {♠ 12}]
洗牌
[{♠ 7}, {♦ 8}, {♦ 3}, {♠ 9}, {♥ 1}, {♠ 1}, {♠ 2}, {♦ 12}, {♦ 10}, {♦ 2}, {♣ 6}, {♣ 1}, {♠ 5}, {♦ 7}, {♦ 6}, {♣ 8}, {♣ 2}, {♦ 5}, {♠ 6}, {♥ 9}, {♥ 6}, {♠ 3}, {♦ 4}, {♥ 8}, {♣ 7}, {♣ 5}, {♥ 5}, {♦ 11}, {♠ 12}, {♥ 2}, {♣ 10}, {♥ 10}, {♥ 4}, {♣ 9}, {♠ 4}, {♦ 9}, {♠ 10}, {♦ 1}, {♥ 12}, {♣ 12}, {♥ 11}, {♠ 8}, {♣ 3}, {♣ 11}, {♣ 4}, {♥ 3}, {♥ 7}, {♠ 11}]
揭牌
[{♠ 7}, {♠ 9}, {♠ 2}, {♦ 2}, {♠ 5}]
[{♦ 8}, {♥ 1}, {♦ 12}, {♣ 6}, {♦ 7}]
[{♦ 3}, {♠ 1}, {♦ 10}, {♣ 1}, {♦ 6}]
五、总结
通过这个纸牌游戏的实现,我们可以学习到Java中类与对象的设计与使用,以及集合类的使用。购买纸牌、洗牌和揭牌等步骤涉及到了对象的创建和属性的操作,通过List集合可以方便地存储和管理多个纸牌对象。同时,通过使用Random类的nextInt()方法来实现随机交换纸牌的位置,实现洗牌的效果。最后,通过将揭牌结果存储在一个包含多个List集合的集合中,可以方便地得到每个玩家手中的纸牌,便于后续游戏的进行。