用DEV C語言寫出一個AB兩方猜拳程式

想請問一個C的題目(非C++)

A和B猜拳(剪刀=1 石頭=0 布=2)

使用者輸入A和B出拳的數字代表

接著用一個function判斷是A獲勝或是B獲勝或平手並顯示

最後若是有一方率先累計三勝則輸出獲勝方的對戰紀錄(例如 A:3WIN 2LOSE 1TIE)

不知道有沒有人可以解答

實在寫不出來

15點奉送

(這題並非跟電腦猜拳所以請不要複製其它知識+發文有亂數方面的解答)

1 Answer

Rating
  • Ashley
    Lv 7
    10 years ago
    Favorite Answer

    #include <stdio.h>

    #include <stdlib.h>

    typedef struct players {

    int win;

    int lose;

    int tie;

    } P;

    P p[2];

    int guess(int a) {

    int g;

    do {

    printf("%c 猜拳(0:石頭,1:剪刀,2:布),請輸入數字:", a + 'A' );

    scanf("%d", &g );

    } while( g != 0 && g != 1 && g != 2 );

    return g;

    }

    void judge( int x, int y ) {

    if( ( x == 0 && y == 0 ) ||

    ( x == 1 && y == 1 ) ||

    ( x == 2 && y == 2 ) ) {

    p[0].tie++;

    p[1].tie++;

    printf("平手\n");

    } else if( ( x == 0 && y == 1 ) ||

    ( x == 1 && y == 2 ) ||

    ( x == 2 && y == 0 ) ) {

    p[0].win++;

    p[1].lose++;

    printf("A 獲勝\n");

    } else if( ( y == 0 && x == 1 ) ||

    ( y == 1 && x == 2 ) ||

    ( y == 2 && x == 0 ) ) {

    p[1].win++;

    p[0].lose++;

    printf("B 獲勝\n");

    }

    }

    int main () {

    int x, y;

    do {

    x = guess(0);

    y = guess(1);

    judge( x, y );

    } while( p[0].win < 3 && p[1].win < 3 );

    if( p[0].win == 3 ) {

    printf("A:%d WIN %d LOSE %d TIE\n", p[0].win, p[0].lose, p[0].tie );

    } else {

    printf("B:%d WIN %d LOSE %d TIE\n", p[1].win, p[1].lose, p[1].tie );

    }

    system("pause");

    return 0;

    }

Still have questions? Get your answers by asking now.