|
Ok below is my code for a Game of Life in C++, my question is how do I make it so after ever generation it stops and the user has to press enter to go to the next generation?
#include <iostream>
using namespace std;
#define alive '1' //This is to make it easier to set a cell to "alive" or "dead" by defining the words as variables equal to a symbol.
#define dead '0'
char currBoard[20][30];
char newBoard[20][30];
char a[20][30];
char b[20][30];
char c[20][30];
char d[20][30];
void printBoard(char a[20][30]);
void resetBoard(char b[20][30]);
int countNeighbors(char c[20][30],int x, int y);
void makeNewBoard(char c[20][30],char d[20][30]);
int main()
{
int g,x,y,i;
resetBoard(currBoard);
resetBoard(newBoard);
cout << "Number of Generations: ";
cin >> g;
x=0;
y=0;
i=1;
while((x>=0) && (y>=0))
{
cout << "Enter point #" << i << ": ";
cin >> y >> x;
currBoard[y][x]=alive;
i++;
}
for (int i=0;i<g;i++)>
{
if (i%2 == 0)
{
printBoard(currBoard);
makeNewBoard(currBoard,newBoard);
}
else
{
printBoard(newBoard);
makeNewBoard(newBoard,currBoard);
}
}
}
void resetBoard(char a[20][30])
{
int y,x;
for(y=0;y<20;y++)
{
for(x=0;x<30;x++)
{
a[y][x]=dead;
}
}
}
void printBoard(char b[20][30])
{
int y,x;
for(y=0;y<20;y++)
{
for(x=0;x<30;x++)
{
cout << b[y][x];
}
cout<<endl;
}
cout<<endl;
}
int countNeighbors(char c[20][30],int y, int x)
{
int prevx;
int prevy;
int nextx ;
int nexty ;
prevy = (y-1 < 0 ? 19 : y-1);
prevx = (x-1 < 0 ? 29 : x-1);
nexty = (y+1 >= 20 ? 0 : y+1);
nextx = (x+1 >= 30 ? 0 : x+1);
int neighbors=0;
if (c[y-1][x-1]==alive)
{
neighbors++;
}
if (c[y-1][x]==alive)
{
neighbors++;
}
if (c[y-1][x+1]==alive)
{
neighbors++;
}
if (c[y][x-1]==alive)
{
neighbors++;
}
if (c[y][x+1]==alive)
{
neighbors++;
}
if (c[y+1][x-1]==alive)
{
neighbors++;
}
if (c[y+1][x]==alive)
{
neighbors++;
}
if (c[y+1][x+1]==alive)
{
neighbors++;
}
return neighbors;
}
void makeNewBoard(char c[20][30],char d[20][30])
{
int neighbors;
int y,x;
for(y=0;y<20;y++)
{
for(x=0;x<30;x++)
{
neighbors=countNeighbors(c,y,x);
switch(neighbors)
{
case 0:
case 1:
d[y][x]=dead;
break;
case 2:
if (c[y][x]==dead)
{
d[y][x]=dead;
}
else
{
d[y][x]=alive;
}
break;
case 3:
d[y][x]=alive;
break;
case 4:
case 5:
case 6:
case 7:
case 8:
d[y][x]=dead;
break;
}
}
}
}</iostream>
|
|
|
|
|
I pick one number by one number from numbers of 1 to 100, then submit them to 5 places (20 numbers for each).
I hope results are differnent, so I use rand() and srand() to generate randomly "pick up" and "submit".
but results are same always. it sounds that rand() function is not random.
do you know how to solve the problem? any function which is real random?
thanks
|
|
|
|
|
I am not sure what you mean by submit. The function srand is to seed the random number generator. I have run some tests on the rand() function that comes with the Microsoft C/C++ compiler. I find it to be an excellent random number generator. Is it a perfect one? No. The function rand() is suppose to produce a random number whose value is independent of the last value it returned.
Are you saying that for two different seeds you are getting the same sequence of random numbers? If this is true, then I think there is a problem.
I have a feeling that I did not help you. I also believe that if I understood your problem better, I could give you a better answer.
Bob
|
|
|
|
|
Calling rand() 100 times does not guarantee 100 unique numbers if that is what you mean.
«_Superman_»
|
|
|
|
|
Please post some code. Which seed did you provide to srand ? If it is always the same, then your generated sequence will always be the same. Could that be the problem ?
|
|
|
|
|
Without some code, I cannot say much. But I've the feeling you're calling srand and rand pair each time generating another random number (and which is the seed for srand?). Generally, srand is called before first call to rand and that is it.
--
Arman
|
|
|
|
|
includeh10 wrote: any function which is real random
No. A purely software function can only ever be pseudo-random. The srand function tells the rand function where to start in its pseudo-random sequence.
You need a hardware source of entropy for a real random source.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
includeh10 wrote: it sounds that rand() function is not random.
It's always been known that rand() , and any other computer algorithm, produces pseudo-random numbers.
includeh10 wrote: any function which is real random?
No.
The problem is not with rand() . The problem is with how you are using it.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
See CryptGenRandom[^] API.
includeh10 wrote: any function which is real random?
From the docs:
The data produced by this function is cryptographically random. It is far more random than the data generated by the typical random number generator such as the one shipped with your C compiler.
...
With Microsoft CSPs, CryptGenRandom uses the same random number generator used by other security components. This allows numerous processes to contribute to a system-wide seed. CryptoAPI stores an intermediate random seed with every user.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Please consider the following code fragement:
<br />
int errNumber = 0;<br />
int retValue = button1.Create( TEXT( "xxx" ), WS_VISIBLE|WS_CHILD|WS_GROUP|BS_AUTORADIOBUTTON,<br />
CRect( 300, 300, 20, 20 ), this, 500 );<br />
if ( retValue != 0 ) {<br />
errNumber = GetLastError();<br />
}<br />
retValue = button2.Create( TEXT( "xxx" ), WS_VISIBLE|WS_CHILD|BS_AUTORADIOBUTTON,<br />
CRect( 300, 350, 20, 20), this, 500 )<br />
I put this code inside my routine CMainWindow::OnCreate(). I would expect it to create and
show two buttons. These buttons should be a place on the screen where the user can
check one of them. However, neither button shows up. The return value from the create
method is 1, which I interpret to mean no error. Is there something else I need to do?
Thanks
Bob
|
|
|
|
|
See the constructor of CRect here[^]. The documentation says, CRect(left, top, right, bottom), not CRect(left, top, width, height), that is probably your problem. Change your CRect(300, 300, 20, 20) to CRect(300, 300, 320, 320) and the other CRect(300, 350, 20, 20) to CRect(300, 350, 320, 370).
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
The CRect constructors look wrong - the third and fourth arguments should be the right and bottom co-ordinates of the rectangle, but it looks live you've interpreted them as width and height?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I've got a frame that's contained in a DLL and called via:
extern "C" __declspec(dllexport) BOOL APIENTRY TermLoaderMode()
{
static CBaseMultiDocTemplate *pDocTemplate = NULL; //Derived from CMultiDocTemplate
CBaseApp *pApp = (CBaseApp *)AfxGetApp();
if (pDocTemplate == NULL)
{
pDocTemplate = new CBaseMultiDocTemplate( IDR_LOADER_USER,
RUNTIME_CLASS(CBaseDoc),
RUNTIME_CLASS(CTermLoaderFrame),
RUNTIME_CLASS(CTermLoaderView));
pApp->AddDocTemplate( pDocTemplate );
}
return( TRUE );
}
I'm trying to change out IDR_LOADER_USER with IDR_LOADER_DEV based upon user type. Users get IDR_LOADER_USER, devs get IDR_LOADER_DEV.
In my view class' OnInitialUpdate(), I call the following function but it fails on ::LoadMenu. (returns NULL).
Here's my menu grab function:
void CTermLoaderView::ReplaceMenu(void)
{
HMENU hm = ((CTermLoaderView*)GetDocument())->m_hMenuDefault = ::LoadMenu( ::AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_LOADER_DEV));
if(((CTermLoaderView*)GetDocument())->m_hMenuDefault == NULL)
{
TRACE0("Failure to get menu object.");
AfxMessageBox(_T("Failure to get menu object."));
return;
}
CMDIFrameWnd* frame = ((CTermLoaderView*)GetParent())->GetMDIFrame();
frame->MDISetMenu(CMenu::FromHandle(((CTermLoaderView*)GetDocument())->m_hMenuDefault), NULL);
frame->DrawMenuBar();
}
Help?
Thanks in advance!
|
|
|
|
|
Hi,
I have created one sample application which plays mp3 files, i have used MCI functions to get that application working. I am able to play the call, but not able to move the slider.
Can any one help me how it is happening like this..?
Any help will be appriciated..
Thanks in advance..
Thanks
Venkatesh.
|
|
|
|
|
Hey everybody
I am writing a service that runs as the SYSTEM user (as always...).
I know how to impersonate threads to the current logged on user, but when I am connecting to a shared folder things look a bit different.
I'll explain the problem in a scenario
I am trying to open a file \\comp1\remoteFolder\file.doc, and the thread is impersonated to MYDOMAIN\user1.
MYDOMAIN\user1 has access to \\comp1\remoteFolder\file.doc.
When I am trying to open the file in the remote location, I am getting ACCESS DENIED.
trying to figure out why, I used sysinternal's filemon.
In there it writes the comment "NT_AUTHORITY\SYSTEM", which means, from what I understood, that although the thread is impersonate it access using SYSTEM.
It seems that to access the file windows checks if I have the right token (if I have access).
and HERE IS THE PROBLEM:
Why does windows says I'm SYSTEM user although the thread is impersonated?
and more than that, how do I impersonate the current user over the network ???
Thanks A LOT in advance!!!!
GF.
|
|
|
|
|
I have some code reading and writing to a USB Windows CE device (it's actually using the ActiveSync driver). On Windows XP I use CreateFile to open the port and ReadFile and WriteFile to do IO - this works fine. On Vista I can open the port but ReadFile and WriteFile always return this invalid function error (error code 1). Any ideas?
|
|
|
|
|
If it works on XP, but not on Vista, the usual response has to be "Mmmm, security?"
Having seen this page[^], though, it could be that you need to use this WIndows Mobile Device Center thing?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
You reply does not help. I need specific details not vague references.
|
|
|
|
|
|
Hi,
In the application that I am developing, I wants to store a list(using map) of structures.
Here I want the key to be an array of wide characters i.e of type TCHAR.
typedef struct
{
int nID;
int somedata;
}MyStruct;
The structure list would be of the form.
typedef std::map<tchar mystruct=""> MyStructMap;
MyStructMap objStructMap;</tchar>
I want to filling the map using a key of type TCHAR which will uniquely identify each instance of the struct that is inserted.
The place where I am inserting an structure with an unique TCHAR key is as follows.
Mystruct structobj1;
structobj1.nID = 100;
structobj2.somedata = 253;
objStructMap.insert(MyStructMap::value_type(_T("Unique1"), structobj1)).second;
The Insert operation mentioned above is failing with error
error C2665: 'std::pair<_Ty1,_Ty2>::pair' : none of the 3 overloads could convert all the argument types e:\vc05\sam\MyApp\MyApp.cpp 398
error C2228: left of '.second' must have class/struct/union e:\vc05\sam\MyApp\MyApp.cpp 398
I could you an pointer to a TCHAR as a Key for the map, but don't want to do that because I have to create an object on the heap and dont want to do that.
Is my technique of inseting an TCHAR array and struct pair in the map object briefed above correct?
Please point the cause of the error.
|
|
|
|
|
struct str
{
char cFileName[247];
};
struct cmp_str
{
bool operator()(str const a, str const b) const
{
return strcmpi(a.cFileName, b.cFileName) < 0;
}
};
typedef std::map<str,> GetFileMap;
typedef GetFileMap::value_type GetFileValType;
typedef GetFileMap::iterator GetFileItor;
|
|
|
|
|
liangyenchyen,
I am not able to get you.
All i want to achieve is add my user defined structure and associate a key which is an TCHAR array.
some thing like
objStructMap.insert(MyStructMap::value_type(_T("Unique1"), structobj1)).second;
objStructMap.insert(MyStructMap::value_type(_T("Unique2"), structobj2)).second;
objStructMap.insert(MyStructMap::value_type(_T("Unique3"), structobj3)).second;
and so on.
Later I can access a member using the key as follows
MyStructMap::iterator iter = objStructMap.find(_T("Unique1"));
How do i achieve this?
|
|
|
|
|
You are using pointers to TCHAR as the string, in effect, not arrays. Arrays are funny things in C and C++ - they're really just pointers in drag.
Anyway, you're better off using a string class, like std::basic_string<TCHAR> , because it already defines comparison operations that std::map needs, whereas a TCHAR* doesn't.
Something like this should work:
typedef std::basic_string<TCHAR> tstring;
typedef std::map<tstring, MyStruct> MyStructMap;
MyStructMap objStructMap;
objStructMap.insert(std::make_pair(_T("Unique1"), structobj1));
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
It works now.
Thanks a lot for the code snippet.
Thanks to others who have contributed to this thread.
|
|
|
|
|
First, you have to insert the < and > symbols yourself using the buttons just above the emoticons, oyherwise they will disappear.
Next, why don't you use a std::string or std::wstring to do that ? It will make your life way much easier. The map will rely on fact that the objects you provide as a key are comparable, which is not the case with C strings (what will be compared is the pointer address, not the contents). Thus this will simply not work. The other reply you got was simply to say that you can get over it by providing your own structure which overrides the == operator.
But, it is making your life complicated. If I were you, I would simply use the std::string (in non unicode build) or std::wstring (in UNICODE build) for that. If you have to support both builds, then simply define your own type:
#if defined _UNICODE || defined UNICODE
typedef std::wstring TMyString;
#else
typedef std::string TMyString;
#endif
|
|
|
|
|