Cothrax
2015-02-12 20:24
不能超过两万字!! <- 评论继续贴
先贴截图:


附代码:
Card.java //扑克牌类
package com.playcards;
/**
* 扑克牌类<br>
* 实现Comparable
*
* @author Cothrax
*
*/
public class Card implements Comparable<Card> {
private Integer type;
private Integer num;
/**
* Crad的无参构造方法
*/
public Card() {
}
/**
* Card的有参构造方法
*
* @param type
* 指牌的花色:黑红梅方<br>
* type = 1; ==> 方片<br>
* type = 2; ==> 梅花<br>
* type = 3; ==> 红心<br>
* type = 4; ==> 黑桃<br>
* @param num
* 指牌的数值:1-13
* @exception Card type在1-4之间
* @exception Card num在1-13之间
*/
public Card(Integer type, Integer num) {
if (type > 4 ^ type < 1) {
throw new IllegalArgumentException("type在1-4之间");
}
if (num < 1 ^ num > 13) {
throw new IllegalArgumentException("num在1-13之间");
}
this.type = type;
this.num = num;
}
/**
* 获取花色的方法
* @return 花色
*/
public int getType() {
return type;
}
/**
* 设置花色的方法
* @param type 新的花色
* @exception Card type在1-4之间
*/
public void setType(int type) {
if (type > 4 ^ type < 1) {
throw new IllegalArgumentException("type在1-4之间");
}
this.type = type;
}
/**
* 获取牌的值的方法
* @return 牌的值
*/
public int getNum() {
return num;
}
/**
* 设置牌的值的方法
* @param num 新的牌值
* @exception Card num在1-13之间
*/
public void setNum(int num) {
if (num < 1 ^ num > 13) {
throw new IllegalArgumentException("num在1-13之间");
}
this.num = num;
}
/**
* 重写的hashCode方法
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + num;
result = prime * result + type;
return result;
}
/**
* 重写的equals方法
* @param obj 与之比较的对象
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (num != other.num)
return false;
if (type != other.type)
return false;
return true;
}
/**
* Card转String方法
*
* @return 牌字符串(类型+值)
*/
@Override
public String toString() {
String typeString = "";
switch (type) {
case 1:
typeString = "方片";
break;
case 2:
typeString = "梅花";
break;
case 3:
typeString = "红心";
break;
case 4:
typeString = "黑桃";
break;
default:
break;
}
String numString;
switch (num) {
case 1:
numString = "A";
break;
case 11:
numString = "J";
break;
case 12:
numString = "Q";
break;
case 13:
numString = "K";
break;
default:
numString = String.valueOf(num);
break;
}
return typeString + numString;
}
@Override
public int compareTo(Card arg0) {
// TODO Auto-generated method stub
if (this.type == arg0.type) {
return this.num.compareTo(arg0.num);
} else {
return this.type.compareTo(arg0.type);
}
}
}Player.java //玩家类
package com.playcards;
import java.util.Arrays;
/**
* 玩家类
* @author Cothrax
*
*/
public class Player {
private int id;
private String name;
private Card[] cards;
/**
* Player的无参构造方法
*/
public Player() {
}
/**
* Player的有参构造方法
* @param id 玩家id
* @param name 玩家名称
*/
public Player(int id, String name) {
this.id = id;
this.name = name;
}
/**
* Player的有参构造方法
* @param id 玩家id
* @param name 玩家名称
* @param cards 玩家手中的牌
*
* @exception Player 一个玩家手中只能有两张牌!
*/
public Player(int id, String name, Card[] cards) {
this.id = id;
this.name = name;
if (cards.length != 2) {
throw new IllegalArgumentException("一个玩家手中只能有两张牌!");
}
this.cards = cards;
}
/**
* 获取玩家id
* @return 玩家id
*/
public int getId() {
return id;
}
/**
* 设置玩家id
* @param id 新的玩家id
*/
public void setId(int id) {
this.id = id;
}
/**
* 获取玩家名称
* @return 玩家名称
*/
public String getName() {
return name;
}
/**
* 设置玩家名称
* @param name 新的玩家名称
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取玩家手中的牌
* @return 玩家手中的牌
*/
public Card[] getCards() {
return cards;
}
/**
* 设置玩家手中的牌
* @param cards 新的手牌
* @exception Player 一个玩家手中只能有两张牌!
*/
public void setCards(Card[] cards) {
if (cards.length != 2) {
throw new IllegalArgumentException("一个玩家手中只能有两张牌!");
}
this.cards = cards;
}
/**
* 重写的hashCode方法
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(cards);
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
/**
* 重写的eqauls方法
* @param obj 与之比较的对象
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Player other = (Player) obj;
if (!Arrays.equals(cards, other.cards))
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
/**
* 返回玩家字符串的方法
* @return 玩家字符串(id+名称+手牌)
*
*/
@Override
public String toString() {
return id + "号玩家" + name + ":" + Arrays.toString(cards);
}
}未完
不能超过两万字!! <- 评论继续贴

