|
Thanks.. I wrote different ways.. pls help me out !
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
struct
{
char face;
unsigned int value;
}
card[13] =
{ {'2', 2},{'2', 3},{'2', 4},
{'5', 5},{'6', 6},{'7', 7},
{'8', 8},{'9', 9},{'10', 10},
{'J', 10},{'Q', 10},{'K', 10},
{'A', 1}
};
int main()
{
cout<<"simple twenty one card"<<endl;
cout<<"------------------------"<<endl;
int compcard=3,compscore=0,urscore=0,draw=0,urcard=0;
char cond;
const int high=21;
do
{
int comptotal=0,urtotal=0;
srand((unsigned)time(0));
cout<<"how many cards u want?(up to 5 cards)"<<endl;
cin>>urcard;
if (urcard>5)
cout<<"You could not get more than five cards!"<<endl;
else
{
cout<<endl<<"Ur cards..."<<endl;
for (int ucard=0;ucard<urcard;ucard++)
{
int x = rand() % 13;
printf(" |%c| ", card[x].face);
urtotal+=card[x].value;
if(urtotal>42)
{
urtotal=urtotal-30;
}
else if(urtotal>31&&urtotal<42)
{
urtotal=urtotal-20;
}
else if(urtotal>21&&urtotal<32)
{
urtotal=urtotal-10;
}
}
cout<<endl;
cout<<"My cards..."<<endl;
for (int Ccard=0;Ccard<compcard;Ccard++)
{
int x = rand() % 13;
printf(" |%c| ", card[x].face);
comptotal+=card[x].value;
if(comptotal>42)
{
comptotal=comptotal-30;
}
else if(comptotal>31&&comptotal<42)
{
comptotal=comptotal-20;
}
else if(comptotal>21&&comptotal<32)
{
comptotal=comptotal-10;
}
}
cout<<endl;
cout<<"I have "<<comptotal<<" and you have "<<urtotal<<" so ";
if(comptotal<=high&& urtotal<comptotal)
{
cout <<"I win "<<endl;
compscore++;
}
else if(comptotal<=high&& urtotal>high)
{
cout <<"I win "<<endl;
compscore++;
}
else if (urtotal<=high&& urtotal>comptotal)
{cout<<" you win"<<endl;
urscore++;
}
else if (urtotal<=high&& comptotal>high)
{cout<<" you win"<<endl;
urscore++;
}
else
{cout<<"no win"<<endl;
draw++;
}
}
cout<<"wana play again(n/y?)"<<endl;
cin>>cond;
}while(cond=='y');
cout<<"I win : "<<compscore<<endl;
cout<<"you win : "<<urscore<<endl;
cout<<"we draw : "<<draw<<endl;
cout<<endl;
}
|
|
|
|
|
The problem is in the following initialization:
struct
{
char face;
unsigned int value;
}
card[13] =
{ {'2', 2},{'2', 3},{'2', 4},
{'5', 5},{'6', 6},{'7', 7},
{'8', 8},{'9', 9},{'10', 10},
{'J', 10},{'Q', 10},{'K', 10},
{'A', 1}
};
since you have declare the struct to hold a char plus
an unsigned int . This works fine for all elements but {'10',10} does not match the struct because '10' isn't a character constant, but a multicharacter constant (see MSDN, C++ Character Constants topic, i.e. a 16-bits number that does not fit inside the struct face member (the compiler warns about ). What occurs is a cast of the 16-bits value (i.e. a truncation) '10'<->0x3130 to the corrensponding character value '0'<->0x30.
To fix the bug you can either choose a (really) single character to represent 10 (e.g. 'X') or change the struct to hold strings, e.g.
struct
{
char * sFace;
unsigned int value;
}
card[13] =
{ {"2", 2},{"2", 3},{"2", 4},
{"5", 5},{"6", 6},{"7", 7},
{"8", 8},{"9", 9},{"10", 10},
{"J", 10},{"Q", 10},{"K", 10},
{"A", 1}
};
hope that helps.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Just a quick question with a hopefully simple solution. Does anybody know how to detect if IE7 is running in 'protected mode'? Perhaps there is a registry setting somewhere I can read, but looking through the registry I'm not sure exactly which one it may be.
|
|
|
|
|
|
Hello everyone,
Im creating an MFC app ( an SDI one ) that uses an explorer style User Interface. There are 3 "panes" in the Child View. All 3 of these "panes" are sized programmatically and are laid horizontally inside the Child View of the Main Frame. The "panes" are literally CWnd objects and are not CDialogs.
What I want is to have a splitter functionality that is quite similar to Visual Studio 2005 where the cursor icon changes to a "splitter sizing icon" when the mouse pointer hovers above the edges of two adjacent panes. And once you click on the edge of one of these panes, it should let the user drag and size up the panes dynamically.... jus like what happens in Visual Studio 2005.
How do I achieve this functionality without using a CSplitterWindow ? Cos I heard from one of ma colleagues say that u dont always need a CSplitterWindow to implement a Splitter functionality. Is it possible ? Please do give me some hints or advice and I will follow up your suggestions on MSDN and the like...
thanx a lot..
wishing u all a happy new year with loads of fun coding !
Eraj
-- modified at 0:39 Sunday 31st December, 2006
|
|
|
|
|
technojay wrote: Cos I heard from one of ma colleagues say that u dont always need a CSplitterWindow to implement a Splitter functionality.
You don't need to use anyone else's code to implement any functionality.
If you want it to look like VS splitters then you'll need to handle drawing the bars,
responding when the mouse is over the bars, dragging the bars for resizing, etc...all the
things that CSplitterWnd does for you.
You could also just use regular sizeable windows and anytime they are resized by the user then
you'd resize the other windows appropriately. it wouldn't look like VS splitters though
What part are you having trouble with?
Mark
|
|
|
|
|
Hey Mark,
Im quite new to MFC and this is kinda my first serious app that I was asked to do to get myself acquainted with it. So right now, although I understand the technique that you are suggesting, I am pretty clueless as to how Im gonna implement it ( for example : how to draw bars between adjacent windows, enabling them to be user dragged etc ).
I am not askin for code snippets or anything like that... All I need is jus a brief description of what type of controls would be needed, what MFC classes Ill have to bring in and any specific events that I mite need to handle. If you could give me that I can reference the MSDN and try it out.
hopin to hear from you soon.
Eraj
|
|
|
|
|
What I did myself:
- implement a separate window class; do the following:
- decides if is a vertical or horizontal splitter bar, and depending on it, registers window class using LoadCursor with IDC_SIZEWE or IDC_SIZENS (I don't remember the exact names, but should be ok)
- manages the draw part similar with MFC splitter (use an erase/redraw new pos. technique - study splitter code)
- maintains the distance from a border (say left border if is a vert. splitter)
- collaborate with parent window (read: send and receive size messages)
- handles mouse messages for left button down and up, mouse move and perform the necessary painting code.
|
|
|
|
|
for (int i = length - 1; i > -1 ; i--)
{
}
for (int i = length - 1; i >= 0; i--)
{
}
I always used to think the bottom way was slower then the top because i saw "i >= 0" as being two different checks being done on i.
For this reason I thought doing "i > -1" was better cause it was just one comparison... but today i just noticed that -1 might be an additional operation being done in each iteration of the for-loop like "...is i greater then (what is the negative of 1)".
So which is the faster way? :P
EDIT: Hmm maybe it still is the first way that is faster cause the negative number might be evaluated at compile-time?
|
|
|
|
|
The difference between those two, if there is any, will never be noticeable by people. Write whichever way makes sense. Assuming that code is looping over an array or other collection, I would always write i >= 0 because i is the array index and 0 is the logical stopping point.
|
|
|
|
|
FocusedWolf wrote: So which is the faster way?
So which is the faster way?
for (int i = length - 1; i >= 0; i--)
The processor has a comparison instruction that performs >= 0 in one step.
Either way there is not a lot of reason to optimize this type of code anymore as on modern processors most integer instructions (like compares) effectively take only 1 clock tick unless a branch mis prediction causes the pipeline to be emptied. In that case it depends on how long the pipeline is. Athlons/Athlon64 have pipelines of around 22 steps while Pentium IV chips have around 30 steps.
John
|
|
|
|
|
|
How to get a dialog size by ID?
|
|
|
|
|
I want to create a view by CFormView::Create(), set the Client area is in the function's fourth parameter, so I must know the view size.
|
|
|
|
|
Do mean size of dialog ? What about GetClientRect() ?
|
|
|
|
|
Basically what I'm trying to do is add the functionality to let the user select a JPEG background image for my game, as eye candy. MFC has functions for handling bitmaps but nothing for JPEGs or other image formats. I'd like to load a JPEG and convert it to a bitmap so that I can blit it as necessary. What is the best way to do this? I'd rather do it without tacking on a huge library to my program, if possible.
|
|
|
|
|
Look at the GDI+ library.
If you are aiming only at Windows XP, it ships with the DLL.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
You can use of CImage class for load jpg file.
|
|
|
|
|
CoffeeAddict19 wrote: I'd like to load a JPEG and convert it to a bitmap so that I can blit it as necessary.
If you use GDI+ it's pretty simple...
Gdiplus::Bitmap SrcBitmap(L"C:\\Images\\MyJPEG.jpg", FALSE);
HBITMAP hBitmap;
SrcBitmap.GetHBITMAP(Gdiplus::Color(0xFF,0xFF,0xFF), &hBitmap);
|
|
|
|
|
|
CoffeeAddict19 wrote: I'd like to load a JPEG and convert it to a bitmap so that I can blit it as necessary. What is the best way to do this? I'd rather do it without tacking on a huge library to my program, if possible.
http://www.codeproject.com/bitmap/extendedbitmap2.asp
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Re
|
|
|
|
|
Hello,
I need to write a simple program that does the following:
- Accept voice from microphone
- Determine the volume of the voice (programatically)
- Increase or decrease a variable based on the volume of the voice from the microphone.
I will appreciate any help in this regard. Please a source code supplied with it will also be very good.
Thanks alot.
Commickey
|
|
|
|
|
OK, you have some demo's in the msdn, you have to search for SAPI (Speech API).
I don't remember where I saw that exactly, but searching for SAPI must help you. if you don't find it, pleaqse tell it to me, I suppose that in a couple of days I'll be able to send something that I must have stored in some backups at home.
Hope this helps.
|
|
|
|
|
Hello Stuart,
I still cannot find it... can you help again as you said?
Greetings,
Commickey
|
|
|
|
|
You could search here on CP for "record sound" for some source code.
Mark
|
|
|
|
|