> Player玩家类
package com.okawari;
import java.util.*;
public class Player {
List<Poker>HandPoker;
public Player()
{
this.HandPoker = new ArrayList<Poker>();
}
public Player(int id,String name)
{
ID = id;
this.name = name;
this.HandPoker = new ArrayList<Poker>();
}
public String getname()
{
return name;
}
public int getID()
{
return ID;
}
public void setname(String a)
{
name = a;
}
public void setID(int a)
{
ID = a;
}
private String name;
private int ID;
}
> Poker 卡牌类
package com.okawari;
public class Poker {
public String Symble;
public String size;
public Poker()
{
System.out.println();
}
public Poker(String Symble,String size)
{
this.Symble = Symble;
this.size = size;
}
public void setsize(String a){
size = a;
}
public String getSymble(){
return Symble;
}
public String getsize(){
return size;
}
}
> initial实现类
package com.okawari;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.*;
public class Initialtext {
public List<Poker>ListSequence;
public List<Player>Players;
Random random;
String []color = {"黑桃","红桃","梅花","方块"};
String []size = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
public Initialtext()
{
this.Players = new ArrayList<Player>();
this.ListSequence = new ArrayList<Poker>();
}
public void Pokeradd()
{
System.out.println("------------创建扑克牌中...------------");
for(int i = 0;i<color.length;i++)
{
for(int j = 0;j<size.length;j++)
{
Poker a = new Poker(color[i],size[j]);
ListSequence.add(a);
}
}
System.out.println("------------扑克牌创建成功!------------");
System.out.print("为:[");
Iterator it = ListSequence.iterator();
while(it.hasNext())
{
Poker cr = (Poker)it.next();
System.out.print(cr.Symble+cr.size+",");
}
System.out.println("]");
System.out.println("------------开始洗牌...------------");
for(int i = 0;i<ListSequence.size();i++)
{
int k = (int)(Math.random()*ListSequence.size());
Poker num1 = (Poker)ListSequence.get(i);
ListSequence.set(i,ListSequence.get(k));
ListSequence.set(k,num1);
}
System.out.println("------------洗牌结束!------------");
}
public void setplayer()
{
int play_num;
System.out.println("------------创建玩家...------------");
while(true)
{
Scanner input = new Scanner(System.in);
System.out.println("请输入玩家数目:");
try{
play_num = input.nextInt();
if(play_num>1&&play_num<10){
break;
}else{
throw new Exception();
}
}catch(InputMismatchException e){
System.out.println("请输入整形类型的数据!");
}catch(Exception e){
System.out.println("请输入规定范围之内的数据!");
}
}
for(int i = 0;i<play_num;i++)
{
System.out.println("请输入第"+(i+1)+"位玩家的姓名与ID:");
int id;
while(true)
{
System.out.println("输入ID:");
Scanner inID = new Scanner(System.in);
try{
id = inID.nextInt();
break;
}catch(InputMismatchException e){
System.out.println("请输入整形的ID!");
}
}
System.out.println("输入姓名:");
Scanner input = new Scanner(System.in);
String name = input.next();
Players.add(new Player(id,name));
}
for(Player player:Players){
System.out.println("----欢迎玩家:"+player.getname());
}
}
public double random(){
double n;
n=Math.random()*ListSequence.size();
return n;
}
public void sendPoker()
{
int i = 0;
System.out.println("------------开始发牌...------------");
for(Player player:Players)
{
System.out.println("----玩家:"+player.getname()+"-拿牌");
for(i = 0;i<2;)
{
double ran = random();
Poker a = ListSequence.get((int)ran);
if(a.getsize()=="0"){
continue;
}
player.HandPoker.add(a);
Poker instead = new Poker("0","0") ;
ListSequence.set((int)ran, instead);
System.out.println(player.HandPoker.get(i).size);
i++;
}
}
System.out.println("------------发牌结束!------------");
}
public int comprosize(String a){
for(int i=0;i<size.length;i++){
if(a==size[i]){
return i;
}
}
return 0;
}
public int comprosymbol(String a){
for(int i=0;i<color.length;i++){
if(a==color[i]){
return i;
}
}
return 0;
}
public void startgame(){
System.out.println("------------开始游戏...------------");
for(Player player:Players)
{
int a1 = comprosize(player.HandPoker.get(0).getsize());
int a2 = comprosize(player.HandPoker.get(1).getsize());
Poker c0 = player.HandPoker.get(0);
Poker c1 = player.HandPoker.get(1);
if(a1<a2){
player.HandPoker.set(0, c1);
player.HandPoker.set(1, c0);
}
System.out.println("玩家:"+player.getname()+"最大的手牌为:"+player.HandPoker.get(0).Symble+player.HandPoker.get(0).size);
}
Player win = new Player();
win = Players.get(0);
int maxsize = comprosize(win.HandPoker.get(0).getsize());
int maxSymbol = comprosymbol(win.HandPoker.get(0).getSymble());
for(Player player:Players)
{
if(comprosize(player.HandPoker.get(0).getsize())>maxsize){
win = player;
}
if(comprosize(player.HandPoker.get(0).getsize())==maxsize){
if(comprosymbol(player.HandPoker.get(0).getSymble())>maxSymbol){
win = player;
}
}
maxsize = comprosize(win.HandPoker.get(0).getsize());
maxSymbol = comprosymbol(win.HandPoker.get(0).getSymble());
}
System.out.println("------------玩家:"+win.getname()+"获胜!------------");
}
public void summary(){
System.out.println("玩家各自的手牌为:");
for(Player player:Players)
{
System.out.println(player.getname()+":["+player.HandPoker.get(0).getSymble()+player.HandPoker.get(0).getsize()+","+
player.HandPoker.get(1).getSymble()+player.HandPoker.get(1).getsize()+"]");
}
}
public static void main(String[] args) {
Initialtext a = new Initialtext();
a.Pokeradd();
a.setplayer();
a.sendPoker();
a.startgame();
a.summary();
}
}
打开App,阅读手记