在linux下成功写Bingo游戏

时光怂恿深爱的人放手 提交于 2020-01-13 13:40:27

帮一个外国友人完成他的小作业
•Bingo is a game of chance in which each player matches numbers printed in different arrangements on cards which the numbers the game host (caller) draws at random, marking the selected numbers with tiles one at a time. •When a player finds the selected numbers are arranged on their card in a row (horizontal or vertical or diagonal), they call out “Bingo!” to alert all participants to a winning card •Caller: 2, 9, 12, 5, 11, 8 (Player 1 wins)

A bingo board has mrows and mcolumns. Each entry has a different number from 1 to m×m. ●The numbers from 1 to m×mwill be randomly called. If a player has all the numbers in a row, a column, or a diagonal called he can declare bingo and win the game. ●Now given the bingo boards of nplayers, determine who wins the bingo.

The program first open and read the input text file, and then simulate the Bingo. ●Simulate the game by printing out the number being called, each of the board with the player names, the number being calledin greencolor and the numbers markedby the player in redcolor in the order as each player appear in input file. ●When one or more players win(s), mark their name with “Bingo!” and print (at the last line) the number by which the winner(s) won the game and the name(s) of the winner(s). If there are more than one winner, print the player one by one according to the order they appear in input file.

总之大致要求效果长这样
在这里插入图片描述
在这里插入图片描述
输出其中间过程
输出结果

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

int order[65536];//note the order of caller 
int winner[100];//mark the winner 
int n,m;//the num of player,the num of rows
int check[10][256][256];//check
int test[10][256][256];//test data
char name[10][50];//the name of the players

//colour define 
#define printRed(x) printf("\x1B[31m %d ",x)
#define printGrn(x) printf("\x1B[32m %d ",x)
#define printWht(x) printf("\x1B[37m %d ",x)
#define printWhtBlank()  printf("\x1B[37m")


void Print_Init()
{
        printf("Start the game\n");
        for(int count = 0; count < n;count++)
        {
                printf("***%s***\n",name[count]);//test whether it is successful
                for (int i = 0; i < m; ++i)
                {
                        for (int j = 0; j < m; ++j)
                                printf("%d ",test[count][i][j]);
                        printf("\n");
                }
        }
        printf("\n");
}

int checkplayer(int cnt)//check the condition of cnt player
{
        for(int i = 0;i < m; i++)
        {
                int sum1 = 0;//row
                int sum2 = 0;//line
                for(int j = 0;j < m; j++)
                {
                        if(check[cnt][i][j]>0)
                                sum1++;
                        if(check[cnt][j][i]>0)
                                sum2++;
                }
                if(sum1==m||sum2==m)//that is OK
                        return 1;
          //    printf("sum1=%d sum2=%d\n",sum1,sum2);//to see whether the output is right
        }

        int sum3 = 0;//diagonal
        int sum4 = 0;
        for(int i =0 ;i < m; i++)
        {
                if(check[cnt][i][i]>0)
                        sum3++;
                if(check[cnt][i][m-i-1]>0)
                        sum4++;
        }
        //printf("sum3=%d sum4=%d\n",sum3,sum4);//to check the output of diagonal
        if(sum3==m||sum4==m)
                return 1;
        return 0;
}

void markcolour(int cnt,int caller)//mark the colour
{
    for(int i = 0; i < m; i++)
    {
            for(int j = 0 ;j < m; j++)
            {
                if(check[cnt][i][j]>0)
                        check[cnt][i][j]++;//mark with red
                if(test[cnt][i][j]==caller)
                        check[cnt][i][j]=1;//mark with green
              //  printf("%d ",check[cnt][i][j]);//to see whether the mark is right
            }
    }
    //printf("%d\n", checkplayer(cnt));//to see whether the return is right
}
int main(int argc, char const *argv[])
{
    FILE *fp=fopen("testbingo.txt","r");
    if (fp == NULL)
    {
        fprintf(stderr, "Can't open input file\n");
        exit(1);        //You need to include stdlib.h for exit function
    }
    fscanf(fp, "%d%d", &n,&m);

    for(int count = 0; count < n;count++)
    {
        fscanf(fp,"%s",name[count]);
        for (int i = 0; i < m; ++i)
            for (int j = 0; j < m; ++j)
                fscanf(fp,"%d",&test[count][i][j]);
        //@ to see whether the data is right
        /*for (int i = 0; i < m; ++i)// test whether it is successful
        {
                for (int j = 0; j < m; ++j)
                        printf("%d ",test[count][i][j]);
                printf("\n");
        }*/
    }
    for (int i = 0; i < m*m; ++i)
        fscanf(fp,"%d",&order[i]);
    //now ,ready perfect

    Print_Init();

    int flag = 0;//to check whether someone win the game
    int callerorder=0;//callers' order
    int wincount=0;//the total number of the winner
    int caller;
    memset(check,0,n*m*m*sizeof(int));//to initial
        
    while(callerorder<m*m)
    {
        caller = order[callerorder];
        printf("Caller called:\n");
        printf("%d\n",caller);
        for(int cnt = 0;cnt < n; cnt++)
        {
            markcolour(cnt,caller);
            if(checkplayer(cnt)==1)
            {
                    winner[wincount]=cnt;//mark the winner
                    wincount++;
                    flag=1;
                    printf("***%s Bingo***\n",name[cnt]);
            }
                   
            else
                printf("***%s***\n",name[cnt]);
            for(int i = 0; i < m; i++)//output
            {
                for(int j = 0 ;j < m; j++)
                {
                        if(check[cnt][i][j]>1)
                            printRed(test[cnt][i][j]);
                        else if(check[cnt][i][j]==1)
                            printGrn(test[cnt][i][j]);
                        else
                            printWht(test[cnt][i][j]);
                }
                printf("\n");
            }

        }
        if(flag==0)
            callerorder++;//nobody wins,again
        else
            break;//someone win the game
    }
        printf("The game result:\n");
        printf("%d\n",caller);
        for(int i = 0;  i < wincount; i++)
                printf("%s ",name[winner[i]]);
        fclose(fp);
        return 0;
}

喜闻乐见的贴代码环节
单纯简便的暴搜与插小旗子标注颜色即可
大概也许效果还行
成功迟到炸鸡

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!