[C/C++ 演算法]- Craps賭博遊戲
[C/C++ 演算法]- Craps賭博遊戲
剛才找資料時發現一個C/C++的教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:
http://openhome.cc/Gossip/AlgorithmGossip/
http://openhome.cc/Gossip/AlgorithmGossip/CrapsGame.htm
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LOST 0
#define WON 1
#define CONTINUE 2
int dice();
int initialRoll(int);
int reRoll(int, int);
int main(void) {
srand(time(0));
int firstPoint = dice();
printf("玩家點數:[%d]\n", firstPoint);
int status = initialRoll(firstPoint);
while(status == CONTINUE) {
int point = dice();
printf("玩家點數:%d\n", point);
status = reRoll(firstPoint, point);
}
puts(status == WON ? "玩家勝" : "玩家輸");
return 0;
}
int dice() {
return (rand() % 6) + (rand() % 6) + 2;
}
int initialRoll(int firstPoint) {
switch(firstPoint) {
case 7: case 11: return WON;
case 2: case 3: case 12: return LOST;
default: return CONTINUE;
}
}
int reRoll(int firstPoint, int point) {
return firstPoint == point ? WON : (7 == point ? LOST : CONTINUE);
}
|