大家對于撲克牌都不陌生,畢竟是作為我們?nèi)粘I钪幸豁椇唵斡腥?,又能促進親朋好友之間關系的游戲。本篇文章將為大家講述如何使用 Java 代碼自己敲一個簡單而有趣的撲克牌小游戲,從而促進 Java 語言的理解。
功能描述
1、創(chuàng)建撲克牌。包括四種花色(黑桃,紅心,梅花,方塊),十三種點數(shù)(2-10,J,Q,K),不考慮大小王。
2、創(chuàng)建兩個玩家。包括玩家id、姓名、手牌等集合,手牌為撲克牌的集合。
3、洗牌。將創(chuàng)建好的撲克牌打亂。
4、發(fā)牌。將洗牌之后的撲克牌集合,從第一張開始,發(fā)給兩個玩家,安裝一人 一張的方式,每人發(fā)兩張。
5、游戲。比較兩名玩家手中的撲克牌。規(guī)則為:去兩名玩家手上點數(shù)最大的牌進行比較,點數(shù)大的贏,若點數(shù)一樣大,就按照色花進行比較。
難點分析:
1、洗牌,如何將撲克牌進行打亂。(主要知識點Random,list.contains())
2、撲克牌如何進行大小比較。(主要知識點collections.sort() , comparable)
實現(xiàn)代碼
Card.java(撲克牌類)
package com.betty.dome2;
public class Card implements Comparable<Card>{
// 牌面
private String num;
// 花色
private String name;
public Card(){
}
public Card(String num,String name){
this.num = num;
this.name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((num == null) ? 0 : num.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
return true;
}
// 獲取牌面大小
private Integer getNumValue(String num){
switch(num){
case "A": return 12;
case "2": return 13;
case "3": return 1;
case "4": return 2;
case "5": return 3;
case "6": return 4;
case "7": return 5;
case "8": return 6;
case "9": return 7;
case "10": return 8;
case "J": return 9;
case "Q": return 10;
case "K": return 11;
}
return -1;
}
// 獲取花色大小
private Integer getNameValue(String name){
switch(name){
case "黑桃": return 4;
case "紅心": return 3;
case "梅花": return 2;
case "方塊": return 1;
}
return -1;
}
@Override
public int compareTo(Card card) {
// 如果牌面相同就看花色
int numCompare = getNumValue(this.num).compareTo(getNumValue(card.num));
if(numCompare == 0){
return getNameValue(this.name).compareTo(getNameValue(card.name));
}
return numCompare;
}
}
Player.java(角色類)
package com.betty.dome2;
import java.util.ArrayList;
import java.util.List;
public class Player {
// 角色id
private Integer ID;
// 角色姓名
private String name;
// 角色獲得的牌
List<Card> cardList;
public Player(){
}
public Player(Integer ID,String name){
this.ID = ID;
this.name = name;
this.cardList = new ArrayList<Card>();
}
public Integer getID() {
return ID;
}
public void setID(Integer iD) {
ID = iD;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Welcome.java(主程序)
package com.betty.dome2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Welcome {
String[] nums = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] names = {"黑桃","紅心","梅花","方塊"};
List<Card> cardStartList; // 開始創(chuàng)建撲克牌時的序列
List<Card> cardEndList; // 洗牌后的序列
List<Player> playerList; // 玩家列表
public Welcome(){
this.cardStartList= new ArrayList<Card>();
this.cardEndList = new ArrayList<Card>();
this.playerList = new ArrayList<Player>();
}
// 創(chuàng)建撲克牌
void createCard(){
System.out.println("-----------創(chuàng)建撲克牌---------------");
for (String name : names) {
for (String num : nums) {
cardStartList.add(new Card(num,name));
}
}
System.out.println("---------撲克牌創(chuàng)建成功--------------");
System.out.print("[");
for(int i=0;i<cardStartList.size();i++){
Card card = cardStartList.get(i);
if(i%13!=0) System.out.print(",");
if((i+1)%13==1 && i!=0){
System.out.println("]");
System.out.print("[");
}
System.out.print(card.getName()+card.getNum());
}
System.out.println("]");
}
// 洗牌
void shuffleCard(){
System.out.println("-----------開始洗牌---------------");
Random random = new Random();
Integer listSize = cardStartList.size();
for(int i=0;i<listSize;i++){
Card cd = new Card();
do{
cd = cardStartList.get(random.nextInt(listSize));
}while(cardEndList.contains(cd));
cardEndList.add(cd);
}
System.out.println("-----------洗牌結束---------------");
}
// 輸入角色姓名
@SuppressWarnings("resource")
private String getName() {
System.out.print("輸入姓名:");
Scanner input = new Scanner(System.in);
return input.next();
}
// 輸入角色id && 輸入整數(shù)判斷
@SuppressWarnings("resource")
private Integer getId() {
Scanner input = new Scanner(System.in);
Integer id;
while(true){
try {
System.out.print("輸入ID:");
id = input.nextInt();
break;
} catch (Exception e) {
System.out.println("請輸入整數(shù)");
input = new Scanner(System.in);
}
}
return id;
}
// 創(chuàng)建角色
private void createPlayers() {
Integer playerNum = 2;
for(int i=0;i<playerNum;i++){
playerList.add(new Player(getId(), getName()));
}
for (Player player : playerList) {
System.out.println("----歡迎玩家 :" + player.getName());
}
}
// 給角色發(fā)牌
private void sendCard() {
System.out.println("-----------開始發(fā)牌---------------");
int cardSendNum = 2,index = 0;
for(int i=0;i<cardSendNum;i++){
for(int j=0;j<playerList.size();j++){
Player player = playerList.get(j);
player.cardList.add(cardEndList.get(index++));
System.out.println("----玩家 :" + player.getName()+" 拿牌");
}
}
System.out.println("-----------發(fā)牌結束---------------");
}
// 游戲開始
private void playGame() {
System.out.println("-----------游戲開始---------------");
Card maxCard = new Card("0","方塊");
Player winner = new Player();
for(int i=0;i<playerList.size();i++){
Player player = playerList.get(i);
List<Card> cardList = player.cardList;
Collections.sort(cardList);
Card card = cardList.get(cardList.size()-1);
if(maxCard.compareTo(card)<0){
maxCard = card;
winner = player;
}
System.out.println("----玩家:"+ player.getName()
+ "最大的牌為:" + card.getName()+ card.getNum());
}
System.out.println("-----------玩家:"+winner.getName()+"獲勝----------");
System.out.println("玩家各自的手牌為:");
for(int j=0;j<playerList.size();j++){
Player player = playerList.get(j);
System.out.print("玩家 :" + player.getName()+"[");
for(int i=0;i<player.cardList.size();i++){
if(i!=0) System.out.print(",");
System.out.print(player.cardList.get(i).getName()+
player.cardList.get(i).getNum());
}
System.out.print("]
");
}
}
public static void main(String[] args) {
Welcome we = new Welcome();
we.createCard(); // 創(chuàng)建撲克牌
we.shuffleCard(); // 洗牌
we.createPlayers(); // 創(chuàng)建角色
we.sendCard(); // 給角色發(fā)牌
we.playGame(); // 游戲開始
}
}
實現(xiàn)的效果圖:
總結
關于本篇“【Java完整實例】實現(xiàn)一個簡單的撲克牌游戲”就介紹到此結束了,想要了解更多和 Java 相關的小游戲內(nèi)容請搜索W3Cschool以前的文章或繼續(xù)瀏覽下面的相關文章,也希望大家以后多多支持!