为了账号安全,请及时绑定邮箱和手机立即绑定

交作业,代码稍稍奇葩,本来是想做灵活一点的,最后还是给写死了

package entity;
//扑克牌类
public class Poker implements Comparable<Poker>{
private String colors;//花色
private String value;//值
public Poker(String colors ,String value) {
this.colors = colors;
this.value = value;
}
public String getColors() {
return colors;
}
public void setColors(String colors) {
this.colors = colors;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public int compareTo(Poker o) {
// TODO Auto-generated method stub
String[] colors = {"方块","梅花","红桃","黑桃"};
String[] values = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int a,b;//记录大小
a=indexOf(values,this.getValue());
b=indexOf(values,o.getValue());
if(a>b) 
return 1;
else if(a<b)
return -1;
else {
a=indexOf(colors,this.getValue());
b=indexOf(colors,o.getValue());
if(a>b) 
return 1;
else if(a<b)
return -1;
else
return 0;
}
}
private int indexOf(String[] str,String findStr) {
for(int i=0;i<str.length;i++) {
if(str[i].equals(findStr))
return i;
}
return -1;
}
}
package entity;
import java.util.ArrayList;
import java.util.List;
//玩家类
public class Player {
private int id;
private String name;
public List<Poker> pokers;
public Player(int id ,String name) {
this.id = id;
this.name = name;
this.pokers = new ArrayList<Poker>();
}
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;
}
}
package test;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import entity.Player;
import entity.Poker;
//测试类
public class Test {
//生成新牌
public void newPokers(String[] colors,String[] values,List<Poker> pokers) {
System.out.println("————————创建扑克牌————————");
for(int i=0;i<colors.length;i++) {//花色
for(int j=0;j<values.length;j++) {//值
Poker p = new Poker(colors[i],values[j]);
pokers.add(p);
}
}
System.out.println("————————扑克牌创建成功!————————");
System.out.print("为:[");
for(Poker p : pokers){
System.out.print(p.getColors()+p.getValue()+",");
}
System.out.println("]");
}
//洗牌
public void shuffle(List<Poker> pokers) {
System.out.println("————————开始洗牌————————");
Random random = new Random();
for(int j=0;j<3;j++) {//洗牌3次
for(int i=0;i<pokers.size();i++) {
//随机产生位置
int temp = random.nextInt(pokers.size());
//交换
Poker a = pokers.get(i);
Poker b = pokers.get(temp);
pokers.set(temp, a);
pokers.set(i, b);
}
}
System.out.print("为:[");
for(Poker p : pokers){
System.out.print(p.getColors()+p.getValue()+",");
}
System.out.println("]");
System.out.println("————————洗牌结束!————————");
}
//创建新玩家
public Player newPlayer(int no) {
int id;
String name;
while(true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第"+no+"位玩家的ID和姓名:");
System.out.println("输入ID:");
try {
id = sc.nextInt();
break;
}catch(InputMismatchException e) {
System.out.println("请输入整型数字!");
}
}
Scanner sc = new Scanner(System.in);
System.out.println("输入姓名:");
name = sc.next();
Player p = new Player(id,name);
return p;
}
//发牌,num为每人拿牌张数
public void cards(List<Player> players,List<Poker> pokers,int num) {
System.out.println("————————开始发牌————————");
for(int i=0;i<num;i++)
for(Player p:players) {
System.out.println("——玩家:"+p.getName()+"拿牌");
p.pokers.add(pokers.get(0));
pokers.remove(0);
}
System.out.println("————————发牌结束!————————");
}
public static void main(String[] args) {
String[] colors = {"方块","梅花","红桃","黑桃"};
String[] values = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
Test test = new Test();
List<Poker> pokers = new ArrayList<Poker>();
//生成新牌
test.newPokers(colors,values,pokers);
//洗牌
test.shuffle(pokers);
//生成玩家
List<Player> players = new ArrayList<Player>();
players.add(test.newPlayer(1));
players.add(test.newPlayer(2));
for(Player p : players){
System.out.println("——欢迎玩家:"+p.getName());
}
//发牌,每人两张
test.cards(players,pokers,2);
//排序
for(Player p:players) {
Collections.sort(p.pokers);
}
//比大小
System.out.println("————————开始游戏————————");
Poker a = players.get(0).pokers.get(1);
Poker b = players.get(1).pokers.get(1);
System.out.println("玩家:"+players.get(0).getName()+"最大的手牌为:"+a.getColors()+a.getValue());
System.out.println("玩家:"+players.get(1).getName()+"最大的手牌为:"+b.getColors()+b.getValue());
if(test.compareTo(a,b)>0) {
System.out.println("————————玩家:"+players.get(0).getName()+"获胜!————————");
}else {
System.out.println("————————玩家:"+players.get(1).getName()+"获胜!————————");
}
System.out.println("玩家各自的手牌为:");
for(Player p:players) {
System.out.println(p.getName()
+":["+p.pokers.get(0).getColors()+p.pokers.get(0).getValue()+","
+p.pokers.get(1).getColors()+p.pokers.get(1).getValue()+"]");
}
}
private int indexOf(String[] str,String findStr) {
for(int i=0;i<str.length;i++) {
if(str[i].equals(findStr))
return i;
}
return -1;
}
private int compareTo(Poker o1,Poker o2) {
// TODO Auto-generated method stub
String[] colors = {"方块","梅花","红桃","黑桃"};
String[] values = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int a,b;//记录大小
a=indexOf(values,o1.getValue());
b=indexOf(values,o2.getValue());
if(a>b) 
return 1;
else if(a<b)
return -1;
else {
a=indexOf(colors,o1.getValue());
b=indexOf(colors,o2.getValue());
if(a>b) 
return 1;
else if(a<b)
return -1;
else
return 0;
}
}
}











正在回答

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409788    人
  • 解答问题       4340    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

交作业,代码稍稍奇葩,本来是想做灵活一点的,最后还是给写死了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信