您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何使用Java代碼實現一個簡單的撲克牌游戲”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“如何使用Java代碼實現一個簡單的撲克牌游戲”文章能幫助大家解決問題。
1、創建撲克牌。包括四種花色(黑桃,紅心,梅花,方塊),十三種點數(2-10,J,Q,K),不考慮大小王。
2、創建兩個玩家。包括玩家id、姓名、手牌等集合,手牌為撲克牌的集合。
3、洗牌。將創建好的撲克牌打亂。
4、發牌。將洗牌之后的撲克牌集合,從第一張開始,發給兩個玩家,安裝一人 一張的方式,每人發兩張。
5、游戲。比較兩名玩家手中的撲克牌。規則為:去兩名玩家手上點數最大的牌進行比較,點數大的贏,若點數一樣大,就按照色花進行比較。
1、洗牌,如何將撲克牌進行打亂。(主要知識點Random,list.contains())
2、撲克牌如何進行大小比較。(主要知識點collections.sort() , comparable)
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; // 開始創建撲克牌時的序列
List<Card> cardEndList; // 洗牌后的序列
List<Player> playerList; // 玩家列表
public Welcome(){
this.cardStartList= new ArrayList<Card>();
this.cardEndList = new ArrayList<Card>();
this.playerList = new ArrayList<Player>();
}
// 創建撲克牌
void createCard(){
System.out.println("-----------創建撲克牌---------------");
for (String name : names) {
for (String num : nums) {
cardStartList.add(new Card(num,name));
}
}
System.out.println("---------撲克牌創建成功--------------");
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 && 輸入整數判斷
@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("請輸入整數");
input = new Scanner(System.in);
}
}
return id;
}
// 創建角色
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());
}
}
// 給角色發牌
private void sendCard() {
System.out.println("-----------開始發牌---------------");
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("-----------發牌結束---------------");
}
// 游戲開始
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(); // 創建撲克牌
we.shuffleCard(); // 洗牌
we.createPlayers(); // 創建角色
we.sendCard(); // 給角色發牌
we.playGame(); // 游戲開始
}
}
關于“如何使用Java代碼實現一個簡單的撲克牌游戲”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。