2014-12-02 39 views
0

我仍然試圖製作骰子游戲,而我只需要知道什麼是最好的方式爲我的遊戲添加分數,這樣我就可以打印出明顯的贏家。我需要一堆if-else語句,還是創建頭文件會更容易?最有效的方法來添加分數?

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#include <time.h> 
#include <string.h> 

void clean_stdin(void) { 
int c; 
do { 
    c = getchar(); 
} while (c != '\n' && c != EOF); 
} 


int main() { 

int i, r, diceRoll; 
char player1[20]; 
int score1[5]; 
int sum; 
srand(time(NULL)); 

printf("\t\t\t Welcome to Farkle Lite!\n"); 
printf(" Highest score wins!\n\n"); 
printf(" The rules:\n"); 
printf(" 1 = 100 pts, 5 = 50 pts. Everything else is 0.\n\n Get 3 of a kind, and multiply the result by 100 (Ex. Three 4s = 400 pts).\n"); 
printf(" If you get more than 3 of a kind, any repeats will double the score \n (Ex. Four 4s = 800 pts, Five 4s = 1600.)\n\n"); 
printf(" Rolling 1-6 gets you an automatic 1500 pts!\n\n"); 
printf("Enter name for Player 1:\n"); 
fgets (player1, 20, stdin); 
clean_stdin(); 

printf("\n\n %s Dice Roll:\n\n", player1); 

for(i = 0; i < 6; i ++){ 
    r = (rand()%6)+1; 
    diceRoll= r; 
    score1[i]= diceRoll; 
    sum = score[i]; 
    printf(" test %d \n", score1[i]); 
} 
printf(" %d", score1[i]); 
return 0; 
} 

回答

1

這是使用函數的好機會。每個玩家都會在六次角色中扮演角色,每個玩家都會將他們的得分合計起來,並且每個玩家都能夠輸入他們的名字。我們可以創建一個函數來集中這種行爲。爲此,我們需要將玩家姓名和玩家分數存儲在數組中,以便第二次運行該函數時,我們不會覆蓋第一次運行中的信息。下面是一個例子:

void play (int playerNo, char* name, int* score) 
{ 
    int loop; 
    int role; 

    printf("Enter name for Player %d:\n", playerNo); 
    fgets (name, 20, stdin); 
    clean_stdin(); 

    printf("\n\n %s Dice Roll:\n\n", name); 

    *score = 0; 
    for(loop = 0; loop<6; loop++) 
    { 
     role = (rand()%6)+1; 
     *score += role; 
     printf (" %d\n", role); 
    } 
} 

該功能將允許用戶輸入名稱,然後將角色死亡6次,總計得分。退出時,playerNames和playerScores數組中的相應條目將被更新。

已經得到了這個,我們只是只需要通過玩家循環,調用這個函數爲每個玩家:

int main() 
{ 
    const int playerCount = 2; 
    char  playerNames [playerCount][20]; 
    int   playerScores[playerCount]; 
    int   loop; 
    int   highestScore = 0; 
    int   highestPlayer; 

    // Seed rng 
    srand(time(NULL)); 

    // Instructions 
    printf("\t\t\t Welcome to Farkle Lite!\n"); 
    printf(" Highest score wins!\n\n"); 
    printf(" The rules:\n"); 
    printf(" 1 = 100 pts, 5 = 50 pts. Everything else is 0.\n\n Get 3 of a kind, and multiply the result by 100 (Ex. Three 4s = 400 pts).\n"); 
    printf(" If you get more than 3 of a kind, any repeats will double the score \n (Ex. Four 4s = 800 pts, Five 4s = 1600.)\n\n"); 
    printf(" Rolling 1-6 gets you an automatic 1500 pts!\n\n"); 

    // Let each player play the game 
    for (loop=0; loop<playerCount; loop++) 
     play (loop+1, &playerNames[loop][0], &playerScores[loop]); 

    // Tally scores 
    for (loop=0; loop<playerCount; loop++) 
    { 
     if (playerScores[loop] > highestScore) 
     { 
      highestScore = playerScores[loop]; 
      highestPlayer = loop; 
     } 
    } 

    // Display winner 
    printf ("Player %d: %s wins with a scrore of %d", 
     highestPlayer+1, 
     playerNames[highestPlayer], 
     playerScores[highestPlayer]); 

    return 0; 
} 

以後我們就可以辦理所有的得分記錄是最高的,然後顯示贏家。

相關問題