Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Project Ideas (http://www.programmingforums.org/forum50.html)
-   -   beginner project -- Bingo! (http://www.programmingforums.org/showthread.php?t=5739)

Jessehk Sep 4th, 2005 11:05 AM

beginner project -- Bingo!
 
I was just recently played bingo, and I decided: "Hey, I could probably make a bingo program!".

The language would be C++ with a bingo class.

A two-dimensional array would hold 5 colums with an array of random numbers one through 15.

The idea would be that there are AI boards, and one computer board.

Random numbers are called out, and when all the numbers are called on somebodie's board ( trying to figure out how to keep track of a line ) , the player that wins ( either AI or human ), gets a point.

I could keep track of numbers that are called by marking an equivelent array with ones and zeroes, then comparing them.

Is this feasable? What do all think?

Polyphemus_ Sep 4th, 2005 11:55 AM

It's okay, I only would not call it AI, since it is just doing some checks.. about finding the lines, just loop five times through your array which checks horizontal lines, five times for the vertical lines, and write two checks for the diagonal lines.

Good luck :)

Jessehk Sep 4th, 2005 5:33 PM

Well, this is what I have so far:

bingo.h:
:

#ifndef BINGO_H_
#define BINGO_H_

//bingo.h -- header file for bingo class

using namespace std;

class bingo
{
        private:
                int numbers[5][5];
                int check[5][5];
        public:
                bingo();
                //bool checkWin(); (to be added when program develops)
                void show();
};

#endif


bingo.cpp:
:

#include <iostream>
#include "bingo.h"
#include <cstdlib>
#include <ctime>

int set[5][15] =
{
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
        {16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
        {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45},
        {46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60},
        {61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75}
};


bingo::bingo()
{
        using namespace std;

        srand(time(0));

        set:
        for(int x=0;x<5;x++)
        {       
                for(int i=0;i<5;i++)
                {       
                        numbers[x][i] = set[x][(rand() % 16) - 1];
                }
        }
        //check for and reset duplicates
       
        bool dupe = false;
        int temp;

        for(int x=0;x<5;x++)
        {
                for(int i=0;i<5;i++)
                {
                        temp = numbers[x][i];
                        for(int y=i+1;y<5;y++)
                        {
                                if(temp == numbers[x][y])
                                        dupe = true;

                                if(dupe == true)
                                        goto set;
                        }
                }
        }

        //sets all the check elements to 0
        int a;
        for(int x=0;x<5;x++)
                for(int i=0;i<5;i++)
                        check[x][i] = 0;
}

void bingo::show()
{
        using namespace std;

        cout << endl << "+----+----+----+----+----+";
        cout << endl<< "|  B    I    N    G    O |" << endl;
        cout << "+----+----+----+----+----+";
       
        cout << endl;
       
        bool first = true;
        int counter = 0;
       
        for(int i=0;i<5;i++)
        {
                cout << "|";
                counter = 0;
                for(int x=0;x<5;x++)
                {
                        ++counter;
                        if(counter == 1)
                        {
                                cout << "  " << numbers[x][i];
                                first = false;
                        }
                       
                        if(numbers[x][i] < 10)
                                cout << " ";
                       
                        if(counter > 1)
                                first = true;
                       
                        if(numbers[x][i] >= 10 && first)
                                cout << "  " << numbers[x][i];
                }
                cout << "|";
                cout << endl;
        }
        cout << "+----+----+----+----+----+" << endl;
       
        cout << endl;
}


Sample (simple ) program to show results:
:


#include <iostream>
#include "bingo.h"

int main()
{
        using namespace std;

        bingo player1;

        player1.show();

        return 0;
}


Ooble Sep 5th, 2005 9:56 AM

Funkadelic. Must try it out soon.

Jessehk Sep 5th, 2005 11:22 AM

Ok, I finished building the bulk of the class, now I just have to build the user interface for the program ( and maybe tweak and add a few functions ).

here is bingo.h

:


#ifndef BINGO_H_
#define BINGO_H_

//bingo.h -- header file for bingo class

using namespace std;

class bingo
{
        private:
                int numbers[5][5];
                char check[5][5];
        public:
                bingo();
                bool checkWin();
                friend ostream &operator<<(ostream &, bingo &);
                friend void callNumber(bingo &, bingo &, bingo &, bingo &);
};

#endif


bingo.cpp (about 240 lines, watch out. )

:


#include <iostream>
#include "bingo.h"
#include <cstdlib>
#include <ctime>



int set[5][15] =
{
        {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
        {16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
        {31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45},
        {46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60},
        {61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75}
};

using namespace std;

bingo::bingo()
{
       
        set:
        for(int x=0;x<5;x++)
        {       
                for(int i=0;i<5;i++)
                {       
                        numbers[x][i] = set[x][(rand() % 16) - 1];
                }
        }
        //check for and reset duplicates
       
        bool dupe = false;
        int temp;

        for(int x=0;x<5;x++)
        {
                for(int i=0;i<5;i++)
                {
                        temp = numbers[x][i];
                        for(int y=i+1;y<5;y++)
                        {
                                if(temp == numbers[x][y])
                                        dupe = true;

                                if(dupe == true)
                                        goto set;
                        }
                }
        }

        //sets all the check elements to 'O'
        int a;
        for(int x=0;x<5;x++)
                for(int i=0;i<5;i++)
                        check[x][i] = ' ';
}

ostream &operator<<(ostream &os, bingo &bing)
{
        using namespace std;
       
        os << endl << "+----+----+----+----+----+";
        os << endl<< "|  B    I    N    G    O |" << endl;
        os << "+----+----+----+----+----+";
       
        os << endl;
       
        bool first = true;
        int counter = 0;
       
        for(int i=0;i<5;i++)
        {
                os << "|";
                counter = 0;
                for(int x=0;x<5;x++)
                {
                        ++counter;
                        if(counter == 1)
                        {
                                os << "  " << bing.numbers[x][i];
                                first = false;
                        }
                       
                        if(bing.numbers[x][i] < 10)
                                os << " ";
                       
                        if(counter > 1)
                                first = true;
                       
                        if(bing.numbers[x][i] >= 10 && first)
                                os << "  " << bing.numbers[x][i];
                }
                os << "|";
                os << endl;
        }

        os << "+----+----+----+----+----+" << endl;
       
        os << endl;

        //display the results board;

        os << endl << "+----+----+----+----+----+";
        os << endl<< "|  B    I    N    G    O |" << endl;
        os << "+----+----+----+----+----+";
       
        bing.check[2][2] = 'X';

        os << endl;
       
        first = true;
        counter = 0;
       
        for(int i=0;i<5;i++)
        {
                os << "|";
                counter = 0;
                for(int x=0;x<5;x++)
                {
                        ++counter;
                        if(counter == 1)
                        {
                                os << "  " << bing.check[x][i];
                                first = false;
                        }
                       
                        if(counter > 1)
                                first = true;
                       
                        if(first)
                                os << "    " << bing.check[x][i];
                }
                os << " |";
                os << endl;
        }

        os << "+----+----+----+----+----+" << endl;
       
        os << endl;

        return os;
}
               
void callNumber(bingo &human, bingo &comp1, bingo &comp2, bingo &comp3)
{
        srand(time(0));
       
        int number1 = rand() % 5 + 1;
        int number2;
        char letter;

        if(number1 == 1)
        {
                number2 = rand() % 15 + 1;
                letter = 'B';
        }

        else if(number1 == 2)
        {
                number2 = rand() % 15 + 16;
                letter = 'I';
        }

        else if(number1 == 3)
        {
                number2 = rand() % 15 + 31;
                letter = 'N';
        }

        else if(number1 == 4)
        {
                number2 = rand() % 15 + 46;
                letter = 'G';
        }

        else if(number1 == 5)
        {
                number2 = rand() % 15 + 61;
                letter = 'O';
        }

        cout << "\"" << letter << "-" << number2 << "\"";

        for(int x=0;x<5;x++)
                if(human.numbers[number1 - 1][x] == number2)
                        human.check[number1 - 1][x] = 'X';

        for(int x=0;x<5;x++)
                if(comp1.numbers[number1 - 1][x] == number2)
                        comp1.check[number1 - 1][x] = 'X';

        for(int x=0;x<5;x++)
                if(comp2.numbers[number1 - 1][x] == number2)
                        comp2.check[number1 - 1][x] = 'X';
       
        for(int x=0;x<5;x++)
                if(comp3.numbers[number1 - 1][x] == number2)
                        comp3.check[number1 - 1][x] = 'X';
}
       
bool bingo::checkWin()
{
        //check for verticle lines
        for(int x=0;x<5;x++)
                if(check[x][0] == 'X' && check[x][1] == 'X' && check[x][2] == 'X' && check[x][3] == 'X' && check[x][4] == 'X')
                {               
                        check[x][0] = check[x][1] = check[x][2] = check[x][3] = check[x][4] = '@';
                        return true;
                }

        //check for horezontal lines
        for(int x=0;x<5;x++)
                if(check[0][x] == 'X' && check[1][x] == 'X' && check[2][x] == 'X' && check[3][x] == 'X' && check[4][x] == 'X')
                {       
                        check[0][x] = check[1][x] = check[2][x] = check[3][x] = check[4][x] = '@';
                        return true;
                }

        //check for diagonal lines
        if(check[0][0] == 'X' && check[1][1] == 'X' && check[2][2] == 'X' && check[3][3] == 'X' && check[4][4] == 'X')
        {       
                check[0][0] = check[1][1] = check[2][2] = check[3][3] = check[4][4] = '@';
                return true;
        }

        if(check[0][4] == 'X' && check[1][3] == 'X' && check[2][2] == 'X' && check[3][1] == 'X' && check[4][0] == 'X')
        {
                check[0][4] = check[1][3] = check[2][2] = check[3][1] = check[4][0] = '@';
                return true;
        }

        //send false if no wins
        else
                return false;
}


and the code to test it ( press enter to continue, untill there is a winner, calling number and filling the spaces at the same time. It ususually takes about 25 hits to gwt a winner.

:


#include <iostream>
#include "bingo.h"

int main()
{
        using namespace std;
       
        srand(time(0));

        bingo player;
        bingo comp[3];

        while(player.checkWin() == false && comp[0].checkWin() == false && comp[1].checkWin() == false && comp[2].checkWin() == false)
        {
                callNumber(player, comp[0], comp[1], comp[2]);
                cout << "player\n" << player << "comp1\n" << comp[0] << "comp2\n" << comp[1] << "comp3\n" << comp[2] << endl;
                cin.get();
        }

        cout << "player\n" << player << "comp1\n" << comp[0] << "comp2\n" << comp[1] << "comp3\n" << comp[2] << endl;

        cout << "Winner!" << endl;

        return 0;
}


Infinite Recursion Sep 6th, 2005 9:01 AM

As soon as I get the net back at home (damn hurricane), I will compile this and check it out.

Jessehk Sep 6th, 2005 10:30 AM

Thanks Infinite Recursion, I would love some feedback.:)

Keep in mind that the test program just tests the class, it isn't the full version.

I am working on the more user friendly version.

Sane Sep 6th, 2005 4:12 PM

Good work, and for a suggestion: Let the user pick the numbers that will be on the board (and an option for it to be chosen at random) .

Jessehk Sep 6th, 2005 5:42 PM

Finished -- check in the finished projects section :)

frankish Oct 24th, 2005 12:10 PM

Have you looked at PHP? I'm asking you this because I'm thinking that you want to make the game graphical, right? The problem is that in C++ you have to download graphics libraries or use some API functions to code such as simple game as bingo. I'd suggest making it a web program but it's your call. Oh... you finished it... cool... I'll try it!


All times are GMT -5. The time now is 11:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC