金钩钓鱼的游戏,,思路混乱,求大神指教 大小王不算,52张牌,,两个玩家,每人26张牌,然后按顺序出牌。当遇到“J”时,则拿回桌面上的所有牌,或者当出的牌,与桌面上的某一张牌,点数相同时,,则拿回相同牌之间的所有牌(包括这两张点数一样的牌)谁手中的牌先出完,就判断输大致这样,,玩过这游戏的应该知道规则。求教一下代码怎么写。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 刚学java20天不到老师布置的作业怎么这么难没有头绪大神快来啊、、、、、 定义扑克牌类,用集合来处理就可以了import java.util.*;public class Test { static class GameCard { //扑克牌点数,这里偷懒了,更严谨一点的处理,可以采用枚举类 private static final String[] FACES = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; //花色,这里也偷懒了 private static final String[] SUITS = {"Spade", "Heart", "Club", "Diamond"}; private int face; private int suit; public GameCard(int face, int suit) { this.face = face; this.suit = suit; } public String getFace() { return FACES[face]; } public char getSuit() { return SUITS[suit].charAt(0); } public String toString() { //根据要求这里直接返回点数(正常应该花色和点数一起返回) return String.format("%s", FACES[face]); } public boolean equals(Object obj) { //这里直接比较点数,偷懒了,为了集合查找方便 //一般可以采用Comparator接口来处理两张牌的比较 if (obj!=null && obj.getClass() == this.getClass()) { return this.face == ((GameCard)obj).face; } return false; } public int hashCode() { //这里也是偷懒了,直接返回点数,为了集合查找方便 return this.face; } } public static void main(String[] args) throws Throwable { List<GameCard> poker = new LinkedList<GameCard>(); for (int i=0; i<4; i++) { //把52张牌保存到集合中 for (int j=0; j<13; j++) { poker.add(new GameCard(j, i)); } } Collections.shuffle(poker); //洗牌 List<List<GameCard>> player = new ArrayList<List<GameCard>>(); for (int i=0; i<2; i++) { //定义2个玩家 player.add(new LinkedList<GameCard>()); } for (int i=0; i<52; i++) { //给每个玩家发牌 player.get(i%2).add(poker.remove(0)); } int order = 0; //玩家出牌顺序,一个出玩就轮到另一个 //玩家手中还有牌就一直循环 while (player.get(0).size() > 0 && player.get(1).size() > 0) { GameCard card = player.get(order).remove(0); //玩家出牌 System.out.printf("-%s", card.toString()); //打印所出过的牌 int idx = poker.lastIndexOf(card); //查找出过的牌中是否有相同点数 poker.add(card); //把玩家出过的牌保存到集合中 if (card.getFace().equals("J")) { //如果玩家出的J System.out.println(); player.get(order).addAll(poker); //玩家获得所有台面的牌 poker.clear();//清空台面 } else if (idx >= 0) { //如果找到有相同点数的牌 System.out.println(); List<GameCard> sub = new ArrayList<GameCard>(poker.subList(idx, poker.size())); player.get(order).addAll(sub);//玩家获得相同点数之间的所有牌 poker.removeAll(sub);//台面牌数整理 for (GameCard gc : poker) {//台面剩余牌数继续表示 System.out.printf("-%s", gc); } } try { Thread.sleep(1000); //为了模拟出牌,这里减缓时间输出 } catch (Exception e) { e.printStackTrace(); } order = (order+1)%2;//玩家出牌轮换 } //打印赢家 System.out.printf("\nwiner is player[%d]\n", (player.get(0).size()>0 ? 0 : 1)); }} 写在一个类是为了快速回答问题,就是把思路告诉你,你可以自由发挥你要分成几个类,就把上面的一些共同部分抽出来就可以了比如player和poker都是card的持有者,可以把它们相同的部分抽象出来for example LZ自己参考发挥吧class Card { private int suit; //花色 private int face; //点数 public Card(int suit, int face) { } ...}abstract class CardHolder { protected Card[] cards[]; //牌 protecte int current = 0; //当前持有牌数,就是数组元素的当前位置 protected int count = 0; //最大持有牌数,就是数组的最大边界 public CardHolder(int count) { if (count < 0) { cards = new Card[0]; this.count = 0; } else { cards = new Card[count]; this.count = count; } } public void shuffle() { //洗牌 } public void sort() { //整理牌(也就是排序,默认规则排序) } public void sort(Comparator<Card> comp) { //可以由用户按定义的规则排序 } public draw(Card card) { //摸牌 } public Card discard() { //出牌 } public int getCardCount() { //剩余牌数 } public int find(Card card) { //找牌 } public void showCards() { for (int i=0; i<cards.current; i++) { System.out.printf("-%s", cards[i].toString()); } }}class Player extends CardHolder { //玩家 public Player(int count) { super(count); }}class Poker extends CardHolder { public static final int Card_Count = 52; public static fianl String[] Card_Face = {...}; public static final String[] Card_Suit = {...}; public Poker() { super(Card_Count); for (int i=0; i<4; i++) { for (int j=0; j<13; j++) { cards[current++] = new Card(i, j); } } } public void showCards() { //重写showCards,只显示点数 }}public class Game { public static void main(String[] args) throws Throwable { Poker poker = new Poker(); //生成扑克牌实例 Player[] player = new Player[2]; for (int i=0; i<2; i++) { //生成玩家实例 player[i] = new Player(Poker.Card_Count); //每个玩家最多持有52张牌 } poker.shuffle(); //洗牌 int order = (int)(Math.random()*100) % 2; //随机让玩家先摸牌 while (poker.getCardCount() > 0) { player[order].draw(poker.discard()); //玩家摸牌 order = (order + 1) % 2; //玩家轮换 } //玩家手中还持有牌的话一直循环 order = (int)(Math.random()*100) % 2; //随机让玩家先出牌 while (player[0].getCardCount() > 0 && player[1].getCardCount() > 0) { //此处基本和上面相同,多了一个J是第一个不收牌判断 Card card = player[order].discard(); System.out.printf("-%s", Poker.Card_Face[card.getFace()]); int idx = poker.find(card); //查找是否有相同点数的牌 poker.draw(card); //玩家出的牌放置到台面上 if (card.getFace() == 11 && poker.getCardCount() > 1) { idx = 0; //如果玩家出的是J并且不是第一张,则台面牌全部拿回,所以从0位置全拿 } if (idx >= 0) { //如果玩家可以拿牌 //从poker取出从idx位置到current位置的牌放到palyer[order]的手上 //提示,可以循环player[order].draw(poker.discard()); } order = (order+1) % 2; //玩家出牌轮换 } System.out.printf("\nwinner is player[%d]\n", (player[0].getCardCount() > 0 ? 0 : 1)); //打印赢家 }} 这个可以参考,不过不要照搬啊http://www.cnblogs.com/CloudTeng/archive/2012/03/24/2379699.html 在线等。。Applet在IE中无法监听http端口,急啊~ 急,模拟提交表单问题 怎样使JTabbedPane的每一个选项可关闭呢?像netbean那样? 如何理解Class类的实体的概念? 请大哥大姐,等各路高手给看看这段代码,谢谢了 关于jtable? 关于的system.out.flush()问题 刚学JAVA遇到一拦路虎,帮帮忙呀! 如何限制applet输入框 最多输入5 个字母,如何限制必须是数字和字母,如何限制必须是电子邮件地址 谁有Java源代码中floatToIntBits,intBitsToFloat的源代码? java网页抓取 关于do while和switch的连用
大神快来啊、、、、、
public class Test {
static class GameCard {
//扑克牌点数,这里偷懒了,更严谨一点的处理,可以采用枚举类
private static final String[] FACES = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//花色,这里也偷懒了
private static final String[] SUITS = {"Spade", "Heart", "Club", "Diamond"}; private int face;
private int suit;
public GameCard(int face, int suit) {
this.face = face;
this.suit = suit;
} public String getFace() {
return FACES[face];
} public char getSuit() {
return SUITS[suit].charAt(0);
} public String toString() { //根据要求这里直接返回点数(正常应该花色和点数一起返回)
return String.format("%s", FACES[face]);
} public boolean equals(Object obj) { //这里直接比较点数,偷懒了,为了集合查找方便
//一般可以采用Comparator接口来处理两张牌的比较
if (obj!=null && obj.getClass() == this.getClass()) {
return this.face == ((GameCard)obj).face;
} return false;
} public int hashCode() { //这里也是偷懒了,直接返回点数,为了集合查找方便
return this.face;
}
} public static void main(String[] args) throws Throwable {
List<GameCard> poker = new LinkedList<GameCard>();
for (int i=0; i<4; i++) { //把52张牌保存到集合中
for (int j=0; j<13; j++) {
poker.add(new GameCard(j, i));
}
} Collections.shuffle(poker); //洗牌
List<List<GameCard>> player = new ArrayList<List<GameCard>>();
for (int i=0; i<2; i++) { //定义2个玩家
player.add(new LinkedList<GameCard>());
} for (int i=0; i<52; i++) { //给每个玩家发牌
player.get(i%2).add(poker.remove(0));
} int order = 0; //玩家出牌顺序,一个出玩就轮到另一个
//玩家手中还有牌就一直循环
while (player.get(0).size() > 0 && player.get(1).size() > 0) {
GameCard card = player.get(order).remove(0); //玩家出牌
System.out.printf("-%s", card.toString()); //打印所出过的牌
int idx = poker.lastIndexOf(card); //查找出过的牌中是否有相同点数
poker.add(card); //把玩家出过的牌保存到集合中
if (card.getFace().equals("J")) { //如果玩家出的J
System.out.println();
player.get(order).addAll(poker); //玩家获得所有台面的牌
poker.clear();//清空台面
} else if (idx >= 0) { //如果找到有相同点数的牌
System.out.println();
List<GameCard> sub = new ArrayList<GameCard>(poker.subList(idx, poker.size()));
player.get(order).addAll(sub);//玩家获得相同点数之间的所有牌
poker.removeAll(sub);//台面牌数整理
for (GameCard gc : poker) {//台面剩余牌数继续表示
System.out.printf("-%s", gc);
}
} try {
Thread.sleep(1000); //为了模拟出牌,这里减缓时间输出
} catch (Exception e) {
e.printStackTrace();
} order = (order+1)%2;//玩家出牌轮换
} //打印赢家
System.out.printf("\nwiner is player[%d]\n", (player.get(0).size()>0 ? 0 : 1));
}
}
你要分成几个类,就把上面的一些共同部分抽出来就可以了
比如
player和poker都是card的持有者,可以把它们相同的部分抽象出来
for example LZ自己参考发挥吧
class Card {
private int suit; //花色
private int face; //点数
public Card(int suit, int face) { } ...
}abstract class CardHolder {
protected Card[] cards[]; //牌
protecte int current = 0; //当前持有牌数,就是数组元素的当前位置
protected int count = 0; //最大持有牌数,就是数组的最大边界 public CardHolder(int count) {
if (count < 0) {
cards = new Card[0];
this.count = 0;
} else {
cards = new Card[count];
this.count = count;
}
} public void shuffle() { //洗牌 } public void sort() { //整理牌(也就是排序,默认规则排序) } public void sort(Comparator<Card> comp) { //可以由用户按定义的规则排序 } public draw(Card card) { //摸牌 } public Card discard() { //出牌 } public int getCardCount() { //剩余牌数 } public int find(Card card) { //找牌 } public void showCards() {
for (int i=0; i<cards.current; i++) {
System.out.printf("-%s", cards[i].toString());
}
}
}class Player extends CardHolder { //玩家
public Player(int count) {
super(count);
}
}class Poker extends CardHolder {
public static final int Card_Count = 52;
public static fianl String[] Card_Face = {...};
public static final String[] Card_Suit = {...}; public Poker() {
super(Card_Count);
for (int i=0; i<4; i++) {
for (int j=0; j<13; j++) {
cards[current++] = new Card(i, j);
}
}
} public void showCards() { //重写showCards,只显示点数 }
}public class Game {
public static void main(String[] args) throws Throwable {
Poker poker = new Poker(); //生成扑克牌实例
Player[] player = new Player[2];
for (int i=0; i<2; i++) { //生成玩家实例
player[i] = new Player(Poker.Card_Count); //每个玩家最多持有52张牌
} poker.shuffle(); //洗牌
int order = (int)(Math.random()*100) % 2; //随机让玩家先摸牌
while (poker.getCardCount() > 0) {
player[order].draw(poker.discard()); //玩家摸牌
order = (order + 1) % 2; //玩家轮换
} //玩家手中还持有牌的话一直循环
order = (int)(Math.random()*100) % 2; //随机让玩家先出牌
while (player[0].getCardCount() > 0 &&
player[1].getCardCount() > 0) {
//此处基本和上面相同,多了一个J是第一个不收牌判断
Card card = player[order].discard();
System.out.printf("-%s", Poker.Card_Face[card.getFace()]);
int idx = poker.find(card); //查找是否有相同点数的牌
poker.draw(card); //玩家出的牌放置到台面上
if (card.getFace() == 11 && poker.getCardCount() > 1) {
idx = 0; //如果玩家出的是J并且不是第一张,则台面牌全部拿回,所以从0位置全拿
} if (idx >= 0) { //如果玩家可以拿牌
//从poker取出从idx位置到current位置的牌放到palyer[order]的手上
//提示,可以循环player[order].draw(poker.discard());
} order = (order+1) % 2; //玩家出牌轮换
} System.out.printf("\nwinner is player[%d]\n", (player[0].getCardCount() > 0 ? 0 : 1)); //打印赢家
}
}
http://www.cnblogs.com/CloudTeng/archive/2012/03/24/2379699.html