选择和使用适当的数据类型 [英] selecting and working with appropriate data type

查看:90
本文介绍了选择和使用适当的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出哪种数据类型适合在游戏中代表一张

卡。我认为可行的想法是一个

结构,foo,有两个整数字段和两个char数组字段:

index cardno description suit

(1,1,两个俱乐部,''c'')

(2,2,三个俱乐部,''c'')

(3,3,四个俱乐部,''c'')

(4,4,五个俱乐部,''c'')

(5,5 ,六个俱乐部,''c'')

I''m trying to figure out what data type is appropriate to represent a
card in a game. The idea that I thought was going to work was a
struct, foo, with two integer fields and two fields of char arrays:
index cardno description suit
( 1, 1,Two of clubs ,''c'')
( 2, 2,Three of clubs ,''c'')
( 3, 3,Four of clubs ,''c'')
( 4, 4,Five of clubs ,''c'')
( 5, 5,Six of clubs ,''c'')

推荐答案

>>>>"LS == lane straatman< gr ********** @ netzero.netwrites:


LSI'我想弄清楚哪种数据类型适合

LS代表游戏中的一张牌。我认为这个想法是好的。

LS工作是一个结构,foo,有两个整数字段和两个
字符串的LS字符串:


为什么在整数足够时使用结构?


card_no / 13会给你套装,0-3。


card_no%13会给你数字,0-12。


数据结构中的其他内容都是多余的信息。


Charlton < br $> b $ b -

Charlton Wilbur
cw ***** @ chromatico .net
>>>>"LS" == lane straatman <gr**********@netzero.netwrites:

LSI''m trying to figure out what data type is appropriate to
LSrepresent a card in a game. The idea that I thought was going
LSto work was a struct, foo, with two integer fields and two
LSfields of char arrays:

Why use a struct when an integer will suffice?

card_no / 13 will give you the suit, 0-3.

card_no % 13 will give you the number, 0-12.

Everything else in your data structure is redundant information.

Charlton
--
Charlton Wilbur
cw*****@chromatico.net




lane straatman写道:

lane straatman wrote:

I我想弄清楚什么数据类型适合在游戏中代表一张

卡。我认为可行的想法是一个

结构,foo,有两个整数字段和两个char数组字段:

index cardno description suit

(1,1,两个俱乐部,''c'')

(2,2,三个俱乐部,''c'')

(3,3,四个俱乐部,''c'')

(4,4,五个俱乐部,''c'')

(5,5 ,六个俱乐部,''c'')



。 [cardno = index%13]



