View Single Post
Old 05-11-2005, 10:55 AM   #1 (permalink)
Zephrose
Registered User
 
Join Date: May 2005
Posts: 3
Zephrose is on a distinguished road
Deck full of cards but no randomization...

ok heres what I got, this is a fully functional program but the shuffle randomization and the deck randomization do not work. I would lke some help because I cant seem to see what is wrong with it. I will continue working on it like I have been doing, but I will also continue looking to this forum for your input.

So here is my code;
Code:
//DeckofCardTest.cpp
#include <iostream>
#include <ctime>
#include <windows.h>
using namespace std;
#include "Card.h"
void main()
{
srand(time(NULL));
DECK d;
while(1)
{
	
	d.Deal();
	cout<<endl<<endl<<endl;
	system ("pause");
}
}

/////////////////////////////////////////////////////////////////


//Card.cpp: implementation file

#include "Card.h"
#include "Utility1.h"
char GetFace(int f);
CARD::CARD()
{
	suit=5;
	face=2;
	color=10;
	r=2;
	c=5;
}
CARD::CARD(int ro,int cl,int su, int fa, int co)
{
	r=ro ; c=cl ; suit=su ; face=fa ; color=co ;
}
void CARD::SetCard(int ro,int cl,int su, int fa, int co)
{
	r=ro ; c=cl ; suit=su ; face=fa ; color=co ;
}
void CARD::Show()
{
	char f;
	SetColor(color, 0);
	Box(r,c,7,7,0);
	SetPos(r+1,c+1);
	if (face>10||face==1||face==0)
	{
	f=GetFace(face);
	cout<<f;
	}
	else
		cout<<face;
	SetPos(r+3,c+4);
		cout<<(char)suit;
	SetPos(r+5,c+5);
		if (face>10||face==1||face==0)
	{
	f=GetFace(face);
	cout<<f;
	}
		else
		cout<<face;
}
int DECK::ctr=0;
int DECK::clm=10;
DECK::DECK()
{
	for (int i=0; i < 52; i++)iD[i]=i;
	for (int cc=0;cc < 52; cc++);
	{
		int r1=rand()%52;
		int r2=rand()%52;
		swap (iD[r1],iD[r2]);
	}
}
void DECK::Shuffle()
{
	ctr=0;
	for (int i=0; i < 52; i++)iD[i]=i;
	for (int cc=0;cc < 52; cc++);
	{
		int r1=rand()%52;
		int r2=rand()%52;
		swap (iD[r1],iD[r2]);
	}
}
void DECK::Deal()
{
	int su,fa,co,row=10;
	su = iD[ctr]/13+3;
	fa = iD[ctr]%13;
	co = (su>4)? 9:12;
	clm += 9;
	if (clm > 68)clm = 10;
	DECK::card[ctr].SetCard(row, clm, su, fa, co);
	DECK::card[ctr++].Show();
}
char GetFace(int f)
{
switch (f)
{
case 0:return 'K';
case 1:return 'A';
case 11:return 'J';
case 12:return 'Q';

}
}

////////////////////////////////////////////

//Card.h:
#ifndef CARD_H
#define CARD_H
class CARD
{
	int suit;
	int face;
	int color;
	int r;
	int c;
public:
	CARD();
	CARD(int ro,int cl,int su, int fa, int co);
	~CARD(){}

	void Show();
	void SetCard(int ro,int cl,int su, int fa, int co);
};
class DECK
{
	CARD card[52];
	int iD[52];
	static int ctr;//card counter
	static int clm;//column counter
public:
	DECK();
	~DECK(){}
	void Shuffle();
	void Deal();
};
#endif

/////////////////////////////////////////////////

#ifndef UTILITY106_H
#define UTILITY106_H
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

#pragma comment(lib, "winmm.lib")

//Name:			SetPos
//Purpose:		Positions the cursor at a specified row and column
//				on the console
//Arguments:	Row number and column number
void SetPos(int row, int column)
{
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = {column,row};
	SetConsoleCursorPosition(h,pos);
}

//Name:			SetColor
//Purpose:		Sets the background and foreground color of text
//				printed to the screen
//Arguments:	Foreground color number(0-15), background color number(0-15)
//				Color number	Color
//				------------	-----
//					0			Black
//					1			Dark Blue
//					2			Dark Green
//					3			...
//					..
//					14			Yellow
//					15			White

bool SetColor(int forecolor, int backcolor)
{
	HANDLE h= GetStdHandle(STD_OUTPUT_HANDLE);
	//Set the text colors
	if(!SetConsoleTextAttribute(h, (WORD)((backcolor << 4) | forecolor)))
		return false;
	return true;
}

//Name:			PlaySound
//Purpose:		Plays a .wav sound through the speakers
//Arguments:	Filename of the sound file to play
void Play(char *filetoplay)
{
	PlaySound(filetoplay,NULL, SND_FILENAME | SND_ASYNC);
	Sleep(10000);
}

//Name:			SetTitle
//Purpose:		Sets the title of the output window
//Arguments:	The title to be displayed
void SetTitle(char *title)
{
	SetConsoleTitle(title);
}

//Name:			Box
//Purpose:		Draws a box anywhere on the screen
//Arguments:	The desired row and column coordinate of the upper right corner
				//the width and the height of the box, and the type of the box
				//		type = 0	single line
				//		type = 1	double line
				//		type = 2	fancy double line
void Box(int row,  int column, int width, int height, int type)
{
	int ul=219, ho=219, ur=219, ve=219, ll=219, lr=219;
	if (type==0){ul=218, ur=191, ho=196, ve=179,ll=192,lr=217;}
	else if (type==1) {ul=201, ho=205, ur=187,ve=186,ll=200,lr=188;}
	else if (type==2) {ul=214, ho=205, ur=183,ve=186,ll=211,lr=189;}
	int i,j;
	SetPos(row,column);
	cout<<(char) ul;
	for(i=1;i<=(width-1); i++)
	{

		cout<<(char) ho;
	}
	cout<<endl;
	SetPos(row,column+width); cout<<(char) ur<<endl;
	for (j=1;j<(height-1);j++)
	{
		SetPos(row+j, column);
		cout<<(char) ve<<endl;
		for(i=1;i<width; i++)
	{
		SetPos(row+j,column + i); 
		cout<<" ";
	}
	SetPos(row+j, column+width);
	cout<<(char) ve<<endl;
	}
	row+=height-1; column;
	SetPos(row,column);
	cout<<(char) ll;
	for(i=1;i<width; i++)
	{
		SetPos(row,column+i); 
		cout<<(char) ho;
	}
	cout<<(char) lr<<endl;

}
#endif
/////////////////////////////////////////////////////////////

Im sorry for long post but Im in dire need of your help, plz review this and give me some feed back I would be very greatful of this.
Zephrose is offline   Reply With Quote