一:
Player
package com.player;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Player {
private int id;
private String name;
ArrayList<String> cardInfo = new ArrayList<String>();
public Player(int id, String name){
this.id = id;
this.name = name;
}
public ArrayList<String> getCardInfo() {
return cardInfo;
}
public void setCardInfo(String cardInfo) {
this.cardInfo.add(cardInfo);
}
public String maxCard(){
Map<String, Integer> cardCompare = new HashMap<String, Integer>();
cardCompare.put("梅花", 4);
cardCompare.put("黑桃", 3);
cardCompare.put("红心", 2);
cardCompare.put("方片", 1);
ArrayList<String> cards = getCardInfo();
String maxCard = null;
int a ;
int b ;
a = transfer(cards.get(0));
b = transfer(cards.get(1));
if(a > b)
maxCard = cards.get(0);
else if(a < b)
maxCard = cards.get(1);
else{
String card1 = cards.get(0).substring(0,2);
String card2 = cards.get(1).substring(0,2);
if(cardCompare.get(card1) > cardCompare.get(card2))
maxCard = cards.get(0);
else if(cardCompare.get(card1) < cardCompare.get(card2))
maxCard = cards.get(1);
else
maxCard = cards.get(0);
}
return maxCard;
}
public int transfer(String str){
int a;
if(str.substring(2).equals("J"))
a = 11;
else if(str.substring(2).equals("Q"))
a = 12;
else if(str.substring(2).equals("K"))
a = 13;
else if(str.substring(2).equals("A"))
a = 1;
else
a = Integer.parseInt(str.substring(2));
return a;
}
public String getName() {
return name;
}
}
Game
package com.game;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import com.card.Card;
import com.player.Player;
public class Game {
public static void main(String[] args) {
// Create new Card class
Card card = new Card();
Player[] player = new Player[2];
Scanner input = new Scanner(System.in);
System.out.println("**********开始创建扑克牌**********");
System.out.println("**********扑克牌创建成功**********");
System.out.println(Arrays.toString(card.getNewCards()));
System.out.println("**********开始洗牌**********");
String[] cardsRandom = card.RandomCard();
System.out.println("**********洗牌结束**********");
System.out.println("**********创建玩家**********");
//Create players
for(int i = 0; i < player.length;i ++){
System.out.println("请输入第" + (i+1) + "玩家的ID和姓名");
System.out.println("请输入ID:");
int id = input.nextInt();
System.out.println("请输入姓名:");
String name = input.next();
Player player1 = new Player(id,name);
player[i] = player1;
}
//Show players' name
for(int i = 0;i < player.length;i ++)
System.out.println("*****欢迎玩家:" + player[i].getName());
System.out.println("**********开始发牌**********");
Random random = new Random();
int index;
for(int i = 0;i < 4;i ++){
System.out.println("*****玩家: " + player[i % 2].getName() + " 拿牌");
index = random.nextInt(cardsRandom.length);
player[i % 2].setCardInfo(cardsRandom[index]);
}
System.out.println("**********发牌结束**********");
System.out.println("**********开始游戏**********");
String[] cardsComp = new String[2];
for(int i = 0;i < player.length;i ++){
cardsComp[i] = player[i].maxCard();
System.out.println("玩家:" + player[i].getName() + "最大的手牌为: " + player[i].maxCard());
}
int result = maxCard(cardsComp);
if(result == 1)
System.out.println("********玩家 " + player[0].getName() + "获胜");
else if(result == -1)
System.out.println("********玩家 " + player[1].getName() + "获胜");
else
System.out.println("********玩家 " + player[1].getName() + "和玩家 " + "手牌一样大,没有输赢");
System.out.println("**********玩家的手牌为:");
for(int i = 0;i < player.length;i ++)
System.out.println(player[i].getName() + ":[ " + player[i].getCardInfo().get(0) + " , "+ player[i].getCardInfo().get(1) + " ]");
}
public static int maxCard(String[] cards){
Map<String, Integer> cardCompare = new HashMap<String, Integer>();
cardCompare.put("梅花", 4);
cardCompare.put("黑桃", 3);
cardCompare.put("红心", 2);
cardCompare.put("方片", 1);
int winer;
int a = 0;
int b = 0;
a = transfer(cards[0]);
b = transfer(cards[1]);
if(a > b)
winer = 1;
else if(a < b)
winer = -1;
else{
String card1 = cards[0].substring(0,2);
String card2 = cards[1].substring(0,2);
if(cardCompare.get(card1) > cardCompare.get(card2))
winer = 1;
else if(cardCompare.get(card1) < cardCompare.get(card2))
winer = -1;
else
winer = 0;
}
return winer;
}
public static int transfer(String str){
int a = 0;
if(str.substring(2).equals("J"))
a = 11;
else if(str.substring(2).equals("Q"))
a = 12;
else if(str.substring(2).equals("K"))
a = 13;
else if(str.substring(2).equals("A"))
a = 1;
else
a = Integer.parseInt(str.substring(2));
return a;
}
}
Card
package com.card;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Card {
private String number;
private String color;
/**
* Create a pair of new cards
* @return
*/
public ArrayList<Card> createCard(){
String[] carNumber = new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] carColor = new String[]{"方块","红心","梅花","黑桃"};
ArrayList<Card> newCards = new ArrayList<Card>();
//Create a pair of new cards
for(int i = 0;i < carColor.length;i ++){
for(int j = 0;j < carNumber.length;j ++){
// card.id = i;
Card card = new Card();
card.color = carColor[i];
card.number = carNumber[j];
newCards.add(card);
}
}
return newCards;
}
/**
* show the new cards
* @return
*/
public String[] getNewCards(){
String[] newCardGroup = new String[createCard().size()];
for(int i = 0;i < createCard().size();i ++){
newCardGroup[i] = createCard().get(i).color +createCard().get(i).number ;
}
return newCardGroup;
}
/**
* Create random cards
* @return
*/
public String[] RandomCard(){
Random random = new Random();
String[] cardsArray = getNewCards();
String swapCard = null;
for(int i = 0;i < cardsArray.length;i ++){
int index = random.nextInt(cardsArray.length);
swapCard = cardsArray[i];
cardsArray[i] = cardsArray[index];
cardsArray[index] = swapCard;
}
return cardsArray;
}
}
二:
Poker.zip
StartGame
import java.util.Scanner;
public class StatGame {
public static void main(String[] args) {
//1.创建扑克牌
//2.洗牌
//3.接收两个玩家
//4.给两个玩家发牌
//5.输出结果
//6.打印两个玩家手中的牌
//7.游戏结束
System.out.println("创建扑克牌");
PokerGame pokerGame = new PokerGame();
//遍历当前扑克牌
pokerGame.testForEach();
System.out.println("洗牌");
pokerGame.shuffleCards();
System.out.println("接收两个玩家");
Player player[] = {new Player(),new Player()};
//获取两个玩家的信息
for(int i=0;i<2;i++){
System.out.println("请输入玩家ID:");
try{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
player[i].setId(n);
}catch (Exception e) {
System.out.println("玩家ID不支持字符串");
i--;
continue;
}
System.out.println("请输入玩家名称");
try {
Scanner scanner = new Scanner(System.in);
String name = scanner.next();
player[i].setName(name);
}catch (Exception e){
e.printStackTrace();
System.out.println("玩家Name不支持数字!");
i--;
continue;
}
}
System.out.println("给两个玩家发牌");
for (int i=0;i<2;i++){
player[i].push(pokerGame.deadCard());
player[i].push(pokerGame.deadCard());
player[i].push(pokerGame.deadCard());
}
System.out.println("输出结果");
Player winner = pokerGame.runGame(player[0],player[1]);
System.out.println(winner.getName()+"胜出"+"获得一个macbook pro!");
System.out.println("打印两个玩家手中的牌");
player[0].Traversal();
player[1].Traversal();
System.out.println("游戏结束");
}
}
Poker
public class Poker implements Comparable<Object>{
public static final String[] pokers={
"黑桃2","红桃2","梅花2","方块2",
"黑桃3","红桃3","梅花3","方块3",
"黑桃4","红桃4","梅花4","方块4",
"黑桃5","红桃5","梅花5","方块5",
"黑桃6","红桃6","梅花6","方块6",
"黑桃7","红桃7","梅花7","方块7",
"黑桃8","红桃8","梅花8","方块8",
"黑桃9","红桃9","梅花9","方块9",
"黑桃10","红桃10","梅花10","方块10",
"黑桃J","红桃J","梅花J","方块J",
"黑桃Q","红桃Q","梅花Q","方块Q",
"黑桃K","红桃K","梅花K","方块K",
"黑桃A","红桃A","梅花A","方块A",
"大王","小王"
};
public String getPokerDesign(){
return pokerDesign;
}
public void setPokerDesign(String pokerDesign){this.pokerDesign=pokerDesign;}
public String getPokerNum(){
return pokerNum;
}
public void setPokerNum(String pokerNum){
this.pokerNum=pokerNum;
}
private String pokerDesign;//扑克牌花色
private String pokerNum;//扑克牌数字
private int iIndex;
public int getiIndex(){
return iIndex;
}
public void setiIndex(int iIndex){ this.iIndex = iIndex;}
public Poker(String pokerDesign,String pokerNum){
super();
this.pokerDesign = pokerDesign;
this.pokerNum = pokerNum;
String pokerString = this.pokerDesign+this.pokerNum;
for(int i=0;i< pokers.length;i++){
if(pokers[i].equals(pokerString)){
iIndex=i;
}
}
}
public int compareTo(Object obj){
//判断是否等于自己
if(this==obj){
return 0;
}
//判断是否能转换成Poker,不能返回
if(obj instanceof Poker){
Poker poker = (Poker) obj;
if(this.iIndex > poker.iIndex){
return 1;
}else if(this.iIndex < poker.iIndex){
return -1;
}else if(this.iIndex == poker.iIndex){
return 0;
}
}
return -1;
}
}
PokerGame
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Player {
private int id;
public int getId(){
return id;
}
public void setId(int id){this.id=id; }
private String name;
public String getName() {return name;}
public void setName(String name) {
this.name = name;
}
private List<Poker> pokerList;
public void setPokerList(List<Poker>pokerList){this.pokerList=pokerList;}
public Player() {
this.id = -1;
this.name = "";
this.pokerList = new ArrayList<Poker>();
}
public Player(int id, String name) {
this.id = id;
this.name = name;
this.pokerList= new ArrayList<Poker>();
}
public void push(Poker poker){
this.pokerList.add(poker);
}
//出最大牌
public Poker popMax(){
//出牌,默认选择最大的开始
//排序
Collections.sort(pokerList,new SortByIndex());
Poker max = pokerList.get(pokerList.size()-1);
return max;
}
public void Traversal(){
System.out.println("玩家"+name+"接到的扑克牌:");
for(Poker poker:pokerList){
System.out.println(""+poker.getPokerDesign()+poker.getPokerNum());
}
System.out.println();
}
}
Player
import java.util.Comparator;
public class SortByIndex implements Comparator<Poker> {
@Override
public int compare(Poker o1, Poker o2) {
if(o1.getiIndex()>o2.getiIndex()){
return 1;
}
else if(o1.getiIndex()<o2.getiIndex()){
return -1;
}
return 0;
}
}
SortByIndex
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class PokerGame {
public List<Poker> pokerList;
private static String[] numList = {
"2","3","4","5","6","7","8","9","10","J","Q","K","A"
};
public PokerGame() {
super();
this.pokerList = new ArrayList<Poker>();
InitPokers(this.pokerList);
}
public static void InitPokers(List<Poker> pokerList){
for(int i =0;i<numList.length;i++){
pokerList.add(new Poker("黑桃",numList[i]));
pokerList.add(new Poker("红桃",numList[i]));
pokerList.add(new Poker("梅花",numList[i]));
pokerList.add(new Poker("方块",numList[i]));
}
pokerList.add(new Poker("大王",""));
pokerList.add(new Poker("小王",""));
}
public void testForEach(){
System.out.println("生成的扑克牌:");
for(Poker poker:pokerList){
System.out.println(""+poker.getPokerDesign()+poker.getPokerNum());
}
System.out.println();
}
public void shuffleCards(){
Random rd = new Random();
for(int i=0;i<pokerList.size();i++){
int j = rd.nextInt(pokerList.size());//生成随机数
Poker poker = pokerList.get(i);//取出当前的牌
pokerList.set(i,pokerList.get(j));//当前牌与随机牌换位置
pokerList.set(j,poker);
}
}
public Poker deadCard() {
Random rd = new Random();
int i = rd.nextInt(pokerList.size());//生成随机数
Poker poker = pokerList.get(i);//取出随机的牌
pokerList.remove(i);
return poker;
}
public Player runGame(Player p1,Player p2){
Poker poker1 = p1.popMax();
Poker poker2 = p2.popMax();
System.out.println("玩家"+p1.getName()+"出牌"+poker1.getPokerDesign()+ poker1.getPokerNum());
System.out.println("玩家"+p2.getName()+"出牌"+poker2.getPokerDesign()+ poker2.getPokerNum());
if(poker1.compareTo(poker2)>0){
return p1;
}
else {
return p2;
}
}
}