fork(10) download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <color.h>
  6.  
  7. #define N 52
  8. #define TIMES 100 // 遊戲戰績儲存次數上限常數
  9. #define BUF_SIZE 3
  10.  
  11. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  12.  
  13.  
  14. const char flower_table[4] = { '\5' , '\4' , '\3' , '\6'}; // ascii code, \5 \4 \3 \6 分別代表梅花、方塊、紅心、黑桃
  15. const char number_table[13][BUF_SIZE] = {
  16. " A" , " 2" , " 3" , " 4" , " 5" , " 6" ,
  17. " 7" , " 8" , " 9" , "10" , " J" , " Q" ," K" , " "};
  18.  
  19. /* ※花色與數字的決定方法
  20. for (i = 0; i!=52; ++i){
  21.   flower = i % 4; // 花色, 0~3
  22.   number = i / 4; // 數字, 0~12
  23.   }
  24.   */
  25.  
  26. typedef struct handle Handle;
  27. struct handle{
  28. Handle *node ;
  29. int card ; // 牌值
  30. int flower; // 計算牌的花色
  31. int number; // 計算牌的點數
  32. float point; // 這一局的點數
  33. float fnumber; // 這張牌的點數
  34. int cardnum; // 手牌數量
  35. };
  36.  
  37.  
  38. typedef struct player Player;
  39. struct player{
  40. char record[TIMES] ; // 戰績
  41. int record_flag ; // 戰績陣列索引
  42. Handle hand; // struct 手牌
  43. int Case; // Case 1: 玩家要牌 遊戲結束
  44. };
  45.  
  46.  
  47.  
  48. // SWAP 交換
  49.  
  50. void Swap(int *a , int *b){
  51.  
  52. // printf("***** Function Swap Begin *****\n\n");
  53.  
  54. int temp;
  55.  
  56. temp = *a ;
  57. *a = *b ;
  58. *b = temp ;
  59.  
  60. return;
  61.  
  62. // printf("***** Function Swap End *****\n\n");
  63.  
  64. }
  65.  
  66. // Shuttle 洗牌
  67. void Shuttle(int *array ){
  68.  
  69. // printf("***** Function Shttle Begin *****\n\n");
  70.  
  71. BLACK_RED;
  72. printf("Creat New Deck And Shuttle !\n\n");
  73. BLACK_WHITE;
  74.  
  75. int i , pos ;
  76. srand((unsigned)time(NULL));
  77. rand(); // 先抽一次亂數 打亂亂數序
  78.  
  79.  
  80. for(i=0;i<52;i++){
  81.  
  82. array[i] = i+1 ;
  83.  
  84. }
  85.  
  86.  
  87. for(i=0;i!=N;++i){
  88. // 取出第pos張牌
  89. pos = (int) (N*(double)rand()/RAND_MAX);
  90. // 交換第i張與第pos張牌
  91. Swap(&array[i],&array[pos]);
  92. }
  93.  
  94. // printf("***** Function Shttle End *****\n\n");
  95.  
  96. return;
  97.  
  98. }
  99.  
  100.  
  101. void Deal(int *array , Player *a , int *flag){
  102.  
  103. BLACK_RED;
  104. printf("Deal !\n\n");
  105. BLACK_WHITE;
  106. a->hand.card = array[*flag];
  107. ++*flag;
  108. a->hand.flower = a->hand.card % 4 ;
  109. a->hand.number = a->hand.card / 4 ;
  110. a->hand.fnumber = a->hand.number ;
  111.  
  112. // printf("test card number : %d \n\n",a->hand.card);
  113.  
  114. // printf("Your card is %c%s \n\n",flower_table[flower],number_table[number]);
  115.  
  116.  
  117. }
  118.  
  119.  
  120. // Cardnum 算手牌數量
  121.  
  122. void Cardnum(int *cardnum){
  123.  
  124. *cardnum = *cardnum + 1 ;
  125.  
  126. }
  127.  
  128.  
  129. // Point 算手牌點數
  130.  
  131. void Point(float *fnumber,float *sumpoint){
  132.  
  133. if(*fnumber <10.0 && *fnumber >0.0){
  134. *fnumber = *fnumber + 1.0 ;
  135. *sumpoint = *sumpoint + *fnumber ;
  136. }
  137. else if (*fnumber == 0.0){
  138. *fnumber = 1.0 ;
  139. *sumpoint = *sumpoint + *fnumber ;
  140. }
  141. else if (*fnumber >=10.0 && *fnumber <= 12.0){
  142. *fnumber = 0.5 ;
  143. *sumpoint = *sumpoint + *fnumber ;
  144. }
  145.  
  146. }
  147.  
  148. // Watchdeal 得牌顯示
  149.  
  150. void Watchdeal(Player *a){
  151.  
  152. BLACK_CYAN;
  153. printf("Your card is %c%s \n\n",flower_table[a->hand.flower],number_table[a->hand.number]);
  154. Point(&a->hand.fnumber,&a->hand.point);
  155. printf("POINT Now: %.1f\n\n",a->hand.point);
  156. Cardnum(&a->hand.cardnum);
  157. printf("Card Num Now: %d\n\n\n\n",a->hand.cardnum);
  158. BLACK_WHITE;
  159.  
  160. }
  161.  
  162.  
  163. // Win Lose 輸贏
  164.  
  165. void Lose(Player *A,Player *B){
  166.  
  167. A->record[A->record_flag] = 'L' ;
  168. A->record_flag ++ ;
  169. B->record[B->record_flag] = 'W' ;
  170. B->record_flag ++ ;
  171.  
  172. }
  173.  
  174. void Win(Player *A,Player *B){
  175.  
  176. B->record[A->record_flag] = 'L' ;
  177. B->record_flag ++ ;
  178. A->record[B->record_flag] = 'W' ;
  179. A->record_flag ++ ;
  180.  
  181. }
  182.  
  183.  
  184. void Getcard(Player *A,Player *B,int card[],int *flag){
  185.  
  186. char temp = '\0';
  187. printf("%p\n",&A->Case);
  188.  
  189. while(1){
  190.  
  191. // 詢問是否要牌
  192. BLACK_YELLOW;
  193. printf("Do you want to get card ? ( max card is 5 )\n\n");
  194. BLACK_YELLOW;
  195. printf("Type 1 for YES , 2 for NO .\n\n");
  196. BLACK_WHITE;
  197.  
  198. scanf("%s",&temp);
  199.  
  200. // 防呆機制
  201. if(temp != '1' && temp != '2'){
  202.  
  203. BLACK_RED;
  204. printf("Your Type is wrong ! Retype Please.\n");
  205. BLACK_WHITE;
  206. scanf("%c",&temp);
  207. printf("\n");
  208.  
  209. }
  210.  
  211. // 不要牌
  212. if(temp == '2'){
  213. BLACK_YELLOW;
  214. printf("You Do Not Get Card Anymore !\n");
  215. BLACK_CYAN;
  216. printf("Your End Point : %.1f\n\n\n\n",A->hand.point);
  217. BLACK_WHITE;
  218. return;
  219. }
  220.  
  221.  
  222. if(temp == '1'){
  223. Deal(card,A,flag);
  224. Watchdeal(A);
  225. if(A->hand.point >10.5){
  226. BLACK_RED;
  227. printf("BOOM ! YOU LOSE !\n\n");
  228. BLACK_WHITE;
  229. Lose(A,B);
  230. A->Case = 1 ;
  231. return;
  232. }
  233. if(A->hand.cardnum == 5){
  234. BLACK_RED;
  235. printf("FIVE CARD ! YOU WIN !\n\n");
  236. BLACK_WHITE;
  237. Win(A,B);
  238. A->Case = 1 ;
  239. return;
  240. }
  241. if(A->hand.point == 10.5){
  242. BLACK_RED;
  243. printf("10.5 ! YOU WIN !\n\n");
  244. BLACK_WHITE;
  245. Win(A,B);
  246. A->Case = 1 ;
  247. return;
  248. }
  249. }
  250.  
  251. }
  252.  
  253. }
  254.  
  255. void Cgetcard(Player *A,Player *B,int card[],int *flag){
  256.  
  257. BLACK_PURPLE;
  258. printf("Computer's First Card is %c%s\n\n",flower_table[B->hand.flower],number_table[B->hand.number]);
  259. printf("Coumputer's Point : %.1f\n\n",B->hand.point);
  260.  
  261. }
  262.  
  263.  
  264.  
  265. void Display(){
  266.  
  267. BLACK_YELLOW;
  268. printf("\nGame Start !\n\n");
  269. BLACK_WHITE;
  270.  
  271. // 建立牌組
  272. int card[52] = {} ;
  273. int flag = 0 ; // 牌組堆疊用旗標
  274.  
  275. // 洗牌
  276. Shuttle(card);
  277.  
  278. // 建立Player
  279. Player A ;
  280. A.hand.cardnum = 0 ;
  281. A.hand.fnumber = 0.0 ;
  282. A.hand.point = 0.0 ;
  283. A.record_flag = 0 ;
  284. A.Case = 0 ;
  285.  
  286. int i = 0 ;
  287. for(i=0;i<TIMES;i++){
  288. A.record[i] = '\0' ;
  289. }
  290.  
  291. Player B ;
  292. B.hand.cardnum = 0 ;
  293. B.hand.fnumber = 0.0 ;
  294. B.hand.point = 0.0 ;
  295. B.record_flag = 0;
  296. B.Case = 0 ;
  297.  
  298. for(i=0;i<TIMES;i++){
  299. B.record[i] = '\0' ;
  300. }
  301.  
  302.  
  303. // 第一次發牌 (玩家)
  304. BLACK_YELLOW;
  305. printf("You Get First.\n\n");
  306. BLACK_WHITE;
  307.  
  308. Deal(card,&A,&flag);
  309.  
  310. BLACK_CYAN;
  311. printf("Your card is %c%s \n\n",flower_table[A.hand.flower],number_table[A.hand.number]);
  312. Point(&A.hand.fnumber,&A.hand.point);
  313. printf("POINT: %.1f\n\n",A.hand.point);
  314. Cardnum(&A.hand.cardnum);
  315. printf("Card Num Now: %d\n\n",A.hand.cardnum);
  316. BLACK_WHITE;
  317.  
  318. // 第一次發牌 (電腦)
  319. BLACK_YELLOW;
  320. printf("Computer Get Now.\n\n");
  321. BLACK_WHITE;
  322.  
  323. Deal(card,&B,&flag);
  324. Cardnum(&B.hand.cardnum);
  325. printf("\n\n");
  326.  
  327. // printf("Computer's card is %5c%s\n\n",flower_table[B.hand.flower],number_table[B.hand.number]); // 電腦的點數(按遊戲規則不顯示)
  328. Point(&B.hand.fnumber,&B.hand.point);
  329.  
  330. // 要牌(玩家)
  331. Getcard(&A,&B,card,&flag);
  332. printf("%p\n",&A.Case);
  333. if (A.Case == 1) return ; // 玩家點數爆 & 過五關 & 10半 結束遊戲
  334.  
  335. // 要牌(電腦)
  336. Cgetcard(&A,&B,card,&flag);
  337.  
  338.  
  339. }
  340.  
  341.  
  342.  
  343.  
  344. int main(int argc, char *argv[]) {
  345.  
  346. char p = 0 ;
  347. BLACK_BLUE;
  348. printf("Program Start !\n\n");
  349. BLACK_WHITE;
  350. do{
  351. Display();
  352. BLACK_YELLOW;
  353. printf("Do you want to play again ?\n(1 for YES 2 for NO.)\n\n");
  354. BLACK_WHITE;
  355. scanf("%s",&p);
  356. while(p != '1' && p!='2'){
  357. BLACK_YELLOW;
  358. printf("Type Wrong ! Retype !\n\n");
  359. BLACK_WHITE;
  360. scanf("%s",&p);
  361. }
  362. }while(p == '1');
  363.  
  364. if(p=='2'){
  365. BLACK_BLUE;
  366. printf("\nProgram End.\n\n");
  367. BLACK_WHITE;
  368. }
  369.  
  370. return 0;
  371. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:5:19: fatal error: color.h: No such file or directory
 #include <color.h>
                   ^
compilation terminated.
stdout
Standard output is empty