有關C語言十八拉遊戲的問題!!

#include <iostream>

#include <stdlib.h>

using namespace std;

int winorlose(int a, int b, int c, int d);

int main(int argc, char *argv[])

{

string yn;

cout << "Do you want to play ? (y/n)\n";

cin >> yn;

if(yn=="y")

{

int roll1, roll2, roll3, roll4, roll5, roll6, roll7, roll8;

int point1, point2;

int win1=0, win2=0;

srand(time(NULL));

cout << "three playing two win" << endl;

for(int j=1; j<=3; j++)

{

printf("the %d playing...\n",j);

for(int i=0; i<1; i++)

{

roll1 = rand()%6;

roll2 = rand()%6;

roll3 = rand()%6;

roll4 = rand()%6;

if (roll1 == 0) roll1 = 6;

if (roll2 == 0) roll2 = 6;

if (roll3 == 0) roll3 = 6;

if (roll4 == 0) roll4 = 6;

cout << "player 1 go..." << endl;

system("PAUSE");

printf("player 1 get the numbers are :%d,%d,%d,%d\n", roll1, roll2, roll3, roll4);

point1 = winorlose(roll1, roll2, roll3, roll4);

if (point1 < 0)

{

i--;

cout << "All different! Please play again!" << endl;

}

}

for(int i=0; i<1; i++)

{

roll5 = rand()%6;

roll6 = rand()%6;

roll7 = rand()%6;

roll8 = rand()%6;

if (roll5 == 0) roll5 = 6;

if (roll6 == 0) roll6 = 6;

if (roll7 == 0) roll7 = 6;

if (roll8 == 0) roll8 = 6;

cout << "player 2 go..." << endl;

system("PAUSE");

printf("player 2 get the numbers are :%d,%d,%d,%d\n", roll5, roll6, roll7, roll8);

point2 = winorlose(roll5, roll6, roll7, roll8);

已更新項目:

if (point2 < 0)

{

i--;

cout << "All different! Please play again!" << endl;

}

}

cout << "win or lose" << endl;

2 個已更新項目:

system("PAUSE");

if (point1 > point2) {cout << "Player 1 win!!!" << endl; win1 = win1 + 1;}

else if (point1 < point2) {cout << "Player 2 win!!!" << endl; win2 = win2 + 1;}

else cout << "equal!!!" << endl;

3 個已更新項目:

if (win1 == 2)

if (j <= 2)

{cout << "Player 1 has two wins" << endl; break;}

if (win2 == 2)

if (j <= 2)

{cout << "Player 2 has two wins" << endl; break;}

4 個已更新項目:

}

cout << "the result is" << endl;

if (win1 > win2) cout << "!!!Congratulation the player 1 win!!!!";

else if (win1 < win2) cout << "!!!Congratulation the player 2 win!!!!";

else cout << "!!!equal!!!";

}

5 個已更新項目:

system("PAUSE");

return 0;

}

int winorlose(int a, int b, int c, int d)

{

int point=0;

if(a == b && b == c && c == d)

6 個已更新項目:

{point = a + 12;}

else if(a == b && c != d) {point = c + d; }

else if(a == b && c == d && a > c)

{point = a + b; }

else if(a == b && c == d && a < c)

{point = c + d; }

7 個已更新項目:

else if(a == c && b != d) {point = b + d;}

else if(a == c && b == d && a > b)

{point = a + c; }

8 個已更新項目:

else if(a == c && b == d && a < b)

{point = b + d; }

else if(a == d && b != c) {point = b + c;}

else if(a == d && b == c && a > b)

{point = a + d; }

9 個已更新項目:

else if(a == d && b == c && a < b)

{point = b + c; }

else if(b == c && a != d) {point = a + d; }

else if(b == d && a != c) {point = a + c; }

else if(c == d && a != b) {point = a + b; }

else

10 個已更新項目:

{

point=-1;

}

return point;

}

11 個已更新項目:

不知道哪裡有錯...

第一個iostream就有問題了~

請各位大大幫個忙~

我們用的是c語言喔!!

12 個已更新項目:

另外說明

這個是我們在另外一個問題中找到的程式碼

是別人的回答

這個是十八啦的遊戲

但是不知為什麼C語言無法執行

怪~~

1 個解答

評分
  • 1 0 年前
    最佳解答

    我使用Visual Studio 2008執行

    修改以下三段程式碼後,即可順利執行:

    1. 於

      #include <iostream>

      #include <stdlib.h> 底下再加入一行

      #include <ctime>

    說明:內頁中有一行 srand(time(NULL));

       是使用現在時間當亂數因子,

       但未載入ctime程式庫

       會無法使用 time()函事。

    2. 將 string yn; 改為 char yn;

    說明:C#好像有 string類型變數

       但至少在C和C++皆無預設解讀 string類型的變數

       所以改為char

    3. 將 if(yn=="y") 改為 if(yn=='y')

    說明:由於string (字串) 改成 char (字元),

       所以將判斷改為' '字元的代表符號

       註: "中間所夾為字串" --->即一串文字

    '中間所夾為字元' --->即單一符號

    如還有錯誤地方,請將您的

    「編譯軟體」與 「錯誤訊息」

    回覆告知,

    這樣好方便抓出問題在那,謝謝。

    參考資料: 自己的粗淺見解
還有問題?馬上發問,尋求解答。