33,010
社区成员
发帖
与我相关
我的任务
分享
void InitGame()
{
State *B1_S1=(State*)malloc(sizeof(State));
State *B2_S1=(State*)malloc(sizeof(State)),*B2_S2=(State*)malloc(sizeof(State));
State *B3_S1=(State*)malloc(sizeof(State)),*B3_S2=(State*)malloc(sizeof(State)),*B3_S3=(State*)malloc(sizeof(State)),*B3_S4=(State*)malloc(sizeof(State));
State *B4_S1=(State*)malloc(sizeof(State)),*B4_S2=(State*)malloc(sizeof(State)),*B4_S3=(State*)malloc(sizeof(State)),*B4_S4=(State*)malloc(sizeof(State));
State *B5_S1=(State*)malloc(sizeof(State)),*B5_S2=(State*)malloc(sizeof(State)),*B5_S3=(State*)malloc(sizeof(State)),*B5_S4=(State*)malloc(sizeof(State));
State *B6_S1=(State*)malloc(sizeof(State)),*B6_S2=(State*)malloc(sizeof(State));
State *B7_S1=(State*)malloc(sizeof(State)),*B7_S2=(State*)malloc(sizeof(State));
Block[1]=B1_S1; B1_S1->next=NULL;
Block[2]=B2_S1; B2_S1->next=B2_S2; B2_S2->next=NULL;
Block[3]=B3_S1; B3_S1->next=B3_S2; B3_S2->next=B3_S3; B3_S3->next=B3_S4; B3_S4->next=NULL;
Block[4]=B4_S1; B4_S1->next=B4_S2; B4_S2->next=B4_S3; B4_S3->next=B4_S4; B4_S4->next=NULL;
Block[5]=B5_S1; B5_S1->next=B5_S2; B5_S2->next=B5_S3; B5_S3->next=B5_S4; B5_S4->next=NULL;
Block[6]=B6_S1; B6_S1->next=B6_S2; B6_S2->next=NULL;
Block[7]=B7_S1; B7_S1->next=B7_S2; B7_S2->next=NULL;
//xx
//xx
int offsetB1S1[4][2]={{0,0},{1,0},{0,1},{1,1}}; InitBlock(B1_S1,offsetB1S1,0,0);
//xxxx
int offsetB2S1[4][2]={{0,0},{1,0},{2,0},{3,0}}; InitBlock(B2_S1,offsetB2S1,0,0);
//x
//x
//x
//x
int offsetB2S2[4][2]={{0,0},{0,1},{0,2},{0,3}}; InitBlock(B2_S2,offsetB2S2,1,0);
// x
//xxx
int offsetB3S1[4][2]={{0,0},{1,0},{1,1},{2,0}}; InitBlock(B3_S1,offsetB3S1,0,0);
//xxx
// x
int offsetB3S2[4][2]={{0,0},{-1,1},{0,1},{1,1}}; InitBlock(B3_S2,offsetB3S2,2,-1);
//x
//xx
//x
int offsetB3S3[4][2]={{0,0},{0,1},{0,2},{1,1}}; InitBlock(B3_S3,offsetB3S3,1,0);
// x
//xx
// x
int offsetB3S4[4][2]={{0,0},{-1,1},{0,1},{0,2}}; InitBlock(B3_S4,offsetB3S4,3,-1);
//x
//xxx
int offsetB4S1[4][2]={{0,0},{0,1},{1,0},{2,0}}; InitBlock(B4_S1,offsetB4S1,2,0);
// x
// x
//xx
int offsetB4S2[4][2]={{0,0},{1,0},{1,1},{1,2}}; InitBlock(B4_S2,offsetB4S2,1,0);
//xxx
// x
int offsetB4S3[4][2]={{0,0},{0,1},{-1,1},{-2,1}}; InitBlock(B4_S3,offsetB4S3,0,-1);
//xx
//x
//x
int offsetB4S4[4][2]={{0,0},{0,1},{0,2},{1,2}}; InitBlock(B4_S4,offsetB4S4,3,0);
// x
//xxx
int offsetB5S1[4][2]={{0,0},{1,0},{2,0},{2,1}}; InitBlock(B5_S1,offsetB5S1,2,0);
//x
//x
//xx
int offsetB5S2[4][2]={{0,0},{1,0},{0,1},{0,2}}; InitBlock(B5_S2,offsetB5S2,3,0);
//xxx
//x
int offsetB5S3[4][2]={{0,0},{0,1},{1,1},{2,1}}; InitBlock(B5_S3,offsetB5S3,0,0);
//xx
// x
// x
int offsetB5S4[4][2]={{0,0},{0,1},{0,2},{-1,2}}; InitBlock(B5_S4,offsetB5S4,1,-1);
//xx
// xx
int offsetB6S1[4][2]={{0,0},{1,0},{0,1},{-1,1}}; InitBlock(B6_S1,offsetB6S1,0,-1);
// x
//xx
//x
int offsetB6S2[4][2]={{0,0},{0,1},{1,1},{1,2}}; InitBlock(B6_S2,offsetB6S2,1,0);
// xx
//xx
int offsetB7S1[4][2]={{0,0},{1,0},{1,1},{2,1}}; InitBlock(B7_S1,offsetB7S1,0,0);
//x
//xx
// x
int offsetB7S2[4][2]={{0,0},{0,1},{-1,1},{-1,2}}; InitBlock(B7_S2,offsetB7S2,1,-1);
}
int _tmain(int argc, _TCHAR* argv[])
{
InitGame();
// TravelBlockList();
Begin();
//GetTempBoard();
//PrintTempBoard();
return 0;
}
// SlideBlock.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
int const LINE=17;
int const WIDTH=10;
typedef struct State{
int xyOffset[4][2]; // block body based on base point x,y
int curDrc; //direction
int side; // the leftest point x
State *next;
}State;
int Board[LINE+1][WIDTH];// Game board 17line*10width
int TempBoard[LINE+1][WIDTH];//Used to get board state
State * Block[8]; // 7 blocks,block list
//Functions in definition
void PrintBlock(State block);
int GetTempBoard();
void PrintTempBoard()
{
//int minLine=GetTempBoard();
for(int i=0;i<LINE;i++,printf("\n"))
for(int j=0;j<WIDTH;j++)
printf("%c",TempBoard[i][j]?'x':'-');
printf("\n");
}
/*
Get the TempBoard which is used to specify the empty space where the block can be put.
minLine : the lowest line in the TempBoard.
*/
int insideBoard(int x,int y)
{
return x>=0&& x<WIDTH && y>=0 && y<LINE;
}
int GetTempBoard()
{
memset(TempBoard,1,sizeof(TempBoard));
int minLine=LINE;
for(int x=0;x<WIDTH;x++)
{
for(int y=LINE-1;y>=0;y--)
{
if(Board[y][x]) break;
TempBoard[y][x]=0;
minLine=(minLine>y?y:minLine);
}
}
PrintTempBoard();
return minLine;
}
/*
Determine whether the block is fit into the TempBoard.
*/
int fitIn(State * block , int x, int y)
{
for(int i=0;i<4;i++)
{
int realX=x+block->xyOffset[i][0];
int realY=y+block->xyOffset[i][1];
if(!insideBoard(realX,realY)||TempBoard[realY][realX]) return 0;
}
return 1;
}
void putBlock(State *block , int x , int y ,int BOARD[][10])
{
for(int i=0;i<4;i++)
{
int realX=x+block->xyOffset[i][0];
int realY=y+block->xyOffset[i][1];
BOARD[realY][realX]=1;
}
}
void removeBlock(State *block ,int x , int y,int BOARD[][10])
{
for(int i=0;i<4;i++)
{
int realX=x+block->xyOffset[i][0];
int realY=y+block->xyOffset[i][1];
BOARD[realY][realX]=0;
}
}
int getEmptyCellsNum(State * block,int x, int y)
{
putBlock(block,x,y,TempBoard);
int count=0;
for(int i=0;i<4;i++)
{
int realX=x+block->xyOffset[i][0];
int realY=y+block->xyOffset[i][1];
while(insideBoard(realX,realY-1) && TempBoard[realY-1][realX]==0)
{
count++;
realY--;
}
}
printf("EmptyCells=%d\n",count);
removeBlock(block,x,y,TempBoard);
return count;
}
/*
Determine which state of the block can be put into the space(TempBoard & Board).
*/
State * getBestState(int block,int x, int y)
{
State *cur=Block[block];
State *temp=NULL;
int min=4;
while(cur)
{
if(fitIn(cur,x,y))
{
int emptyCells=getEmptyCellsNum(cur,x,y);
if(min>emptyCells)
{
temp=cur;
min=emptyCells;
}
}
cur=cur->next;
}
return temp;
}
void PrintBoard()
{
for(int y=0;y<LINE;y++,printf("\n"))
for(int x=0;x<WIDTH;x++)
printf("%c",Board[LINE-y-1][x]?'x':'-');
}
/*
Determine it is a full line
*/
int fullLine(int line)
{
for(int i=0;i<WIDTH;i++)
if(Board[line][i]==0) return 0;
return 1;
}
/*
delete a line .
line:0 - LINE
*/
void deleteLineAt(int line)
{
for(int y=line;y<LINE;y++)
for(int x=0;x<WIDTH;x++)
Board[y][x]=Board[y+1][x];
}
/*
Delete all full lines.
*/
int RemoveFullLines()
{
int count=0;
for(int line=LINE-1;line>=0;line--)
{
if(fullLine(line))
{
count++;
deleteLineAt(line);
}
}
return count;
}
/*
Test whether the postion is a proper place to put block.
Put it when proper.
*/
State * TryAndPutBlock(int minLine,int block)
{
State * cur;
State *best=NULL;
int minNum=17*10;
int bestx,besty;
for(int y=minLine;y<LINE;y++)
for(int x=0;x<WIDTH;x++)
{
if(TempBoard[y][x]==0 && (cur=getBestState(block,x,y))!=NULL )
{
if(minNum>getEmptyCellsNum(cur,x,y))
{
minNum=getEmptyCellsNum(cur,x,y);
best=cur;
bestx=x;
besty=y;
}
}
}
putBlock(best,bestx,besty,Board);
RemoveFullLines();
PrintBoard();
return best;
}
void Begin()
{
int block;
//while(scanf("%d",&block)!=EOF)
while(1)
{
block=rand()%7+1;
int minLine=GetTempBoard();
State * pos=TryAndPutBlock(minLine,block);
printf("%d\t%d\t%d\n",block,pos->curDrc,pos->side);
system("pause");
}
}
void TravelBlockList()
{
State * cur,*temp;
for(int i=0;i<8;i++)
{
cur=Block[i];
temp=cur;
while(temp)
{
PrintBlock(*temp);
temp=temp->next;
}
}
}
void PrintBlock(State block)
{
int xy[6][6]={0};
for(int i=0;i<4;i++)
{
printf("(%d,%d)\t",block.xyOffset[i][0],block.xyOffset[i][1]);
int x=block.xyOffset[i][0]+2;
int y=block.xyOffset[i][1];
xy[x][y]=1;
}
printf("\n");
for(int i=0;i<6;i++,printf("\n"))
for(int j=0;j<6;j++)
{
printf("%c",xy[j][i]?'X':'-');
}
printf("\n\n");
}
void InitBlock(State *block, int offset[][2] , int drc,int side)
{
for(int i=0;i<4;i++)
{
block->xyOffset[i][0]=offset[i][0];
block->xyOffset[i][1]=offset[i][1];
}
block->curDrc=drc;
block->side=side;
//PrintBlock(*block);
}