|
hello everyone, i made a tic tac toe in C language, using the minimax algorithm but i have some problem when i try to play with larger matrix more than 3x3 for example 4x4 and my program go out of memory.
So i search in the net and i discover the aplpha-beta pruning that reduce the computional coast and so i can play with 4x4 matrix whiteout problems but i have not idea to implement this algorithm to my minimax.
if there is a person who want to help me i would be grateful.
sorry for my bad english but i am italian
i can post my code if you want
|
|
|
|
|
You could start (if you haven't already done) with the Wikipedia's article on the subject: Alpha–beta pruning[^].
|
|
|
|
|
 hi, i read the article on wikipedia and i understand it but the problem is that i don't know how to implement it to my minimax
here is my minimax if want to give me an idea ..
int miniMax(char wnr, int deep){
if (controlloVincita(2)) return INT_MAX;
if (endgame())
return INT_MIN;
int i, j, res, tmp;
if (wnr == 1) {
res = 1;
for (i = 0;i<M;i++)
for (j = 0;j<N;j++)
{
if (BOARD[i][j]==0)
{
BOARD[i][j] = 1;
if (controlloVincita(1))
if (deep == 20)
{
BOARD[i][j] = 0;
return INT_MIN;
}
else
res -= 2;
else if ((tmp = miniMax(2, deep - 1))<res || (tmp == INT_MIN))
res = tmp;
BOARD[i][j] = 0;
}
}
}
else
{
res = -1;
for (i = 0;i<M;i++)
for (j = 0;j<N;j++)
{
if (BOARD[i][j]==0)
{
BOARD[i][j] = 2;
if (controlloVincita(2))
res += 2;
else if ((tmp = miniMax(1, deep - 1))>res || (tmp == INT_MAX))
res = tmp;
BOARD[i][j] = 0;
}
}
}
return res;
}
int get_next_move(unsigned int *i, unsigned int *j)
{
int max = INT_MIN, mi = 1, mj = 1, t;
int alfa = INT_MIN, beta = INT_MAX;
for (*i = 0;*i<M;(*i)++)
for (*j = 0;*j<N;(*j)++)
if (BOARD[*i][*j]==0) {
BOARD[*i][*j] = 2; t = miniMax(1, 20); if (t>max) {
max = t;
mi = *i;
mj = *j;
}
BOARD[*i][*j] = 0;
}
BOARD[mi][mj] = 2; *i = mi;
*j = mj;
return 1;
}
|
|
|
|
|
Hi all,
I need to define a structure (its some spec frame-structure), so that I can assign data and retrieve-fields easily.
So my definition is like below
struct MyDef
{
DWORD B1 : 8;
DWORD B2 : 8;
DWORD B3 : 8;
DWORD B4 : 8;
};
(Note that, the whole struct contains 32 bits.)
Now, I need a way to set some DWORD value to it directly. something like this
MyDef Def;
Def.data = 0xAABBCCDD;
(please note that, I need to keep the bit-fields so that I can set the fields as well.)
Please help me.
|
|
|
|
|
Without actually trying this out.... you should be able to take the pointer to the first element and load it with whatever data you want. Assuming your data matches the size of the structure, there should be no issue, be ware of data packing/memory alignment though.
Read this before you get yourself into trouble.[^]
|
|
|
|
|
You can do it by using a union[^] thus:
union MyDef
{
DWORD W1;
struct bits
{
DWORD B1 : 8;
DWORD B2 : 8;
DWORD B3 : 8;
DWORD B4 : 8;
};
};
which maps W1 to the same memory space as the four byte fields. You can now refer to the whole DWORD as W1 of the union structure.
|
|
|
|
|
how to parse html code in c language.
|
|
|
|
|
|
What books would you recommend for data structures and algorithms in C language?
It would be good if recommended books are books of solved problems.
|
|
|
|
|
|
This is a classic:
Algorithms in C[^]
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I believe this is a later edition of the book I initially learned C with.... if that is the case, it's a pretty good book that emphasizes proper algorithms, which is great.
|
|
|
|
|
There is a windows message to know when windows scheme has changed from Windows 7 to Windows classic ? If yes, which is ? I have tried ON_WM_SETTINGCHANGE , but this one has fired several times, not just once ...
modified 5-Oct-15 8:56am.
|
|
|
|
|
I dig into ON_WM_SETTINGCHANGE message, but I didn't found something that tell me when the color scheme has changed ... I am thinking that this message is not the one of what I am looking for ...
|
|
|
|
|
|
|
After few trials, I ended using this code, which seem to work:
void CMainFrame::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
CFrameWnd::OnSettingChange(uFlags, lpszSection);
if(SPI_SETWORKAREA == uFlags)
}
In case that anyone need it ...
|
|
|
|
|
> I am uploading adobe files in local host it is uploading file successfully but it is showing corrupt or damage file after opening file please help me out to sort out this problem i will be thankful to you below is my entire code
int main()
{
static char *filename = "tutorial.pdf"; //Filename to be loaded
static char *filepath = "C:\\tutorial.pdf"; //Filename to be loaded
`static char *type = "text/pdf";`
static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";
static char boundary[] = "-----------------------------7d82751e2bc0858"; //Header boundary
static char nameForm[] = "uploadedfile"; //Input form name
static char iaddr[] = "localhost"; //IP address
static char url[] = "/xampp/testing/upload.php?folder=aaaa&&foldername=bbbb";
char * buffer; //Buffer containing file + headers
char * content; //Buffer containing file
FILE * pFile; //File pointer
long lSize; //File size
size_t result;
char *pos; // used in the loop
// Open file
pFile = fopen(filepath, "rb");
if (pFile == NULL)
{
printf("ERROR_OPEN_FILE");
getchar();
return ERROR_OPEN_FILE;
}
printf("OPEN_FILE\n");
// obtain file size:
fseek(pFile, 0, SEEK_END);
lSize = ftell(pFile);
rewind(pFile);
// allocate memory to contain the whole file:
content = (char*)malloc(sizeof(char)*lSize);
if (content == NULL)
{
printf("ERROR_MEMORY");
getchar();
return ERROR_OPEN_FILE;
}
printf("MEMORY_ALLOCATED\t \"%d\" \n", lSize);
// copy the file into the buffer:
result = fread(content, 1, lSize, pFile);
rewind (pFile);
if (result != lSize)
{
printf("ERROR_SIZE");
getchar();
return ERROR_OPEN_FILE;
}
printf("SIZE_OK\n");
// terminate
fclose(pFile);
printf("FILE_CLOSE\n");
//allocate memory to contain the whole file + HEADER
buffer = (char*)malloc(sizeof(char)*lSize + 2048);
//print header
sprintf(buffer, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", boundary, nameForm, filename);
sprintf(buffer, "%sContent-Type: %s\r\n", buffer, type);
sprintf(buffer, "%sContent-Length: %d\r\n", buffer, lSize);
sprintf(buffer, "%s\r\n", buffer);
memcpy(buffer + strlen(buffer),content,lSize);
sprintf(buffer, "%s\r\n", buffer);
sprintf(buffer, "%s%s--\r\n", buffer, boundary);
//Open internet connection
HINTERNET hSession = InternetOpen("WINDOWS", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (hSession == NULL)
{
printf("ERROR_INTERNET_OPEN");
getchar();
return ERROR_OPEN_FILE;
}
printf("INTERNET_OPENED\n");
HINTERNET hConnect = InternetConnect(hSession, iaddr, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if (hConnect == NULL)
{
printf("ERROR_INTERNET_CONN");
getchar();
return ERROR_INTERNET_CONN;
}
printf("INTERNET_CONNECTED\n");
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST", _T(url), NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 1);
if (hRequest == NULL)
{
printf("ERROR_INTERNET_REQ");
getchar();
}
printf("INTERNET_REQ_OPEN\n");
BOOL sent = HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
if (!sent)
{
printf("ERROR_INTERNET_SEND");
getchar();
return ERROR_INTERNET_CONN;
}
printf("INTERNET_SEND_OK\n");
printf("\r\n%s\r\n",buffer);
//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
getch();
return 0;
}
|
|
|
|
|
You should not pass the buffer itself as format argument to sprintf . It depends on the implementation (the used standard C library) if it is supported or not. But even if it is supported it is bad style. And in your case it is the reason for the corrupted buffer content here:
sprintf(buffer, "%s\r\n", buffer);
memcpy(buffer + strlen(buffer),content,lSize);
sprintf(buffer, "%s\r\n", buffer);
The PDF file is a binary file. While you use memcpy to append the binary data to your buffer, the next sprintf call stops at the first null byte truncating the buffer.
A common solution is to use a buffer offset variable which is incremented by the return value of sprintf :
int ofs = strlen(buffer);
ofs += sprintf(buffer + ofs, "\r\n");
memcpy(buffer + ofs,content,lSize);
ofs += lSize;
ofs += sprintf(buffer + ofs, "\r\n");
|
|
|
|
|
I derived a class from CFileDialog , named CFileDialogExt . Here, I have the following filters:
1.bmp
2.gif
3.jpg
4.png
5.tiff
In order to setup the right extension for file name, I override OnTypeChange, just like this:
void CFileDialogExt::OnTypeChange()
{
switch(GetOFN().nFilterIndex)
{
case 1:
SetDefExt(_T("bmp"));
break;
case 2:
SetDefExt(_T("gif"));
break;
case 3:
SetDefExt(_T("jpg"));
break;
case 4:
SetDefExt(_T("png"));
break;
case 5:
SetDefExt(_T("tiff"));
break;
}
CFileDialog::OnTypeChange();
}
Default type is bmp.
So, I type "aaa" as file name.
Then, I change the type (from combo type), as gif. The file name is still "aaa".
Then, I change again as jpg, the file name is changed as "aaa.gif" !! Is delayed by one changing ... strange ... why ?
Furthermore, I change extension as tiff, the file name is "aaa.jpg", not "aaa.tiff" .... I have done something wrong here ?
|
|
|
|
|
Maybe if I could get a pointer to CComboBox from CFileDialog , I can find more about this problem ... but I can not ... I tried this:
void CFileDialogExt::OnTypeChange()
{
CString sTemp;
CComboBox* pCombo = (CComboBox*)GetDlgItem(cmb1);
if(NULL != pCombo->GetSafeHwnd())
{
int nIndex = pCombo->GetCurSel();
if(CB_ERR != nIndex)
pCombo->GetLBText(nIndex, sTemp);
}
TRACE(">>>%s\n", sTemp);
switch(GetOFN().nFilterIndex)
{
case 1:
SetDefExt(_T("bmp"));
break;
case 2:
SetDefExt(_T("gif"));
break;
case 3:
SetDefExt(_T("jpg"));
break;
case 4:
SetDefExt(_T("png"));
break;
case 5:
SetDefExt(_T("tiff"));
break;
case 6:
SetDefExt(_T("dcm"));
break;
}
CFileDialog::OnTypeChange();
}
but pCombo seem to be null ...
|
|
|
|
|
We do something similar.
In the OnTypeChange virtual method:
We have a external list of extension (external to the file dialog)
We get the selected filter extension (m_ofn.nFilterIndex).
We get the current filename (GetFileName())
Extract the filename without extension and replace with the new extension
and call this :
SetControlText(edt1, fileNameNoExtension );
This will change the name of the file in the file dialog editbox.
Good luck.
I'd rather be phishing!
|
|
|
|
|
Yes, there is a solution, but the file dialog has an old style ...
|
|
|
|
|
Farmers want to be able to project business investment needs over a given period of time (in months). During
the program development, you consult a Rabbit specialist who gives you certain facts about rabbits as stated
below.
i) A female rabbit is called doe, a male rabbit is a buck and baby rabbits are kittens.
ii) Female rabbits take two months to mature and start mating. Once they start mating, they produce every
month. On average, a single rabbit produces 8 kittens every time they produce.
Assuming that the framers will only invest in female rabbits and that male rabbits will always be hired during matting
period and their number is negligible. Assume that no rabbit dies on the farm and that they will all grow at the same
rate.
Write a c program that prompts the farmer to input the initial number of rabbits on the farm and the number of
months the farmer wants to sustain the farm. The program should compute and print out the expected number of
rabbits on the farm for each month until the last month.
|
|
|
|
|
 Ive been able to write this but i feel it can be sized down and made better your direct ideas will be of much help as I am only abt 2weeks in c programming but hv been working very hard to learn
thks take a look
#include<stdio.h>
int main()
{
int initialrabbits,no_months,Kittens[10],Rabbits,count=1,kits,totalkittens;
printf("\nEnter initial no of rabbits:\a ");
scanf("%d",&initialrabbits);
printf("\nEnter number of months to be projected:\a ");
scanf("%d",&no_months);
for(count=1;count<=no_months;count++)
{
if (count==1){
Kittens[0]=initialrabbits*8;
Rabbits=initialrabbits;
kits=Kittens[0];
totalkittens=Kittens[0];}
else if (count==2){
Kittens[1]=initialrabbits*8;
Rabbits=initialrabbits;
kits=Kittens[1];
totalkittens=Kittens[0]+Kittens[1];}
else if (count==3){
Kittens[2]=(Kittens[0]+initialrabbits)*8;
Rabbits=Kittens[0]+initialrabbits;
kits=Kittens[2];
totalkittens=Kittens[1]+Kittens[2];}
else if (count==4){
Kittens[3]=(Kittens[0]+Kittens[1]+initialrabbits)*8;
Rabbits=Kittens[0]+Kittens[1]+initialrabbits;
kits=Kittens[3];
totalkittens=Kittens[2]+Kittens[3];}
else if (count==5){
Kittens[4]=(Kittens[0]+Kittens[1]+Kittens[2]+initialrabbits)*8;
Rabbits=Kittens[0]+Kittens[1]+Kittens[2]+initialrabbits;
kits=Kittens[4];
totalkittens=Kittens[3]+Kittens[4];}
else if (count==6){
Kittens[5]=(Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+initialrabbits)*8;
Rabbits=Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+initialrabbits;
kits=Kittens[5];
totalkittens=Kittens[4]+Kittens[5];}
else if (count==7){
Kittens[6]=(Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+Kittens[4]+initialrabbits)*8;
Rabbits=Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+Kittens[4]+initialrabbits;
kits=Kittens[6];
totalkittens=Kittens[5]+Kittens[6];}
else if (count==8){
Kittens[7]=(Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+Kittens[4]+Kittens[5]+initialrabbits)*8;
Rabbits=Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+Kittens[4]+Kittens[5]+initialrabbits;
kits=Kittens[7];
totalkittens=Kittens[6]+Kittens[7];}
else if (count==9){
Kittens[8]=(Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+Kittens[4]+Kittens[5]+Kittens[6]+initialrabbits)*8;
Rabbits=Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+Kittens[4]+Kittens[5]+Kittens[6]+initialrabbits;
kits=Kittens[8];
totalkittens=Kittens[7]+Kittens[8];}
else if (count==10){
Kittens[9]=(Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+Kittens[4]+Kittens[5]+Kittens[6]+Kittens[7]+initialrabbits)*8;
Rabbits=Kittens[0]+Kittens[1]+Kittens[2]+Kittens[3]+Kittens[4]+Kittens[5]+Kittens[6]+Kittens[7]+initialrabbits;
kits=Kittens[9];
totalkittens=Kittens[8]+Kittens[9];}
printf("\n\tMONTH %d",count);
printf("\nNUMBER OF RABBITS: %d",Rabbits);
printf("\nKittens produced this month: %d",kits);
printf("\nTotal kittens now: %d\n",totalkittens);
}
return 0;
}
|
|
|
|
|