求助,编了一个宾果游戏,有些地方不明白
程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #include <ctype.h> #define MAX_INTS 5 #define MAX_ELEMS 76 #define WORD_SIZE 50 void getRandBingoCard(int card [][MAX_INTS]); void printBingoCard (int card[][MAX_INTS]); int playBingo (int card1 [][MAX_INTS], int card2 [][MAX_INTS], int matches1 [][MAX_INTS], int matches2 [][MAX_INTS]); bool repeatFcn(void); int isInCard (int searchArray [][MAX_INTS],int searchCol, int searchRows, int targetNum); int getBingoNum(int *numArray); void printWinInfo (int card [][MAX_INTS], int matches [][MAX_INTS],char nameStr[WORD_SIZE]); void displayBingoNum (int number); bool checkMatch (int card [][MAX_INTS], int matches [][MAX_INTS], int displayNum); bool checkWin (int matches [][MAX_INTS]); int main (void) { int winner; int card1 [MAX_INTS][MAX_INTS]; int card2 [MAX_INTS][MAX_INTS]; int matches1 [MAX_INTS][MAX_INTS]; int matches2 [MAX_INTS][MAX_INTS]; srand(time(NULL)); do{ printf("\nYour Bingo Card:\n"); getRandBingoCard (card1); printBingoCard (card1); printf("\nComputer's Bingo Card:\n"); getRandBingoCard (card2); printBingoCard (card2); winner=playBingo (card1,card2,matches1,matches2); if (winner==1) { printWinInfo (card1,matches1,"You"); } if (winner==2) { printWinInfo (card2,matches2,"Computer"); } } while(winner==0||repeatFcn()==false); system ("pause"); return 0; } //A void getRandBingoCard(int card [][MAX_INTS]) { int col, row, min,target,index; //target=0; min=1; for (col=0;col<MAX_INTS;++col) { for (row=0;row<MAX_INTS;++row) { card [row][col] = rand()%15+min; target=card[row][col]; // printf("%d ,%d,%d, %d\n ", row,col,card [row][col],target); index=isInCard ( card,col,row,target); // printf("\n%d", index); while(index!=-1){ card [row][col] = rand()%15+min; target = card [row][col]; index=isInCard ( card,col,row, target); } // printf("%d ,%d,%d, %d||\n ", row,col,card [row][col],target); } min +=15; } card [2][2]=0; } //B void printBingoCard (int card[][MAX_INTS]) { int i,j; printf (" B I N G O\n"); for (i=0;i<MAX_INTS;++i) { for (j=0;j<MAX_INTS;++j) { printf ("%4d", card[i][j]); } printf("\n"); } } //C int playBingo (int card1 [][MAX_INTS], int card2 [][MAX_INTS], int matches1 [][MAX_INTS], int matches2 [][MAX_INTS]) { int chosenArray[MAX_ELEMS]={0}; int bingoNum,k; memset(matches1,0,sizeof(matches1)); memset(matches1,0,sizeof(matches2)); //bool flag; //where to initialize matches //use bingoNum k=0; while(k<MAX_ELEMS){ bingoNum = getBingoNum(chosenArray); printf("Bingo Number drawn: "); displayBingoNum(bingoNum); printf("\n"); if ((checkMatch (card1,matches1,bingoNum))!=false) { printf("\nYou got a match!\n"); } checkMatch (card2,matches2,bingoNum); if ((checkWin (matches1))!=false) return 1; else if ((checkWin (matches2))!=false) return 2; ++k; }; return 0; //how about both won? } //D void printWinInfo (int card [][MAX_INTS], int matches [][MAX_INTS],char nameStr[WORD_SIZE]) { int i2, j2; printf("%s won!\nWinning Numbers: ", nameStr); for (i2=0;i2<MAX_INTS;++i2) { for (j2=0;j2<MAX_INTS;++j2) { if (matches[i2][j2]==2) displayBingoNum (card[i2][j2]); } } } //E /*======================repeatFcn=========================== reads the user's answer in a local string, returns if the first char in the answer string is 'Y' or 'y'. Pre none Post True if it is 'y' or 'Y',false if it is else */ bool repeatFcn(void) { char checkStr[WORD_SIZE]; printf("Continue to play? (N for No): "); fgets( checkStr, WORD_SIZE, stdin); if(toupper(checkStr[0])=='N') return true; else return false; } //F int isInCard (int searchArray [][MAX_INTS],int searchCol, int searchRows, int targetNum) { int index; index = searchRows; do{ //printf("%d %d %d\n",index, searchArray[index][searchCol], targetNum); --index; }while (index>=0 && targetNum != searchArray [index][searchCol]); if (targetNum == searchArray [index][searchCol]) return index; else return -1; } //G int getBingoNum(int *numArray) { int currNum; do{ currNum = rand()%75+1; }while (numArray[currNum] !=0); numArray[currNum]=1; return currNum; } //H void displayBingoNum (int number) { char colLetter; if (number>0&&number<=15) colLetter='B'; if (number>15&&number<=30) colLetter='I'; if (number>30&&number<=45) colLetter='N'; if (number>45&&number<=60) colLetter='G'; if (number>60&&number<=75) colLetter='O'; // colLetter= printf("%c-%d ", colLetter, number); } //I bool checkMatch (int card [][MAX_INTS], int matches [][MAX_INTS], int displayNum) { int temp,foundIndex; temp = displayNum/15; foundIndex = isInCard (card, temp, MAX_INTS, displayNum); if (foundIndex >=0) { matches [foundIndex][temp]=1; return true; } else return false; } //J bool checkWin (int matches [][MAX_INTS]) { int row1,col1,tot; matches[2][2]=1; for (row1=0;row1<MAX_INTS;++row1) { for (col1=0,tot=0;col1<MAX_INTS;++col1) { tot += matches [row1][col1]; } if (tot == (MAX_INTS-1)) { for (col1;col1<MAX_INTS;++col1) { matches [row1][col1]=2; return true; } } } for (col1=0;col1<MAX_INTS;++col1) { for (row1=0, tot=0;row1<MAX_INTS;++row1) { tot += matches [row1][col1]; } if (tot == (MAX_INTS-1)) { for (row1;row1<MAX_INTS;++row1) { matches [row1][col1]=2; return true; } } } //check right diagonal for (row1=0, col1=MAX_INTS-1;row1<MAX_INTS;++row1,--col1) { tot += matches [row1][col1]; } if (tot == (MAX_INTS-1)) { for (row1=0, col1=0, tot=0;row1<MAX_INTS;++row1,++col1) { matches [row1][col1]=2; } return true; } //check right diagonal for (row1=0, col1=0, tot=0;row1<MAX_INTS;++row1,++col1) { tot += matches [row1][col1]; } if (tot == (MAX_INTS-1)) { for (row1=0, col1=0, tot=0;row1<MAX_INTS;++row1,++col1) { matches [row1][col1]=2; } return true; } return false; }1.用visual c++的时候可以运行,用dev的时候排除重复数字的方程不工作
2.在我赢的时候,debug显示 Run-Time Check Failure #3 - The variable 'colLetter' is being used without being initialized.,而且N是一个?
You won!
Winning Numbers: B-11 I-23 ?0 G-48 O-67 Continue to play? (N for No):
3。只能输出我赢,没办法输出电脑赢
4。怎么把最当中的那个0改成free,是个int的二维数组
5。 计算输赢的公式好像错了,但不知道是哪里
谢谢