创建两个类一个是Card(手牌),一个player(玩家);
Card类中继承了一个接口Comparator,用于定义后面系统方法设置中引用Collections类中方法的比较规则;
package homework;
import java.util.Comparator;
public class Card implements Comparator<Card> {
private String colour;
private String number;
/*
* 封装属性
*/
public String getColour(){
return colour;
}
public void setColour(String colour){
this.colour=colour;
}
public String getNumber(){
return number;
}
public void setNumber(String number){
this.number=number;
}
/*
* 创建Player的构造方法
*/
Card(String colour,String number2){
this.setColour(colour);
this.setNumber(number2);
}
/*
* 定义compare方法(比较手牌大小的规则)
*/
public int compare(Card o1, Card o2) {
String rank1 = null;
String rank2 = null;
String[] numberRule={"A","K","Q","J","10","9","8","7","6","5","4","3","2"};//通过数字在数组中的顺序,来设置比较规则,即第0位的"A"比第5位的"10"大
for(int i=1;i<numberRule.length;i++){
if(numberRule[i]==o1.getNumber()){
rank1=String.valueOf(i);
}
if(numberRule[i]==o2.getNumber()){
rank2=String.valueOf(i);
}
}
if(o1.getNumber()!=o2.getNumber()){
return -rank1.compareTo(rank2);//因为位数是从小到大排列,所以最终取值需要通过“-”来纠正
}else {
String[] colourRule={"黑桃","红心","梅花","方块"};//通过花色在数组中的顺序,来设置比较规则,即第0位的黑桃比第1位的红心大
for(int i=1;i<colourRule.length;i++){
if(colourRule[i]==o1.getColour()){
rank1=String.valueOf(i);
}
if(colourRule[i]==o2.getColour()){
rank2=String.valueOf(i);
}
}
return -rank1.compareTo(rank2);
}
}
}
Player类:
package homework;
import java.util.ArrayList;
import java.util.List;
public class Player{
private int id;
private String name;
List<Card>playerCards=new ArrayList<Card>();//定义集合框架(玩家的手牌),这个游戏中总的扑克牌为一个集合框架,然后每个玩家手上都有一副手牌为另一个集合框架;
/*
* 封装属性
*/
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;
}
/*
* 创建Player的构造方法
*/
Player(int id,String name){
this.setId(id);
this.setName(name);
}
}
主系统Game类:
package homework;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
public class Game {
/*
* 初始化输入对象
*/
Scanner input=new Scanner(System.in);
/*
* 定义花色和数字并创建两个Player对象
*/
String[] colour={"方块","梅花","红心","黑桃"};
String[] number={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
Player player1;
Player player2;
Card temp;//创建Card对象temp,用于临时储存一些中间对象,过渡用
/*
* 创建扑克牌
*/
public List<Card>cards=new ArrayList<Card>();
void creatCards(){
System.out.println("-----------创建扑克牌-----------");
for(int i=0;i<colour.length;i++){
for(int j=0;j<number.length;j++){
cards.add(new Card(colour[i],number[j]));
}
}
System.out.println("-----------扑克牌创建成功!-----------");
}
/*
* 遍历显示所有扑克牌
*/
void showCards(){
System.out.print("为:[");
for(int i=0;i<cards.size();i++){
Card temp=(Card)cards.get(i);
System.out.print(temp.getColour()+temp.getNumber());
if(i<(cards.size()-1)){
System.out.print(",");
}else{
System.out.println("]");
}
}
}
/*
* 洗牌
*/
void shuffle(){
System.out.println("-----------洗牌开始...-----------");
Collections.shuffle(cards);//引用Collections类中的shuffe方法打乱顺序
System.out.println("-----------洗牌结束!-----------");
}
/*
* 输入玩家以及给玩家发牌
*/
void playerIn(){
int id = 0;//用于临时存放输入的id
String name;//用于临时存放输入的name
System.out.println("请输入第1位玩家的ID和姓名:");
while(true){
try{
System.out.println("输入ID:");
id=input.nextInt();
break;
}catch(InputMismatchException e){
System.out.println("请输入整数类型的ID!");
input=new Scanner(System.in);//重置input(不重置的话第一次的输入值会一直存在input中,系统就会不断报错,while就会无限死循环
}
}
System.out.println("输入姓名:");
name=input.next();
player1=new Player(id,name);
System.out.println("请输入第2位玩家的ID和姓名:");
while(true){
try{
System.out.println("输入ID:");
id=input.nextInt();
break;
}catch(Exception e){
System.out.println("请输入整数类型的ID!");
input=new Scanner(System.in);
}
}
System.out.println("输入姓名:");
name=input.next();
player2=new Player(id,name);
System.out.println("欢迎玩家:"+player1.getName()+"\n"+"欢迎玩家:"+player2.getName());
System.out.println("-----------开始发牌...-----------");
System.out.println("玩家:"+player1.getName()+"-拿牌");
player1.playerCards.add(cards.get(0));
System.out.println("玩家:"+player2.getName()+"-拿牌");
player2.playerCards.add(cards.get(1));
System.out.println("玩家:"+player1.getName()+"-拿牌");
player1.playerCards.add(cards.get(2));
System.out.println("玩家:"+player2.getName()+"-拿牌");
player2.playerCards.add(cards.get(3));
System.out.println("-----------发牌结束!-----------");
}
/*
* 比较大小(利用了Collection类中的max方法)
*/
void compareCards(){
List<Card>cardsCompare=new ArrayList<Card>();//用于存放两个玩家的最大手牌,然后再进行比较;
temp=null;//清空temp(不知道为什么不清空的话就会报错,但是在其他方法里不清空也可以用,如果有大神知道原因的,帮我解释一下)
temp= Collections.max(player1.playerCards,new Card(null, null));
System.out.println("玩家"+player1.getName()+"的最大手牌为:"
+temp.getColour()+temp.getNumber());
cardsCompare.add(temp);
temp= Collections.max(player2.playerCards,new Card(null, null));
System.out.println("玩家"+player2.getName()+"的最大手牌为:"
+temp.getColour()+temp.getNumber());
cardsCompare.add(temp);
temp= Collections.max(cardsCompare,new Card(null, null));
if(player1.playerCards.contains(temp)){
System.out.println("-----------玩家"+player1.getName()+"获胜-----------");
}else{
System.out.println("-----------玩家"+player2.getName()+"获胜-----------");
}
}
/*
* 显示玩家各自手牌
*/
void showPlayerCards(){
System.out.print("玩家各自的手牌为:"+"\n"
+player1.getName()+":[");
for(int i=0;i<player1.playerCards.size();i++){
temp=player1.playerCards.get(i);//temp临时储存对象(也可以不用),这样可以使代码不会显得冗长
System.out.print(temp.getColour()+temp.getNumber());
if(i<(player1.playerCards.size()-1)){
System.out.print(",");
}else{
System.out.println("]");
}
}
System.out.print(player2.getName()+":[");
for(int i=0;i<player2.playerCards.size();i++){
temp=player2.playerCards.get(i);
System.out.print(temp.getColour()+temp.getNumber());
if(i<(player2.playerCards.size()-1)){
System.out.print(",");
}else{
System.out.println("]");
}
}
}
public static void main(String[] args) {
Game gs=new Game();
gs.creatCards();
gs.showCards();
gs.shuffle();
gs.playerIn();
gs.compareCards();
gs.showPlayerCards();
}
}
热门评论
用collections的max方法 这点不错