桥牌游戏和显示牌演示(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/a-bridge-card-game-and-display-card-presentation-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2501 个词 阅读量 0桥牌游戏和显示牌演示(译文)
原文地址:https://www.codeproject.com/Articles/328668/A-Bridge-Card-Game-and-Display-Card-Presentation
原文作者:C Yang
译文由本站 robot-v1.0 翻译
前言
Play random drawing four players bridge card game
玩随机抽奖的四人桥牌游戏
- 下载源(BridgeGameHand)-13.4 KB(Download source (BridgeGameHand) - 13.4 KB)
- 下载源(BridgeCardGame V3)-22.2 KB(Download source (BridgeCardGame V3) - 22.2 KB)
要玩以上游戏,请下载(To play the above game, download)BridgeCardGameV3.zip(BridgeCardGameV3.zip).(.)
介绍(Introduction)
我相信那些学习数学或编程的人迟早会学习纸牌游戏.过去我曾多次访问过该主题.在我学习面向对象程序设计或概率与统计时.最近,我读了(I believe that those who study math or programming will study Card games sooner or later. I have visited this topic several times in the past; either when I studied object-oriented programming or probability and statistics. Recently, I read) Gary Stafford在C#中进行的战争卡片游戏模拟(War Card Game Simulation in C# by Gary Stafford) 我非常喜欢它,因为它与其他Card程序不同,并且具有一些独特的功能,并且他的代码在整个文档中都有很好的记录,并且易于遵循.(and I liked it very much because it is different and had some distinct features over other Card programs and his code is very well documented throughout and easy to follow.) 最大的优点是它的简单性,不需要链接到任何DLL,例如(The greatest advantage is its simplicity and there is no need to link to any DLLs such as)Cards.dll(Cards.dll).(.) 通常,链接到正确的DLL并查找和下载DLL不是一件容易的事.花了一些时间下载并链接后才发现它是错误版本的DLL.使用Gary的代码,您可以立即启动并运行它,并专注于研究纸牌游戏算法,而不必处理编程接口问题.(Usually, it is not a simple task to link to the right DLLs and to locate and download DLLs. After spending time downloading and linking only to find out it was the wrong version of the DLLs. With Gary’s code, you can get it up and running in no time and focus on studying the card game algorithms instead of dealing with programming interface issues.) 对于编程实践,我采用了他的代码和我在Google上搜索过的其他示例卡类,并将其转变为Bridge Game演示.如果您在机场很无聊,则可以随机显示Bridge的四只手进行查看.本文还可以作为将来扩展的基础.(For programming practice, I adopted his code and other sample card classes I Googled and made it into a Bridge Game presentation. If you are bored at the airport, you can randomly display the four hands of Bridge for viewing. This article can also be a base for expanding in future.) 本文将演示以下内容:(This article will demonstrate the following:)
- 对现有CodeProject文章进行修改(Making a variation out of an existing CodeProject article)
- 使用面向对象的程序设计(The use of object-oriented programming)
- 使用XML和XML样式表进行漂亮的演示(Making a nice presentation with XML and XML style-sheet)
- 讨论未来的编程思想(Discuss future programming ideas)
使用代码(Using the Code)
我发现一个很基本的(I found a very basic) Deck
来自的班级(class from) introcs.cs.princeton.edu Java中的Deck类代码(introcs.cs.princeton.edu Deck class code in Java) .令我惊讶的是,Java和C#非常相似.班级很短,一副52张随机洗牌的牌.我添加了一个模块,两个(. To my surprise, Java and C# are very similar. The class is very short, that is a deck of 52 random shuffle cards. I added one module, two) dictionary
对象,以及一套显示字符集的漂亮西装.该项目是在Microsoft Visual C#2010 Express下编译的.(objects, and a nice suit displaying the characters set. The project was compiled under Microsoft Visual C# 2010 Express.)
static string[] suit = { "♦", "♣", "♥", "♠" };
static string[] rank = { "2", "3", "4", "5", "6",
"7", "8", "9", "10", "J", "Q", "K", "A" };
// Dictionary contains the index value to card suit and rank
// for example, A♠ = 52, 2♦ = 0
// so it can display nicely as player groups his cards on hand
Dictionary<string, int> cardOrder = new Dictionary<string, int>();
Dictionary<int, string> orderedDeck = new Dictionary<int, string>();
/// <summary>
/// format drawing cards to xml tagged string for nice display
/// refer to t.xml and t.css
/// </summary>
//
public string drawCardXml(int numCard, int sideIdx)
{
string handxml = "";
orderedDeck.Clear();
if (CardIndex + numCard <= N) //enough cards left to draw
{
for (int i = CardIndex; i < CardIndex + numCard; i++)
{
Console.Out.WriteLine(deck[i]);
orderedDeck.Add(cardOrder[deck[i]], deck[i]);
}
CardIndex += numCard;
// order cards on hand by suit
var sortedDict = (from entry in orderedDeck orderby entry.Key descending
select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
string[] suitOnHand = { "", "", "", "" };
char[] suitchar = { '♦', '♣', '♥', '♠' };
char[] side = { 'N', 'E', 'W', 'S' };
string[] suitName = { "DIAMONDS-", "CLUBS-", "HEARTS-", "SPADES-" };
foreach (KeyValuePair<int, string> pair in sortedDict)
{
int idx = pair.Key / RANKS;
// extract only the value of the card
suitOnHand[idx] += pair.Value.TrimEnd(suitchar[idx]) + ", ";
}
for (int i = SUITS-1; i >= 0; i--) // reverse order, spade on top
{
handxml += "<" + suitName[i] + side[sideIdx] + ">" + suit[i] + ": "
+ suitOnHand[i].TrimEnd(new Char[] { ' ', ',' })
+ @"</" + suitName[i] + side[sideIdx] + ">";
}
}
return handxml;
}
drawCardXml
返回一个(returns a) string
将会保存到(that will be saved to)**文件(t.xml)**用于显示(for displaying;)**文件(t.xml)**显示如下:(shows as follows:)
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="t.css"?>
<GAME><BRIDGE>
<SPADES-N>♠: 5</SPADES-N>
<HEARTS-N>♥: Q , 9 , 8 , 2</HEARTS-N>
<CLUBS-N>♣: 10 , 5</CLUBS-N>
<DIAMONDS-N>♦: 10 , 9 , 7 , 6 , 5 , 4</DIAMONDS-N>
<SPADES-E>♠: 9 , 4 , 2</SPADES-E>
<HEARTS-E>♥: K , 7 , 4</HEARTS-E>
<CLUBS-E>♣: K , Q , 7 , 3</CLUBS-E>
<DIAMONDS-E>♦: K , 8 , 2</DIAMONDS-E>
<SPADES-W>♠: A , Q , J , 10 , 3</SPADES-W>
<HEARTS-W>♥: J , 10 , 6</HEARTS-W>
<CLUBS-W>♣: 9 , 8 , 6 , 4</CLUBS-W>
<DIAMONDS-W>♦: J</DIAMONDS-W>
<SPADES-S>♠: K , 8 , 7 , 6</SPADES-S>
<HEARTS-S>♥: A , 5 , 3</HEARTS-S>
<CLUBS-S>♣: A , J , 2</CLUBS-S>
<DIAMONDS-S>♦: A , Q , 3</DIAMONDS-S>
</BRIDGE></GAME>
最后,我们需要一个样式表(At last, we need a stylesheet)**吨(t.css)**在屏幕上以不同的颜色排列四个桥式指针.(to arrange four bridge hands on the screen with different colors.)
GAME
{
background-color: #ffffff;
width: 100%;
}
BRIDGE
{
display: block;
margin-bottom: 30pt;
margin-left: 0;
}
SPADES-N
{
display: block;
color: #000000;
font-size: 20pt;
margin-left: 200pt;
}
CLUBS-N
{
display: block;
color: #000000;
font-size: 20pt;
margin-left: 200pt;
}
HEARTS-N
{
display: block;
color: #FF0000;
font-size: 20pt;
margin-left: 200pt;
}
DIAMONDS-N
{
display: block;
color: #FF0000;
font-size: 20pt;
margin-left: 200pt;
}
未来项目构想(Future Project Idea)
数据库中包含一些报纸出版的带有竞标合同的桥牌游戏,并且游戏步骤逐步显示纸牌游戏的进度.(A database contains a few newspaper published bridge games with the bidding contract and steps of the game slowly displaying the progress of the card game.)
参考文献(References)
历史(History)
- 11(11)日(th)2012年2月-第一版(显示卡)(February, 2012 - First version (display card))
- 17(17)日(th)2012年2月-V3(播放卡);修复了错误并添加了AI以生成默认合同(February, 2012 - V3 (Play Card); fixed bugs and added AI to generate default contract)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C#3.5 C# .NET Windows Dev 新闻 翻译