首先创建一个扑克牌类Card
package com.yx.card;
/**
*
* @author
* 卡牌类
* point 点数
* suit 花色
*
*/
public class Card {
// 卡牌点数
private String point;
// 卡牌花色
private String suit;
public Card(String suit, String point){
this.suit = suit;
this.point = point;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.suit + " " + this.point;
}
}
接着创建一个玩家类Player
package com.yx.person;
import java.util.ArrayList;
import java.util.List;
import com.yx.card.Card;
/**
*
* @author
* 玩家类
* id ID
* name 名字
* cards 手上持有的牌
*
*/
public class Player {
// 玩家ID
private int id;
// 名字
private String name;
// 手牌
private ArrayList<Card> cards;
// 玩家手中最大的手牌
private Card maxCard;
public Player(int id, String name){
this.id = id;
this.name = name;
this.cards = new ArrayList<Card>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Card> getCards() {
return cards;
}
public void setCards(ArrayList<Card> cards) {
this.cards = cards;
}
public Card getMaxCard() {
return maxCard;
}
/**
* 从玩家手中挑选出最大的那张手牌
*/
public void setMaxCard( String[] points, String[] suits ) {
Card tempMaxCard = this.cards.get(0);
int tempMaxCardIdx =0;
int tmpCardIdx=0;
int tempMaxCardSuitIdx = 0;
int tmpCardSuitIdx = 0;
for(int i=1; i<this.cards.size(); i++){
Card tmpCard = this.cards.get(i);
for(int j=0; j<points.length; j++){
if(tempMaxCard.getPoint().equals(points[j])){
tempMaxCardIdx = j;
}
if(tmpCard.getPoint().equals(points[j])){
tmpCardIdx = j;
}
}
// System.out.println("比较点数时获取的索引值:" + tempMaxCardIdx + " : " + tmpCardIdx);
if(tmpCardIdx > tempMaxCardIdx){
tempMaxCard = tmpCard;
}
if(tmpCardIdx == tempMaxCardIdx){
for(int k=0; k<suits.length; k++){
if(tempMaxCard.getSuit().equals(suits[k])){
tempMaxCardSuitIdx = k;
}
if(tmpCard.getSuit().equals(suits[k])){
tmpCardSuitIdx = k;
}
}
// System.out.println("比较 花色时获取的索引值:" + tempMaxCardSuitIdx + " : " + tmpCardSuitIdx);
if(tmpCardSuitIdx < tempMaxCardSuitIdx){
tempMaxCard = tmpCard;
}
}
}
this.maxCard = tempMaxCard;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.name;
}
}
然后我们在创建一个游戏类Game
package com.yx.game;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import com.yx.card.Card;
import com.yx.person.Player;
/**
*
* @author
* 1、创建一副扑克:
* 花色:黑桃、红桃、梅花、方块
* 点数:2-10、J、Q、K、A (不考虑大小王)
* 2、创建两名或多名玩家:
* 玩家至少有属性:ID, 名字,手牌等(手牌为扑克牌集合)
* 3、洗牌:
* 将创建好的扑克牌打乱顺序
* 4、 将洗好的牌一张一张发给玩家,按一人一张轮流发
* 5、 游戏:
* 比较两个玩家手中的扑克牌,规则为取两个手上最大的手牌进行比较,点数大的赢;
* 若两个手牌点数一样,再按手牌花色比较(黑、红、梅、方)
*/
public class Game {
// 一副扑克牌
private List<Card> selectToCards = new ArrayList<Card>();
// 洗过的扑克牌
private List<Card> selectedCards = new ArrayList<Card>();
// 玩家
private List<Player> players = new ArrayList<Player>();
// 花色
private String[] suits = {"黑桃", "红桃", "梅花", "方块"};
// 点数
private String[] points = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
// 创建随机数
private Random random = new Random();
// 玩家个数
private int playerNum;
// 玩家所持有牌数
private int cardNum;
public Game(){
this.createCards();
this.shuffleCards();
}
/**
* 创建一副扑克牌(不包括大小王)
*/
private void createCards(){
for(int i=0;i<this.suits.length;i++){
for(int j=0;j<this.points.length;j++){
Card tempCard = new Card(this.suits[i], this.points[j]);
selectToCards.add(tempCard);
}
}
System.out.println("****************扑克牌*********************");
System.out.print("[ ");
for (Card card : selectToCards) {
System.out.print(card.toString()+ " | ");
}
System.out.print(" ]");
System.out.println();
}
/**
* 洗牌
*/
private void shuffleCards(){
int k = 0;
int randomIndex;
Card randomCard;
while(k < this.selectToCards.size()){
do{
randomIndex = random.nextInt(this.selectToCards.size());
randomCard = this.selectToCards.get(randomIndex);
}while(this.selectedCards.contains(randomCard));
this.selectedCards.add(randomCard);
k++;
}
System.out.println("****************洗过后的扑克牌*********************");
System.out.print("[ ");
for (Card card : selectedCards) {
System.out.print(card.toString()+ " | ");
}
System.out.print(" ]");
System.out.println();
}
/**
* 创建玩家
*/
public void createPlayer(){
Scanner console = new Scanner(System.in);
try {
System.out.println("请输入参加游戏的玩家数:");
this.playerNum = console.nextInt();
System.out.println("请输入玩家能持牌的数量:");
this.cardNum = console.nextInt();
int i = 1;
while( i <= this.playerNum ){
System.out.println("请输入第"+i+"位玩家的ID和姓名:");
System.out.println("请输入ID:");
int ID = console.nextInt();
boolean flag = true;
for(int p=0;p<this.players.size();p++){
if(this.players.get(p).getId() == ID){
flag = false;
System.out.println("该ID已被玩家占用");
break;
}
}
if(flag){
System.out.println("请输入玩家名字:");
String name = console.next();
Player tempPlayer = new Player(ID, name);
this.players.add(tempPlayer);
i++;
}
}
} catch (InputMismatchException e) {
System.out.println("请输入有效的整型数字");
createPlayer();
}
System.out.println("****************游戏玩家*********************");
System.out.print("[ ");
for (Player player : this.players) {
System.out.print(player.toString()+ " | ");
}
System.out.print(" ]");
System.out.println();
}
/**
* 发牌
* 给玩家每人一张的发,并存储在玩家手牌中
*/
public void dealCard(){
System.out.println("****************给玩家发牌*********************");
int i=0;
while( i < this.cardNum ){
for( int j=0; j<this.players.size(); j++ ){
// System.out.println("玩家"+this.players.get(j).getName()+"拿到"+this.selectedCards.get(j+i*this.playerNum).toString());
this.players.get(j).getCards().add(this.selectedCards.get(j+i*this.playerNum));
}
i++;
}
System.out.println("****************玩家持有的扑克牌*********************");
for (Player player : this.players) {
System.out.print(player.getName()+ ":[");
for (Card card : player.getCards()) {
System.out.print(card.toString());
}
System.out.println(" ]");
}
System.out.println();
}
/**
* 开始比较牌的大小
*/
public void playGame(){
for (Player player : this.players) {
player.setMaxCard(this.points, this.suits);
}
System.out.println("****************玩家手中各自最大的手牌*********************");
for (Player player : this.players) {
System.out.print(player.getName()+ ":[" + player.getMaxCard().toString());
System.out.println(" ]");
}
System.out.println("****************最大手牌的玩家********************* "+ this.players.size());
Player maxCardPlayer = this.players.get(0);
int curMaxPointIdx = 0;
int maxPointIdx = 0;
int curMaxSuitIdx = 0;
int maxSuitIdx = 0;
if( this.players.size() == 1 ){
System.out.println("最大手牌的玩家是:" + maxCardPlayer.getName() + ", 手牌为:" + maxCardPlayer.getMaxCard().toString());
return;
}
for(int i=1; i<this.players.size(); i++){
Player tmpPlayer= this.players.get(i);
for(int j=0; j<this.points.length; j++){
if(maxCardPlayer.getMaxCard().getPoint().equals(this.points[j])){
curMaxPointIdx = j;
}
if(tmpPlayer.getMaxCard().getPoint().equals(this.points[j])){
maxPointIdx = j;
}
}
if( maxPointIdx > curMaxPointIdx ){
maxCardPlayer = tmpPlayer;
}
if(maxPointIdx == curMaxPointIdx){
for(int k=0; k<this.suits.length; k++){
if(maxCardPlayer.getMaxCard().getSuit().equals(this.suits[k])){
curMaxSuitIdx = k;
}
if(tmpPlayer.getMaxCard().getSuit().equals(this.suits[k])){
maxSuitIdx = k;
}
}
if(maxSuitIdx < curMaxSuitIdx){
maxCardPlayer = tmpPlayer;
}
}
}
System.out.println("最大手牌的玩家是:" + maxCardPlayer.getName() + ", 手牌为:" + maxCardPlayer.getMaxCard().toString());
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("*******************游戏结束********************");
}
}
最后我们再建一个测试类,运行我们游戏
import com.yx.game.Game;
public class Application {
public static void main(String[] args) {
Game game = new Game();
game.createPlayer();
game.dealCard();
game.playGame();
}
}
点击查看更多内容
8人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