慕的地3275243
2018-08-22 11:16
package dapai;
//player.java
public class player {
String name;
String ID;
card card1=new card();
card card2=new card();
player(){}
player(String name,String ID){
this.name=name;
this.ID=ID;
}
}
package dapai;
//card.java
import java.util.ArrayList;
import java.util.List;
public class card {
final static List<String> numList=new ArrayList<String>();
final static List<String> colorList=new ArrayList<String>();
public final static String[] aanum=new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
public final static String[] aacolor=new String[]{"♦️","♣️","♥️","♠️"};
String num=new String();
String color=new String();
//分别将对应数字和花色放于两个list中
public static void zhuang(){
for(int i=0;i<13;i++){
numList.add(aanum[i]);
}
for(int i=0;i<4;i++){
colorList.add(aacolor[i]);
}
}
//牌的显示
public static void showCards(){
System.out.println("牌组有:");
for(int i=0;i<4;i++){
//System.out.print(colorList.get(i));
for(int j=0;j<13;j++){
System.out.print(colorList.get(i));
System.out.print(numList.get(j)+",");
}
}
}
//求出较大的牌,因为索引对应着卡牌,所以可以利用比较索引来比较大小
public static card theBig(card card1,card card2){
//先比较数字
if(numList.indexOf(card1.num)>numList.indexOf(card2.num)){
return card1;
}else if(numList.indexOf(card2.num)>numList.indexOf(card1.num)){
return card2;
}
//数字相等时,比较花色
else{
if(colorList.indexOf(card1.color)>colorList.indexOf(card2.color))
{
return card1;
}else
return card2;
}
}
/*@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + ((num == null) ? 0 : num.hashCode());
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 (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
return true;
}
}
package dapai;
//game.java
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class game {
public List<Integer> inList=new ArrayList<Integer>();
static Scanner input=new Scanner(System.in);
static Random random=new Random();
static String name1=new String();
static String name2=new String();
static String ID1=new String();
static String ID2=new String();
public void fapai(player p1,player p2){
inList.clear();
Integer color1,num1,sum1;
System.out.println("发牌开始");
for(int i=0;i<4;i++){
//color*10000+num表示一张牌,可以使每张牌都是不同的
//random产生随机数(不同的),代替发牌过程
do{
color1=random.nextInt(4)*10000;
num1=random.nextInt(12);
sum1=color1+num1;
}while(inList.contains(sum1));
inList.add(sum1);
}
//测试,遍历inList
/*System.out.println("inList内容为:");
for(Integer ig:inList){
System.out.println(ig);
}*/
//将值存入人的手牌中
p1.card1.num=card.aanum[inList.get(0)%10000];
p1.card1.color=card.aacolor[(int)inList.get(0)/10000];
p1.card2.color=card.aacolor[(inList.get(1)/10000)];
p1.card2.num=card.aanum[inList.get(1)%10000];
p2.card1.color=card.aacolor[(inList.get(2)/10000)];
p2.card1.num=card.aanum[inList.get(2)%10000];
p2.card2.color=card.aacolor[(inList.get(3)/10000)];
p2.card2.num=card.aanum[inList.get(3)%10000];
System.out.println("发牌完成");
System.out.println("玩家"+p1.name+"的手牌为-----"+p1.card1.color+p1.card1.num+"和"+p1.card2.color +p1.card2.num);
System.out.println("玩家"+p2.name+"的手牌为-----"+p2.card1.color+p2.card1.num+"和"+p2.card2.color +p2.card2.num);
}
//比较谁的手牌更大,并且输出
public void gamenext(player p1,player p2){
card cardBig1=new card();
card cardBig2=new card();
cardBig1=card.theBig(p1.card1, p1.card2);
System.out.println("玩家"+p1.name+"的大手牌是---"+cardBig1.color+cardBig1.num);
cardBig2=card.theBig(p2.card1, p2.card2);
System.out.println("玩家"+p2.name+"的大手牌是---"+cardBig2.color+cardBig2.num);
if((card.theBig(cardBig1, cardBig2))==cardBig1){
System.out.println("玩家"+p1.name+"赢!!!");
}else{
System.out.println("玩家"+p2.name+"赢!!!");
}
}
//输入ID
public String inputID(){
int a;
System.out.println("请输入ID");
while(true){
try{
a=input.nextInt();
break;
}catch(InputMismatchException e){
System.out.println("输入错误,请重新输入");
//e.printStackTrace();
input.next();
}
}
String s=a+"";
return s;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
game GG=new game();
System.out.println("请输入第一位玩家数据:");
ID1=GG.inputID();
System.out.println("请输入名字");
name1=input.next();
System.out.println("请输入第二位玩家数据:");
ID2=GG.inputID();
System.out.println("请输入名字");;
name2=input.next();
player p1=new player(name1,ID1);
player p2=new player(name2,ID2);
card.zhuang();
card.showCards();
GG.fapai(p1, p2);
GG.gamenext(p1, p2);
}
}
我都不知道咋下手,三季跟着看过来,发现还是没啥改变,自己太笨了啊
Java入门第三季
409792 学习 · 4340 问题
相似问题
回答 2