1. 請列印主選單
ex: 歡迎來到我的小賭場 v2.0
-1.進入賭場
-2.離開程式
請輸入選項1或2
2. 讀取使用者輸入的選項
3. 若使用者輸入1,進入判斷,否則執行步驟4
3.1 請撰寫者宣稱一個結構(名字自取),結構內容為1張手牌及一個節點(ex:struct x{int a, b;x *next;};=>a為數字 b為花色 next為下一點,請參考課本12章)
3.2 請透過函式利用亂數方式出牌且排序後列印出來(利用結構串接,第七章)
3.3 並能夠讓使用者選擇隨機的0~5張牌打出去(0張代表pass,可自行定義pass的方式)(3.2)
-出完牌之後將手牌刪去出的牌(12章)
-判斷是否出完牌了,不是回到3.3,是的3.4
3.4 告知使用者牌已出完,詢問是否繼續(3.5 3.6等步驟)
3.5 等待使用者輸入任意鍵
3.6 清空畫面並回到步驟1
4. 離開程式
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h> //使用getch()
#include<time.h>
typedef struct Card{
int suit[13];
int face[13];
}card;
typedef struct player {
int number;
int face;
int suit;
struct player * left;
struct player * right;
} player;
void printFunctionKey(){ //選單
printf("1.重新\n");
printf("2.出牌\n");
printf("X.結束\n\n");
}
int ischar(char c, const char* s) { //讀取使用者輸入
int i, output = 0;
for (i = 0; s[i] != '\0'; i++)
output |= (c == s[i]);
return output;
}
int count(player* node) { //目前有幾張牌
if (node != NULL)
return count(node->left) + count(node->right) + 1;
else
return 0;
}
player* newnode(int number, int face, int suit) { // 建立新節點
player* p = (player*) malloc(sizeof(player));
p->number = number;
p->face = face;
p->suit = suit;
p->left = p->right = NULL;
return p;
}
void insert(player* ins, player* &node) { //寫入牌
if (node != NULL) {
if (ins->number < node->number)
insert(ins, node->left);
else if (ins->number> node->number)
insert(ins, node->right);
} else
node = ins;
}
int deletenode(int number, player* &node) { //刪除牌
if (node != NULL) {
if (number < node->number)
return deletenode(number,node->left);
else if (number > node->number)
return deletenode(number,node->right);
else {
if (node->left == NULL &&node->right== NULL) {
free(node);
node = NULL;
} else if (node->left == NULL &&node->right!= NULL) {
player* memory = node;
node = node->right;
free(memory);
} else if (node->left != NULL &&node->right== NULL) {
player* memory = node;
node = node->left;
free(memory);
} else if (node->left != NULL &&node->right!= NULL) {
player* memory = node;
player* memoryright =node->right;
node = node->left;
free(memory);
insert(memoryright,node);
}
return true;
}
} else
return false;
}
int printall(player* node) { //印出牌
if (node != NULL) {
printall(node->left);
printf("%3d %12d %4c\n",node->number, node->face, node->suit);
printall(node->right);
return true;
} else
return false;
}
int main() {
srand(time(NULL));
card deck;
player* root = NULL;
int i,j,c,flower,b1,b2,hold; //用來跑回圈 ,花色, 暫存;
char choose; //使用者輸入選項
char howmany; //使用者要出多少牌
int number,number1,number2,number3,number4; //使用者出牌
start:
for(i=0;i<13;i++){ //亂數發牌 不重複
deck.face[i]=rand()%13+1;
deck.suit[i]=rand()%4;
for(j=0;j<i;j++){
if(deck.face[j]==deck.face[i] && deck.suit[j]==deck.suit[i]){
i--;
}
}
}
for(b1=0; b1<12; b1++){ //泡泡排序
for(b2=b1+1; b2<13; b2++){
if(deck.face[b1]>deck.face[b2]){ //對牌的點數做由小到大的排序
hold = deck.face[b1];
deck.face[b1]=deck.face[b2];
deck.face[b2]=hold;
hold = deck.suit[b1]; //在數字相同時, 對花色做一次由小到大的排序
deck.suit[b1]=deck.suit[b2];
deck.suit[b2]=hold;
}
}
}
for(i=0;i<13;i++){ // 花色
c=deck.suit[i];
switch(c){
case 0:
flower=5;//梅花
break;
case 1:
flower=4; //方塊
break;
case 2:
flower=3;//紅心
break;
case 3:
flower=6; //黑桃
break;
default:
break;
}
player * newStudent; //寫入節點
newStudent =newnode(i+1, deck.face[i],flower );
insert(newStudent,root);
}
do {
system("cls"); //清空
printFunctionKey(); //選單
if (!printall(root)) //如果沒牌 就顯示 重新發牌
printf("如果要重新發排請案 1 ! 案x 結束\n");
printf("\n目前共有 %d 張牌\n", count(root));
do {
fflush(stdin); choose = getch();
}while (!ischar(choose, "Yy12Xx"));
printf("%c", choose);
switch (choose) {
case '1': {
goto start;
}
break;
case '2': {
printf(" 出牌\n想要出幾張牌 0 為pass\n");
fflush(stdin); howmany = getch();
switch (howmany) {
case '1': {
printf("輸入要刪除的 1 個號碼\n" );
scanf("%d", &number);
(deletenode(number,root));
}
break;
case '2': {
printf("輸入 2 個號碼 用空白分開\n ");
scanf("%d%d", &number,&number1);
(deletenode(number,root));
(deletenode(number1,root));
}
break;
case '3': {
printf("輸入 3 個號碼 用空白分開\n ");
scanf("%d%d%d", &number,&number1,&number2);
(deletenode(number,root));
(deletenode(number1,root));
(deletenode(number2,root));
}
break;
case '4': {
printf("輸入 4 個號碼 用空白分開\n ");
scanf("%d%d%d%d", &number,&number1,&number2,&number3);
(deletenode(number,root));
(deletenode(number1,root));
(deletenode(number2,root));
(deletenode(number3,root));
}
break;
case '5': {
printf("輸入 5 個號碼 用空白分開\n ");
scanf("%d%d%d%d%d", &number,&number1,&number2,&number3,&number4);
(deletenode(number,root));
(deletenode(number1,root));
(deletenode(number2,root));
(deletenode(number3,root));
(deletenode(number4,root));
}
break;
}
}
break;
}
} while (!ischar(choose, "Xx")); //案 x 結束
return 0;
}
沒有留言:
張貼留言