Jason asked in 電腦與網際網路程式設計 · 10 years ago

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

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

15點奉送

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

Rating
• Ashley
Lv 7
10 years ago

#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;

}