俄罗斯方块自动游戏,想听听各位的意见!

languagec 2009-05-16 02:38:14
说明
俄罗斯方块中共有以下七种方块,每种方块都由四个方格组成,如下图所示,我们把这七种方块分别编号为 1~7。

游戏中,每次落下一个方块,落到一个宽度为 10 格的槽中。方块的下部一旦碰到槽的底部,或槽中已有的方块,就不能再移动。方块落下不动后,如果有某些行因落下的方块而填满,这些行将被消去。方块下落前,你可以控制方块的左右移动和旋转,以将其放在合适的位置。你对方块的所有移动和旋转操作在下落前(槽外)就计算完毕,然后直接下落到底,下落过程中不能再做操作。如果方块刚刚落下后顶部高度大于 17 行,游戏结束——即使此时有些行可以消除。

让r 表示方块旋转方式( r=0, 1, 2, 3 分别表示顺时针旋转 0 度、 90 度、 180 度、 270 度,0度代表上图所表示的位置),c 表示方块在旋转后的最左边一格的列编号(从左到右依次为 1, 2, … , 10 )。

一次消去 1 行、 2 行、 3 行、 4 行的得分分别为 0, 2, 4, 8,也就是说,消去单独的 1 行不得分。

2、 要求

(1)输入:
提供一个文本文件,其格式为:第一行为一个整数n(实际输入100000000 >= n >= 50000000), 表示接下来有n个方块要落下,接下来有n行, 每行有一个整数t(0< t < 8),代表上图中的某个方块要落下。(每个方块等概率出现)
测试数据格式举例如下:
10
1
2
2
7
5
4
4
6
3
2
(其中10表示n=10,代表有n个方块落下;其它的数字表示t,代表上图中的第t个方块下落)

(2)输出文件格式:
输出n行, 每一行有三个整数t,r, c, 代表第t个方块具体落下的方式(r为最终的旋转方式,c为格数),三个数字之间采用空格符分隔。(不需要提供图形界面)
(3)编程语言:C/C++。
(4)不允许使用开源代码。

...全文
237 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
languagec 2009-05-18
  • 打赏
  • 举报
回复


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;
}

languagec 2009-05-18
  • 打赏
  • 举报
回复
代码写得很丑,加了很多调试信息在里面。 仅供参考,请大侠忽略。

// 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);
}






languagec 2009-05-18
  • 打赏
  • 举报
回复
不好意思,题目的地址是
http://legendcode.alibaba-inc.com/intro-project.jsp#a

是机器自动判断方块要放的位置,我有一些想法,但实现出来以后的效果都不是很理想。
所以想听听你们的意见,该如何判断方块放的最好的位置。

绿色夹克衫 2009-05-18
  • 打赏
  • 举报
回复
不知道题目中的7种指的是否为经典俄罗斯方块的7种,

这样的题似乎没有那么简单吧?既要考虑不出现game over,还要考虑尽可能多的得分,而且数据量不小。

感觉最好先对一些固定的组合作预处理,做到局部最优,但要实现全局最优,恐怕还有问题!
hemiya 2009-05-18
  • 打赏
  • 举报
回复
是人玩游戏,还是游戏自己玩?
coxfilur_2008 2009-05-17
  • 打赏
  • 举报
回复
目标是什么? 得分最多,还是把所有方块都消完,还是其他什么?
StFairy 2009-05-17
  • 打赏
  • 举报
回复
百度之星……这题很简单……LZ要努力……
  • 打赏
  • 举报
回复
看看先
sam_pei 2009-05-16
  • 打赏
  • 举报
回复
这个更简单

33,010

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