(48,9,十分黑桃,'s'')

(49, 10,黑桃杰克,'s'')
(50,11,黑桃皇后,'s'')

(51,12,King of of黑桃,'s'')

(52,13,黑桃王牌,'s'')

让我们说一个人被发牌5这意味着foo.index = 5&

foo.index = 48。一个人如何测试他是否有一对?

我无法理解它并怀疑我的数据类型是什么

isn''这和我想的那样有用。任何提示

赞赏。 LS
I''m trying to figure out what data type is appropriate to represent a
card in a game. The idea that I thought was going to work was a
struct, foo, with two integer fields and two fields of char arrays:
index cardno description suit
( 1, 1,Two of clubs ,''c'')
( 2, 2,Three of clubs ,''c'')
( 3, 3,Four of clubs ,''c'')
( 4, 4,Five of clubs ,''c'')
( 5, 5,Six of clubs ,''c'')
.
. [cardno = index % 13]
.
( 48, 9,Ten of spades ,''s'')
( 49, 10,Jack of spades ,''s'')
( 50, 11,Queen of spades ,''s'')
( 51, 12,King of spades ,''s'')
( 52, 13,Ace of spades ,''s'')
Let''s say a person is dealt card 5 and 48. This means foo.index = 5 &&
foo.index = 48 . How does a person test to see whether he has a pair?
I can''t get my head around it and suspect that my datatype as it is
isn''t as useful as I thought it was going to be. Any hints
appreciated. LS



这不是典型的枚举应用 - 一个用于套装

和一个用于价值?

Isn''t this classically an application of enumerations - one for suit
and one for value?


" Charlton Wilbur" < cw ***** @ chromatico.netwrote in message

news:87 ************ @ mithril.chromatico.net ...
"Charlton Wilbur" <cw*****@chromatico.netwrote in message
news:87************@mithril.chromatico.net...

>>>>>"" LS" == lane straatman< gr ********** @ netzero.netwrites:
>>>>>"LS" == lane straatman <gr**********@netzero.netwrites:



LSI'想要搞清楚什么数据类型适合

LS代表游戏中的卡片。我认为这个想法是好的。

LS工作是一个结构,foo,有两个整数字段和两个
字符串的LS字符串:


为什么在整数足够时使用结构?


card_no / 13会给你套装,0-3。


card_no%13会给你数字,0-12。


数据结构中的其他所有内容都是冗余信息。


LSI''m trying to figure out what data type is appropriate to
LSrepresent a card in a game. The idea that I thought was going
LSto work was a struct, foo, with two integer fields and two
LSfields of char arrays:

Why use a struct when an integer will suffice?

card_no / 13 will give you the suit, 0-3.

card_no % 13 will give you the number, 0-12.

Everything else in your data structure is redundant information.



关于OP的问题...


有几种善良的衡量标准。代表:


a)您是否可以有效地存储和提取您通常需要的信息

。 (i / 13和i%13非常高效。)


b)表示是否是人类可解释的(解释内存

转储,调试信息, (b))是否所有可能出现的位模式都是有效的(如果没有,则会导致逻辑错误)。* b $ b导致逻辑错误。 (一般来说,最好的情况是一组

枚举,其中实际空间是每个

个别空间的笛卡尔积。)

< a rel =nofollowhref =http://en.wikipedia.org/wiki/Cartesian_producttarget =_ blank> http://en.wikipedia.org/wiki/Cartesian_product


(幸运的是,上面的URL给出了一个扑克牌示例)。


Wilbur先生的建议可能是最好的。


一个或许相等的回退是(数字)x(西装)。然而,这是我的理解,笑话没有西装,所以这意味着笛卡尔产品

比实际空间大 - 不好。


一个更不可取的表示形式是(数字)x(套装)x

(is_a_face_card)x(is_a_joker)。该表示的问题是

,实际空间小于每个

个别空间的笛卡尔积。换句话说,(数字= 9)&& (is_a_face_card == T)

矛盾(例如)。


由于各种原因违反(c)是不可避免的。但是,对于

这个问题,一个0..51范围内的整数(或稍微大一点来处理

jokers)可能是最好的,特别是考虑到速度与这些天最多的b $ b $处理器将进行整数划分。

With regard to the OP''s question ...

There are several measures of the "goodness" of a representation:

a)Whether you can store and extract the information you commonly need
efficiently. (i / 13 and i %13 are VERY efficient.)

b)Whether the representation is human-interpretable (to interpret memory
dumps, debugging info, etc.).

c)Whether all of the bit patterns that may occur are valid (if not, it tends
to lead to logical errors). (Generally, the best case is a set of
enumerations where the actual space is the cartesian product of each
individual space).

http://en.wikipedia.org/wiki/Cartesian_product

(and what luck, the URL above gives a playing card example).

Mr. Wilbur''s suggestion is probably the best one possible.

A perhaps equal fallback would be (number) x (suit). However, it is my
understanding that jokers have no suit, so this means the cartesian product
is larger than the actual space -- bad.

An even less desirable representation would be (number) x (suit) x
(is_a_face_card) x (is_a_joker). The problem with that representation is
that the actual space is smaller than the cartesian product of each
individual space. In other words, (number = 9) && (is_a_face_card == T) is
a contradiction (for example).

It is unavoidable to violate (c) for a variety of reasons. However, for
this problem, an integer in the range of 0..51 (or slightly larger to handle
jokers) is probably the best, especially given the speed with which most
processors will do an integer division these days.


这篇关于选择和使用适当的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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