package com.java1996;
/**
- 主程序
- @author HBX
-
*/
public class GameStart {public static void main(String[] args){
Game game=new Game();
game.cardCreat();
game.gameRunning();
game.cardSend();}
}
package com.java1996;
/**
- 卡牌类
- @author HBX
-
*/
public class Cards {private String cardNum;//牌
private String color;//花色public Cards(String color, String cardNum){
this.cardNum=cardNum;
this.color=color;}
public String getCardNum() {
return cardNum;
}public void setCardNum(String cardNum) {
this.cardNum = cardNum;
}public String getColor() {
return color;
}public void setColor(String color) {
this.color = color;
}@Override
public String toString() {
return color+cardNum;
}
}
package com.java1996;
import java.util.ArrayList;
import java.util.List;
/**
- 玩家类
- @author HBX
-
*/
public class Player {private int id;//玩家ID
private String name;//玩家名字
private List<Cards> cardPlayer;//玩家手牌public Player(int id,String name){
this.id=id;
this.name=name;
this.cardPlayer=new ArrayList<Cards>();
}public Player(){
//构造时候new一个ArrayList实例
this.cardPlayer=new ArrayList<Cards>();
}public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public String getName() {
return name;
}public void setName(String name) {
this.name = name;
}public List<Cards> getCardPlayer() {
return cardPlayer;
}public void setCardPlayer(List<Cards> cardPlayer) {
this.cardPlayer = cardPlayer;
}
}
package com.java1996;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
/**
- 游戏类
- @author HBX
-
*/
public class Game {Scanner sc=new Scanner(System.in);
//创建两名玩家对象
Player p1=new Player();
Player p2=new Player();
List<Cards> maxCards=new ArrayList<Cards>();//用来比较两名玩家最大的牌
List<Cards> allCards=new ArrayList<Cards>();//全部的牌,创建的扑克牌
String[] color={"黑桃","红桃","梅花","方块"};
String[] cardNum={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};/**
-
创建扑克牌
*/
public void cardCreat(){System.out.println("---------------- 创建扑克牌-------------------------");
System.out.println("---------------- loading---------------------------");
for(String str:color){
for(String str1:cardNum){
//因为重写了toString方法添加之后以字符串显示
allCards.add(new Cards(str,str1));
}
}System.out.println("----------------创建扑克牌成功------------------------");
System.out.println("----------------扑克牌为下----------------------------");
System.out.println(allCards);
System.out.println("-----------------开始洗牌------------------------------");
for(int i=0;i<3;i++){
//多洗几次
Collections.shuffle(allCards);
}
System.out.println("-----------------洗牌结束------------------------------");
}
/**
- 游戏开始
*/
public void gameRunning(){
/** -
创建玩家
*/for(int i=0;i<2;i++){
System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名:");
System.out.println("输入ID:");
if(i==0){
while(true){
try{
p1.setId(sc.nextInt());
break;
}catch(InputMismatchException e){
System.out.println("老哥,请输入数字");
sc.next();
}
}
System.out.println("输入姓名:");
p1.setName(sc.next());
}else{
p2.setId(sc.nextInt());
System.out.println("输入姓名:");
p2.setName(sc.next());
}}
System.out.println("----欢迎玩家:"+p1.getName());
System.out.println("----欢迎玩家:"+p2.getName());
}
/**
-
开始发牌
*/
public void cardSend(){
System.out.println("-------------------开始发牌-------------------------");p1.getCardPlayer().add(allCards.get(0));//给p1玩家发牌
p1.getCardPlayer().add(allCards.get(10));
System.out.println("玩家:"+p1.getName()+"-拿牌");p2.getCardPlayer().add(allCards.get(1));//给p2玩家发牌
p2.getCardPlayer().add(allCards.get(9));
System.out.println("玩家:"+p2.getName()+"-拿牌");p1.getCardPlayer().add(allCards.get(2));
System.out.println("玩家:"+p1.getName()+"-拿牌");
p2.getCardPlayer().add(allCards.get(3));
System.out.println("玩家:"+p2.getName()+"-拿牌");
System.out.println("--------------------发牌结束-----------------------");
//现在发牌结束 两名玩家都有各自的手牌集合
//把两个人的牌加入总的比较牌
maxCards.addAll(p1.getCardPlayer());
maxCards.addAll(p2.getCardPlayer());System.out.println(p1.getName()+":"+p1.getCardPlayer());
System.out.println(p2.getName()+":"+p2.getCardPlayer());
//排序了,把大的牌牌到最后
Collections.sort(p1.getCardPlayer(),new Mycomparator());
Collections.sort(p2.getCardPlayer(), new Mycomparator());
//这是保存两个玩家所有手牌的集合,也排序下
Collections.sort(maxCards, new Mycomparator());System.out.println("玩家:"+p1.getName()+"的最大手牌是:"+p1.getCardPlayer().get(2));
System.out.println("玩家:"+p2.getName()+"的最大手牌是:"+p2.getCardPlayer().get(2));
System.out.println("-----------------玩家"+maxCards()+"获胜------------------------");System.out.println("玩家各自的手牌为:");
System.out.println(p1.getName()+":"+p1.getCardPlayer());
System.out.println(p2.getName()+":"+p2.getCardPlayer());
}
public String maxCards(){
//判断如果maxCards所有玩家牌的集合里最大的一张
//等于玩家最大的那一张则返回该玩家
if(maxCards.get(maxCards.size()-1)==p1.getCardPlayer().get(2)){
return p1.getName();
}else{
return p2.getName();
}}
-
}
package com.java1996;
import java.util.Comparator;
/**
- 这个排序方法 参考幕友的
*非常感谢 - @author HBX
-
*/
public class Mycomparator implements Comparator<Cards>{@Override
public int compare(Cards o1, Cards o2) {
//自定义了一个排序方法 按照下标排序
String color = "黑桃 红桃 梅花 方块";
String point = "2 3 4 5 6 7 8 9 10 J Q K A";
//如果o1-o2大于0,说明o1比o2打 返回1,排在后面
int result= point.indexOf(o1.getCardNum())-point.indexOf(o2.getCardNum());
if(result>0){
return 1;
}else if(result<0){
return -1;
}else{
//因为color数组是从大到小排这里要倒过来
int result1=color.indexOf(o1.getColor())-color.indexOf(o2.getColor());
if(result1>0){
return -1;
}else{
return 1;
}
}}
}