package com.imooc.jpw;
/*
- 承装4种花色
*/
import java.util.ArrayList;
import java.util.List;
public class Colour {
public List<String> colourList;
public Colour(){
colourList=new ArrayList<String>();
colourList.add("方块");
colourList.add("梅花");
colourList.add("红桃");
colourList.add("黑桃");
}
}
import java.util.ArrayList;
import java.util.List;
/*
- 承装13张无花色牌
*/
public class Suit {
public List<String> suitList;
public Suit(){
suitList=new ArrayList<String>();
for(int i=1;i<10;i++){
suitList.add((i+1)+"");
}
suitList.add("J");
suitList.add("Q");
suitList.add("k");
suitList.add("A");
}
}
package com.imooc.jpw;
/*
- 定义牌类
*/
public class Card {
String colour;
String suit;
public Card(String colour,String suit){
this.colour=colour;
this.suit=suit;
}
public Card(){}
}
package com.imooc.jpw;
/*
- 定义玩家
*/
public class Player {
public int ID;
public String name;
public Card c1,c2;
public Player(int ID,String name,Card c1,Card c2){
this.ID=ID;
this.name=name;
this.c1=c1;
this.c2=c2;
}
}
package com.imooc.jpw;
public class ErrorCodeException extends Exception {
public ErrorCodeException(){
}
}
package com.imooc.jpw;
/*
- 扑克牌的操作
*/
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Method {
public List<Card> cardlist;
public Method(){
cardlist=new ArrayList<Card>();
}
/*
* 创建一副扑克牌
*/
public void createCard(){
Colour h=new Colour();
Suit d=new Suit();
for(int i=0;i<13;i++)
for(int j=0;j<4;j++){
Card c=new Card(h.colourList.get(j),d.suitList.get(i));
cardlist.add(c);
}
int i=0;
for(Card card:cardlist){
if(i==4){
System.out.println("");//每4个换行
i=0;
}
if(i!=4){
System.out.print("<"+card.colour+card.suit+">"+" ");
i++;
}
}
System.out.print("\n---------------扑克牌创建成功!-----------------\n");
}
/*
* 洗牌
*/
public List<Card> Wash(){
List<Card> lc= new ArrayList<Card>();
Random random=new Random();
int i=0;
while(i<52){
Card c=cardlist.get(random.nextInt(52));
if(!lc.contains(c)) {
lc.add(c);
i++;
}
}
return lc;
}
/*
* 发牌
*/
public Card deal(){
Random random=new Random();
Card c= Wash().get(random.nextInt(52));//随机取牌
return c;
}
/*
* 比较大小
*/
public Card compare(Card c1,Card c2){
Suit d=new Suit();
Colour h=new Colour();
Integer i1=d.suitList.indexOf(c1.suit);
Integer i2=d.suitList.indexOf(c2.suit);//比较点数的大小
int i=i1.compareTo(i2);
if(i==0){
Integer i3=h.colourList.indexOf(c1.colour);
Integer i4=h.colourList.indexOf(c2.colour);//若大小相等比较花色 :黑红梅方
int j=i3.compareTo(i4);
if(j==0)
return c1;
if(j<0)
return c2;
if(j>0)
return c1;
}
if(i>0){
return c1;//card1大
}else
return c2;//card2大
}
}
package com.imooc.jpw;
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Game g=new Game();
g.game();
}
public void game(){
System.out.println("------------------游戏开始!-------------------");
System.out.println("-----------------创建扑克牌中...---------------");
Method m=new Method();
m.createCard();
System.out.println("-----------------开始洗牌...-----------------");
m.Wash();
System.out.println("------------------洗牌完成!-------------------");
System.out.println("------------------创建玩家:------------------");
while(true){
Scanner console = new Scanner(System.in);
try{
System.out.println("请输入第一位玩家ID:");
int ID1=console.nextInt();
System.out.println("请输入第一位玩家姓名:");
String name1=console.next();
System.out.println("\n请输入第二位玩家ID:");
int ID2=console.nextInt();
System.out.println("请输入第二位玩家姓名:");
String name2=console.next();
System.out.println("----------------创建玩家成功!--------------------");
System.out.println("------------------开始发牌--------------------");
System.out.println("-------玩家"+name1+"拿牌-------");
System.out.println("-------玩家"+name2+"拿牌-------");
System.out.println("-------玩家"+name1+"拿牌-------");
System.out.println("-------玩家"+name2+"拿牌-------");
//发牌操作
Card c11=m.deal();
Card c12=m.deal();
Card c21=m.deal();
Card c22=m.deal();
System.out.println("------------------发牌结束--------------------\n");
//创建玩家
Player p1=new Player(ID1,name1,c11,c12);
Player p2=new Player(ID2,name2,c21,c22);
//比较大小
Card c1=m.compare(c11, c12); //第一位玩家的较大牌
Card c2=m.compare(c21, c22); //第二位玩家的较大牌
Card c0=m.compare(c1, c2); //比较两位玩家的最大牌
System.out.println("玩家"+p1.name+"的牌为<"+c11.colour+c11.suit+"> <"+c12.colour+c12.suit+">");
System.out.println("玩家"+p2.name+"的牌为<"+c21.colour+c21.suit+"> <"+c22.colour+c22.suit+">");
if(c0.equals(c1) && !c0.equals(c2)){
System.out.println("-------------"+p1.name+"获胜!-----------------");
}
if(c0.equals(c2) && !c0.equals(c1)){
System.out.println("-------------"+p2.name+"获胜!-----------------");
}
if(c0.equals(c1) && c0.equals(c2)){
System.out.println("-------------"+p1.name+p2.name+"平局-----------------");
}
}catch(Exception e){
System.out.println("输入有误,请输入整数ID!");
}
}
}
}