我的代码除了长 没啥优点了=.=
package Poker_game;
public class main {
static String a1,a2;
public static void main(String[] args) {
//进入游戏界面
Start start=new Start();
start.Startgame();
String b = start.geta();
if(b.equals("1")) {
//创建扑克牌
Pokercard poker=new Pokercard();
poker.foundpoker();
//无序洗牌
poker.reshuffle();
//创建玩家
Foundplayer Fplayer=new Foundplayer();
Fplayer.foundplayer();
Player player1=new Player();
Player player2=new Player();
player1.id=Fplayer.id[0];
player1.name=Fplayer.name[0];
player2.id=Fplayer.id[1];
player2.name=Fplayer.name[1];
//有序发牌
//poker.distribute();
System.out.println("*****开始发牌");
for(int i=0;i<4;i++) {
if((i+1)%2==1) System.out.println("---玩家:"+player1.name+"拿牌");
else System.out.println("---玩家:"+player2.name+"拿牌");
}
player1.pokercd1=poker.newsort[0];
player2.pokercd1=poker.newsort[1];
player1.pokercd2=poker.newsort[2];
player2.pokercd2=poker.newsort[3];
//开始游戏
System.out.println("*****开始游戏");
if(poker.k[0]>poker.k[2]) a1=poker.newsort[0];
else a1=poker.newsort[2];
if(poker.k[1]>poker.k[3]) a2=poker.newsort[1];
else a2=poker.newsort[3];
System.out.println("---玩家:"+player1.name+"最大的手牌为"+a1);
System.out.println("---玩家:"+player2.name+"最大的手牌为"+a2);
//游戏结果
System.out.println("*****游戏结果");
if (poker.k[0]>poker.k[1] && poker.k[0]>poker.k[3])
System.out.println("---玩家:"+player1.name+"胜利");
else if(poker.k[2]>poker.k[1] && poker.k[2]>poker.k[3])
System.out.println("---玩家:"+player1.name+"胜利");
else System.out.println("---玩家:"+player2.name+"胜利");
System.out.println("玩家各自的手牌为");
System.out.println("玩家:"+player1.name+"手牌为"+poker.newsort[0]+" "+poker.newsort[2]);
System.out.println("玩家:"+player2.name+"手牌为"+poker.newsort[1]+" "+poker.newsort[3]);
}
}
}
package Poker_game;
import java.util.Scanner;
public class Start {
String a;
public void Startgame(){
System.out.println("是否进入扑克牌游戏"
+ "请输入:"
+ "1进入 0退出 ");
Scanner inputstart =new Scanner(System.in);
do {
a= inputstart.next();
if (!a.equals("1") && !a.equals("0"))
System.out.println("输入有误! 请输入1或0,谢谢!");
if(a.equals("1"))
System.out.println("*****欢迎进入");
}while(!a.equals("1") && !a.equals("0"));
if (a.equals("0")) System.out.println(
"已退出游戏") ;
}
public String geta() {
return a;
}
}
package Poker_game;
import java.util.Scanner;
import java.util.regex.Pattern;
public class Player {
String id;
String name;
String pokercd1;
String pokercd2;
}
package Poker_game;
import java.awt.List;
import java.util.ArrayList;
import java.util.Random;
public class Pokercard {
Random random;
String[] poker=new String[52];
String[] newsort=new String[52];//存储洗牌后的牌序列
int[] k= new int[52];//用来存储[0,51]的不重复随机数列
public void foundpoker() {
String[] flower={"方块","梅花","红桃","黑桃"};
String[] point= {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
int k=0;
for(int i=0;i<4;i++) {
for (int j=0;j<13;j++) {
poker[k]=flower[i]+point[j];
k++;
}
}
System.out.println("*****创建扑克牌成功");
System.out.print("*****扑克牌为");
for (String puke : poker) {
System.out.print(puke+" ");
}
System.out.println("");
}
public void reshuffle() {
ArrayList mylist = new ArrayList(); //生成数据集,用来保存随即生成数,并用于判断
Random random = new Random();
while(mylist.size() < 52) {
int num = random.nextInt(52);//生成[0,51]的随机数
if(!mylist.contains(num)) //如果mylist不包含此随机数num
{
mylist.add(num); //往集合里面添加数据。
}
}
for(int i = 0;i <mylist.size();i++) {
k[i] = (Integer)(mylist.get(i)); //获取mylist依次放入k[]中
newsort[i]=poker[k[i]]; //将poker的下标设为k[0,51]不重复随机数 依次存入新序列 完成洗牌
}
/*for (Object object : newsort) {
System.out.println(object); //遍历验证newsort是否洗牌成功
}*/
System.out.println("*****洗牌完成");
}
public void distribute() {
System.out.println("*****开始发牌");
Player player1=new Player();
for(int i=0;i<4;i++) {
if((i+1)%2==1) { player1.pokercd1=newsort[0];
System.out.println("---玩家:"+player1.name+"拿牌");}
}
}
}
package Poker_game;
import java.util.Scanner;
import java.util.regex.Pattern;
public class Foundplayer {
Scanner input =new Scanner(System.in);
//String[] id=new String[2];
String[] id=new String[2];
String text;
String[] name=new String[2];
public void foundplayer() {
String[] id=new String[2];
System.out.println("*****创建玩家");
Player player1=new Player();
Player player2=new Player();
for(int i=0;i<2;i++) {
System.out.println("*****请输入第"+(i+1)+"个玩家的id");
do {
id[i] = input.next();
if (!isInteger(id[i]))
System.out.println("输入有误! 请输入整数,谢谢!");
} while (!isInteger(id[i]));
/* do {
try {id[i] = input.nextInt();
}catch (Exception e) {
System.out.println("输入有误! 请输入整数,谢谢!");
continue;
}
}while(id[i].equals("1"));*/
System.out.println("*****请输入第"+(i+1)+"个玩家的姓名");
name[i]=input.next();
}
for (String string : name) {
System.out.println("---欢迎玩家:"+string);
}
player1.id=id[0];
player1.name=name[0];
player2.id=id[1];
player2.name=name[1];
}
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
}