View Single Post
Old Sep 5th, 2005, 11:22 AM   #5
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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;
}
Jessehk is offline   Reply With Quote