Skip to content

Instantly share code, notes, and snippets.

@andrewrcollins
Created January 6, 2012 02:40
Show Gist options
  • Save andrewrcollins/1568684 to your computer and use it in GitHub Desktop.
Save andrewrcollins/1568684 to your computer and use it in GitHub Desktop.
#TJHSST ~ Polyiminoes: Pentominoes, Hexominoes, etc.
/*
Polyiminoes :
Pentominoes,Hexominoes, etc.
*/
#include <stdio.h>
#include "conio.h"
#include "stdlib.h"
#define OMINOS 8
/* To check diagonal connections define DIAGONALS, otherwise for
just four direction connections define NORMAL.
*/
#define DIAGONALS
/*
#define NORMAL
*/
main()
{
int grid[OMINOS][OMINOS],oldgrid[100][OMINOS][OMINOS];
int cntx,cnty,oldcnt=0,cnt;
while(!kbhit()&&oldcnt<20) {
cleargrid(grid);
fillgrid(grid);
if(checkgrid(grid,-1,-1)==OMINOS) {
movegrid(grid);
for(cnt=0;cnt<oldcnt&&!compgrid(grid,oldgrid[cnt]);cnt++);
if(cnt==oldcnt) {
copygrid(oldgrid[oldcnt],grid);
showgrid(grid);
oldcnt++;
}
}
}
for(cnt=0;cnt<oldcnt;cnt++)
showgrid(oldgrid[cnt]);
}
int cleargrid(grid)
int grid[OMINOS][OMINOS];
{
int cntx,cnty;
for(cnty=0;cnty<OMINOS;cnty++)
for(cntx=0;cntx<OMINOS;cntx++)
grid[cnty][cntx]=0;
}
int fillgrid(grid)
int grid[OMINOS][OMINOS];
{
int cnt,randx,randy;
for(cnt=0;cnt<OMINOS;cnt++) {
do {
randx=random(OMINOS);
randy=random(OMINOS);
} while(grid[randy][randx]);
grid[randy][randx]=1;
}
}
int checkgrid(grid,gridx,gridy)
int grid[OMINOS][OMINOS],gridx,gridy;
{
int cntx,cnty,startx,starty,okay;
if(gridx==(-1)||gridy==(-1)) {
okay=0;
for(cnty=0;cnty<OMINOS&&!okay;cnty++)
for(cntx=0;cntx<OMINOS&&!okay;cntx++)
if(grid[cnty][cntx]==1) {
startx=cntx;
starty=cnty;
okay=1;
}
}
else {
startx=gridx;
starty=gridy;
}
okay=1;
grid[starty][startx]=2;
for(cnty=(-1);cnty<2;cnty++)
#ifdef NORMAL
for(cntx=(-1);cntx<2&&abs(cntx+cnty)==1;cntx++)
#else
for(cntx=(-1);cntx<2;cntx++)
#endif
if((cnty+starty)>=0&&(cnty+starty)<OMINOS&&
(cntx+startx)>=0&&(cntx+startx)<OMINOS) {
if(grid[cnty+starty][cntx+startx]==1)
okay=okay+checkgrid(grid,startx+cntx,starty+cnty);
}
return okay;
}
int showgrid(grid)
int grid[OMINOS][OMINOS];
{
int cntx,cnty;
for(cnty=0;cnty<OMINOS;cnty++) {
for(cntx=0;cntx<OMINOS;cntx++)
putchar((grid[cnty][cntx])?'#':'.');
puts("");
}
puts("");
}
int movegrid(grid)
int grid[OMINOS][OMINOS];
{
int newgrid[OMINOS][OMINOS];
int cntx,cnty,movex,movey,canmove;
canmove=1;
for(cnty=0;cnty<OMINOS&&canmove;cnty++)
for(cntx=0;cntx<OMINOS&&canmove;cntx++)
canmove=canmove&&!grid[cnty][cntx];
movey=cnty-1;
canmove=1;
for(cntx=0;cntx<OMINOS&&canmove;cntx++)
for(cnty=0;cnty<OMINOS&&canmove;cnty++)
canmove=canmove&&!grid[cnty][cntx];
movex=cntx-1;
if(movex||movey) {
cleargrid(newgrid);
for(cnty=movey;cnty<OMINOS;cnty++)
for(cntx=movex;cntx<OMINOS;cntx++)
newgrid[cnty-movey][cntx-movex]=grid[cnty][cntx];
for(cnty=0;cnty<OMINOS;cnty++)
for(cntx=0;cntx<OMINOS;cntx++)
grid[cnty][cntx]=newgrid[cnty][cntx];
}
}
int compgrid(grid1,grid2)
int grid1[OMINOS][OMINOS],grid2[OMINOS][OMINOS];
{
int cntx,cnty,same;
same=1;
for(cnty=0;cnty<OMINOS&&same;cnty++)
for(cntx=0;cntx<OMINOS&&same;cntx++)
same=grid1[cnty][cntx]==grid2[cnty][cntx];
return same;
}
int copygrid(grid1,grid2)
int grid1[OMINOS][OMINOS],grid2[OMINOS][OMINOS];
{
int cntx,cnty;
for(cnty=0;cnty<OMINOS;cnty++)
for(cntx=0;cntx<OMINOS;cntx++)
grid1[cnty][cntx]=grid2[cnty][cntx];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment