Thread: Bingo code not detecting BINGO.

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    4

    Bingo code not detecting BINGO.

    I did post this before but I was in the wrong forum,I was in C and my code is C++. My bingo program works its just not detecting BINGOs. Any help will be appreciated.
    Code:
    # include <iostream># include <string.h>
    # include <time.h>
    # include <cstdlib>
    # include <stdio.h>
    # include <termios.h>
    # include <unistd.h>
    # include <iomanip>
    using namespace std;
    ///////////////////////////////////////////////////////////////////////
    int getch()
    {
    	struct termios oldt, newt;
    	int ch;
    	tcgetattr(STDIN_FILENO, &oldt);
    	newt=oldt;
    	newt.c_lflag &= ~( ICANON | ECHO );
    	tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    	ch=getchar();
    	tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    	return ch;
    }
    ///////////////////////////////////////////////////////////////////////
    int MakeCard(int Board[5][15][2])
    {
    	srand(time(NULL));
    	int x,y,z,temp,random,swap;
    	for (y=0;y<15;y++)
    		for (x=0;x<5;x++)
    			Board[x][y][0]=x*15+y+1;
    	for (y=0;y<5;y++)
    	{
    		for (x=0;x<5;x++)
    		{
    			random=rand()%15;
    			temp=Board[x][random][0];
    			Board[x][random][0]=Board[x][y][0];
    			Board[x][y][0]=temp;
    		}
    	}
    	for (z=0;z<5;z++)
    		for (y=0;y<5;y++)
    			for (x=0;x<5;x++)
    				if (Board[x][y][0]>Board[x][y+1][0])
    				{
    					swap=Board[x][y][0];			// Sort dice
    					Board[x][y][0]=Board[x][y+1][0];
    					Board[x][y+1][0]=swap;
    				}
    	for (y=0;y<5;y++)
    		for (x=0;x<5;x++)
    			Board[x][y][1]=0;
    	return 0;	
    }
    ///////////////////////////////////////////////////////////////////////
    int Color(int Board[5][15][2],int posx,int posy)
    {
    	char normal[]={ 0x1b, '[', '0', ';', '3', '9', 'm', 0 }; //Declare Variables
    	char red[]={ 0x1b, '[', '0', ';', '3', '1', 'm', 0 };
    	char blue[]={ 0x1b, '[', '0', ';', '3', '4', 'm', 0 };
    	char green[]={ 0x1b, '[', '0', ';', '3', '2', 'm', 0 };
    	char purple[]={ 0x1b, '[', '0', ';', '3', '5', 'm', 0 };
    	char cyan[]={ 0x1b, '[', '1', ';', '3', '6', 'm', 0 };
    	char yellow[]={ 0x1b, '[', '0', ';', '3', '3', 'm', 0 };
    	int x,y;
    	cout<<blue<<"   B "<<green<<"   I "<<red<<"   N "<<purple<<"   G "<<yellow<<"   O "<<normal<<"\n";
    	cout<<cyan<<"╔════╦════╦════╦════╦════╗\n";
    	for (y=0;y<5;y++)
    	{
    		for (x=0;x<5;x++)
    			{
    				cout<<cyan<<"║ ";
    				if (Board[x][y][0]<10) cout<<" ";
    				if (Board[x][y][0]<76) cout<<" ";
    				if ((x==posx)&&(y==posy)) cout<<blue;
    				else if (Board[x][y][0]==0) cout<<red;
    				else
    					cout<<normal;
    					cout<<Board[x][y][0];
    			}
    		cout<<cyan<<"║\n";
    	}
    	 cout<<cyan<<"╚════╩════╩════╩════╩════╝\n"<<normal;
    	cout<<"\n\n";
    	for (y=0;y<5;y++)
    	{
    		for (x=0;x<5;x++)
    		{
    			cout<<Board[x][y][1]<<" ";
    		}
    		cout<<"\n";
    	}
    	return 0;
    }
    ///////////////////////////////////////////////////////////////////////
    int DetectBingo(int Board[5][15][2])
    {
    	int x,y,loseH,loseV,loseD1,loseD2;
    	for (y=0;y<5;y++)
    	{
    		loseH=0;
    		loseV=0;
    		loseD1=0;
    		loseD2=0;
    		for (x=0;x<5;x++)
    		{
    			loseH=Board[x][y][0];
    			loseV=Board[x][y][0];
    			loseD1+=Board[x][y][0];
    			loseD2+=Board[4-x][y][0];
    		}
    		if (!loseH) cout<<Board[x][0][0]<<Board[0][y][0]<<"BINGO!";
    		if (!loseV) cout<<"Bingo!";
    		if (!loseD1)cout<<"Bingo!";
    		if (!loseD2)cout<<"Bingo!";
    	}
    	return 0;
    }
    ///////////////////////////////////////////////////////////////////////
    int main()
    {
    	int x=2,y=2,Board[5][15][2],input;
    	MakeCard(Board);
    	do
    	{
    		system("clear");
    		Color(Board,x,y);
    		system("sleep .01");
    		input=toupper(getch());
    		if (input=='D') x++;
    		if (input=='A') x--;
    		if (input=='S') y++;
    		if (input=='W') y--;
    		if (input==' ') 
    		{
    			if (Board[x][y][0]!=0)
    			{
    				Board[x][y][1]=Board[x][y][0];
    				Board[x][y][0]=0;
    			}
    		}
    	}while ((input!=27)||(DetectBingo(Board)));
    	//Declare Variables
    	//Input
    	//Calculations
    	//Output
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    This is just silly:
    Code:
        char normal[]={ 0x1b, '[', '0', ';', '3', '9', 'm', 0 };
        char red[]={ 0x1b, '[', '0', ';', '3', '1', 'm', 0 };
        char blue[]={ 0x1b, '[', '0', ';', '3', '4', 'm', 0 };
        char green[]={ 0x1b, '[', '0', ';', '3', '2', 'm', 0 };
        char purple[]={ 0x1b, '[', '0', ';', '3', '5', 'm', 0 };
        char cyan[]={ 0x1b, '[', '1', ';', '3', '6', 'm', 0 };
        char yellow[]={ 0x1b, '[', '0', ';', '3', '3', 'm', 0 };
    You mean:
    Code:
        const char *normal  = "\x1b[0;0m";     // not 39 !!!
                   *black   = "\x1b[0;30m";
                   *red     = "\x1b[0;31m";
                   *green   = "\x1b[0;32m";
                   *yellow  = "\x1b[0;33m";
                   *blue    = "\x1b[0;34m";
                   *magenta = "\x1b[0;35m";   // it's called "magenta"
                   *cyan    = "\x1b[0;36m";
                   *white   = "\x1b[0;37m";
    Or:
    Code:
    #define COLOR(n) "\x1b[0;" #n "m"
    
        const char *normal  = COLOR(0);
                   *black   = COLOR(30);
                   *red     = COLOR(31);
                   *green   = COLOR(32);
                   *yellow  = COLOR(33);
                   *blue    = COLOR(34);
                   *magenta = COLOR(35);
                   *cyan    = COLOR(36);
                   *white   = COLOR(37);
    Or even:
    Code:
    #define COLOR(n) "\x1b[0;" #n "m"
    And use COLOR(...) for color.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > srand(time(NULL));
    This should be done once, at the start of main.

    If you want to understand why, run this loop.
    Code:
    for ( i = 0 ; i < 100 ; i++ ) {
        srand(time(NULL));
        printf("Rand=%d\n", rand() % 100 );
    }
    > cout<<blue<<" B "<<green<<" I "<<red<<" N "<<purple<<" G "<<yellow<<" O "<<normal<<"\n";
    I would suggest you start with
    cout<<" B "<<" I "<<" N "<<" G "<<" O "<<"\n";

    Until you have all the program logic sorted out, trying to maintain eye candy just gets in the way and wastes time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bingo code not detecting BINGO.
    By Gage_Hadley in forum C Programming
    Replies: 3
    Last Post: 05-11-2018, 11:01 AM
  2. Having problem with a program for Bingo
    By Magmadragoon in forum C Programming
    Replies: 13
    Last Post: 05-04-2011, 07:20 PM
  3. Bingo with words
    By smitty007 in forum C Programming
    Replies: 19
    Last Post: 04-14-2009, 05:14 PM
  4. Bingo Card Program. Any input?
    By dukebdx12 in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2008, 03:00 PM

Tags for this Thread