C#實(shí)現(xiàn)撲克游戲(21點(diǎn))的示例代碼
一、游戲介紹
1.1 游戲規(guī)則
21點(diǎn)又名黑杰克,該游戲由2到6個(gè)人玩,使用除大小王之外的52張牌,游戲者的目標(biāo)是使手中的牌的點(diǎn)數(shù)之和不超過(guò)21點(diǎn)且盡量大。
1.2 牌點(diǎn)計(jì)算
A至10牌,按其原點(diǎn)數(shù)計(jì)算;J、Q、K都算作10點(diǎn)。
1.3 判斷勝負(fù)
二十一點(diǎn)玩法規(guī)則和概率在二十一點(diǎn)游戲中,擁有最高點(diǎn)數(shù)的玩家獲勝,其點(diǎn)數(shù)必須等于或低于21點(diǎn);超過(guò)21點(diǎn)的玩家稱為爆牌。擁有最高點(diǎn)數(shù)的玩家獲勝,其點(diǎn)數(shù)必須等于或低于21點(diǎn);超過(guò)21點(diǎn)之間判負(fù)。
二、游戲設(shè)計(jì)
2.1 游戲流程
發(fā)牌:
玩家和AI每人發(fā)兩張牌,由手牌點(diǎn)數(shù)和大的玩家優(yōu)先選擇是否在牌堆中取牌
取牌:
手牌點(diǎn)數(shù)和小于21,等待1中優(yōu)先選擇后再順時(shí)針輪到其他玩家選擇是否取牌
取牌后:
若牌點(diǎn)大于21則直接判負(fù)出局,場(chǎng)上只剩1人,直接游戲結(jié)束;否則重復(fù)2-3
若牌點(diǎn)小于等于21則輪到下家取牌,重復(fù)2-3
游戲結(jié)束
其他玩家取牌后都超過(guò)21點(diǎn),只剩1人,直接獲勝
所有玩家都選擇不取牌后,按規(guī)則比較所有玩家手牌點(diǎn)數(shù)和,牌點(diǎn)大的獲勝。
2.2 玩家類
由玩家自己選擇是否繼續(xù)拿牌。(輸入Y繼續(xù)拿牌,N為不拿牌)
2.3 AI類
簡(jiǎn)化AI邏輯,發(fā)牌后AI手牌和為4-8時(shí)繼續(xù)拿牌,一直到17點(diǎn)或17點(diǎn)以上不再拿牌;因?yàn)榇藭r(shí)再拿牌就有一半以上的概率超過(guò)21點(diǎn)。
三、參考代碼
using System; namespace _21dian { using System; using System.Collections.Generic; namespace Game21Points { class Project { static void Main(string[] args) { Console.WriteLine("----- 游戲開始 -----"); // 撲克牌 List<int> cards = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 }; // 創(chuàng)建對(duì)象 Poker poker = new Poker(cards); Player player = new Player(); AIPlayer ai = new AIPlayer(); // --> 玩家入場(chǎng) player.playerName = "Czhenya"; ai.playerName = "AI"; poker.AddPlayer(player); poker.AddPlayer(ai); // 事件關(guān)系綁定 poker.GameSratrHandler += player.GameStart; poker.GameSratrHandler += ai.GameStart; // 游戲開始 poker.GameStart(); // 每人發(fā)兩張牌 poker.SendCard(); poker.SendCard(); // 詢問(wèn)取牌 poker.TaskCard(); Console.ReadKey(); } } abstract class AbsPlayer { public string playerName; public bool IsContinueTakeCard = true; public List<int> handCards = new List<int>(); public abstract void GameStart(); public virtual void SendCard(int card) { handCards.Add(card); } public abstract bool TakeCard(); public bool GameOver() { bool isGameOver; if (HandCardsPoint > 21) { isGameOver = true; } else { isGameOver = !IsContinueTakeCard; } return isGameOver; } public int HandCardsPoint { get { return PokeTools.HandCardsSum(handCards); } } } class Player : AbsPlayer { public override void GameStart() { handCards.Clear(); Console.WriteLine("玩家整理了一下衣服,準(zhǔn)備開局;"); } public override void SendCard(int card) { handCards.Add(card); Console.WriteLine("玩家發(fā)牌:" + PokeTools.PokerBrandByPoint(card)); } public override bool TakeCard() { Console.WriteLine("當(dāng)前您的手牌點(diǎn)數(shù)和為:" + HandCardsPoint); Console.WriteLine("是否繼續(xù)取牌(Y/N)?"); string readStr = Console.ReadLine(); // 輸入Y取牌,其他為不取牌 IsContinueTakeCard = readStr.Equals("Y"); return IsContinueTakeCard; } } class AIPlayer : AbsPlayer { public override void GameStart() { handCards.Clear(); Console.WriteLine("AI:清理一下內(nèi)存,與之一戰(zhàn);"); } public override void SendCard(int card) { base.SendCard(card); Console.WriteLine("AI發(fā)牌:" + PokeTools.PokerBrandByPoint(card)); } public override bool TakeCard() { // 手牌數(shù)點(diǎn)數(shù)小于17,就繼續(xù)取牌 return HandCardsPoint < 17; } } class Poker { List<AbsPlayer> players = new List<AbsPlayer>(); public Action GameSratrHandler; public Action<int> SendCardHandler; public Func<int, bool> TaskCardHandler; // 發(fā)牌用 List<int> sendCards; public Poker(List<int> cards) { // 復(fù)制一份發(fā)牌用 sendCards = new List<int>(cards); } public void AddPlayer(AbsPlayer player) { players.Add(player); } public void GameStart() { for (int i = 0; i < players.Count; i++) { if (!players[i].GameOver()) { players[i].GameStart(); } } } /// <summary> /// 發(fā)牌 -- 會(huì)剔除已經(jīng)發(fā)過(guò)的牌 /// </summary> public void SendCard() { for (int i = 0; i < players.Count; i++) { players[i].SendCard(SendOneCard()); } } int SendOneCard() { // 隨機(jī)發(fā)一張牌 Random random = new Random(); int index = random.Next(0, sendCards.Count); // 取到牌值 int cardPoint = sendCards[index]; // 從手牌中移除 --> 為避免發(fā)到相同的牌 sendCards.RemoveAt(index); return cardPoint; } public void TaskCard() { for (int i = 0; i < players.Count; i++) { // 選擇取牌后再發(fā)一張牌 if (players[i].TakeCard()) { players[i].SendCard(SendOneCard()); } Console.WriteLine($"玩家:{players[i].playerName} 手牌:{PokeTools.ShowHandCard(players[i].handCards)}"); } if (!GameOver()) { TaskCard(); } } public bool GameOver() { int playerCount = 0; for (int i = 0; i < players.Count; i++) { if (!players[i].GameOver()) { playerCount++; } } bool isGameOver = playerCount <= 1; if (isGameOver) { Console.WriteLine("游戲結(jié)束:"); List<AbsPlayer> playerList = new List<AbsPlayer>(); int maxPoint = 0; for (int i = 0; i < players.Count; i++) { if (players[i].HandCardsPoint > 21) { Console.WriteLine($"玩家:{players[i].playerName} 爆牌了" ); } else { playerList.Add(players[i]); if (maxPoint < players[i].HandCardsPoint) { maxPoint = players[i].HandCardsPoint; } } } if (playerList.Count == 0) { Console.WriteLine("平局"); } else if (playerList.Count == 1) { Console.WriteLine($"玩家:{playerList[0].playerName} 贏了"); } else { for (int i = 0; i < playerList.Count; i++) { if (maxPoint == playerList[i].HandCardsPoint) { Console.WriteLine($"玩家:{playerList[i].playerName} 贏了"); } } } } return isGameOver; } } } class PokeTools { /// <summary> /// 根據(jù)牌點(diǎn)返回牌名 如:14 ->紅桃3 /// </summary> /// <param name="card"></param> /// <returns></returns> public static string PokerBrandByPoint(int card) { if (card > 52 || card <= 0) { Console.WriteLine("不是撲克牌點(diǎn)"); return "不是撲克牌點(diǎn)"; } string[] flowerColor = new string[4] { "黑桃", "紅桃", "梅花", "方片" }; string[] points = new string[13] { "K", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q" }; int huaSe = (card - 1) / 13; int point = card % 13; // 返回花色 + 牌點(diǎn) 如:紅桃3 return flowerColor[huaSe] + points[point]; } /// <summary> /// 手牌求和 /// </summary> /// <param name="handCards"></param> /// <returns></returns> public static int HandCardsSum(List<int> handCards) { // "K", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q" int[] points = new int[13] { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10 }; int sumRes = 0; for (int i = 0; i < handCards.Count; i++) { sumRes += points[handCards[i] % 13]; } return sumRes; } // 顯示手牌 public static (string, string) ShowHandCard(List<int> handCards) { string resStr = ""; for (int i = 0; i < handCards.Count; i++) { resStr += PokeTools.PokerBrandByPoint(handCards[i]); if (handCards.Count - 1 != i) { resStr += ","; } } return (resStr, "牌點(diǎn)和:" + PokeTools.HandCardsSum(handCards)); } } }
測(cè)試結(jié)果:
到此這篇關(guān)于C#實(shí)現(xiàn)撲克游戲(21點(diǎn))的示例代碼的文章就介紹到這了,更多相關(guān)C#撲克游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#基礎(chǔ):Equals()與運(yùn)算符==的區(qū)別分析
本篇文章是對(duì)c#中的Equals()與運(yùn)算符==的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05淺析C# 9.0 新特性之 Lambda 棄元參數(shù)
這篇文章主要介紹了C# 9.0 新特性之 Lambda 棄元參數(shù)的的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),想學(xué)習(xí)c#的朋友可以了解下2020-06-06C#實(shí)現(xiàn)在listview中插入圖片實(shí)例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)在listview中插入圖片實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03C#操作Windows服務(wù)類System.ServiceProcess.ServiceBase
這篇文章介紹了C#操作Windows服務(wù)類System.ServiceProcess.ServiceBase,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C#以流方式讀socket超時(shí)設(shè)置的實(shí)例
這篇文章主要為大家詳細(xì)介紹了C#以流方式讀socket超時(shí)設(shè)置的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03基于C#實(shí)現(xiàn)簡(jiǎn)易的鍵盤記錄器
本文將利用C#語(yǔ)言和HOOK技術(shù)來(lái)做一個(gè)鍵盤記錄器,看看一天下來(lái),我們點(diǎn)擊了多少次鍵盤,哪些鍵的使用頻率最高,感興趣的小伙伴可以嘗試一下2022-08-08從Request.Url中獲取根網(wǎng)址的簡(jiǎn)單操作
這篇文章主要介紹了從Request.Url中獲取根網(wǎng)址的簡(jiǎn)單操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01