慕斯7534182
2019-10-14 15:57
好多讲的都没用上,感觉要重新看一边java入门
package com.lv;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Collections;
public class CardGame {
Map<Integer,Cards> puKe;
List<People> gamer;
int[] order=new int[53];
public CardGame() {
puKe=new HashMap<Integer,Cards>();
gamer=new ArrayList<People>();
}
public void cardBuild() {
String type1="梅花";
String type2="方块";
String type3="红心";
String type4="黑桃";
String Num="A 2 3 4 5 6 7 8 9 10 J Q k";
String[] NUM=Num.split(" ");
System.out.println(Num.toString());
int h=0;
for(int i=0;i<13;i++) {
Cards c=new Cards();
c.num=NUM[i];
c.type=type1;
puKe.put(h,c);
Cards a=new Cards();
a.num=NUM[i];
h++;
a.type=type2;
puKe.put(h,a);
Cards b=new Cards();
b.num=NUM[i];
h++;
b.type=type3;
puKe.put(h,b);
Cards d=new Cards();
d.num=NUM[i];
h++;
d.type=type4;
puKe.put(h,d);
h++;
}
/*
* for(int i=0;i<13;i++) { Cards c=new Cards(); c.type=type2; c.num=NUM[i];
*
* puKe.put(i+13,c); }
*
* for(int i=0;i<13;i++) { Cards c=new Cards(); c.type=type3; c.num=NUM[i];
*
* puKe.put(i+26,c); } for(int i=0;i<13;i++) { Cards c=new Cards();
* c.type=type4; c.num=NUM[i];
*
* puKe.put(i+39 ,c); }
*/
}
public void cardShow() {
//通过keyset方法,返回Map中的所有建的值
Set<Integer>KeySet=puKe.keySet();
System.out.println("共有:"+KeySet.size()+"张牌;");
//遍历取得每一个建
for(Integer s:KeySet) {
Cards uu=puKe.get(s);
System.out.println(uu.type+"\t "+uu.num);
}
}
public void cardPeopleShow() {
//通过keyset方法,返回Map中的所有建的值
//Set<String>KeySet=gamer.keySet();
System.out.println("创建"+gamer.size()+"个人物;");
//遍历取得每一个建
for(People s:gamer) {
System.out.println(s.ID+"\t "+s.name);
}
}
public void cardSetPeople() {
Scanner input=new Scanner(System.in);
for(int i=1;i<3;i++) {
People player=new People();
System.out.println("请输入玩家"+i+"的ID:");
boolean r=true;
while(r){
r=false;
String id =input.next();
for(People p:gamer) {
if(p.ID.equals(id))
{
System.out.println("该ID已被使用,请重新输入:");
r=true;
break;
}
}
if(r==false) {
player.ID =id;
}
}
System.out.println("请输入玩家"+i+"的姓名:");
boolean e=true;
while(e){
e=false;
String name =input.next();
for(People p:gamer) {
if(p.name.equals(name))
{
System.out.println("该姓名已被使用,请重新输入:");
e=true;
break;
}
}
if(e==false) {
player.name =name;
}
}
gamer.add(player);
}
}
public Integer[] createSolution2(int len)
{ Integer solutionArr[] = new Integer[len];
List list=new ArrayList<Integer>();
for (int i = 0; i < len; i++)
list.add(i+1);
Collections.shuffle(list);
list.toArray(solutionArr);
return solutionArr;
}
public void cardGet(int i) {
Integer[] num=createSolution2 (53);
int max1,max2;
gamer.get(0).card .add(puKe.get(num[4*i]));
gamer.get(1).card .add(puKe.get(num[4*i+1]));
gamer.get(0).card .add(puKe.get(num[4*i+2]));
gamer.get(1).card .add(puKe.get(num[4*i+3]));
for(int j=0;j<2;j++)
{
System.out.print(gamer.get(j).name +"拿牌\t"+gamer.get(j).card .get(0).type+gamer.get(j).card .get(0).num );
System.out.println("\t"+gamer.get(j).card .get(1).type+gamer.get(j).card .get(1).num );
}
if(num[4*i]<num[4*i+2]) {
System.out.println(gamer.get(0).name +"的最大牌为:"+puKe.get(num[4*i+2]).type +puKe.get(num[4*i+2]).num);
max1=num[4*i+2];
}
else{
System.out.println(gamer.get(0).name +"的最大牌为:"+puKe.get(num[4*i]).type +puKe.get(num[4*i]).num);
max1=num[4*i];
}
if(num[4*i+1]<num[4*i+3]) {
System.out.println(gamer.get(0).name +"的最大牌为:"+puKe.get(num[4*i+3]).type +puKe.get(num[4*i+3]).num);
max2=num[4*i+3];
}
else{
System.out.println(gamer.get(0).name +"的最大牌为:"+puKe.get(num[4*i+1]).type +puKe.get(num[4*i+1]).num);
max2=num[4*i+1];
}
if(max1<max2) {
System.out.println(gamer.get(0).name +"获胜");
}
else {
System.out.println(gamer.get(1).name +"获胜");
}
}
/*
* public void cardComparater() {
*
*
* Collections.sort(car,new CardsComparator()); for(Cards c:car) {
* System.out.println(c.type +c.num +"\t"); } }
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CardGame cg=new CardGame();
cg.cardBuild();
cg.cardShow();
cg.cardSetPeople();
cg.cardPeopleShow();
cg.cardGet(1);
//cg.cardComparater();
}
}
你写的好复杂啊
扑克牌的花色和数字不要用变量存 应该存在字符串数组里面
存的时候可以按花色和数字由小到大的顺序存 这样洗牌的时候可以用random随机产生一个下标 取出这个下标对应的花色(数字同理);比较扑克牌的大小的时候可以用indexOf方法取到花色或数字对应的下标 因为花色和数字在数组里面是按顺序存的 这样比较下标就可以了
秀一下代码
----------------------------------洗牌---------------------------------------
final String[] Types= {"黑桃","红桃","梅花","方片"};
final String[] Numbers= {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
public void shuffle() {
System.out.println("-------------------开始洗牌。。。------------------");
Random random=new Random();
int cardIndex;
for(int i=0;i<cards.length;i++) {
do{
cardIndex=random.nextInt(cards.length);
}while(shuffleCards.contains(cards[cardIndex]));
shuffleCards.add(cards[cardIndex]);
}
System.out.println("-------------------洗牌结束!-------------------");
System.out.print("为:[");
for(Cards c:shuffleCards) {
System.out.print(c.cardType+c.cardNumber+", ");
}
System.out.print("]");
System.out.println();
}
-----------------------------------扑克牌比较---------------------------------------------
public Cards compareTwoCards(Cards c1,Cards c2) {
final String[] Types= {"黑桃","红桃","梅花","方片"};
final String[] Numbers= {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
List<String> arrayTypes=Arrays.asList(Types);
List<String> arrayNumbers=Arrays.asList(Numbers);
if(arrayNumbers.indexOf(c1.getCardNumber()) < arrayNumbers.indexOf(c2.getCardNumber())) {
return c2;
}else if(arrayNumbers.indexOf(c1.getCardNumber()) > arrayNumbers.indexOf(c2.getCardNumber())) {
return c1;
}else {
if(arrayTypes.indexOf(c1.getCardType()) < arrayTypes.indexOf(c2.getCardType()))
return c2;
else
return c1;
}
}
Java入门第三季
409792 学习 · 4340 问题
相似问题