package com.poker.imooc;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Game {
/**
* 一、创建一副扑克牌
* 包括四种花色: 黑桃、红桃、梅花、方片
* 十三种点数:2——10,J、Q、K、A,不考虑大小王
* 二、创建两名玩家
* 玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
* 三、洗牌
* 将之前创建的“一副扑克牌”打乱顺序
* 四、发牌
* 将洗牌之后的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发两张
* 五、游戏
* 比较两名玩家手中的扑克牌,规则为:取两人各自手中点数最大的牌进行比较,点数大的赢;
* 若两人各自的点数最大的牌相等,则再按花色(黑红梅方)比较。
*/
public static Scanner scanner = new Scanner(System.in);
public static Player[] playersCreate(int n) {
System.out.println("------##### 创建玩家 #####------");
Player[] players = new Player[n];
for(int i = 0; i < n; i++) {
System.out.println("请输入第" + (i+1) + "个玩家的ID和姓名:");
int id = 0;
String name;
boolean bool = true;
while(bool == true) {
try {
System.out.println("输入ID:");
id = scanner.nextInt();
if(i > 0) {
for(int j = i - 1; j >= 0; j--) {
if(players[j].getId() == id) {
System.out.println("**此ID已存在,请重新输入!");
bool = true; break;
}
else bool = false;
}
}else break;
} catch (InputMismatchException e) {
// TODO: handle exception
System.out.println("请输入整数类型的ID!");
scanner = new Scanner(System.in);
}
}
System.out.println("输入name:");
name = scanner.next();
players[i] = new Player(id, name);
}
return players;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PokerList cardList = new PokerList();
cardList.cardShows();
int n = 2; //创建两名玩家,n为玩家数目
Player[] players = playersCreate(n);
for(int i = 0; i < players.length; i++)
System.out.println("------欢迎玩家ID:" + players[i].getId() + ", name:" + players[i].getName() + "------");
System.out.println("---$$$$$ 开始洗牌! $$$$$---");
cardList.cardShufList();
System.out.println("---$$$$$ 完成洗牌!$$$$$---");
// cardList.cardShows();
//发牌给两名玩家,num为发的张数
int num = 2;
for(int c = 0, i = 0, j = 1; c < num; c++) {
players[i].cardList.add(cardList.cardGet());
players[j].cardList.add(cardList.cardGet());
}
//展示玩家的手牌
for(int i = 0; i < n; i++) {
System.out.print("玩家 " + players[i].getName() + " 的手牌为:");
for(int j = 0; j < num; j++) {
System.out.print(players[i].getCardList().get(j).getColor() + players[i].getCardList().get(j).getPoint() + ", ");
}
System.out.println();
}
//得出每个玩家最大的手牌,放在玩家手牌的第一位
for(int i = 0; i < n; i++) {
if(compare(players[i].getCardList().get(0), players[i].getCardList().get(1)) < 0) {
Poker poker = players[i].getCardList().get(0);
players[i].getCardList().set(0, players[i].getCardList().get(1));
players[i].getCardList().set(1, poker);
}
}
//两位玩家最大手牌相比较
if(compare(players[0].getCardList().get(0), players[1].getCardList().get(0)) < 0) {
System.out.println(" @@@@@ 恭喜玩家 " + players[1].getName() + " 获得胜利! @@@@@");
}else {
System.out.println(" @@@@@ 恭喜玩家 " + players[0].getName() + " 获得胜利! @@@@@");
}
}
private static int compare(Poker poker, Poker poker2) {
// TODO Auto-generated method stub
String[] colors = {"方片", "梅花", "红桃", "黑桃"};
String[] points = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
int p = 0, p2 = 0;
for(int j = 0; j < points.length; j++) {
if(poker.getPoint().equals(points[j])) p = j;
if(poker2.getPoint().equals(points[j])) p2 = j;
}
if(p < p2) {
return -1;
}else if(p > p2) {
return 1;
}else {
int pp = 0, pp2 =0;
for(int i = 0; i < colors.length; i++) {
if(poker.getColor().equals(colors[i])) pp = i;
if(poker2.getColor().equals(colors[i])) pp2 = i;
}
if(pp < pp2) {
return -1;
}else if(pp > pp2) {
return 1;
}
}
return 0;
}
}
package com.poker.imooc;
import java.util.ArrayList;
public class Player {
// 二、创建两名玩家
// 玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合
private int id;
private String name;
public ArrayList<Poker> cardList;
public Player(int id, String name) {
this.id = id;
this.name = name;
cardList = new ArrayList<Poker>();
}
public int getId() {return id;}
public String getName() {return name;}
public void setId(int id) {this.id = id;}
public void setName(String name) {this.name = name;}
public ArrayList<Poker> getCardList() {
return cardList;
}
}
package com.poker.imooc;
public class Poker {
private String color;
private String point;
public Poker(String color, String point) {
this.color = color;
this.point = point;
}
public String getColor() {
return color;
}
public String getPoint() {
return point;
}
}
package com.poker.imooc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PokerList {
// 一、创建一副扑克牌
// 包括四种花色: 黑桃、红桃、梅花、方片
// * 十三种点数:2——10,J、Q、K、A,不考虑大小王
public List<Poker> cardList = new ArrayList<Poker>();
public PokerList() {
System.out.println("——————————创建扑克牌——————————");
String[] colors = {"黑桃", "红桃", "梅花", "方片"};
String[] points = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
for(int i = 0; i < colors.length; i++) {
for(int j = 0; j < points.length; j++) {
cardList.add(new Poker(colors[i], points[j]));
}
}
System.out.println("——————————扑克牌创建成功!——————————");
}
//展示扑克牌排列
public void cardShows() {
System.out.println("当前扑克牌排序为:");
for(int i = 0; i < cardList.size(); i++) {
System.out.print(cardList.get(i).getColor() + cardList.get(i).getPoint() + ", ");
}
System.out.println();
}
//洗牌
public void cardShufList() {
Collections.shuffle(cardList);
}
//发牌
public Poker cardGet() {
Poker card = cardList.get(0);
cardList.remove(0);
return card;
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章