有错误啊??
继续贴...
Main.java //测试类
package com.playcards;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("————————————创建扑克牌...——————————————");
ArrayList<Card> cards = DoCards.createCards();
System.out.println("——————————成功创建扑克牌!—————————————");
System.out.println("——————————————创建玩家...——————————————");
ArrayList<Player> players = DoCards.createPlayer();
System.out.println("—————————————创建成功!————————————————");
int times = 1;
int winner = 0;
while (true) {
System.out.println("—————————————洗牌中...—————————————————");
cards = DoCards.randomCards(cards);
System.out.println("—————————————成功洗牌!————————————————");
System.out.println("——————————————发牌...——————————————————");
players = DoCards.handCards(players, cards);
System.out.println("—————————————成功发牌!————————————————");
System.out.println("———————————游戏开始-第"+times+"局—————————————————");
ArrayList<Card> maxCards = DoCards.playCards(players);
int isWin = DoCards.whoWin(maxCards);
System.out.println(players.get(0).getName() + "出牌: " + maxCards.get(0));
System.out.println(players.get(1).getName() + "出牌: " + maxCards.get(1));
if (isWin > 0) {
System.out.println(players.get(0).getName() + "赢!");
winner++;
} else if(isWin == 0) {
System.out.println("平局==");
} else {
System.out.println(players.get(1).getName() + "赢!");
winner--;
}
System.out.println("————————————列出玩家手牌——————————————————");
for (int i = 0; i < players.size(); i++) {
System.out.println(players.get(i));
}
Scanner console = new Scanner(System.in);
System.out.println("——————————————第"+times+"局结束!——————————————————");
System.out.print("是否在来一局?(yes/no)");
String isGoOn = console.next();
if(isGoOn.toLowerCase().equals("yes")) {
System.out.println("再来一局!:)");
times++;
continue;
} else if (isGoOn.toLowerCase().equals("no")) {
System.out.println("——————————————大结局————————————————————");
if (winner>0) {
System.out.println("最终"+players.get(0).getName()+"胜利!O(∩_∩)O");
} else if(winner<0) {
System.out.println("最终"+players.get(1).getName()+"胜利!(^o^)/~");
} else {
System.out.println("最终,平局!-_-!");
}
System.out.println("已退出!欢迎再来!( ^_^ )/~~");
break;
} else {
System.out.println("输入有误!╭∩╮(︶︿︶)╭∩╮~~退出!");
break;
}
}
}
}完毕。
继续贴
DoCards.java //将创建牌、创建玩家、洗牌、发牌、玩牌方法放在一起
package com.playcards;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
/**
* Card工具类(也含有Player相关)
* @author Cothrax
*
*/
public class DoCards {
/**
* 创建一副扑克牌的方法
* @return 一副有序的扑克牌
*/
public static ArrayList<Card> createCards() {
ArrayList<Card> cards = new ArrayList<Card>();
for (int i = 1; i < 5; i++) {
Integer type = i;
Integer num = 1;
while (num < 14) {
Card temp = new Card(type, num);
cards.add(temp);
num++;
}
}
return cards;
}
/**
* 洗牌的方法
* @param originalList 原始的牌
* @return 打乱的牌
*/
public static ArrayList<Card> randomCards(ArrayList<Card> originalList) {
ArrayList<Card> temp = originalList;
ArrayList<Card> newList = new ArrayList<Card>();
int bound = originalList.size();
Random random = new Random();
while(temp.size() != 0) {
int index = random.nextInt(bound);
newList.add(originalList.get(index));
originalList.remove(index);
bound--;
}
return newList;
}
/**
* 发牌的方法
* @param players 玩家序列
* @param cards 未发的牌的序列
* @return 发过牌的玩家的序列
* @exception DoCards 玩家应该有两人!
* @exception DoCards 牌不够了!
*/
public static ArrayList<Player> handCards(ArrayList<Player> players, ArrayList<Card> cards) {
if (players.size() != 2) {
throw new IllegalArgumentException("玩家应该有两人!");
}
if (cards.size() < 4) {
throw new IllegalArgumentException("牌不够了!(" + cards.size() + ")");
}
Card[] cards1 = {cards.get(0), cards.get(2)};
Card[] cards2 = {cards.get(1), cards.get(3)};
ArrayList<Player> newPlayers = players;
newPlayers.get(0).setCards(cards1);
newPlayers.get(1).setCards(cards2);
return newPlayers;
}
/**
* 玩牌的方法
* @param players 玩家的序列
* @return 最大牌的序列(玩家1的最大牌,玩家2的最大牌)
* @exception DoCards 玩家应该有两人!
* @exception 玩家的手牌数不对~~!
*/
public static ArrayList<Card> playCards(ArrayList<Player> players) {
if (players.size() != 2) {
throw new IllegalArgumentException("玩家应该有两人!");
}
if (players.get(0).getCards().length != 2 ^
players.get(1).getCards().length != 2) {
throw new IllegalArgumentException("玩家的手牌数不对~~!");
}
Card[] cards1 = players.get(0).getCards();
Card[] cards2 = players.get(1).getCards();
Arrays.sort(cards1);
Arrays.sort(cards2);
ArrayList<Card> maxCards = new ArrayList<Card>();
maxCards.add(cards1[1]);
maxCards.add(cards2[1]);
return maxCards;
}
/**
* 判断胜负的方法
* @param maxCards 最大牌序列
* @return 哪个玩家胜<br>
* 大于0:玩家1胜<br>
* 等于0:平局<br>
* 小于0:玩家2胜<br>
* @exception DoCards 参数非法!
*/
public static int whoWin(ArrayList<Card> maxCards) {
if (maxCards.size()!=2) {
throw new IllegalArgumentException("参数非法!");
}
return maxCards.get(0).compareTo(maxCards.get(1));
}
/**
* 创建玩家的方法
* @return 玩家序列
*/
public static ArrayList<Player> createPlayer() {
ArrayList<Player> al = new ArrayList<Player>();
Scanner console = new Scanner(System.in);
for (int i = 1; i < 3; i++) {
System.out.println("玩家"+i+":");
Integer id = 0;
while (true) {
try {
System.out.print("玩家"+i+"的id:");
String idStr = console.next();
id = Integer.valueOf(idStr);
if (i == 2 && id == al.get(0).getId()) {
System.out.println("id重复!");
continue;
}
break;
} catch (NumberFormatException e) {
// TODO: handle exception
System.out.println("id应该是数字!");
continue;
}
}
String name = "";
while (true) {
System.out.print("输入玩家"+i+"的名称:");
name = console.next();
if (i == 2 && name.equals(al.get(0).getName())) {
System.out.println("名字重复!");
continue;
}
break;
}
Player p = new Player(id, name);
al.add(p);
}
return al;
}
}未完 ———— 不能超过20000字
Java入门第三季
409776 学习 · 4546 问题
相似问题