From Gossip@caterpillar

Algorithm Gossip: Craps賭博遊戲

說明

一個簡單的賭博遊戲,遊戲規則如下:玩家擲兩個骰子,點數為1到6,如果第一次點數和為7或11,則玩家勝,如果點數和為2、3或12,則玩家輸,如果和 為其它點數,則記錄第一次的點數和,然後繼續擲骰,直至點數和等於第一次擲出的點數和,則玩家勝,如果在這之前擲出了點數和為7,則玩家輸。

解法

規則看來有些複雜,但是其實只要使用switch配合if條件判斷來撰寫即可,小心不要弄錯勝負順序即可。

實作:C    Java    Python    Scala    Ruby

  • C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WON 0
#define LOST 1
#define CONTINUE 2

int rollDice() {
return (rand() % 6) + (rand() % 6) + 2;
}

int main(void) {
printf("Craps賭博遊戲,按Enter鍵開始遊戲****");
srand(time(0));

int firstRoll = 1;
int gameStatus = CONTINUE;
int firstPoint = 0;

while(1) {
getchar();
int sumOfDice = rollDice();
printf("\n玩家擲出點數和:%d\n", sumOfDice);
if(firstRoll) {
switch(sumOfDice) {
case 7: case 11:
gameStatus = WON; break;
case 2: case 3: case 12:
gameStatus = LOST; break;
default:
firstRoll = 0;
gameStatus = CONTINUE;
firstPoint = sumOfDice;
break;
}
}
else {
if(sumOfDice == firstPoint)
gameStatus = WON;
else if(sumOfDice == 7)
gameStatus = LOST;
}

if(gameStatus == CONTINUE)
puts("未分勝負,再擲一次****\n");
else {
if(gameStatus == WON)
puts("玩家勝");
else
puts("玩家輸");

printf("再玩一次?");
char c;
scanf("%c", &c);
if(c == 'n') {
puts("遊戲結束");
break;
}

firstRoll = 1;
}
}

return 0;
}

  • Java
import java.io.*; 

public class Craps {
public static void main(String[] args)
throws IOException {
final int WON = 0, LOST = 1, CONTINUE = 2;
boolean firstRoll = true;
int gameStatus = CONTINUE;
int die1, die2, sumOfDice;
int firstPoint = 0;

System.out.print(
"Craps賭博遊戲,按Enter鍵開始遊戲****");

while(true) {
System.in.read();
sumOfDice = rollDice();
System.out.println(
"\n玩家擲出點數和:" + sumOfDice);
if(firstRoll) {
switch(sumOfDice) {
case 7: case 11:
gameStatus = WON; break;
case 2: case 3: case 12:
gameStatus = LOST; break;
default:
firstRoll = false;
gameStatus = CONTINUE;
firstPoint = sumOfDice;
break;
}
}
else {
if(sumOfDice == firstPoint)
gameStatus = WON;
else if(sumOfDice == 7)
gameStatus = LOST;
}

if(gameStatus == CONTINUE)
System.out.println("未分勝負,再擲一次****");
else {
System.out.println(gameStatus == WON ? "玩家勝" : "玩家輸");

System.out.print("再玩一次?");
if(System.in.read() == 'n') {
System.out.println("遊戲結束");
break;
}

firstRoll = true;
}
}
}

public static int rollDice() {
int roll = ((int)(Math.random() * 6) +
(int)(Math.random() * 6));

if(roll < 2) {
roll = 2;
}

return roll;
}
}

  • Python
import random
WON = 0,
LOSE = 1
CONTINUE = 2
firstRoll = True
gameStatus = CONTINUE
firstPoint = 0
print("Craps賭博遊戲,按Enter鍵開始遊戲****")
while True:
input()
sumOfDice = random.randint(1, 6) + random.randint(1, 6)
print("\n玩家擲出點數和:", sumOfDice)
if firstRoll:
if sumOfDice in [7, 11]:
gameStatus = WON
elif sumOfDice in [2, 3, 12]:
gameStatus = LOSE
else:
firstRoll = False
gameStatus = CONTINUE
firstPoint = sumOfDice
else:
if sumOfDice == firstPoint:
gameStatus = WON
elif sumOfDice == 7:
gameStatus = LOSE
if gameStatus == CONTINUE:
print("未分勝負,再擲一次****")
else:
print("玩家勝" if gameStatus == WON else "玩家輸")

if input("再玩一次?") == 'n':
print("遊戲結束")
break
firstRoll = True

  • Scala
import scala.util.Random

def rollDice() = {
val r = new Random
val roll = (r.nextDouble * 6).toInt + (r.nextDouble * 6).toInt
if(roll >= 2) roll else 2
}

val WON = 0
val LOST = 1
val CONTINUE = 2

var firstRoll = true
var gameStatus = CONTINUE
var firstPoint = 0
var again = true

print("Craps賭博遊戲,按Enter鍵開始遊戲****")
readLine

do {
val sumOfDice = rollDice
println("\n玩家擲出點數和:" + sumOfDice)
if(firstRoll) {
sumOfDice match {
case 7|11 => gameStatus = WON
case 2|3|12 => gameStatus = LOST
case _ => {
firstRoll = false
gameStatus = CONTINUE
firstPoint = sumOfDice
}
}
}
else {
if(sumOfDice == firstPoint)
gameStatus = WON
else if(sumOfDice == 7)
gameStatus = LOST
}

if(gameStatus == CONTINUE) {
println("未分勝負,再擲一次****")
}
else {
println(gameStatus == WON ? "玩家勝" : "玩家輸")

print("再玩一次?")
if(readLine == "n") {
again = false
}
else {
firstRoll = true
}
}
} while(again)

  • Ruby
# encoding: Big5
WON = 0
LOSE = 1
CONTINUE = 2
firstRoll = true
gameStatus = CONTINUE
firstPoint = 0
puts "Craps賭博遊戲,按Enter鍵開始遊戲****"
while true
gets
sumOfDice = (rand * 6 + 1).to_i + (rand * 6 + 1).to_i
print "\n玩家擲出點數和:", sumOfDice, "\n"
if firstRoll
case sumOfDice
when 7, 11
gameStatus = WON
when 2, 3, 12
gameStatus = LOSE
else
firstRoll = false
gameStatus = CONTINUE
firstPoint = sumOfDice
end
else
if sumOfDice == firstPoint
gameStatus = WON
elsif sumOfDice == 7
gameStatus = LOSE
end
end
if gameStatus == CONTINUE
puts "未分勝負,再擲一次****"
else
puts gameStatus == WON ?
"玩家勝" : "玩家負"

print "再玩一次?"
if gets.chomp == "n"
puts "遊戲結束"
break
end
firstRoll = true
end
end