📄 mmigame.c
字号:
#ifdef LSCREEN
manOfsX = GAME_POS_X + 3;
manOfsY = GAME_POS_Y + 3;
#else
manOfsX = GAME_POS_X + 1;
manOfsY = GAME_POS_Y + 1;
#endif
xOfs = GAME_POS_X;
yOfs = GAME_POS_Y;
xScale = GAME_SCALE_X;
yScale = GAME_SCALE_Y;
//dspl_ClearAll(); //Clears to white, not BGD!
TRACE_EVENT("display clear");
resources_setColour( COLOUR_GAME );
dspl_Clear( 0,0, SCREEN_SIZE_X,SCREEN_SIZE_Y);
#ifdef LSCREEN
softKeys_displayId(TxtHelp,TxtExit,0,COLOUR_LIST_SUBMENU);
#endif
resources_setColour( COLOUR_GAME );
dspl_Clear( xOfs, yOfs, xOfs+MAX_X_BOARD*xScale, yOfs+MAX_Y_BOARD*yScale ); //Clears to white, not BGD!
//dspl_BitBlt(column*xScale+xOfs+1,yOfs-yScale,8,8,0,(void*)game_cursor,0); //place the cursor bitmap
dspl_show_bitmap(column*xScale+xOfs+1,yOfs-yScale, &gameCursor, 0 );
for (y_axis=0;y_axis<=MAX_Y_BOARD;y_axis++)
{
dspl_DrawLine( xOfs, yOfs+y_axis*yScale,
xOfs+MAX_X_BOARD*xScale, yOfs+y_axis*yScale);
}
for(x_axis=0;x_axis<=MAX_X_BOARD;x_axis++)
{
dspl_DrawLine( xOfs+x_axis*xScale, yOfs,
xOfs+x_axis*xScale, yOfs+MAX_Y_BOARD*yScale);
}
//go through the board-array and check for W or B and set the stones
for (y_axis=0;y_axis<MAX_Y_BOARD;y_axis++)
{
for(x_axis=0;x_axis<MAX_X_BOARD;x_axis++)
{
xPos = manOfsX+x_axis*xScale;
yPos = manOfsY+y_axis*yScale;
if(data->board_array[y_axis][x_axis] EQ 'W'){ //found white
//set white_stone bitmap
dspl_SetFgdColour( COL_R );
dspl_show_bitmap(xPos,yPos, whiteStone, 0 );
}
if(data->board_array[y_axis][x_axis] EQ 'B'){ //found black
//set black_stone bitmap
dspl_SetFgdColour( COL_G ); //0x00404040
dspl_show_bitmap(xPos,yPos, blackStone, 0 );
}
}
}
dspl_SetFgdColour( COL_RB );
dspl_SetBgdColour( COL_RG );
dspl_show_bitmap(GAME_NAMEPOSX1, GAME_NAMEPOSY1, &gameName1, 0 );
displayPosX = (SCREEN_SIZE_X/2);
temp = strlen((char*)playAgain);
//this if-clause is to set the bitmap, for win or lost game
if(winner EQ 'W'){
//human player wins, so set win bitmap
#ifdef LSCREEN
dspl_SetFgdColour( COL_BLK );
dspl_SetBgdColour( COL_TRANSPARENT );
Game_Result = TRUE;
softKeys_displayId(TxtYes,TxtNo,0,COLOUR_LIST_SUBMENU);
dspl_TextOut((displayPosX-((temp*CHAR_WIDTH)/2)),170,DSPL_TXTATTR_NORMAL,(char*)playAgain);
#endif
dspl_SetFgdColour( COL_RB );
dspl_SetBgdColour( COL_GB );
dspl_show_bitmap(GAME_WINPOSX1, GAME_WINPOSY1, &plWin1, 0 );
dspl_show_bitmap(GAME_WINPOSX2, GAME_WINPOSY2, &plWin2, 0 );
}
else
if(winner EQ 'B'){
//human player lost, so set lost bitmap
#ifdef LSCREEN
dspl_SetFgdColour( COL_BLK );
dspl_SetBgdColour( COL_TRANSPARENT );
Game_Result = TRUE;
softKeys_displayId(TxtYes,TxtNo,0,COLOUR_LIST_SUBMENU);
dspl_TextOut((displayPosX-((temp*CHAR_WIDTH)/2)),170,DSPL_TXTATTR_NORMAL,(char*)playAgain);
#endif
dspl_SetFgdColour( COL_RB );
dspl_SetBgdColour( COL_GB );
dspl_show_bitmap(GAME_WINPOSX1, GAME_WINPOSY1, &plLost1, 0 );
dspl_show_bitmap(GAME_WINPOSX2, GAME_WINPOSY2, &plLost2, 0 );
}
else
if(winner EQ 'N'){
//board is full, nobody (N) wins
#ifdef LSCREEN
dspl_SetFgdColour( COL_BLK );
dspl_SetBgdColour( COL_TRANSPARENT );
Game_Result = TRUE;
softKeys_displayId(TxtYes,TxtNo,0,COLOUR_LIST_SUBMENU);
dspl_TextOut((displayPosX-((temp*CHAR_WIDTH)/2)),170,DSPL_TXTATTR_NORMAL,(char*)playAgain);
#endif
dspl_SetFgdColour( COL_R );
dspl_SetBgdColour( COL_G );
dspl_show_bitmap(GAME_WINPOSX1, GAME_WINPOSY1, &draw1, 0 );
dspl_show_bitmap(GAME_WINPOSX2, GAME_WINPOSY2, &draw2, 0 );
}
else
{
dspl_SetFgdColour( COL_RB );
dspl_SetBgdColour( COL_RG );
dspl_show_bitmap(GAME_NAMEPOSX1, GAME_NAMEPOSY1, &gameName1, 0 );
dspl_show_bitmap(GAME_NAMEPOSX2, GAME_NAMEPOSY2, &gameName2, 0 );
dspl_show_bitmap(GAME_NAMEPOSX3, GAME_NAMEPOSY3, &gameName2, 0 );
}
break;
default:
return 0;
}
return 1;
}
/*******************************************************************************
$Function: four_in_a_row_kbd_cb
$Description:
$Returns:
$Arguments:
*******************************************************************************/
int four_in_a_row_kbd_cb (MfwEvt e, MfwKbd *k)
{
T_MFW_HND win = mfw_parent (mfw_header());
T_MFW_WIN * win_data = ((T_MFW_HDR *)win)->data;
T_four_in_a_row * data = (T_four_in_a_row *)win_data->user;
TRACE_EVENT ("four_in_a_row_kbd_cb()");
switch (k->code)
{
case KCD_HUP: //quit the game "four in a row"
case KCD_RIGHT: //quit the game "four in a row"
TRACE_EVENT ("quit four_in_a_row");
dspl_ClearAll(); //clear mobile screen
four_in_a_row_destroy (win); //destroy win-handler
break;
case KCD_MNULEFT:
case KCD_1:
if(winner EQ '0'){ //while nobody has won....
TRACE_EVENT ("KCD_1");
if(column > 0)
{ //cursor must be greater than 0
column=column-1;
}
winShow(data->win);
}
break;
case KCD_MNUSELECT:
case KCD_2:
if(winner EQ '0'){ //while nobody has won....
TRACE_EVENT ("KCD_2");
drop_stone(data); //drop stone
}
winShow(data->win);
break;
case KCD_MNURIGHT:
case KCD_3:
if(winner EQ '0'){ //while nobody has won....
TRACE_EVENT ("KCD_3");
if(column<MAX_X_BOARD-1 ){ //cursor must be less than 50
column=column+1;
}
winShow(data->win);
}
break;
case KCD_LEFT:
if(Game_Result == TRUE)
{
Game_Result = FALSE;
four_in_a_row_destroy(win); //destroy win-handler
four_in_a_row_start(win,0);
}
else
Game_Info(win);
break;
}
return MFW_EVENT_CONSUMED;
}
/*******************************************************************************
$Function: four_in_a_row_aktivate
$Description: Starts the game function on user selection
$Returns: MFW_EVENT_CONSUMED if event handled, otherwise
MFW_EVENT_PASSED
$Arguments: menu, menu item
*******************************************************************************/
int four_in_a_row_aktivate(MfwMnu* m, MfwMnuItem* i)
{
T_four_in_a_row * data;
T_MFW_HND parent = mfwParent( mfw_header());
TRACE_EVENT("four_in_a_row_aktivate()");
four_in_a_row_start(parent,0);
return MFW_EVENT_CONSUMED;
}
/*******************************************************************************
$Function: drop_stone
$Description: decide whether a stone can be droped and where it can be droped
$Returns:
$Arguments: T_four_in_a_row
*******************************************************************************/
void drop_stone(T_four_in_a_row *data)
{
int drop_position; //position (y in current board column) where to drop the stone
TRACE_EVENT("drop_stone()");
request_stone=0; //white is on turn
drop_position=check_column(data,request_stone); //set the drop position for the white stone in the current white, board column
if(drop_position>=0){ //if the current column is not filled up set stone
data->board_array[drop_position][column]='W'; //white is first and set a 'W' in the virtual board for a white stone
check_for_winner(data,drop_position); //check whether the last move(white) was a win-move...
if(winner EQ '0'){ //...if not, then the mobile(black) is on turn
drop_position=mobile_thinking(data); //set the drop position for the black stone in the current black, board column
data->board_array[drop_position][column_black]='B'; //set a 'B' for in the virtual board for a black stone
check_for_winner(data,drop_position); //check whether the last move(black) was a win-move...
}
}
}
/*******************************************************************************
$Function: check_for_winner
$Description: checks the current drop position for a win-position
$Returns:
$Arguments: T_four_in_a_row
*******************************************************************************/
void check_for_winner(T_four_in_a_row *data, int check_out_position)
{
int check_with_this_color; //set the color to check for
int color_regulator; //helps to switch between both colors
int check_this_column; //declares the column to check for
TRACE_EVENT("check_for_winner()");
color_regulator=0; //at first, use white to check for
winner='0'; //at first we don't know who wins, so nobody is set as winner
while(color_regulator<2){ //while checking for white(0) and black(1) is not ready....
if(color_regulator EQ 0){ //if color_regulator is 0, the board will be checked for white stones ('W')
check_with_this_color='W';
}else{ //if color_regulator is 1, the board will be checked for black stones ('B')
check_with_this_color='B';
}
if(request_stone EQ 0){ //if the board will be checked for white, use column as the current column
check_this_column=column;
}else{ //if the board will be checked for black, use column_black as the current column
check_this_column=column_black;
}
/*Begin with the checks, the mobile checks for vertical, horizontal and diagonal possibilities*/
//check row
if(data->board_array[check_out_position][check_this_column-3] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column-2] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column-1] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_this_column>=3 && check_this_column<=10){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position][check_this_column-1] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column-2] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column+1] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_this_column>=2 && check_this_column<=9){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position][check_this_column+1] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column+2] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column+3] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_this_column>=0 && check_this_column<=7){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position][check_this_column-1] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column+1] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column+2] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_this_column>=1 && check_this_column<=8){
winner=check_with_this_color; //set the current color to check for as the winner
break;
//check column
}else if(data->board_array[check_out_position+1][check_this_column] EQ check_with_this_color &&
data->board_array[check_out_position+2][check_this_column] EQ check_with_this_color &&
data->board_array[check_out_position+3][check_this_column] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_out_position<=4){
winner=check_with_this_color; //set the current color to check for as the winner
break;
//check diagonal
}else if(data->board_array[check_out_position+1][check_this_column-1] EQ check_with_this_color &&
data->board_array[check_out_position+2][check_this_column-2] EQ check_with_this_color &&
data->board_array[check_out_position+3][check_this_column-3] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_out_position<=4 && check_this_column>=3 && check_this_column<=10){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position+1][check_this_column+1] EQ check_with_this_color &&
data->board_array[check_out_position+2][check_this_column+2] EQ check_with_this_color &&
data->board_array[check_out_position+3][check_this_column+3] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_out_position<=4 && check_this_column>=0 && check_this_column<=7){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position-1][check_this_column+1] EQ check_with_this_color &&
data->board_array[check_out_position-2][check_this_column+2] EQ check_with_this_color &&
data->board_array[check_out_position-3][check_this_column+3] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_out_position>=3 && check_this_column>=0 && check_this_column<=7){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position-1][check_this_column-1] EQ check_with_this_color &&
data->board_array[check_out_position-2][check_this_column-2] EQ check_with_this_color &&
data->board_array[check_out_position-3][check_this_column-3] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_out_position>=3 && check_this_column>=3 && check_this_column<=10){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position+1][check_this_column-1] EQ check_with_this_color &&
data->board_array[check_out_position-1][check_this_column+1] EQ check_with_this_color &&
data->board_array[check_out_position-2][check_this_column+2] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_out_position<=6 && check_out_position>=2 && check_this_column>=1 && check_this_column<=8){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position+1][check_this_column-1] EQ check_with_this_color &&
data->board_array[check_out_position+2][check_this_column-2] EQ check_with_this_color &&
data->board_array[check_out_position-1][check_this_column+1] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_out_position<=5 && check_out_position>=1 && check_this_column>=2 && check_this_column<=9){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position-1][check_this_column-1] EQ check_with_this_color &&
data->board_array[check_out_position+1][check_this_column+1] EQ check_with_this_color &&
data->board_array[check_out_position+2][check_this_column+2] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_out_position<=5 && check_out_position>=1 && check_this_column>=1 && check_this_column<=8){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else if(data->board_array[check_out_position-1][check_this_column-1] EQ check_with_this_color &&
data->board_array[check_out_position-2][check_this_column-2] EQ check_with_this_color &&
data->board_array[check_out_position+1][check_this_column+1] EQ check_with_this_color &&
data->board_array[check_out_position][check_this_column] EQ check_with_this_color &&
check_out_position>=2 && check_out_position<=6 && check_this_column>=2 && check_this_column<=9){
winner=check_with_this_color; //set the current color to check for as the winner
break;
}else{
winner='0'; //no win-moves found, so nobody is the winner and the game goes on
}
color_regulator++; //set the check color one up (to black)
}
}
/*******************************************************************************
$Function: check_column
$Description: checks for already set stones
$Returns: int unused_position
$Arguments: T_four_in_a_row
*******************************************************************************/
int check_column(T_four_in_a_row *data, int where_from)
{
int unused_position; //is for a free drop position
int column_for_check; //column (vertical board line) to check for
if(where_from EQ 0){ //is the current game color(player) 0 then use column
column_for_check=column;
}else{ //is the current game color(player) 1 then use column_black
column_for_check=column_black;
}
unused_position=-9999;
for(y_runer=0;y_runer<MAX_Y_BOARD;y_runer++){ //test all vertical fields whether they are empty('0') or not
if(data->board_array[y_runer][column_for_check] EQ '0'){
unused_position=y_runer; //.this field is empty, so it is a unused position
}
}
return unused_position; //return the unused position
}
/*******************************************************************************
$Function: mobile_thinking
$Description: mobile moves
$Returns: drop position of black stone (drop_me)
$Arguments: T_four_in_a_row
*******************************************************************************/
int mobile_thinking(T_four_in_a_row *data)
{
int position_score[11]; //this array takes the scoring values of each column in the game board
int drop_position; //drop position for stone
int drop_me; //buffer value
int scores[4]; //this array keeps the scores for row, column and diagonals
int add_random_score;
TRACE_EVENT("mobile_thinking()");
request_stone=1; //black is
//check for good spots to drop the black stone
for(x_runer=0;x_runer<MAX_X_BOARD;x_runer++){
//found the possible drop position
column_black=x_runer; //set the x_runer to the current column (column_black)
drop_position=check_column(data,request_stone); //check for possible drop positions
if(drop_position>=0){ //if there is a possible drop position.....
//check the scoring of the current point
add_random_score=random_scoring();
data->board_array[drop_position][x_runer]='B'; //set a test black stone
scores[0]=check_line(data,x_runer,drop_position,1,0,0); //scores row
scores[1]=check_line(data,x_runer,drop_position,1,1,0); //scores diagonal right-up/left-down
scores[2]=check_line(data,x_runer,drop_position,1,-1,0); //scores diagonal left-up/right-down
scores[3]=check_line(data,x_runer,drop_position,0,1,0); //scores down(column)
position_score[x_runer]=scores[0]+scores[1]+scores[2]+scores[3]; //count all scores together to one total score
if(drop_position-1 >=0){ //Is it possible to set a future stone here? If Yes go on...
data->board_array[drop_position-1][x_runer]='W'; //set a test white stone for a possible next opponent move
scores[0]=check_line(data,x_runer,drop_position-1,1,0,1); //scores row
scores[1]=check_line(data,x_runer,drop_position-1,1,1,1); //scores diagonal right-up/left-down
scores[2]=check_line(data,x_runer,drop_position-1,1,-1,1); //scores diagonal left-up/right-down
scores[3]=check_line(data,x_runer,drop_position-1,0,1,1); //scores down(column)
position_score[x_runer]=position_score[x_runer]-scores[0]-scores[1]-scores[2]-scores[3]+add_random_score; //substraction of the future score from the normal score
data->board_array[drop_position-1][x_runer]='0'; //remove test white stone
}
data->board_array[drop_position][x_runer]='0'; //remove test black stone
}else{ //if there is no possible drop position set the score to -1
position_score[x_runer]=-9999;
}
}
drop_me=-9999; //buffer is set to absolutely minus
column_black=-9999; //position of black cursor is left outer side of board
for(x_runer=0;x_runer<MAX_X_BOARD;x_runer++){ //goes through the scoring array to find the highest score
if(position_score[x_runer]>drop_me){
drop_me=position_score[x_runer];
column_black=x_runer; //set the x_runer to the current column(column_black)
drop_position=check_column(data,request_stone); //this is the best drop position for the mobile
}
}
if(column_black EQ -9999){ //if the column is -1, then the game board is filled up and nobody has won
TRACE_EVENT("Board Full!!!");
winner='N'; //set winner to nobody (N)
}
return drop_position; //return the mobile drop position
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -