2014-04-27 78 views
-1

好的,首先我在BASIC中編寫它,因爲它是我所知的最多的語言。這個程序假設有6個骰子正在滾動,它貫穿[1] [1] [1] [1] [1] [1]到[6] [6] [6]的所有可能組合6] [6] [6]。不要擔心數字旁邊的「b」和「r」。從數組中識別組

我希望能夠做的(希望),而不必做一個複雜的IF語句是通過每個「滾動」,並挑出組,如對和三元組等等......,但我也想如果說一個三重數就是一個數 - 一對不是。


IF A=B AND A<>C AND A<>D AND A<>E AND A<>F THEN PAIR=PAIR+1 
IF A<>B AND A=C AND A<>D ... 

做這樣說,這是要採取永遠和需要(如果我能正確計算)15 IF語句只是爲了表明,如果有1對,我之前的三倍後開始,四邊形等...

這是我的代碼 - 這是一個正在進行的工作,因此奇數編號。


10 REM DEFINE DICE TYPE X 
20 DIM X$(6) 
30 X$(1) = "1r" 
40 X$(2) = "2b" 
50 X$(3) = "3r" 
60 X$(4) = "4b" 
70 X$(5) = "5r" 
80 X$(6) = "6b" 
90 
100 REM DEFINE DICE TYPE Y 
110 DIM Y$(6) 
120 Y$(1) = "1b" 
130 Y$(2) = "2r" 
140 Y$(3) = "3b" 
150 Y$(4) = "4r" 
160 Y$(5) = "5b" 
170 Y$(6) = "6r" 
180 
190 
200 REM SET DICE FACES AND ROLL COUNTER 
210 LET A = 1 
220 LET B = 1 
230 LET C = 1 
240 LET D = 1 
250 LET E = 1 
260 LET F = 1 
270 LET R = 0 
280 
281 
282 REM DEFINE HANDS 
283 LET P = 0 
284 LET PP = 0 
285 LET PPP = 0 
286 LET T = 0 
287 LET TP = 0 
288 LET TT = 0 
289 LET Q = 0 
290 LET QP = 0 
291 LET QU = 0 
292 LET S = 0 
293 
300 REM ROLL THE DICE 
310 LET R = R + 1 
320 IF A = B AND A = C AND A = D AND A = E AND A = F THEN LET S = S + 1 
1300 PRINT X$(F), X$(E), X$(D), Y$(C), Y$(B), Y$(A), "ROLL "; R 
1330 PRINT "SEXTUPLETS: "; S 
1340 
1350 REM INDEX THE ROLLS 
1360 LET A = A + 1 
1370 IF A < 7 GOTO 300 
1380 LET A = 1 
1390 
1400 LET B = B + 1 
1410 IF B < 7 GOTO 300 
1420 LET B = 1 
1430 
1440 
1450 LET C = C + 1 
1460 IF C < 7 GOTO 300 
1470 LET C = 1 
1480 
1490 
1500 LET D = D + 1 
1510 IF D < 7 GOTO 300 
1520 LET D = 1 
1530 
1540 
1550 LET E = E + 1 
1560 IF E < 7 GOTO 300 
1570 LET E = 1 
1580 
1590 
1600 LET F = F + 1 
1610 IF F < 7 GOTO 300 
1620 
1630 
1640 
1650 END 

如果你沒有一個BASIC編譯器(我使用QB64),彈出代碼到Qloud這裏:http://www.qb64.net/並按運行

+0

你應該學習Python。它是現代的,並且很容易。 – ooga

+0

感謝您的回覆。如果我要求將一個短語翻譯成法語,那麼你的答案就像告訴我試試德語一樣。我很欣賞BASIC已經過時,但這是我所知道的。爲什麼不告訴我一個可能有用的部分代碼示例,而不是告訴我用我不知道的語言重新編碼。我相信他們必須在語法結構之間有一些相似之處 - 如果我能夠掌握如何用一種語言完成它,我可以使用它來更新我的原始程序或掌握新的語言語言。 – Escribblings

+0

我是唯一一個對你說過什麼話的人。這是我第一次在這個網站上看到BASIC的古代形式。絕對沒有人再用它來做任何事情。我的評論可能是你要到這裏來的。 – ooga

