package ifinal;
import java.util.ArrayList;
public class player {
int id;
String name;
ArrayList<card> hand=new ArrayList<card>();
public player(){
}
public player(int id, String name,card hand){
this.id=id;
this.name=name;
this.hand.add(hand);
}
}package ifinal;
public class card {
int num;
String color;
int colornum;
public card(String color,int num){
this.color=color;
this.num=num;
if(this.color.equals("黑桃")){this.colornum=4;}
if(this.color.equals("红桃")){this.colornum=3;}
if(this.color.equals("梅花")){this.colornum=2;}
if(this.color.equals("方块")){this.colornum=1;}
}
public card(){
}
public card compare(card obj){
if( this.num > obj.num){
return this;
}
if(this.num< obj.num){
return obj;
}
if(this.num==obj.num){
if(this.colornum>obj.colornum){
return this;
}
if(this.colornum<obj.colornum){
return obj;
}
}
return null;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + colornum;
result = prime * result + num;
return result;
}
@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 (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (colornum != other.colornum)
return false;
if (num != other.num)
return false;
return true;
}
}package ifinal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class initial {
ArrayList<card> possible=new ArrayList<card>();
player a=new player();
player b=new player();
public void create(){
System.out.println("-------创建扑克牌--------");
for (int i=1; i<=13;i++){
card temp=new card();
temp.color="黑桃";
temp.num=i;
possible.add(temp);
}
for (int i=1; i<=13;i++){
card temp=new card();
temp.color="梅花";
temp.num=i;
possible.add(temp);
}
for (int i=1; i<=13;i++){
card temp=new card();
temp.color="红桃";
temp.num=i;
possible.add(temp);
}
for (int i=1; i<=13;i++){
card temp=new card();
temp.color="方块";
temp.num=i;
possible.add(temp);
}
System.out.println("-------扑克牌创建完成--------");
}
public void test(){
for(int i=0; i<=possible.size()-1;i++){
System.out.print(possible.get(i).color+possible.get(i).num);
}
System.out.println();
System.out.println("共有"+possible.size()+"张牌");
}
public void shuffle(){
System.out.println("------开始洗牌----------");
Collections.shuffle(possible);
System.out.println("------洗牌结束----------");
}
public void newplayer(){
Scanner s=new Scanner(System.in);
System.out.println("请输入玩家1的id:");
int playerid1= s.nextInt();
a.id=playerid1;
System.out.println("请输入玩家1的姓名:");
String playername1= s.next();
a.name=playername1;
System.out.println("请输入玩家2的id:");
int playerid2= s.nextInt();
b.id=playerid2;
System.out.println("请输入玩家2的姓名:");
String playername2= s.next();
b.name=playername2;
}
public void distribute(){
System.out.println("---------开始发牌-----------");
System.out.println("玩家---"+a.name+"---拿牌");
a.hand.add(possible.get(0));
System.out.println("玩家---"+b.name+"---拿牌");
b.hand.add(possible.get(1));
System.out.println("玩家---"+a.name+"---拿牌");
a.hand.add(possible.get(2));
System.out.println("玩家---"+b.name+"---拿牌");
b.hand.add(possible.get(3));
System.out.println("---------发牌结束-----------");
}
public void start(){
System.out.println("---------开始游戏...-----------");
card Ace1=a.hand.get(0).compare(a.hand.get(1));
card Ace2=b.hand.get(0).compare(b.hand.get(1));
card Ace=Ace1.compare(Ace2);
System.out.println("玩家:"+a.name+"的最大手牌为:"+Ace1.color+Ace1.num);
System.out.println("玩家:"+b.name+"的最大手牌为:"+Ace2.color+Ace2.num);
if(Ace.equals(Ace1)){String winner=a.name;
System.out.println("-------玩家:"+winner+"获胜------------");}
if(Ace.equals(Ace2)){String winner=b.name;
System.out.println("-------玩家:"+winner+"获胜------------");}
}
public void showhand(){
System.out.println("玩家各自手牌为:");
System.out.println(a.name+":["+a.hand.get(0).color+a.hand.get(0).num+a.hand.get(1).color+a.hand.get(1).num+"]");
System.out.println(b.name+":["+b.hand.get(0).color+b.hand.get(0).num+b.hand.get(1).color+b.hand.get(1).num+"]");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
initial I=new initial();
I.create();
I.test();
I.shuffle();
I.newplayer();
I.distribute();
I.showhand();
I.start();
}
}
添加回答
举报
0/150
提交
取消