二十一点卡,字符串或整数的数组? [英] Array for Blackjack cards, strings or integers?

查看:78
本文介绍了二十一点卡,字符串或整数的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看了一些有关BlackJack的问题后,人们对使用哪种更好的卡感到困惑,这些卡要么是由字符串/项目组成的数组,要么是由整数组成的数组.

在我的BlackJack游戏中,A仅11,而J,Q和K都为10,与西装无关.什么使我更容易用字符串或整数对卡进行编码?

Visual Studio C#2010,Windows窗体应用程序

解决方案

除了实际的文本外,字符串几乎绝不是任何东西的最佳内部表示形式.弦是给人的;弦是给人的.计算机使用数字.尤其对于卡来说,这很容易,因为卡经常需要按等级进行比较,并按价值相加.告诉一个程序12> 11或向总数中添加10所需的代码比告诉它"K">"Q"或向总数中添加"J"所花费的代码少得多.对于新手程序员来说,在内部使用字符串是很常见的,因为他们懒得学习数据表示.

当然,在像C ++这样的面向对象的语言中,您可以使用对象,但是拥有卡级别和适合条件的那些对象的成员变量应该是整数,以便您可以索引查找表,比较范围等./p>

我在此处.

对于二十一点,如果您不需要通用卡表示形式,那么仅使用1到10的整数是理想的选择.将1用作A,而不是11,这将使您的总计算速度更快:您只需要将一个A从1提升到11,但是就需要将几个A从11降为1.

例如,如果您有一个表示手的这些整数组成的数组,则将手总数相加是这样的(当然是充实的):

int total = 0, acefound = 0, soft = 0;

for (int i = 0; i < cardsinhand; ++i) {
    total += hand[i];
    if (1 == hand[i]) acefound = 1;
}
if (acefound && total < 12) {
    soft = 1;
    total += 10;
}

简单,快速.如果您代表实际的卡,那么您将拥有面卡的等级为11、12、13,则只需在其中添加if (r > 10) r = 10;之类的东西(将A设为1的另一个原因)即可.我可以在几分钟内模拟十亿的手.

Having a look at related BlackJack questions, there is a confusion on what is better to use for the cards, either making an array of strings/items or integers.

With my BlackJack game Aces are 11 only and 10, J, Q and K are all value 10, doesn't matter of the suit. What would make it easier for me to code the cards with, strings or integers?

Visual Studio C# 2010, Windows form application

解决方案

Strings are almost never the best internal representation for anything but actual text. Strings are for people; computers use numbers. For cards especially it's a no-brainer, since cards often need to be compared by rank and added by value. It takes a lot less code to tell a program that 12 > 11 or to add 10 to a total than it does to tell it that "K" > "Q" or to add "J" to a total. Using strings internally is common with rookie programmers too lazy to learn about data representation.

Of course in an object-oriented language like C++ you can use objects, but the member variables of those objects that hold the card rank and suits should be integers so you can index lookup tables, compare ranges, and so on.

I wrote an essay on card representations here.

For blackjack, if you don't need a general-purpose card representation, then just using the integers 1 to 10 is ideal. Use 1 for aces, not 11, it will make your total calculations faster: you only ever need to promote one ace from 1 to 11, but you'd need to demote several from 11 to 1.

For example, if you have an array of these integers representing a hand, adding the hand total is something like this (of course, fleshed out):

int total = 0, acefound = 0, soft = 0;

for (int i = 0; i < cardsinhand; ++i) {
    total += hand[i];
    if (1 == hand[i]) acefound = 1;
}
if (acefound && total < 12) {
    soft = 1;
    total += 10;
}

Simple, and blindingly fast. If you are representing actual cards, so you'll have face cards with ranks 11, 12, 13, then just add something like if (r > 10) r = 10; in there (another reason to make aces 1). I can simulate billions of hands in minutes like this.

这篇关于二十一点卡,字符串或整数的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