java练习项目之纸牌游戏实例源码



java应用练习项目之纸牌游戏实例源码。java纸牌项目练习题目要求:

  1. 定义纸牌的花色
  2. 定义纸牌的大小2A
  3. 花色与数字组成一张牌
  4. 组成一整副纸牌
  5. 构造玩家
  6. 发牌

题目 :

     创建一副扑克牌,这副牌是标准的包含52张,4种不同花色(方,梅花,红心,黑桃),上面的数字是2到ACE(14|A)的牌。

     要求:

      1.打乱这副牌

       2.发牌给四个玩家.

     请通过代码实现这个要求,如果不怎么熟悉Java/C/C++相关的Api的话,可以用伪代码来完成.

1.定义纸牌的花色

//CardsType.java
public class CardsType {
 //定义牌的花色
 private static final String[] ct = {
  ”黑桃”,
  ”红桃”,
  ”梅花”,
  ”方块”
 };
 
 //获取纸牌的花色
 public static String getType(int i){
  return ct[i];
 }
 
 //判断是否是纸牌成员
 private static int find(String s){
  int i=-1;
  //遍历数组ct的每一个成员
  for(String st:ct ){
   i++;
   if(st.equals(s))
    return i;
  }
  return i;
 }
 
 //比较两张纸牌花色是否相同
 public static int com(String s, String c){
  int s1 = find(s);
  int s2 = find(c);
  if(s1 == -1)
   return 100;
  if(s2 == -1)
   return -100;
  return s2-s1;
 }
 
}

2. 定义纸牌的大小(2——A)

//Num.java
public class Num {
 //定义纸牌的号码
 private static final String[] n = {
  ”2″,”3″,”4″,”5″,”6″,”7″,”8″,”9″,”10″,”J”,”Q”,”K”,”A”
 };
 
 //获得纸牌大小
 public static String getN(int i){
  if(i<0||i>13)
   return null;
  return n[i];
 }
 
}

3.  花色与数字组成一张牌

//Cards.java
public class Cards {
 private String type;
 private int num;

 // 构造函数
 public Cards() {
 }

 public Cards(String s, int n) {
  this.type = s;
  this.num = n;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }

 public int getNum() {
  return num;
 }

 public void setNum(int num) {
  this.num = num;
 }

 public boolean equals(Object o) {
  if (!(o instanceof Cards))
   return false;
  Cards c = (Cards) o;

  if (this.num == c.getNum() && this.type.equals(c.getType()))
   return true;


  return false;
 }

 public int compareTo1(Object o) {
  if (!(o instanceof Cards))
   return 1;
  Cards c = (Cards) o;
  int bjjg;
  if ((bjjg = CardsType.com(this.type, c.getType())) != 0)
   return bjjg;

  return c.getNum() – this.num;
 }

 public String toString() {
  return type + ” : ” + Num.getN(num);
 }
}

4. 组成一整副纸牌//Pck.java
public class Pck {
 public static Cards[] getNewCards() {
  Cards[] nc = new Cards[52];
  int k = 0;
  for (int i = 0; i < 4; i++) {
   String t = CardsType.getType(i);
   for (int j = 0; j < 13; j++) {
    nc[k++] = new Cards(t, j);
   }
  }
  return nc;
 }
}

5.  构造玩家

//Player.java
public class Player {

 private Cards[] mCards;

 public Cards[] getmCards() {
  return mCards;
 }

 public void setmCards(Cards[] mCards) {
  this.mCards = mCards;
 }

 public Player(Cards[] mCards){
  this.mCards = mCards;
 }

 @Override
 public String toString() {
  String result;
  result = “Player [my card is: \n";
  for (int i = 0; i < mCards.length; i++) {
   result = result + mCards[i].toString() + “\n”;
  }
  result = result + “]”;
  return result;
 }
 
}

6.  发牌

//Game.java
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Game {
 private Cards[] gc = new Cards[52];
 private int[] num = new int[52];

 public Game() {
  int i = 0;
  for (Cards c : Pck.getNewCards()) {
   System.out.println(“index: ” + i + “==>card: ” + c.toString() + “”);
   gc[i++] = c;
  }

  for (int j = 0; j < 13; j++)
   num[j] = j;
 }

 // 发牌
 public Player[] fp() {
  Player[] players = new Player[4];
  List<Cards> wordList = Arrays.asList(gc);
  for (int i = 0; i < wordList.size(); i++) {
   System.out.println(“wordList.index: ” + i + “==>card: ”
     + wordList.get(i));
  }
  Collections.shuffle(wordList);
  for (int i = 0; i < wordList.size(); i++) {
   System.out.println(“wordList.index: ” + i + “==>card: ”
     + wordList.get(i));
  }
  int k = 0;
  for (int i = 0; i < 4; i++) {
   Cards[] card = new Cards[13];
   for (int j = 0; j < 13; j++) {
    card[j] = wordList.get(k);
    k++;
   }
   players[i] = new Player(card);
  }
  return players;
 }

 public static void main(String[] args) {
  Game g = new Game();
  Player[] pf = g.fp();
  for (int i = 0; i < pf.length; i++) {
   System.out.println(pf[i].toString());
  }

 }
}