java--纸牌游戏
来源:互联网 发布:网络彩票销售好做吗 编辑:程序博客网 时间:2024/05/29 19:41
题目 :
创建一副扑克牌,这副牌是标准的包含52张,4种不同花色(方,梅花,红心,黑桃),上面的数字是2到ACE(14|A)的牌。
要求:
1.打乱这副牌
2.发牌给四个玩家.
请通过代码实现这个要求,如果不怎么熟悉Java/C/C++相关的Api的话,可以用伪代码来完成.
1.定义纸牌的花色
//CardsType.javapublic 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.javapublic 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.javapublic 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.javapublic 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.javapublic 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;}@Overridepublic 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.javaimport 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());}}}
0 0
- java--纸牌游戏
- 纸牌游戏
- 纸牌游戏
- 纸牌游戏
- 纸牌游戏
- 纸牌游戏
- 纸牌游戏
- 纸牌游戏
- 纸牌游戏
- 纸牌游戏
- java--纸牌游戏自动出牌
- 一道JAVA 21点纸牌游戏
- Java实现24点纸牌游戏
- 浅析:纸牌游戏 Java算法——之重复发牌
- 翻纸牌游戏_hdu_2209(双向广搜).java
- java实现纸牌游戏-小猫钓鱼算法
- java ----实现简单的21点纸牌游戏
- 纸牌游戏开始
- Qt相关概念
- validform中自定验证提示位置
- 详细解析手表的构造
- 宏的扩展——GOTO
- 为梦想而战
- java--纸牌游戏
- 澄城发现最大秦国国人墓地 已探明墓葬3211座
- Jmeter 接口测试发送数据乱码的问题
- ZOJ 3781 Paint the Grid Reloaded BFS
- AlphaControls v9.01
- linux中inittab文件详解
- PopupWindow实现对话框的位置变化
- Android Wear:我为未来代言
- 服务器epoll初用