回答

0

這是不完全你想要什麼,但它展示了它在Python中看起來像什麼的想法。

Python將會從(行編號)基礎中看起來很奇怪。但它是學習最簡單的現代語言之一,功能非常強大(具有標準庫和其他庫)。這將是一段旅程,但如果您有任何興趣或需要編程,那就值得。

在Python程序中,縮進很關鍵,所以要小心移動東西。

要得到python:https://www.python.org/ Python 3.4.0可能對你最好。文檔:https://docs.python.org/3/

# A hash tag indicates a comment to the end of the line. 

def increment(dice): # define a function (aka, subroutine) 
    dice[0] += 1 
    if dice[0] > 6: 
    dice[0] = 1 
    dice[1] += 1 
    if dice[1] > 6: 
     dice[1] = 1 
     dice[2] += 1 
     if dice[2] > 6: 
     dice[2] = 1 
     dice[3] += 1 
     if dice[3] > 6: 
      dice[3] = 1 
      dice[4] += 1 
      if dice[4] > 6: 
      dice[4] = 1 
      dice[5] += 1 
      if dice[5] > 6: 
       dice[5] = 1 
       return False # return False when done 
    return True 

def count_pips(dice): 
    counts = [0, 0, 0, 0, 0, 0] 
    for x in dice: 
    counts[x-1] += 1 # x-1 because of 0-based indices 
    return counts 

def analyze_counts(counts): 
    stats = [] # will hold number of doubles, number of triples, etc. 
    for i in range(2, 7): 
    stats.append(counts.count(i)) 
    return stats 

# Note that list indices are 0-based 
# so indices will go from 0 to 5 for 6 elements. 
dice = [1, 1, 1, 1, 1, 1] 
counts = count_pips(dice) 
stats = analyze_counts(counts) 
print dice, stats 
while increment(dice): 
    counts = count_pips(dice) 
    stats = analyze_counts(counts) 
    print(dice, stats) 

如果你能更具體地描述你正在試圖做我也許可以解決這個問題了一點什麼。

代碼的一些說明:

dice是一個列表,其元素通過所有的組合被增加等的里程錶(具有值1至6)從1,1,1,1,1,1至6 ,6,6,6,6,6。 (但是你知道的!)

counts是在dice中填充了1,2等數字的計數的列表。

stats是在count中填入2,3等數字的列表。列表具有與它們相關的功能,例如count(其巧妙地命名爲類似於我們的變量counts)。 count函數返回給定值中給定列表中的元素數。

還要注意的是,線:

for i in range(2, 7): 

是一個循環,i呈現值從2比第二個數字,這似乎有些奇怪6(少了一個,但是這是它的工作方式)。

+0

