Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-11-2005, 09: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
Old 05-11-2005, 11:54 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
ISO C++ never heard of <windows.h>. Furthermore, it is not clear how the a deck and a card are internally represented.

However, in general, if you are using a STL container, or C-style arrays, you may wish to implement something like this:
Code:
  void Deck::Shuffle() 
  {
    static bool seeded;
    if (!seeded)
    {
      srand(time(0));
      seeded = true;
    }
    std::random_shuffle(theDeck.begin(), theDeck.end());
  }
In general, there's a lot to improve on your code, but you have to remove the luxury stuff like the Windows API header and calls. Then we could take a better peek at to implement the code with more quality.
__________________
Valmont is offline   Reply With Quote
Old 05-11-2005, 12:40 PM   #3 (permalink)
Zephrose
Registered User
 
Join Date: May 2005
Posts: 3
Zephrose is on a distinguished road
Im am working with visual studio .net 2003

The reason for windows.h is because I am working with C++. The code you have posted is great and all but I dont think that its quite what I need for C++ if you are using just C. I am familar with the first part of the code but the end of it is something I havent seen before. I am just a student in college right now, and I am learning the ways of programming. If you could explain what the end part of the function might do or come up with a vision for C++ I would appritiate it.
Zephrose is offline   Reply With Quote
Old 05-11-2005, 06:05 PM   #4 (permalink)
Zephrose
Registered User
 
Join Date: May 2005
Posts: 3
Zephrose is on a distinguished road
And its all on a semi-colon

Thats right a semi-colon... I was loooking and looking and all I needed to do was remove a typo semi-colon. When I figured this out all I had to do was move the srand to a different location within the code to make it a continuous randomizer.

Thanks for your help though. It was killing me.
Zephrose is offline   Reply With Quote
Old 05-12-2005, 03:37 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Well, next time, post the minimal compilable code. Otherwise I am supposed to look for typos for you .

Try my STL based version of shuffling. All you need is your cards to be stored in a "normal" array or a std::vector from <vector>.
Include <algorithm> to use std::shuffle.
Just quickly startup a simple C++ console project and experiment with it. See if you like it .
__________________
Valmont is offline   Reply With Quote
Old 05-12-2005, 09:29 AM   #6 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
To randomize an array:

Code:
for (i=0; i<ARRAY_SIZE-1; i++)
{
     j = i + rand()%(ARRAY_SIZE-i);
     if (i != j)
          swap (array[i], array[j]);
}
The idea is this:

The first element gets a random card.
The second element gets a random card from the remaining 51
...
The last guy gets the last leftover card.

Last edited by redhead; 05-12-2005 at 11:32 AM. Reason: Deleted the unfinished post
QUantumAnenome is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shotgunning network cards Belisarius Linux / BSD / OS X 13 04-21-2005 11:11 AM


All times are GMT -8. The time now is 10:19 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting