当前位置:网站首页>Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
2022-07-31 18:33:00 【Chen's words must be done】
C# 之 扑克游戏 -- 21Some rules introduction and code implementation
一,游戏介绍
1.1 游戏规则
21, also known as black jack,该游戏由2到6个人玩,使用除大小王之外的52张牌,游戏者的目标是使手中的牌的点数之和不超过21点且尽量大.
1.2 Card point calculation
A至10牌,按其原点数计算;J、Q、K都算作10点.
1.3 判断胜负
二十一点玩法规则和概率在二十一点游戏中,拥有最高点数的玩家获胜,其点数必须等于或低于21点;超过21点的玩家称为爆牌.拥有最高点数的玩家获胜,其点数必须等于或低于21点;超过21Sentenced to between negative.
二,游戏设计
2.1 游戏流程
发牌:
玩家和AI每人发两张牌,By hand points and big players preference whether to take CARDS in a pile of CARDS取牌:
手牌点数和小于21,等待1Priority in clockwise turn again after other players choose whether to take cardTake after:
If the card point greater than21Directly to the negative out,On the left1人,直接游戏结束;否则重复2-3
If the card points less than or equal to21It is buyers take card,重复2-3游戏结束
Other players get after all over21点,只剩1人,直接获胜
After all players chose not to take the card,According to the rules to compare all players hand points and,Some brand of the big win.
2.2 玩家类
By taking card players to choose whether to continue.(输入YContinue to take card,NIs don't take card)
2.3 AI类
简化AI逻辑,发牌后AIHand and as4-8Continue to take the card,一直到17点或17Above no longer take card;Because at this time to take card more than more than half of the probability21点.
三,参考代码
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
};
// 创建对象
Poker poker = new Poker(cards);
Player player = new Player();
AIPlayer ai = new AIPlayer();
// --> 玩家入场
player.playerName = "Czhenya";
ai.playerName = "AI";
poker.AddPlayer(player);
poker.AddPlayer(ai);
// Relationship between event binding
poker.GameSratrHandler += player.GameStart;
poker.GameSratrHandler += ai.GameStart;
// 游戏开始
poker.GameStart();
// 每人发两张牌
poker.SendCard();
poker.SendCard();
// Ask to take card
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("Players to sort out the clothes,准备开局;");
}
public override void SendCard(int card)
{
handCards.Add(card);
Console.WriteLine("Players licensing:" + PokeTools.PokerBrandByPoint(card));
}
public override bool TakeCard()
{
Console.WriteLine("Your current hand points and as:" + HandCardsPoint);
Console.WriteLine("Whether or not to continue to take card(Y/N)?");
string readStr = Console.ReadLine();
// 输入Y取牌,The other is not take card
IsContinueTakeCard = readStr.Equals("Y");
return IsContinueTakeCard;
}
}
class AIPlayer : AbsPlayer
{
public override void GameStart()
{
handCards.Clear();
Console.WriteLine("AI:清理一下内存,与之一战;");
}
public override void SendCard(int card)
{
base.SendCard(card);
Console.WriteLine("AI发牌:" + PokeTools.PokerBrandByPoint(card));
}
public override bool TakeCard()
{
// Hand a few points less than17,Will continue to take the card
return HandCardsPoint < 17;
}
}
class Poker
{
List<AbsPlayer> players = new List<AbsPlayer>();
public Action GameSratrHandler;
public Action<int> SendCardHandler;
public Func<int, bool> TaskCardHandler;
// Licensing with
List<int> sendCards;
public Poker(List<int> cards)
{
// A copy of licensing with
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>
/// 发牌 -- Would eliminate has sent a card
/// </summary>
public void SendCard()
{
for (int i = 0; i < players.Count; i++)
{
players[i].SendCard(SendOneCard());
}
}
int SendOneCard()
{
// Randomly send a card
Random random = new Random();
int index = random.Next(0, sendCards.Count);
// To brand value
int cardPoint = sendCards[index];
// Removed from the hand --> In order to avoid to the same card
sendCards.RemoveAt(index);
return cardPoint;
}
public void TaskCard()
{
for (int i = 0; i < players.Count; i++)
{
// Select take card again after hair card
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("游戏结束:");
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} Got a card" );
}
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>
/// According to the card point returns brand 如:14 ->红桃3
/// </summary>
/// <param name="card"></param>
/// <returns></returns>
public static string PokerBrandByPoint(int card)
{
if (card > 52 || card <= 0)
{
Console.WriteLine("Not poker points");
return "Not poker points";
}
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;
// Return to design and color + 牌点 如:红桃3
return flowerColor[huaSe] + points[point];
}
/// <summary>
/// Hand sum
/// </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, "Card point and:" + PokeTools.HandCardsSum(handCards));
}
}
}
测试结果:
边栏推荐
- Huawei mobile phone one-click to open "maintenance mode" to hide all data and make mobile phone privacy more secure
- How programmers learn open source projects, this article tells you
- Combinatorics Notes (6) Associative Algebra of Locally Finite Partially Ordered Sets, Möbius Inversion Formula
- 【Yugong Series】July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
- AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
- Istio介绍
- Thymeleaf是什么?该如何使用。
- MySQL---单行函数
- MySQL---Create and manage databases and data tables
- go mode tidy出现报错go warning “all“ matched no packages
猜你喜欢

MySQL - multi-table query

Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds

2022 Android interview summary (with interview questions | source code | interview materials)

iNeuOS工业互联网操作系统,设备运维业务和“低代码”表单开发工具

IP protocol from 0 to 1

Flink_CDC搭建及简单使用

杰理语音芯片ic玩具芯片ic的介绍_AD14NAD15N全系列开发

flyway的快速入门教程

MySQL---单行函数

Tkinter 入门之旅
随机推荐
【luogu P8326】Fliper (Graph Theory) (Construction) (Eulerian Circuit)
移动web开发02
自动化测试—web自动化—selenium初识
Bika LIMS 开源LIMS集—— SENAITE的使用(检测流程)
Write a database document management tool based on WPF repeating the wheel (1)
After Effects tutorial, How to adjust overexposed snapshots in After Effects?
matplotlib ax bar color Set the color, transparency, label legend of the ax bar
MySQL---排序与分页
基于WPF重复造轮子,写一款数据库文档管理工具(一)
【NLP】什么是模型的记忆力!
多数据中心操作和检测并发写入
MySQL---基本的select语句
1161. 最大层内元素和 : 层序遍历运用题
MySQL---aggregate function
UVM RAL模型和内置seq
【码蹄集新手村600题】不通过字符数组来合并俩个数字
【AcWing】第 62 场周赛 【2022.07.30】
九齐ny3p系列语音芯片替代国产方案KT148A性价比更高420秒长度
每日练习------随机产生一个1-100之间的整数,看能几次猜中。要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”。
Automated testing - web automation - first acquaintance with selenium