謝謝。我會盡力讓我的頭腦得到滿足。我只是把代碼塞進[CompileOnline](http://www.compileonline.com/),有趣的是[Python 3](http://www.compileonline.com/execute_python3_online.php)(3.2.3)我得到語法錯誤:第42行SyntaxError:無效語法 有趣的是,在[Python](http://www.compileonline.com/execute_python_online.php)(2.7.4)它似乎運行良好。 – Escribblings

+0

這是python不幸的事情之一。它分爲python 2和python 3,它們是不兼容的。我通常仍然使用2.它可能只是打印語句,它需要python 3中的圓括號。我將編輯上面的內容來添加它們。該在線編譯器可能不會執行整個程序的運行,因爲它打印大約46656輸出線!但它可能會告訴你至少前幾行。 – ooga

+0

你對編譯器沒有顯示所有的輸出很正確,但它已經做得夠多了 - 它在統計中也完成了我想要的。 - 你能解釋一下「analyze_counts」函數嗎?它做我想要的,但我不明白爲什麼。其餘的似乎是有道理的。 – Escribblings

0

玩過Python代碼後,我也設法讓它在BASIC中工作。

這是我的新程序(編輯 - 如果任何人有興趣,這是現​​在的完整程序,不僅計數卷,而且顏色分組也一樣) - 如果你想測試這個塞進Qloud http://www.qb64.net準備好等一下 - 不過太長了。

這是我的新代碼:

10 start_time = TIMER 

    20 OUT &H3C8, 0 'Set COLOR 0 to black 

    30 OUT &H3C9, 1 'R% 

    40 OUT &H3C9, 1 'G% 

    50 OUT &H3C9, 1 'B% 

    60 OUT &H3C8, 1 'set COLOR 1 to red 

    70 OUT &H3C9, 63 'R% 

    80 OUT &H3C9, 1 'G% 

    90 OUT &H3C9, 1 'B% 

100 OUT &H3C8, 2 'set COLOR 2 to white 

110 OUT &H3C9, 63 'R% 

120 OUT &H3C9, 63 'G% 

130 OUT &H3C9, 63 'B% 

140 OUT &H3C8, 3 'set COLOR 3 to dark green 

150 OUT &H3C9, 1 'R% 

160 OUT &H3C9, 19 'G% 

170 OUT &H3C9, 1 'B% 

180 COLOR 2, 3 'Set forground (text) colour to white and background colour to dark green 

190 CLS 

200 DIM face(6) 

210 DIM count_spots(6) 

220 DIM colour(6) 

230 DIM r(6) 

240 DIM b(6) 

250 DIM sr(6) 

260 DIM sb(6) 

270 roll = 0 

280 one_pair = 0: xx = 0: xy = 0 

290 two_pair = 0: xx_xx = 0: xx_yy = 0: xx_xy = 0: xy_xy = 0 

300 three_pair = 0: xx_xx_xy = 0: xx_yy_xy = 0: xy_xy_xy = 0 

310 one_trip = 0: xxx = 0: xxy = 0 

320 one_trip_one_pair = 0: xxx_xx = 0: xxx_yy = 0: xxy_xx = 0: xxy_yy = 0: xxy_xy = 0 

330 two_trips = 0: xxx_xxx = 0: xxx_yyy = 0: xxx_xxy = 0: xxy_xxy = 0 

340 one_quad = 0: xxxy = 0: xxyy = 0 

350 one_quad_one_pair = 0: xxxy_xx = 0: xxxy_yy = 0: xxxy_xy = 0: xxyy_xx = 0: xxyy_xy = 0 

360 one_quint = 0 

370 one_sextuple = 0 

380 one_straight = 0: xxxxxx = 0: xxxxyy = 0: xxxyyy = 0 

390 FOR die = 1 TO 6 

400  face(die) = 1 

410 NEXT die 

420 PRINT " NO. SHOWING ON | ROLL | NO. OF GROUPS OF | COLOUR OF FACE" 

430 PRINT " THE DICE FACE | NO. | 6 5 4 3 2 1 | RED=1 BLACK=0" 

440 PRINT "------------------+-------+------------------+-----------------" 

450 FOR position = 1 TO 6 

460  colour(position) = (face(position) + ((position - 1) \ 3)) MOD 2 

470  count_spots(position) = 0 

480  group(position) = 0 

490  r(position) = 0 

500  b(position) = 0 

510  sr(position) = 0 

520  sb(position) = 0 

530 NEXT position 

540 g = 0 

550 sg = 0 

560 FOR dice = 1 TO 6 

570  tally_spots = 0 

580  tally_red = 0 

590  tally_black = 0 

600  FOR spots = 1 TO 6 

610   IF face(spots) = dice THEN 

620    tally_spots = tally_spots + 1 

630    IF colour(spots) = 1 THEN 

640     tally_red = tally_red + 1 

650    ELSEIF colour(spots) = 0 THEN 

660     tally_black = tally_black + 1 

670    END IF 

680   END IF 

690  NEXT spots 

700  IF tally_spots > 1 THEN 

710   count_spots(tally_spots) = count_spots(tally_spots) + 1 

720   g = g + 1 

730   r(g) = r(g) + tally_red 

740   b(g) = b(g) + tally_black 

750  ELSEIF tally_spots = 1 THEN 

760   count_spots(tally_spots) = count_spots(tally_spots) + 1 

770   sg = sg + 1 

780   sr(sg) = sr(sg) + tally_red 

790   sb(sg) = sb(sg) + tally_black 

800  END IF 

810 NEXT dice 

820 IF count_spots(2) = 1 AND count_spots(3) = 0 AND count_spots(4) = 0 THEN 

830  one_pair = one_pair + 1 

840  IF r(1) = 2 OR b(1) = 2 THEN 

850   xx = xx + 1 

860  ELSE 

870   xy = xy + 1 

880  END IF 

890 ELSEIF count_spots(2) = 2 THEN 

900  two_pair = two_pair + 1 

910  IF r(1) + r(2) = 4 OR b(1) + b(2) = 4 THEN 

920   xx_xx = xx_xx + 1 

930  ELSEIF r(1) + r(2) = 3 OR b(1) + b(2) = 3 THEN 

940   xx_xy = xx_xy + 1 

950  ELSEIF r(1) = 1 AND r(2) = 1 AND b(1) = 1 AND b(2) = 1 THEN 

960   xy_xy = xy_xy + 1 

970  ELSE 

980   xx_yy = xx_yy + 1 

990  END IF 

1000 ELSEIF count_spots(2) = 3 THEN 

1010  three_pair = three_pair + 1 

1020  IF r(1) + r(2) + r(3) = 5 OR b(1) + b(2) + b(3) = 5 THEN 

1030   xx_xx_xy = xx_xx_xy + 1 

1040  ELSEIF r(1) = 1 AND r(2) = 1 AND r(3) = 1 AND b(1) = 1 AND b(2) = 1 AND b(3) = 1 THEN 

1050   xy_xy_xy = xy_xy_xy + 1 

1060  ELSE 

1070   xx_yy_xy = xx_yy_xy + 1 

1080  END IF 

1090 ELSEIF count_spots(3) = 1 AND count_spots(2) = 0 THEN 

1100  one_trip = one_trip + 1 

1110  IF r(1) = 3 OR b(1) = 3 THEN 

1120   xxx = xxx + 1 

1130  ELSE 

1140   xxy = xxy + 1 

1150  END IF 

1160 ELSEIF count_spots(3) = 1 AND count_spots(2) = 1 THEN 

1170  one_trip_one_pair = one_trip_one_pair + 1 

1180  IF r(1) + r(2) = 5 OR b(1) + b(2) = 5 THEN 

1190   xxx_xx = xxx_xx + 1 

1200  ELSEIF r(1) + b(2) = 5 OR r(2) + b(1) = 5 THEN 

1210   xxx_yy = xxx_yy + 1 

1220  ELSEIF (r(1) = 2 AND b(1) = 1 AND r(2) = 2) OR (r(1) = 1 AND b(1) = 2 AND b(2) = 2) OR (r(1) = 2 AND r(2) = 2 AND b(2) = 1) OR (b(1) = 2 AND r(2) = 1 AND b(2) = 2) THEN 

1230   xxy_xx = xxy_xx + 1 

1240  ELSEIF (r(1) = 2 AND b(1) = 1 AND b(2) = 2) OR (r(1) = 1 AND b(1) = 2 AND r(2) = 2) OR (r(1) = 2 AND r(2) = 1 AND b(2) = 2) OR (b(1) = 2 AND r(2) = 2 AND b(2) = 1) THEN 

1250   xxy_yy = xxy_yy + 1 

1260  ELSEIF ((r(1) = 2 AND b(1) = 1) OR (r(1) = 1 AND b(1) = 2)) AND (r(2) = 1 AND b(2) = 1) OR (r(1) = 1 AND b(1) = 1) AND ((r(2) = 2 AND b(2) = 1) OR (r(2) = 1 AND b(2) = 2)) THEN 

1270   xxy_xy = xxy_xy + 1 

1280  END IF 

1290 ELSEIF count_spots(3) = 2 THEN 

1300  two_trips = two_trips + 1 

1310  IF r(1) + r(2) = 6 OR b(1) + b(2) = 6 THEN 

1320   xxx_xxx = xxx_xxx + 1 

1330  ELSEIF r(1) + b(2) = 6 OR r(2) + b(1) = 6 THEN 

1340   xxx_yyy = xxx_yyy + 1 

1350  ELSEIF r(1) < 3 AND r(2) < 3 AND b(1) < 3 AND b(2) < 3 THEN 

1360   xxy_xyy = xxy_xyy + 1 

1370  END IF 

1380 ELSEIF count_spots(4) = 1 AND count_spots(2) = 0 THEN 

1390  one_quad = one_quad + 1 

1400  IF r(1) = 3 OR b(1) = 3 THEN 

1410   xxxy = xxxy + 1 

1420  ELSEIF r(1) = 2 AND b(1) = 2 THEN 

1430   xxyy = xxyy + 1 

1440  END IF 

1450 ELSEIF count_spots(4) = 1 AND count_spots(2) = 1 THEN 

1460  one_quad_one_pair = one_quad_one_pair + 1 

1470  IF (r(1) = 3 AND r(2) = 2) OR (r(1) = 2 AND r(2) = 3) OR (b(1) = 3 AND b(2) = 2) OR (b(1) = 2 AND b(2) = 3) THEN 

1480   xxxy_xx = xxxy_xx + 1 

1490  ELSEIF (r(1) = 3 AND b(2) = 2) OR (r(1) = 2 AND b(2) = 3) OR (b(1) = 3 AND r(2) = 2) OR (b(1) = 2 AND r(2) = 3) THEN 

1500   xxxy_yy = xxxy_yy + 1 

1510  ELSEIF ((r(1) = 3 AND b(1) = 1) OR (r(1) = 1 AND b(1) = 3)) AND (r(2) = 1 AND b(2) = 1) OR ((r(2) = 3 AND b(2) = 1) OR (r(2) = 1 AND b(2) = 3)) AND (r(1) = 1 AND b(1) = 1) THEN 

1520  xxxy_xy = xxxy_xy + 1 

1530  ELSEIF (r(1) = 1 AND r(2) = 2) OR (b(1) = 2 AND b(2) = 2) THEN 

1540   xxyy_xx = xxyy_xx + 1 

1550  ELSEIF (r(1) = 2 AND b(1) = 2 AND r(2) = 1 AND b(2) = 1) OR (r(1) = 1 AND b(1) = 1 AND r(2) = 2 AND b(2) = 2) THEN 

1560   xxyy_xy = xxyy_xy + 1 

1570  END IF 

1580 ELSEIF count_spots(5) = 1 THEN 

1590  one_quint = one_quint + 1 

1600 ELSEIF count_spots(6) = 1 THEN 

1610  one_sextuple = one_sextuple + 1 

1620 ELSEIF count_spots(1) = 6 THEN 

1630  one_straight = one_straight + 1 

1640  IF sr(1) + sr(2) + sr(3) + sr(4) + sr(5) + sr(6) = 6 OR sb(1) + sb(2) + sb(3) + sb(4) + sb(5) + sb(6) = 6 THEN 

1650   xxxxxx = xxxxxx + 1 

1660  ELSEIF sr(1) + sr(2) + sr(3) + sr(4) + sr(5) + sr(6) = 4 OR sb(1) + sb(2) + sb(3) + sb(4) + sb(5) + sb(6) = 4 THEN 

1670   xxxxyy = xxxxyy + 1 

1680  END IF 

1690 END IF 

1700 LET roll = roll + 1 

1710 FOR throw_spots = 1 TO 6 

1720  COLOR colour(throw_spots): PRINT face(throw_spots); 

1730 NEXT throw_spots 

1740 COLOR 2: PRINT "|"; 

1750 IF roll < 10 THEN 

1760  PRINT " "; roll; 

1770 ELSEIF roll < 100 THEN 

1780  PRINT " "; roll; 

1790 ELSEIF roll < 1000 THEN 

1800  PRINT " "; roll; 

1810 ELSEIF roll < 10000 THEN 

1820  PRINT " "; roll; 

1830 ELSE 

1840  PRINT roll; 

1850 END IF 

1860 PRINT "|"; count_spots(6); count_spots(5); count_spots(4); count_spots(3); count_spots(2); count_spots(1); "|"; 

1870 FOR throw_colour = 1 TO 6 

1880  COLOR colour(throw_colour): PRINT colour(throw_colour); 

1890 NEXT throw_colour 

1900 COLOR 2: PRINT " " 

1910 face(6) = face(6) + 1 

1920 IF face(6) < 7 THEN 

1930  GOTO 450 

1940 ELSE 

1950  face(6) = 1 

1960  face(5) = face(5) + 1 

1970  IF face(5) < 7 THEN 

1980   GOTO 450 

1990  ELSE 

2000   face(5) = 1 

2010   face(4) = face(4) + 1 

2020   IF face(4) < 7 THEN 

2030    GOTO 450 

2040   ELSE 

2050    face(4) = 1 

2060    face(3) = face(3) + 1 

2070    IF face(3) < 7 THEN 

2080     GOTO 450 

2090    ELSE 

2100     face(3) = 1 

2110     face(2) = face(2) + 1 

2120     IF face(2) < 7 THEN 

2130      GOTO 450 

2140     ELSE 

2150      face(2) = 1 

2160      face(1) = face(1) + 1 

2170      IF face(1) < 7 THEN 

2180       GOTO 450 

2190      ELSE 

2200       face(1) = 1 

2210      END IF 

2220     END IF 

2230    END IF 

2240   END IF 

2250  END IF 

2260 END IF 

2270 PRINT "TOTAL NUMBER OF HANDS CONTAINING" 

2280 PRINT "1 PAIR:", one_pair; "- XX:"; xx; "- XY:"; xy 

2290 PRINT "2 PAIRS:", two_pair; "- XX XX:"; xx_xx; "- XX YY:"; xx_yy; "- XX XY:"; xx_xy; "- XY XY:"; xy_xy 

2300 PRINT "3 PAIRS:", " "; three_pair; "- XX XX XY:"; xx_xx_xy; "- XX YY XY:"; xx_yy_xy; "- XY XY XY:"; xy_xy_xy 

2310 PRINT "1 TRIP:", " "; one_trip; "- XXX:"; xxx; "- XXY:"; xxy 

2320 PRINT "1 TRIP/1 PAIR:"; " "; one_trip_one_pair; "- XXX XX:"; xxx_xx; "- XXX YY:"; xxx_yy; "- XXY XX:"; xxy_xx 

2330 PRINT " ", "   "; "XXY YY:"; xxy_yy; "- XXY XY:"; xxy_xy 

2340 PRINT "2 TRIPS:", " "; two_trips; "- XXX XXX:"; xxx_xxx; "- XXX YYY"; xxx_yyy; "- XXY XYY:"; xxy_xyy 

2350 PRINT "1 QUAD:", " "; one_quad; "- XXXY:"; xxxy; "- XXYY"; xxyy 

2360 PRINT "1 QUAD/1 PAIR:"; " "; one_quad_one_pair; "- XXXY XX:"; xxxy_xx; "- XXXY YY:"; xxxy_yy; "- XXYY_XX:"; xxyy_xx; "- XXYY XY:"; xxyy_xy 

2370 PRINT "1 QUINT:", " "; one_quint; "- XXXYY:"; one_quint 

2380 PRINT "1 SEXTUPLE:", " "; one_sextuple; "- XXXYYY:"; one_sextuple 

2390 PRINT "1 STRAIGHT:", " "; one_straight; "- XXXXXX:"; xxxxxx; "- XXXXYY:"; xxxxyy 

2400 end_time = TIMER 

2410 PRINT "This program took "; end_time - start_time; " seconds to execute" 

2420 FOR reset_COLOR = 1 TO 256 

2430  OUT &H3C7, reset_COLOR 

2440  R% = INP(&H3C9) 

2450  G% = INP(&H3C9) 

2460  B% = INP(&H3C9) 

2470 NEXT reset_COLOR 

2480 END 
0

此代碼剪斷從6模具計數對,三胞胎和4卷以上:

DIM dice(1 TO 6) AS INTEGER 
RANDOMIZE TIMER 
FOR l = 1 TO 6 
    dice(l) = INT(RND * 6 + 1) 
    PRINT dice(l); 
NEXT 
PRINT 
FOR l = 1 TO 6 
    c = 0 
    FOR m = 1 TO 6 
     IF dice(m) = l THEN 
      c = c + 1 
     END IF 
    NEXT 
    IF c >= 2 THEN 
     PRINT "there are"; c; "dice of value"; l 
    END IF 
NEXT 
END