自己写加参考,解决了抛异常只抛一次的问题 ,使程序更加完整
卡片类
package practice;
public class Card {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((count == null) ? 0 : count.hashCode());
result = prime * result + ((kInd == null) ? 0 : kInd.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Card))
return false;
Card other = (Card) obj;
if (count == null) {
if (other.count != null)
return false;
} else if (!count.equals(other.count))
return false;
if (kInd == null) {
if (other.kInd != null)
return false;
} else if (!kInd.equals(other.kInd))
return false;
return true;
}
public String kInd;
public String count;
public Card(String kInd,String count){
this.kInd = kInd;
this.count = count;
}
@Override
public String toString() {
return kInd + count;
}
}
玩家类
package practice;
import java.util.ArrayList;
import java.util.List;
public class Player {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Player))
return false;
Player other = (Player) obj;
if (id != other.id)
return false;
return true;
}
public int id;
public String name;
List<Card> pcard = new ArrayList<Card>();
public Player(int id,String name){
this.id = id;
this.name = name;
this.pcard = new ArrayList<Card>();
}
public Player() {
// TODO Auto-generated constructor stub
}
}
扑克比较大小
package practice;
import java.util.Comparator;
public class CardCompatator implements Comparator<Card> {
@Override
public int compare(Card o1, Card o2) {
// TODO Auto-generated method stub
//创建从小到大的花色与字符串
String[] a = {"方片","梅花","红桃","黑桃"};
String b = "2345678910JQKA";
//查找字符在字符串中的位置
int b1 = b.indexOf(o1.count);
int b2 = b.indexOf(o2.count);
int a1 = 0;
int a2 = 0;
for(int i = 0; i<a.length; i++){
if(o1.count.equals(a[i])){
a1 = a1+1;
}
if(o2.count.equals(a[i])){
a2 = a2+1;![图片描述][1]
}
}
//通过索引位置判断牌面值的大小
if(b1 > b2){
return 1;
}else if(b1 == b2){
if(a1 > a2){
return 1;
}else if(a1 == a2){
return 0;
}else{
return -1;
}
}else{
return -1;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
主类:
package practice;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import com.imooc.Person;
public class GameTest {
//构造两个数组,用于存放花色与数值
String[] kind = {"方片","梅花","红桃","黑桃"};
String[] count = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
//创建一个List存放扑克
List<Card> cards = new ArrayList<Card>();
//创建一个用于存放玩家信息的对象
List<Player> player = new ArrayList<Player>();
//导入Scanner用于输入玩家信息
Scanner console = new Scanner(System.in);
/**
* 通过循环将值存放入List中
*/
public void CreateCard(){
for(int i=0;i<kind.length;i++){
for(int j=0;j<count.length;j++){
//创建一个Card的实例对象
Card card = new Card(kind[i],count[j]);
cards.add(card);
}
}
}
/**
* foreach遍历出所有的扑克牌
* @param args
*/
public void forecchcard(){
System.out.println("------------扑克牌创建成功--------------");
System.out.print("扑克牌为:[");
for(Card cd : cards){
System.out.print( cd.kInd + cd.count + "," );
}
System.out.println("]");
}
/**
* 创建玩家信息
* @param args
*/
public void gameplayer(){
int j = 0;
while(j<2){
System.out.println("请输入第" + (j+1) +"位玩家的ID和姓名");
System.out.println("请输入ID");
int id = 0;
Player person = new Player();
//捕获异常,并处理,当输入为非整数时提示并重新输入
while(true){
try{
id = console.nextInt();
person.id = id;
break;
}catch(Exception e){
// e.printStackTrace();
System.out.println("请输入整型数据ID");
console = new Scanner(System.in);//在抛出异常后console挂,需重新new一个。
}
}
//判断ID值是否重复
if(player.contains(person)){
System.out.println("ID值重复,请重新输入!!!");
continue;
}else{
System.out.println("请输入姓名");
String pname = console.next();
person.name = pname;
player.add(person);
j++;
}
}
for(int i=0;i<2;i++){
System.out.println("-----欢迎玩家" + player.get(i).name );
}
}
/**
* 洗牌
* @param args
*/
public void mixCard(){
System.out.println("--------------开始洗牌---------------");
Collections.shuffle(cards);
System.out.println("--------------洗牌结束---------------");
System.out.print("扑克牌为:[");
for(Card cd : cards){
System.out.print( cd.kInd + cd.count + "," );
}
System.out.println("]");
}
/**
* 发牌并开始游戏
* @param args
*/
public void fCard(){
System.out.println("--------------开始发牌---------------");
// System.out.println("-------玩家" + player.get(0).name + "拿牌");
// Card p1 = cards.get(0);
// //将扑克牌取出分发给玩家
// player.get(0).pcard.add(p1);
// //将已取出的牌从列表中移除
// cards.remove(0);
// System.out.println("-------玩家" + player.get(1).name + "拿牌");
// Card p2 = cards.get(1);
// //将扑克牌取出分发给玩家
// player.get(1).pcard.add(p2);
// //将已取出的牌从列表中移除
// cards.remove(1);
// System.out.println("-------玩家" + player.get(0).name + "拿牌");
// Card p3 = cards.get(2);
// //将扑克牌取出分发给玩家
// player.get(0).pcard.add(p3);
// //将已取出的牌从列表中移除
// cards.remove(2);
// System.out.println("-------玩家" + player.get(1).name + "拿牌");
// Card p4 = cards.get(3);
// //将扑克牌取出分发给玩家
// player.get(1).pcard.add(p4);
// //将已取出的牌从列表中移除
// cards.remove(3);
int j = 0;
for(int i=0;i<4;i++){
System.out.println("-------玩家" + player.get(j).name + "拿牌");
Card p = new Card(cards.get(i).kInd,cards.get(i).count);
//将扑克牌取出分发给玩家
player.get(j).pcard.add(p);
if(j == 0){
j = 1;
}else{
j = 0;
}
}
System.out.println("--------------发牌结束---------------");
System.out.println("--------------开始游戏---------------");
//将玩家手中的进行排序
Collections.sort(player.get(0).pcard,new CardCompatator());
Collections.sort(player.get(1).pcard,new CardCompatator());
//输出玩家手中最大的牌
for(int i = 0;i<2;i++){
System.out.println("玩家:" + player.get(i).name + " " + "最大的牌为:"
+ player.get(i).pcard.get(1));
}
//比较两位玩家手中牌的大小
CardCompatator p = new CardCompatator();
Card a = player.get(0).pcard.get(1);
Card b = player.get(1).pcard.get(1);
int res = p.compare(a, b);
if(res == 1){
System.out.println("--------------玩家"+ player.get(0).name +"获胜---------------");
}else if(res == 0){
System.out.println("--------------玩家平局---------------");
}else{
System.out.println("--------------玩家"+ player.get(1).name +"获胜---------------");
}
System.out.println("----------玩家各自的手牌为:");
System.out.println(player.get(0).name + "[" +player.get(0).pcard.get(0)
+ "," + player.get(0).pcard.get(1) + "]");
System.out.println(player.get(1).name + "[" +player.get(1).pcard.get(0)
+ "," + player.get(1).pcard.get(1) + "]");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
GameTest gt = new GameTest();
System.out.println("--------------创建扑克牌---------------");
gt.CreateCard();
gt.forecchcard();
gt.gameplayer();
gt.mixCard();
gt.fCard();
}
}
点击查看更多内容
13人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