📄 mmigame.c
字号:
}
/*******************************************************************************
$Function: check_line
$Description: set score points for the mobile, so the mobile knows where to set the next stone
$Returns: total_score
$Arguments: T_four_in_a_row, x_value, drop_position, dx, dy, where_from
*******************************************************************************/
int check_line(T_four_in_a_row *data, int x_value, int y_value, int dx, int dy, BOOL where_from)
{
int lSc,nSc,oSc,total_score,sx,sy,i,j;
int pl_nSc; //no of empty squares that have a man under them (i.e. we/opponent can use next shot)
total_score=0;
for (i=0;i<4;i++){
sx = x_value-i*dx;
sy = y_value-i*dy;
lSc=0;
nSc=0; pl_nSc=0;
oSc=0;
for (j=0;j<4;j++){
if ((sx<0) || (sx>MAX_X_BOARD) ||(sy<0) || (sy>MAX_Y_BOARD)){
lSc = -1;
}else if (lSc>=0){
if(where_from EQ 0){
if (data->board_array[sy][sx] EQ 'B'){
lSc++;
}else if (data->board_array[sy][sx] EQ '0'){
nSc++;
if ((sy EQ MAX_Y_BOARD) || (data->board_array[sy+1][sx] NEQ '0'))
pl_nSc++;
}else if (data->board_array[sy][sx] EQ 'W'){
oSc++;
}else //edge of board found
lSc=-1;
sx = sx+dx;
sy = sy+dy;
}else{
if (data->board_array[sy][sx] EQ 'W'){
lSc++;
}else if (data->board_array[sy][sx] EQ '0'){
nSc++;
if ((sy EQ MAX_Y_BOARD) || (data->board_array[sy+1][sx] NEQ '0'))
pl_nSc++;
}else if (data->board_array[sy][sx] EQ 'B'){
oSc++;
}else //edge of board found
lSc=-1;
sx = sx+dx;
sy = sy+dy;
}
}
}
if (lSc >= 0){
if (lSc EQ 4){
return(9999);//got 4 in a row.
}else if (oSc EQ 3) //and ISc==1
{
// 3 opp men - good spot!
if(where_from EQ 0){
total_score = total_score + 1000; //this is for the normal, current position score
}else{
total_score = -total_score - 1000; //this is for the possible future score, it must be minus, because it will be substracted from normal score
}
}else if ((lSc EQ 3) && (nSc EQ 1)){
//playing here will form 3 in a row and a blank - good spot
if (pl_nSc EQ 0) //can't reach the 4th position
total_score = total_score + 75;
else
total_score = total_score + 125;//better if we can reach 4th position
}else if ((oSc EQ 2) && (nSc EQ 1)){
// 2 opp men + 1 blank- good spot!
if (pl_nSc EQ 0) //can't reach the 4th position-not too bad
total_score = total_score + 50;
else
total_score = total_score + 200;//can get to both positions - have to block.
}else if ((lSc EQ 2) && (nSc EQ 2)){
// 2 in a row and 2 blanks - OK spot
if (pl_nSc EQ 0) //can't reach the 3rd or 4th position-not too good
total_score = total_score + 20;
else if (pl_nSc EQ 1) //can reach one of the 3rd or 4th position - good
total_score = total_score + 45;
else if (pl_nSc EQ 2) //can reach both 3rd and 4th position - very good
total_score = total_score + 95;
}else if ((lSc EQ 1) && (nSc EQ 3)){
// 1 in a row and 3 blanks - OK spot, better if we can reach the other 3 points.
total_score = total_score + 2*(pl_nSc+1); //pl_nSc=0,1,2 or 3 sc=sc+2,4,6 or 8
}
//else //line is neither good nor bad.
}
else //line is not valid
total_score = total_score - 1;
}
return(total_score);
}
/*******************************************************************************
$Function: check_line
$Description: generates a random number to add on the field scores
$Returns: random_score_result
$Arguments:
*******************************************************************************/
int random_scoring(void)
{
return(rand()%100);
}
#define MAX_TET_X 11
#define MAX_TET_Y 20
typedef struct
{
T_MMI_CONTROL mmi_control; // common control parameter
T_MFW_HND win;
T_MFW_HND hKbd;
T_MFW_HND menu;
T_MFW_HND parent_win;
T_MFW_HND info_tim;
int dropTime;
int maxDropTime;
int timeStep;
int xPos;
int yPos;
int px;
int py;
int shapeId;
char shapeStr[20];
int rotateId;
int score;
int level;
int linesToGo;
int resetSpeed;
int gameOver;
char tetrisScreen[MAX_TET_X][MAX_TET_Y];
} T_TETRIS;
static MfwHnd TetrisCreate(MfwHnd hParentWin);
static void TetrisExecCb (MfwHnd hWin, USHORT uiE, SHORT iValue, void *pParameter);
static int TetrisKbdCb (MfwEvt uiE, MfwKbd *psK);
static int tetris_tim_cb(MfwEvt e, MfwTim *t);
static int TetrisWinCb (MfwEvt uiE, MfwWin *psWin);
#define E_INIT 0x5000
#define E_EXIT 0x5001
void initTetGame(T_TETRIS *data)
{
int x,y;
for (y=0;y<MAX_TET_Y;y++)
{
for (x=0;x<MAX_TET_X;x++)
data->tetrisScreen[x][y]=' ';
data->tetrisScreen[0][y]='#';
data->tetrisScreen[MAX_TET_X-1][y]='#';
}
for (x=0;x<MAX_TET_X;x++)
data->tetrisScreen[x][MAX_TET_Y-1]='#';
}
#define MAX_SHAPE 7
void tetris_getShape(int shapeId, int rotate, char* shapeStr)
{
int j,k;
int index;
const char* tetShape[4*4*MAX_SHAPE] =
{ " ", " A ", " B ", " C ", " ", "F ", " G ",
"EEEE", " AA ", " BB ", "CCC ", " DD ", "FFF ", "GGG ",
" ", " A ", " B ", " ", " DD ", " ", " ",
" ", " ", " ", " ", " ", " ", " ",
" E ", " AA ", "BB ", " C ", " ", " FF ", " G ",
" E ", "AA ", " BB ", " CC ", " DD ", " F ", " G ",
" E ", " ", " ", " C ", " DD ", " F ", " GG ",
" E ", " ", " ", " ", " ", " ", " ",
" ", " A ", " B ", " ", " ", " ", " ",
"EEEE", " AA ", " BB ", "CCC ", " DD ", "FFF ", "GGG ",
" ", " A ", " B ", " C ", " DD ", " F ", "G ",
" ", " ", " ", " ", " ", " ", " ",
" E ", " AA ", "BB ", " C ", " ", " F ", "GG ",
" E ", "AA ", " BB ", "CC ", " DD ", " F ", " G ",
" E ", " ", " ", " C ", " DD ", "FF ", " G ",
" E ", " ", " ", " ", " ", " ", " "
};
for (k=0;k<4;k++)
{
index = rotate*4*MAX_SHAPE+k*MAX_SHAPE;
for (j=0;j<4;j++)
shapeStr[j+k*4] = tetShape[index+shapeId][j];
}
}
int tetris_getNextShapeId( void )
{
static int shapeIndex = 0;
shapeIndex = (shapeIndex+1) % MAX_SHAPE;
return (shapeIndex);
}
void tetris_getNextShape(T_TETRIS *data)
{
data->shapeId = tetris_getNextShapeId( );
data->rotateId = 0;
tetris_getShape(data->shapeId, data->rotateId, data->shapeStr);
data->px = (MAX_TET_X-2)/2;
data->py = 0;
}
void tetris_getNextLevel(T_TETRIS *data)
{
initTetGame(data);
tetris_getNextShape(data);
data->level = data->level+1;
data->linesToGo = 5*(data->level+1);
data->resetSpeed = 0;
data->maxDropTime = (120-data->level*5);
}
void initTetrisData( T_TETRIS *data )
{
data->level = 0;
data->score = 0;
data->gameOver = 0;
tetris_getNextLevel(data);
}
void dspl_FillRect( int x1, int y1, int sx, int sy)
{
dspl_DrawFilledBgdRect(x1,y1,x1+sx,y1+sy);
}
void tetris_drawScreen( int allScreen, T_TETRIS *data)
{
int x1,y1,x2,y2;
int bCol;
int xOfs =5;
int yOfs=10;
int xOfs2=30;
int yOfs2=10;
int sx,sy;
int px,py;
U32 oldfCol=0;
U32 oldbCol=0;
int x = data->px;
int y = data->py;
char dbg[80];
#ifdef LSCREEN
//Calculate size based on the screen properties
sx = (SCREEN_SIZE_X-xOfs-xOfs2)/MAX_TET_X;
sy = (SCREEN_SIZE_Y-yOfs-yOfs2-Mmi_layout_softkeyHeight())/MAX_TET_Y;
#else
//Screen is very small - ensure shapes are as large as possible
sy = SCREEN_SIZE_Y/MAX_TET_Y;
sx = SCREEN_SIZE_X/MAX_TET_X;
if (sx >sy+1)
sx = sy+1;
xOfs = 5;
xOfs2 = SCREEN_SIZE_X - (sx*MAX_TET_X+xOfs);
yOfs = 0;
yOfs2 = SCREEN_SIZE_Y - (sy*MAX_TET_Y+xOfs);
#endif
//allScreen= 1;
resources_setColour(COLOUR_GAME);
if (allScreen)
{ //redraw everything
x1 = 0;
y1 = 0;
x2 = MAX_TET_X;
y2 = MAX_TET_Y;
}
else //just draw+1 box around current man
{
x1 = x-1;
y1 = y-1;
x2 = x+5;
y2 = y+4;
if (x1<1)x1=1;
if (y1<0)y1=0;
if (x2>MAX_TET_X-1) x2=MAX_TET_X-1;
if (y2>MAX_TET_Y-1) y2=MAX_TET_Y-1;
}
#ifndef COLOURDISPLAY
dspl_Clear (xOfs+x1*sx,yOfs+y1*sy,xOfs+x2*sx-1,yOfs+y2*sy-1);
#endif
for (px = x1;px<x2;px++)
{
for (py = y1;py<y2;py++)
{
char boardChr = data->tetrisScreen[px][py];
if ((px >= x ) && (px < x+4) &&
(py >= y ) && (py < y+4))
{
int shapeX,shapeY;
shapeX = px-x;
shapeY = py-y;
if (data->shapeStr[shapeX+shapeY*4] != ' ')
boardChr = data->shapeStr[shapeX+shapeY*4];
}
#ifdef COLOURDISPLAY
switch (boardChr)
{
case '#': bCol = 0x00FFFFFF; break;
case ' ': bCol = 0x00404040; break;
case 'A': bCol = 0x000000FF; break;
case 'B': bCol = 0x0000FF00; break;
case 'C': bCol = 0x00FF0000; break;
case 'D': bCol = 0x0000FFFF; break;
case 'E': bCol = 0x00FF00FF; break;
case 'F': bCol = 0x00FFFF00; break;
default: bCol = 0x00FFFFFF; break;
}
dspl_SetBgdColour(bCol);
dspl_FillRect(xOfs+px*sx,yOfs+py*sy,sx,sy);
// dspl_SetFgdColour(0x00802040);
if (boardChr != ' ')
dspl_DrawRect (xOfs+px*sx,yOfs+py*sy,xOfs+px*sx+sx-1,yOfs+py*sy+sy-1);
#else
if (boardChr != ' ')
dspl_DrawRect (xOfs+px*sx,yOfs+py*sy,xOfs+px*sx+sx-1,yOfs+py*sy+sy-1);
#endif
}
}
if (data->gameOver)
{
int oldfCol = dspl_SetFgdColour(0x00FFFF00);
dspl_SetBgdColour(0x008000FF);
dspl_FillRect(xOfs+sx*3-5,yOfs+sy*3-5, xOfs+sx*3+5+6*4,yOfs+sy*3+5+18);
dspl_TextOut(xOfs+sx*3,yOfs+sy*3, 0, "GAME");
dspl_TextOut(xOfs+sx*3,yOfs+sy*3+Mmi_layout_line_height()+4, 0, "OVER");
dspl_SetFgdColour(oldfCol);
}
resources_restoreColour();
}
int tetris_addShapeToScreen(T_TETRIS *data)
{
int x,y;
for (x=0;x<4;x++)
for (y=0;y<4;y++)
if (data->shapeStr[x+y*4] != ' ')
{
if (data->tetrisScreen[data->px+x][data->py+y]!= ' ')
return(1);
else
data->tetrisScreen[data->px+x][data->py+y] = data->shapeStr[x+y*4];
}
return(0);
}
int tetris_testForCompleteLines( T_TETRIS *data )
{
int nLines=0;
int x,y;
int blanks;
for (y=MAX_TET_Y-2;y>0;y--)
{
blanks = 0;
for (x=1;x<MAX_TET_X-1;x++)
{
if (data->tetrisScreen[x][y] == ' ')
blanks++;
if (nLines >0)
data->tetrisScreen[x][y+nLines] = data->tetrisScreen[x][y];
}
if (blanks==0)
nLines++;
}
return (nLines);
}
void tetris_testGameOver( T_TETRIS *data )
{
int linesComplete;
data->gameOver = tetris_addShapeToScreen(data);
if (!data->gameOver)
{
linesComplete = tetris_testForCompleteLines(data);
if (linesComplete >0)
{
data->score = data->score + linesComplete*linesComplete;
data->linesToGo = data->linesToGo - linesComplete;
if (data->linesToGo <0)
tetris_getNextLevel(data);
}
tetris_getNextShape(data);
}
}
void tetris_destroy (T_MFW_HND own_window)
{
T_MFW_WIN * win = ((T_MFW_HDR *)own_window)->data;
T_TETRIS * data = (T_TETRIS *)win->user;
TRACE_EVENT ("tetris_destroy()");
if (own_window == NULL)
{
TRACE_EVENT ("Error :- Called with NULL Pointer");
return;
}
if (data)
{
/*
* Exit ICON & KEYBOARD Handle
*/
kbdDelete (data->hKbd);
timDelete(data->info_tim);
/*
* Delete WIN Handler
*/
winDelete (data->win);
/*
* Free Memory
*/
FREE_MEMORY ((void *)data, sizeof (T_TETRIS));
}
}
int MmiTetrisStart(void )
{
MfwHnd hWin;
MfwHnd hParentWin = mfwParent(mfwHeader());
TRACE_FUNCTION ("TetrisStart()");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -