目录
一、洗牌算法
1.1 Poker
1.2 PokerBox
1.3 Test
二、杨辉三角
一、洗牌算法
【业务需求】:
第一步:生成52张牌,没有大小王,一共四种花色({"♠", "♥", "♣", "♦"),每种花色共13张,J、Q、K用11、12、13来代替。
第二步:洗牌。
第三步:给三个人轮流发5张牌。
第四步:将三个人的牌 和 剩余的牌 按顺序打印出来。
【点睛】:把扑克牌box、三个人的手牌、都设置成ArrayList,对它们进行操作时直接调用ArrayList原生方法即可。
1.1 Poker
package poker;/*** Created with IntelliJ IDEA.* Description:* User: tangyuxiu* Date: 2024-08-06* Time: 10:23*/
public class Poker implements Comparable<Poker> {private int value;private String flower;public Poker(int value, String flower) {this.value = value;this.flower = flower;}@Overridepublic String toString() {return "{" + this.value + " " + this.flower + "}";}@Overridepublic int compareTo(Poker o) {return this.value - o.value;}
}
1.2 PokerBox
package poker;import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;/*** Created with IntelliJ IDEA.* Description:* User: tangyuxiu* Date: 2024-08-06* Time: 10:26*/
public class PokerBox {private ArrayList<Poker> pokers;//52张牌private static final String[] flowers = {"♠", "♥", "♣", "♦"};//花色public PokerBox() {this.pokers = new ArrayList<>();/* 生成52张排 */for (int i = 0; i < 4; i++) {for (int j = 1; j <= 13; j++) {Poker poker = new Poker(j,flowers[i]);this.pokers.add(poker);}}}/* 洗牌 */public void shuffle() {Random random = new Random();for (int i = this.pokers.size() - 1; i > 0 ; i--) {//生成0~i-1之间的随机值int j = random.nextInt(i);//交换pokers中下标为i和j的值//注意:可以发现我们在得到/设置顺表中的元素时,会直接利用方法🧐Poker cur = this.pokers.get(i);this.pokers.set(i, this.pokers.get(j));this.pokers.set(j,cur);}}/* 给三个人分配牌 */public void organize() {ArrayList<Poker> hand1 = new ArrayList<>();ArrayList<Poker> hand2 = new ArrayList<>();ArrayList<Poker> hand3 = new ArrayList<>();//为了方便组织三个人,于是定义了这个ArrayList<ArrayList<Poker>> hands = new ArrayList<>();hands.add(hand1);hands.add(hand2);hands.add(hand3);for (int i = 0; i < 5; i++) {//外层循环表示发牌的轮数for (int j = 0; j < 3; j++) {//内存循环表示发给哪个人//remove方法的返回值是,被删除元素的值//每次拿牌都拿最上面的那张Poker poker = this.pokers.remove(0);hands.get(j).add(poker);}}System.out.println("三人的牌面值:");System.out.println(hand1);System.out.println(hand2);System.out.println(hand3);}/* 对牌排序:根据牌面值的大小进行了排序 */public void mySort() {//Collections是操作集合的工具类,可以利用里面的sort方法对集合里面的元素进行排序Collections.sort(this.pokers);}@Overridepublic String toString() {return "PokerBox{" +"pokers=" + pokers +'}';}
}
1.3 Test
package poker;/*** Created with IntelliJ IDEA.* Description:* User: tangyuxiu* Date: 2024-08-06* Time: 10:29*/
public class Test {public static void main(String[] args) {/* 生成52张牌 */PokerBox pokerBox = new PokerBox();System.out.println("新牌:");System.out.println(pokerBox);/* 洗牌 */pokerBox.shuffle();System.out.println("洗牌:");System.out.println(pokerBox);/* 给三个人分配牌 */pokerBox.organize();System.out.println("剩余牌:");System.out.println(pokerBox);/* 对牌排序 */pokerBox.mySort();System.out.println("排序:");System.out.println(pokerBox);}
}
二、杨辉三角
public List<List<Integer>> generate(int numRows) {//二维数组:List<List<Integer>> arrayList = new ArrayList<>();/* 第一行 */List<Integer> arrayList0 = new ArrayList<>();arrayList0.add(1);//将第一行放入arrayList顺序表中arrayList.add(arrayList0);/* 其他行 *///我们把第一行认为是0行所以这里i从1开始,i<numRowsfor (int i = 1; i < numRows; i++) {List<Integer> cur = new ArrayList<>();cur.add(1);//j表示列号,从1开始是因为0列已经放了元素了//j<i是因为,当在i==j时,第j行一定放1,所以不算在里面for (int j = 1; j < i; j++) {Integer x1 = arrayList.get(i - 1).get(j - 1);Integer x2 = arrayList.get(i - 1).get(j);cur.add(x1 + x2);}cur.add(1);arrayList.add(cur);}return arrayList;
}
本篇已完结 ......