![]() |
| | #106 |
| Registered User Join Date: Dec 2006 Location: Scranton, Pa
Posts: 221
| |
| Oldman47 is offline | |
| | #107 |
| Registered User Join Date: Feb 2008 Location: Lehi, UT
Posts: 179
| Ya know, this is a good thread, but a little to slow moving. I mean, 3 years, still sticked, and only 8 pages? I'm gonna throw one out here: Code: Operating system: Any Compiler: MinGW, but it should work for any Graphics Library: stdio.h Other Libraries: Description: Reflex
__________________ Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week! |
| guesst is offline | |
| | #108 |
| Registered User Join Date: Sep 2009
Posts: 2
| Knights tour C Source code for knights tour Code: /****Knight's Tour*******/
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
typedef struct
{
int x,y,s2;
}chessboard;
typedef struct
{
int u,v,s1;
}hamiltonian;
int b,p,q,w=5;
int j=0;
chessboard chess[8][8];
hamiltonian path[64];
void search();
void push();
void pop();
void pawn(int,int,int);
void knight(int,int,int);
void main()
{
int i,j,l,m;
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
FILE *fp;
clrscr();
/* initialize graphics, local variables*/
initgraph(&gdriver, &gmode, "f:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)
/* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
/* terminate with an error code */
}
printf("\n\n\n\n\n\n\n\n\n\n\n\tPROGRAM TO CHECK WHETHER A KNIGHT CAN REACH A PAWN IN A CHESSBOARD");
printf("\n\n\n\t\t THE CO-ORDINATES MUST BE GIVEN FROM 1-8");
printf("\n\n\nEnter the position of the Pawn: ");
scanf("%d,%d",&a,&b);
printf("\n\nEnter the position of the Knight: ");
scanf("%d,%d",&p,&q);
cleardevice();
//chessboard
setcolor(3);
for(i=80;i<=560;i+=60)
{
line(i,0,i,480);
line(i+1,0,i+1,480);
}
for(i=0;i<480;i+=60)
{
line(80,i,560,i);
line(80,i+1,560,i+1);
}
line(80,478,560,478);
line(80,479,560,479);
setfillstyle(1,15);
for(i=0;i<=7;i++)
for(j=0;j<=7;j++)
chess[i][j].s2=0;
for(i=0;i<=63;i++)
path[i].s1=-1;
for(i=20;i<=440;i+=120)
for(j=100;j<=520;j+=120)
floodfill(j,i,3);
for(i=80;i<=440;i+=120)
for(j=160;j<=520;j+=120)
floodfill(j,i,3);
for(i=0,l=30;i<=7,l<=450;i++,l+=60)
for(j=0,m=110;j<=7,m<=530;j++,m+=60)
{
chess[i][j].x=m;
chess[i][j].y=l;
}
pawn(chess[b][a].x,chess[b][a].y,RED);
setcolor(1);
setfillstyle(1,1);
/*settextstyle(10,1,2);
setcolor(MAGENTA);
outtextxy(10,20,"Magenta = Start = Knight");
settextstyle(10,1,2);
setcolor(RED);
outtextxy(575,5,"Red = Destination = Pawn ");
fclose(fp);*/
path[0].u=chess[q][p].x;
path[0].v=chess[q][p].y;
chess[q][p].s2=1;
path[j].s1=1;
knight(chess[q][p].x,chess[q][p].y,MAGENTA);
getch();
search();
getch();
cleardevice();
printf("\t\t\tThe Knight reached the pawn");
getch();
closegraph();
}
void search()
{
int r,s,c=0,flag;
char ch;
if(path[j].u==chess[b][a].x&&path[j].v==chess[b][a].y)
{
knight(path[j].u,path[j].v+10,MAGENTA);
return;
}
while(1)
{
r=random(8);
s=random(8);
if(path[j].u+120==chess[r][s].x&&path[j].v+60==chess[r][s].y)
{
if(path[j].s1==chess[r][s].s2)
{
c++;
continue;
}
flag=1;
knight(path[j].u+120,path[j].v+70,GREEN);
break;
}
else
if(path[j].u+120==chess[r][s].x&&path[j].v-60==chess[r][s].y)
{
if(path[j].s1==chess[r][s].s2)
{
c++;
if(c==8)
break;
continue;
}
flag=1;
knight(path[j].u+120,path[j].v-50,GREEN);
break;
}
else
if(path[j].u-120==chess[r][s].x&&path[j].v+60==chess[r][s].y)
{
if(path[j].s1==chess[r][s].s2)
{
c++;
if(c==8)
break;
continue;
}
flag=1;
knight(path[j].u-120,path[j].v+70,GREEN);
break;
}
else
if(path[j].u-120==chess[r][s].x&&path[j].v-60==chess[r][s].y)
{
if(path[j].s1==chess[r][s].s2)
{
c++;
if(c==8)
break;
continue;
}
flag=1;
knight(path[j].u-120,path[j].v-50,GREEN);
break;
}
else
if(path[j].u+60==chess[r][s].x&&path[j].v+120==chess[r][s].y)
{
if(path[j].s1==chess[r][s].s2)
{
c++;
if(c==8)
break;
continue;
}
flag=1;
knight(path[j].u+60,path[j].v+130,GREEN);
break;
}
else
if(path[j].u+60==chess[r][s].x&&path[j].v-120==chess[r][s].y)
{
if(path[j].s1==chess[r][s].s2)
{
c++;
if(c==8)
break;
continue;
}
flag=1;
knight(path[j].u+60,path[j].v-110,GREEN);
break;
}
else
if(path[j].u-60==chess[r][s].x&&path[j].v-120==chess[r][s].y)
{
if(path[j].s1==chess[r][s].s2)
{
c++;
if(c==8)
break;
continue;
}
flag=1;
knight(path[j].u-60,path[j].v-110,GREEN);
break;
}
else
continue;
}
/*ch=getch();
if(ch==' ')
{
j--;
search();
}*/
if(flag==1)
{
push();
path[j].u=chess[r][s].x;
path[j].v=chess[r][s].y;
chess[r][s].s2=1;
path[j].s1=1;
getch();
search();
}
}
void pawn(int r, int s,int c)
{
s=s+4;
setcolor(c);
setfillstyle(6,c);
circle(r,s-10,5);
floodfill(r,s-7,c);
line(r,s-10,r-11,s+15);
line(r,s-10,r+11,s+15);
rectangle(r-13,s+15,r+13,s+18);
floodfill(r,s+10,c);
floodfill(r,s+17,c);
}
void knight(int r,int s,int c)
{
setcolor(c);
setfillstyle(6,c);
arc(r,s-10,0,180,14);
line(r-14,s-10,r,s-10);
line(r-3,s-10,r-14,s+10);
line(r+14,s-10,r+14,s+10);
rectangle(r-17,s+10,r+17,s+13);
floodfill(r,s-8,c);
floodfill(r,s+12,c);
}
void push()
{
j++;
}
void pop()
{
j--;
}
|
| anakin is offline | |
| | #109 |
| Registered User Join Date: Sep 2009
Posts: 2
| Anothe code for knights tour Code: #include <stdio.h>
#include <conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdlib.h>
typedef struct
{
int n,color;
}sq;
struct link
{
int x,y;
struct link *next;
};
typedef struct link l;
l *h=NULL,*e=NULL,*p=NULL;
void inilink();
void graph();
void chessboard(sq [8][8]);
void iniboard(sq [8][8]);
void drawpawn(int,int);
void drawknight(int,int);
void rec(l*,int,int);
char name[][2]={"A","B","C","D","E","F","G","H","0","1","2","3","4","5","6","7"};
int flag=0;
void main()
{
int i,j,i1,i2,j1,j2;
sq c[8][8];
clrscr();
printf("Enter the coordinates of pawn");
scanf("%d,%d",&i1,&j1);
printf("Enter the coordinates of the knight");
scanf("%d,%d",&i2,&j2);
if(i1==i2&&j1==j2)
exit(1);
for(i=0;i<8;i++)
for(j=0;j<8;j++)
c[8][8].n=0;
c[i1][j1].n=2;
c[i2][j2].n=1;
inilink();
e->next=h;
graph();
iniboard(c);
chessboard(c);
setcolor(BLUE);
setfillstyle(1,BLUE);
drawpawn(i1,j1);
setfillstyle(1,RED);
drawknight(i2,j2);
setfillstyle(1,GREEN);
for(p=h;p->next!=h;p=p->next)
if(p->x==i2&&p->y==j2)
break;
getch();
rec(p,i1,j1);
drawknight(i1,j1);
getch();
closegraph();
}
void rec(l *p,int i2,int j2)
{
if((p->x==i2&&p->y==j2)||(flag==1))
return;
drawknight(p->next->x,p->next->y);
delay(1000);
rec(p->next,i2,j2);
}
void inilink()
{
FILE *fp;
l *n;
fp=fopen("N:\\K.txt","r");
while(!feof(fp))
{
n=(l*)malloc(sizeof(l));
fscanf(fp,"%d %d",&n->x,&n->y);
n->next=NULL;
if(h==NULL)
h=e=n;
else
{
n->next=h;
h=n;
}
}
fclose(fp);
}
void iniboard(sq c[8][8])
{
int i,j,k;
for(i=0;i<8;i++)
for(j=0;j<8;j++)
if(i%2==1)
{
c[i][j++].color=BROWN;
c[i][j].color=LIGHTRED;
}
else
{
c[i][j++].color=LIGHTRED;
c[i][j].color=BROWN;
}
}
void chessboard(sq c[8][8])
{
int i,j,k,l;
cleardevice();
setbkcolor(GREEN);
//to make the board
setlinestyle(SOLID_LINE,1,2);
setcolor(BLUE);
for(i=60;i<=540;i+=60)
line(i,20,i,460);
for(i=20;i<=460;i+=55)
line(60,i,540,i);
for(i=60,k=0;i<540;i+=60,k++)
for(j=20,l=0;j<468;j+=56,l++)
{
setfillstyle(1,c[k][l].color);
floodfill(i+2,j+2,BLUE);
}
//to draw the border
line(40,1,40,479);
line(560,1,560,479);
line(40,1,560,1);
line(40,479,560,479);
line(40,1,60,20);
line(560,479,540,460);
line(40,479,60,460);
line(560,1,540,20);
setfillstyle(1,YELLOW);
floodfill(45,2,BLUE);
floodfill(45,25,BLUE);
floodfill(45,478,BLUE);
floodfill(545,455,BLUE);
//to label the board
settextstyle(2,0,4);
setcolor(RED);
for(i=50;i<=545;i+=495)
for(j=43,k=0;j<468;j+=56,k++)
outtextxy(i,j,name[k]);
for(j=5;j<=468;j+=460)
for(i=90,k=8;i<=550;i+=60,k++)
outtextxy(i,j,name[k]);
}
void graph(void)
{
// request auto detection
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "e:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurRED */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
/* terminate with an error code */
}
}
void drawpawn(int i,int j)
{
int pawn[]={25,20,25,22,28,22,28,35,20,35,20,40,15,40,15,45,45,45,45,40,40,40,40,35,32,35,32,22,35,22,35,20,25,20};
int k,fac1=(j+1)*60,fac2=i*55+25;
for(k=0;k<34;k+=2)
{
pawn[k]+=fac1;
pawn[k+1]+=fac2;
}
circle(30+fac1,15+fac2,5);
floodfill(31+fac1,16+fac2,BLUE);
fillpoly(17,pawn);
}
void drawknight(int i,int j)
{
int knight[]={20,55,20,60,15,60,15,65,45,65,45,60,40,60,40,55};
int k,fac1=(j+1)*60,fac2=i*55+5;
for(k=0;k<16;k+=2)
{
knight[k]+=fac1;
knight[k+1]+=fac2;
}
drawpoly(8,knight);
if(j<4)
{
arc(33+fac1,40+fac2,105,230,20);
line(30+fac1,17+fac2,33+fac1,22+fac2);
line(30+fac1,17+fac2,27+fac1,22+fac2);
line(33+fac1,22+fac2,48+fac1,30+fac2);
line(48+fac1,30+fac2,48+fac1,35+fac2);
line(48+fac1,35+fac2,42+fac1,35+fac2);
arc(7+fac1,45+fac2,343,17,35);
circle(35+fac1,30+fac2,1);
floodfill(45+fac1,34+fac2,BLUE);
circle(35+fac1,30+fac2,3);
}
else
{
arc(25+fac1,42+fac2,320,70,20);
line(30+fac1,18+fac2,33+fac1,23+fac2);
line(30+fac1,18+fac2,27+fac1,23+fac2);
line(27+fac1,23+fac2,12+fac1,31+fac2);
line(12+fac1,31+fac2,12+fac1,36+fac2);
line(12+fac1,36+fac2,18+fac1,36+fac2);
arc(52+fac1,45+fac2,165,199,35);
circle(25+fac1,31+fac2,1);
floodfill(13+fac1,31+fac2,BLUE);
circle(25+fac1,31+fac2,3);
}
}
|
| anakin is offline | |
| | #110 |
| Registered User Join Date: Feb 2009
Posts: 22
| Game Title: Dwarf Caretaker Version: 0.5 Operating System: XP Compiler: Code::Blocks w/ Mingw Graphics Library: Console Description: A sort of sim game (very) loosely based on the world sim game, Dwarf Fortress. This is my first real project that isn't out of a book or tutorial, so despite how simple (and boring...) it currently is, I'm rather proud of it. The code is probably horribly written, and once I've gotten a few other things cleaned up I may rewrite it completely to make use of some of the things I've learned along the way. Download: v0.6 - Includes source files, README Guide and compiled EXE Comments, critisism, advice, bug reports, etc. should be sent to timmysassone@yahoo.com, thank you! I'm apparently not allowed to attach .zip files to my posts, so the source code is attached as separate files, and the pre-compiled version with the guide is avaliable via the zip file above. I know I'm not supposed to use outside links, but since I can't upload exe, zip or rtf files I don't see any other way to make them available. There is a discussion topic for my game here. [EDIT]: I almost forgot, there are quite a few Dwarf Fortress reference in the game that may not make any sense to someone who hasn't played it. My appologies, but it was originally intended for the DF community, and besides, if you haven't played Dwarf Fortress at least once you should go play it now anyways. v0.4 Update:
v0.5 Changes:
v0.6 Update:
Last edited by timmeh; 09-07-2009 at 08:29 PM. |
| timmeh is offline | |
| | #111 |
| Registered User Join Date: Feb 2009
Posts: 22
| Dwarf Caretaker v1.3 Game Title: Dwarf Caretaker Version: 1.3 Operating System: Built for Windows XP, although it may work on others with minimal changes. Compiler: Code::Blocks w/ Mingw Graphics Library: PDCurses Description: I recently finished version 1.3 of my "Dwarf Caretaker" game. I know I'm supposed to edit my old post, but for whatever reason vBulletin won't let me... As it is, unless something changes to make updating the post here easier, I think I'll just stick to updating it's topic on the DF forums, the google code page, and the blog. As a side effect, while the external links I'll post in addition to the source code I'll upload directly will always be current, the source code here may or may not be, depending on my ability to change it later without having to make a new post.... If any of the mods get's a chance, I'd really appreciate it if they could either merge this post with my old one, or delete the old one. [EDIT]: Now that I think about it, the source code here is actually slightly different from the v1.3 code, as it's part way through the next update... all I've done is a quick bug-fix (gotta stop dividing by zero...) though, so it should be fine.
__________________ My Dev Blog - The most up-to-date info on my current projects. Last edited by timmeh; 09-22-2009 at 03:45 PM. Reason: Forgot something... |
| timmeh is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Resources for professional games | EVOEx | Game Programming | 8 | 06-17-2009 02:42 PM |
| need help with HTTP POST | Anddos | Networking/Device Communication | 5 | 03-22-2009 08:41 AM |
| Unknown memory leak with linked lists... | RaDeuX | C Programming | 6 | 12-07-2008 04:09 AM |
| Auto POST | vasanth | A Brief History of Cprogramming.com | 10 | 06-07-2003 10:42 AM |