21点游戏 书上的的范例,稍改了点,不够精简,只好先放着了
来源:互联网 发布:淘宝网店的发展前景 编辑:程序博客网 时间:2024/05/02 03:09
using System;public enum CardSuit { Zero_Error, 梅花, 方块, 红桃, 黑桃}public enum CardValue { Zero_Error, Ace, 二, 三, 四, 五, 六, 七, 八, 九, 十, Jack, Queen, King}struct card { public CardSuit suit; public CardValue val; public int CardValue { get { int retval; if ((int)this.val >= 10) retval = 10; else if ((int)this.val == 1) retval = 11; else retval = (int)this.val; return retval; } } public override string ToString() { return (string.Format("{0}{1}", this.suit.ToString("G"), this.val.ToString("G"))); }}class deck { public card[] cards = new card[53]; int next; public deck() { next = 1; cards[0].val = 0; cards[0].suit = 0; int currcard = 0; for (int suitctr = 1; suitctr < 5; suitctr++) { for (int valctr = 1; valctr < 14; valctr++) { currcard = valctr + (suitctr - 1) * 13; cards[currcard].val = (CardValue)valctr; cards[currcard].suit = (CardSuit)suitctr; } } } public void shuffle() { Random rnd = new Random(); int sort1; int sort2; card tmpcard = new card(); for (int ctr = 0; ctr < 100; ctr++) { sort1 = (int)(rnd.NextDouble() * 52 + 1); sort2 = (int)(rnd.NextDouble() * 52 + 1); tmpcard = this.cards[sort1]; this.cards[sort1] = this.cards[sort2]; this.cards[sort2] = tmpcard; } this.next = 1; } public card dealCard() { if (next > 52) return (this.cards[0]); else return this.cards[next++]; }}class CardGame { static deck mydeck = new deck(); static card[] pHand = new card[10]; static card[] cHand = new card[10]; public static void Main() { int pCardCtr = 0; int pTotal = 0; int cTotal = 0; bool playing = true; while (playing == true) { pTotal = 0; cTotal = 0; pCardCtr = 0; for (int ctr = 0; ctr < 10; ctr++) { pHand[ctr].val = 0; pHand[ctr].suit = 0; } Console.WriteLine("\nShuffling cards..."); mydeck.shuffle(); Console.WriteLine("Dealing cards..."); pHand[0] = mydeck.dealCard(); cHand[0] = mydeck.dealCard(); pHand[1] = mydeck.dealCard(); cHand[1] = mydeck.dealCard(); cTotal = cHand[0].CardValue; bool PlayersTurn = true; do { Console.WriteLine("\n玩家:"); pCardCtr = 0; pTotal = 0; do { Console.WriteLine(" Card{0}:{1}", pCardCtr + 1, pHand[pCardCtr].ToString()); pTotal += pHand[pCardCtr].CardValue; pCardCtr++; } while ((int)pHand[pCardCtr].val != 0); Console.WriteLine("庄家:"); Console.WriteLine("Card1:{0}", cHand[0].ToString()); Console.WriteLine("===================="); Console.WriteLine("玩家 Total={0} \n庄家 Total={1}", pTotal, cTotal); if (pTotal <= 21) PlayersTurn = GetPlayerOption(pCardCtr); else PlayersTurn = false; } while (PlayersTurn == true); if (pTotal > 21) Console.WriteLine("\n\n****BUSTED****\n"); else { cTotal += cHand[1].CardValue; int cCardCtr = 2; Console.WriteLine("\n\n玩家\'s Total:{0}", pTotal); Console.WriteLine("\nComputer:"); Console.WriteLine(" {0}", cHand[0].ToString()); Console.WriteLine(" {0} Total:{1}", cHand[1].ToString(), cTotal); while (cTotal < 17) { cHand[cCardCtr] = mydeck.dealCard(); cTotal += cHand[cCardCtr].CardValue; Console.WriteLine(" {0} Total:{1}", cHand[cCardCtr].ToString(), cTotal); cCardCtr++; } if (cTotal > 21) { Console.WriteLine("\n\nComputer Busted!"); Console.WriteLine("YOU WON!!!"); } else { if (pTotal > cTotal) Console.WriteLine("\n\nYOU WON!!!"); else if (pTotal == cTotal) Console.WriteLine("\n\n平局"); else Console.WriteLine("\n\nSorry,The Computer won"); } Console.Write("\n\nDo you want to play again?"); string answer = Console.ReadLine(); try { if (answer[0] != 'y' && answer[0] != 'Y') playing = false; } catch { playing = false; } } } } static bool GetPlayerOption(int cardctr) { string buffer; bool cont = true; bool retval = true; while (cont == true) { Console.Write("\n\nH=Hit,S=Stay "); buffer = Console.ReadLine(); try { if (buffer[0] == 'h' || buffer[0] == 'H') { pHand[cardctr] = mydeck.dealCard(); cont = false; } else if (buffer[0] == 's' || buffer[0] == 'S') { retval = false; cont = false; } else Console.WriteLine("\n***Please enter an H or S and press Enter..."); } catch { cont = true; } } return retval; }}
0 0
- 21点游戏 书上的的范例,稍改了点,不够精简,只好先放着了
- 点名帖,只好回了。
- 昨晚看了点书上的查找和排序算法
- DELPHI 图片任意角旋转。 我看着书上的C++代码改的,加了点自己的理解。huruihappy
- 之前的博客名字不能改了,没办法只好重新注册了。。。
- 妈蛋,怎么发现百度空间以前写的东西居然连个分类都没有了,只好转到CSDN存点学习材料了
- 个人写的一个比较繁琐的表达式了,能够精简点吗?
- 点就展开的范例
- 实现满二叉树的节点查找 书上的,竟然还错了,我给他改了
- 终于用上大点的显示器了
- 做了点美工的工作
- 看了点SOA的文章
- 肚子饿了,去找点吃的
- 工作了三年,心的转点
- 最近找到了点,变成的乐趣
- 学了点前端的总结
- 了解了点C#的基础知识
- 求了一天的割点
- 构建Qt 开发环境
- libvirt库的API参考文档总结
- Android事件分发、View事件Listener全解析
- 2014年读书学习计划
- hdu2024 C语言合法标识符
- 21点游戏 书上的的范例,稍改了点,不够精简,只好先放着了
- [高斯消元+拓展欧几里得] poj 2065 SETI
- 读书笔记-Filter
- asp.net中的<%%>形式的详细用法总结
- 【环境配置】配置svn
- Asterisk呼叫中心之数据库查询与播报(二)
- OpenCV目标跟踪之质心跟踪(Centroid)
- 保定哪个医院做人流手术好
- jQuery上传插件Uploadify 3.2.1 简单例子