用ArrayList实现简单的扑克牌
Card
我们首先要构建一个牌堆。
class Card {
public String suit;//花色
public int rank;
public Card(String suit, int rank) {
this.suit = suit;
this.rank = rank;
}
@Override
public String toString() {
return suit+"" + rank ;
}
}
接下来,进行的是洗牌和发牌的过程。
buyCard
我们首先要构建好52张扑克牌,我们可以使用
public class PuKePai {
public static final String[] SUITS = {"♥","♠","♣","♦"};
public static List<Card> buyCard() {
List<Card> cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
String suit = SUITS[i];
int rank = j;
Card card = new Card(suit,rank);
cards.add(card);
}
}
return cards;
}
}
这样子直接发牌是不行的,因为顺序是一样的,所以我们要进行的是洗牌过程。
shuffle
我们直接使用random随机数进行打乱。
public static void shuffle(List<Card> cards) {
//洗牌
Random random = new Random();
for (int i = cards.size()-1; i > 0; i--) {
int index = random.nextInt(i);
Card tmp = cards.get(i);
cards.set(i,cards.get(index));
cards.set(index,tmp);
}
}
Main
最后写一个main函数,模拟发牌。
public static void main(String[] args) {
List<Card> cardList = buyCard();
shuffle(cardList);
// 现在有三个人,轮流抓牌
List<List<Card>> hand = new ArrayList<>();
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<Card> hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for (int i = 0; i < 17; i++) {
for (int j = 0; j < 3; j++) {
Card card = cardList.remove(0);
hand.get(j).add(card);
}
}
System.out.println("第一个 "+hand1);
System.out.println("第二个 "+hand2);
System.out.println("第三个 "+hand3);
}
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Card {
public String suit;//花色
public int rank;
public Card(String suit, int rank) {
this.suit = suit;
this.rank = rank;
}
@Override
public String toString() {
return suit+"" + rank ;
}
}
public class PuKePai {
public static final String[] SUITS = {"♥","♠","♣","♦"};
public static List<Card> buyCard() {
List<Card> cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
String suit = SUITS[i];
int rank = j;
Card card = new Card(suit,rank);
cards.add(card);
}
}
return cards;
}
public static void shuffle(List<Card> cards) {
//洗牌
Random random = new Random();
for (int i = cards.size()-1; i > 0; i--) {
int index = random.nextInt(i);
Card tmp = cards.get(i);
cards.set(i,cards.get(index));
cards.set(index,tmp);
}
}
public static void main(String[] args) {
List<Card> cardList = buyCard();
shuffle(cardList);
// 现在有三个人,轮流抓牌
List<List<Card>> hand = new ArrayList<>();
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<Card> hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for (int i = 0; i < 17; i++) {
for (int j = 0; j < 3; j++) {
Card card = cardList.remove(0);
hand.get(j).add(card);
}
}
System.out.println("第一个 "+hand1);
System.out.println("第二个 "+hand2);
System.out.println("第三个 "+hand3);
}
}
杨辉三角
1. 我们看到返回值是一个List<List<Integer>>,所以我们先new一个返回ret;
2. 然后就是进行每一小步的计算;
3. 观察发现,每一行的最左边和最右边都是1,我们可以将其单列出来,只需进行中间的运算;
4. 我们get到上一行的数据,根据公式可以进行运算得出左后 的结果。
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
List<Integer> list1 = new ArrayList<>();
list1.add(1);
ret.add(list1);
for(int i = 1;i < numRows;i++) {
List<Integer> list = new ArrayList<>();
list.add(1);
for(int j = 1;j < i;j++){
List<Integer> cur = ret.get(i-1);
int val = cur.get(j) + cur.get(j-1);
list.add(val);
}
list.add(1);
ret.add(list);
}
return ret;
}