package com.pocker;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/*
* 扑克牌游戏
* 1.创建一副扑克牌,分黑桃、红桃、梅花、方片,1-12
* 2.创建两名玩家:ID 姓名 手牌
* 3.洗牌:打乱扑克牌的顺序
* 4.发牌:分别给两名玩家各发两站扑克牌
* 5.点数之和大者赢得比赛 若点数相同则按花色比较 依次黑桃>红桃>梅花>方片
*/
public class Main {
private Scanner in = new Scanner(System.in);
private List<Card> cardList;
private static Player player1;
private static Player player2;
/*
* 创建一副扑克牌
*/
public void createCard() {
cardList = new ArrayList<Card>();
for(int i=0;i<4;i++) {
String type = null ;
switch(i) {
case 0:
type="黑桃";
break;
case 1:
type="红桃";
break;
case 2:
type="梅花";
break;
case 3:
type="方片";
break;
}
for(int num=1;num<=12;num++) {
Card c = new Card(type,num);
cardList.add(c);
}
}
}
/*
* 创建两名玩家
*/
public void createPlayer() {
System.out.println("请输入第一位玩家ID");
String id1 = in.next();
System.out.println("请输入玩家姓名");
String name1 = in.next();
player1 = new Player(id1,name1);
System.out.println("请输入第二位玩家ID");
String id2 = in.next();
System.out.println("请输入玩家姓名");
String name2 = in.next();
player2 = new Player(id2,name2);
}
/*
* 打乱扑克牌顺序,直接将牌从List中取出放入Set中即实现无序
*/
public void washCard() {
System.out.println("正在洗牌...");
System.out.println("洗牌完毕");
}
/*
* 向两位玩家发牌
* 每位玩家分两张牌
*/
public void distributeCard(Player p) {
Random random = new Random();
System.out.println("向玩家"+p.name+"发牌中...");
for(int i=0;i<2;i++) {
int n = random.nextInt(47);
Card card = new Card();
card = cardList.get(n);
p.ownCard.add(card);
}
System.out.println("发牌完毕");
}
/*
* 判断输赢
*/
public void judge() {
int sum1 = 0,sum2 = 0;
int type1 = 0, type2 = 0;
//输出一号玩家手中的牌
System.out.println("玩家"+player1.name+"手中的牌分别是:");
for(Card c : player1.ownCard) {
if(c.type.equals("黑桃")) type1+=3;
if(c.type.equals("红桃")) type1+=2;
if(c.type.equals("梅花")) type1+=1;
if(c.type.equals("方片")) type1+=0;
sum1 += c.num;
System.out.println(c.type+c.num);
}
System.out.println("玩家"+player2.name+"手中的牌分别是:");
for(Card c : player2.ownCard) {
if(c.type.equals("黑桃")) type1+=3;
if(c.type.equals("红桃")) type1+=2;
if(c.type.equals("梅花")) type1+=1;
if(c.type.equals("方片")) type1+=0;
sum2 += c.num;
System.out.println(c.type+c.num);
}
System.out.print("本轮比赛的获胜者是----");
if(sum1>sum2) System.out.print(player1.name);
else if(sum1<sum2) System.out.print(player2.name);
else {
if(type1>type2) System.out.print(player1.name);
if(type1<type2) System.out.print(player2.name);
}
}
public static void main(String[] args) {
Main m = new Main();
m.createPlayer();
m.createCard();
m.washCard();
m.distributeCard(player1);
m.distributeCard(player2);
m.judge();
}
}
package com.pocker;
import java.util.ArrayList;
import java.util.List;
public class Player {
public String ID;
public String name;
public List<Card> ownCard;
public Player(String ID,String name) {
this.ID = ID;
this.name = name;
ownCard = new ArrayList<Card>();
}
}
package com.pocker;
public class Card {
public String type;
public int num;
public Card(String type,int num) {
this.type = type;
this.num = num;
}
public Card() {
}
}