📄 game.c
字号:
#include <config.h>#include "game.h"/** * clear_board: * @app: the application struct * * function description: mark the fields * * return values: nothing */voidclear_board(GoalApp *app){ gint x, y; if(app == NULL) return; for (x=0; x<=1; x++) for(y=0; y<=1; y++) app->game.CellStatus[x][y] = UNKOWN; for (x=5; x<=6; x++) for(y=0; y<=1; y++) app->game.CellStatus[x][y] = UNKOWN; for (x=0; x<=1; x++) for(y=5; y<=6; y++) app->game.CellStatus[x][y] = UNKOWN; for (x=5; x<=6; x++) for(y=5; y<=6; y++) app->game.CellStatus[x][y] = UNKOWN; for (x=2; x<=4; x++) for(y=0; y<=1; y++) app->game.CellStatus[x][y] = EMPTY; for (x=0; x<=6; x++) for(y=2; y<=4; y++) app->game.CellStatus[x][y] = EMPTY; for (x=2; x<=4; x++) for(y=5; y<=6; y++) app->game.CellStatus[x][y] = EMPTY;}/** * init_new_game: * @app: the application struct * * function description: set standart values for each game type * * return values: nothing */void init_new_game(GoalApp *app){ gint x, y; switch(app->game.GameType) { case CROSS : clear_board(app); /* clear all */ app->game.CellStatus[3][1] = OCCUPIED; app->game.CellStatus[2][2] = OCCUPIED; app->game.CellStatus[3][2] = OCCUPIED; app->game.CellStatus[4][2] = OCCUPIED; app->game.CellStatus[3][3] = OCCUPIED; app->game.CellStatus[3][4] = OCCUPIED; gtk_window_set_title(GTK_WINDOW(app->gui.MainWindow), _("Goal - Cross")); break; case PLUS : clear_board(app); /* clear all */ app->game.CellStatus[3][1] = OCCUPIED; app->game.CellStatus[1][3] = OCCUPIED; app->game.CellStatus[3][2] = OCCUPIED; app->game.CellStatus[2][3] = OCCUPIED; app->game.CellStatus[3][3] = OCCUPIED; app->game.CellStatus[4][3] = OCCUPIED; app->game.CellStatus[5][3] = OCCUPIED; app->game.CellStatus[3][4] = OCCUPIED; app->game.CellStatus[3][5] = OCCUPIED; gtk_window_set_title(GTK_WINDOW(app->gui.MainWindow), _("Goal - Plus")); break; case CHIMNEY : clear_board(app);/* clear all */ app->game.CellStatus[2][0] = OCCUPIED; app->game.CellStatus[2][1] = OCCUPIED; app->game.CellStatus[3][0] = OCCUPIED; app->game.CellStatus[3][1] = OCCUPIED; app->game.CellStatus[4][0] = OCCUPIED; app->game.CellStatus[4][1] = OCCUPIED; app->game.CellStatus[2][2] = OCCUPIED; app->game.CellStatus[2][3] = OCCUPIED; app->game.CellStatus[3][2] = OCCUPIED; app->game.CellStatus[4][3] = OCCUPIED; app->game.CellStatus[4][2] = OCCUPIED; gtk_window_set_title(GTK_WINDOW(app->gui.MainWindow), _("Goal - Chimney")); break; case PYRAMID : clear_board(app);/* clear all */ app->game.CellStatus[3][1] = OCCUPIED; app->game.CellStatus[2][2] = OCCUPIED; app->game.CellStatus[3][2] = OCCUPIED; app->game.CellStatus[4][2] = OCCUPIED; app->game.CellStatus[1][3] = OCCUPIED; app->game.CellStatus[2][3] = OCCUPIED; app->game.CellStatus[3][3] = OCCUPIED; app->game.CellStatus[0][4] = OCCUPIED; app->game.CellStatus[1][4] = OCCUPIED; app->game.CellStatus[2][4] = OCCUPIED; app->game.CellStatus[3][4] = OCCUPIED; app->game.CellStatus[4][4] = OCCUPIED; app->game.CellStatus[5][4] = OCCUPIED; app->game.CellStatus[6][4] = OCCUPIED; app->game.CellStatus[4][3] = OCCUPIED; app->game.CellStatus[5][3] = OCCUPIED; gtk_window_set_title(GTK_WINDOW(app->gui.MainWindow), _("Goal - Pyramid")); break; case ARROW : clear_board(app);/* clear all */ app->game.CellStatus[3][0] = OCCUPIED; app->game.CellStatus[2][1] = OCCUPIED; app->game.CellStatus[3][1] = OCCUPIED; app->game.CellStatus[4][1] = OCCUPIED; app->game.CellStatus[1][2] = OCCUPIED; app->game.CellStatus[2][2] = OCCUPIED; app->game.CellStatus[3][2] = OCCUPIED; app->game.CellStatus[4][2] = OCCUPIED; app->game.CellStatus[5][2] = OCCUPIED; app->game.CellStatus[3][3] = OCCUPIED; app->game.CellStatus[3][4] = OCCUPIED; app->game.CellStatus[2][5] = OCCUPIED; app->game.CellStatus[3][5] = OCCUPIED; app->game.CellStatus[4][5] = OCCUPIED; app->game.CellStatus[2][6] = OCCUPIED; app->game.CellStatus[3][6] = OCCUPIED; app->game.CellStatus[4][6] = OCCUPIED; gtk_window_set_title(GTK_WINDOW(app->gui.MainWindow), _("Goal - Arrow")); break; case RUBIN : clear_board(app);/* clear all */ app->game.CellStatus[3][0] = OCCUPIED; app->game.CellStatus[2][1] = OCCUPIED; app->game.CellStatus[3][1] = OCCUPIED; app->game.CellStatus[4][1] = OCCUPIED; app->game.CellStatus[1][2] = OCCUPIED; app->game.CellStatus[2][2] = OCCUPIED; app->game.CellStatus[3][2] = OCCUPIED; app->game.CellStatus[4][2] = OCCUPIED; app->game.CellStatus[5][2] = OCCUPIED; app->game.CellStatus[2][3] = OCCUPIED; app->game.CellStatus[3][3] = OCCUPIED; app->game.CellStatus[4][3] = OCCUPIED; app->game.CellStatus[1][4] = OCCUPIED; app->game.CellStatus[2][4] = OCCUPIED; app->game.CellStatus[3][4] = OCCUPIED; app->game.CellStatus[4][4] = OCCUPIED; app->game.CellStatus[5][4] = OCCUPIED; app->game.CellStatus[2][5] = OCCUPIED; app->game.CellStatus[3][5] = OCCUPIED; app->game.CellStatus[4][5] = OCCUPIED; app->game.CellStatus[3][6] = OCCUPIED; gtk_window_set_title(GTK_WINDOW(app->gui.MainWindow), _("Goal - Rubin")); break; case DIAMOND : clear_board(app);/* clear all */ app->game.CellStatus[3][0] = OCCUPIED; app->game.CellStatus[2][1] = OCCUPIED; app->game.CellStatus[3][1] = OCCUPIED; app->game.CellStatus[4][1] = OCCUPIED; app->game.CellStatus[1][2] = OCCUPIED; app->game.CellStatus[2][2] = OCCUPIED; app->game.CellStatus[3][2] = OCCUPIED; app->game.CellStatus[4][2] = OCCUPIED; app->game.CellStatus[5][2] = OCCUPIED; app->game.CellStatus[0][3] = OCCUPIED; app->game.CellStatus[1][3] = OCCUPIED; app->game.CellStatus[2][3] = OCCUPIED; app->game.CellStatus[3][3] = OCCUPIED; app->game.CellStatus[4][3] = OCCUPIED; app->game.CellStatus[5][3] = OCCUPIED; app->game.CellStatus[6][3] = OCCUPIED; app->game.CellStatus[1][4] = OCCUPIED; app->game.CellStatus[2][4] = OCCUPIED; app->game.CellStatus[3][4] = OCCUPIED; app->game.CellStatus[4][4] = OCCUPIED; app->game.CellStatus[5][4] = OCCUPIED; app->game.CellStatus[2][5] = OCCUPIED; app->game.CellStatus[3][5] = OCCUPIED; app->game.CellStatus[4][5] = OCCUPIED; app->game.CellStatus[3][6] = OCCUPIED; gtk_window_set_title(GTK_WINDOW(app->gui.MainWindow), _("Goal - Diamond")); break; case SOLITAIRE : for (x=2; x<=4; x++) for(y=0; y<=1; y++) app->game.CellStatus[x][y] = OCCUPIED; for (x=0; x<=6; x++) for(y=2; y<=4; y++) app->game.CellStatus[x][y] = OCCUPIED; for (x=2; x<=4; x++) for(y=5; y<=6; y++) app->game.CellStatus[x][y] = OCCUPIED; gtk_window_set_title(GTK_WINDOW(app->gui.MainWindow), _("Goal - Solitaire")); break; default: g_error(":: PACKAGE: %s :: FILE: %s :: LINE: %i :: unkown game type\n" , PACKAGE, __FILE__, __LINE__); } /* show pieces */ for(x = 0; x < NUMBER_CELLS; x++) for(y = 0; y < NUMBER_CELLS; y++) switch(app->game.CellStatus[x][y]) { case UNKOWN : /* with this value we mark not used fields */ break; case EMPTY : gnome_canvas_item_hide(app->gui.PieceNormal[x][y]); break; case OCCUPIED : gnome_canvas_item_show(app->gui.PieceNormal[x][y]); break; default: g_error(":: PACKAGE: %s :: FILE: %s :: LINE: %i :: wrong status type (x=%i :: y=%i)\n" , PACKAGE, __FILE__, __LINE__, x, y); } /* hide the "helper" pieces */ gnome_canvas_item_hide(app->gui.PieceEmpty); gnome_canvas_item_hide(app->gui.PieceMarked); gnome_canvas_item_hide(app->gui.PieceTouched); gnome_canvas_item_hide(app->gui.PieceNegativ); gnome_canvas_item_hide(app->gui.PieceEmptyPositiv); gnome_canvas_item_hide(app->gui.PieceEmptyNegativ); /* set important variables */ app->game.JumpStartPosX = 0; app->game.JumpStartPosY = 0; app->game.MovePosX = 0; app->game.MovePosY = 0;}/** * valid_move: * @app: * @from_x: * @from_y: * @to_x: * @to_y: * * function description: * * return values: */gboolean valid_move(GoalApp *app, gint from_x, gint from_y, gint to_x, gint to_y){ gint diff; /* preconditions */ if((/**/app->game.CellStatus[from_x][from_y] != UNKOWN) && (/**/ app->game.CellStatus[to_x][to_y] != UNKOWN) && (/**/app->game.CellStatus[to_x][to_y] == EMPTY) && (/**/app->game.CellStatus[from_x][from_y] == OCCUPIED) && ((from_x == to_x) || (from_y == to_y))) { if(from_x == to_x) {/* here check horizontal move */ diff = from_y - to_y; if((diff == 2) || (diff == -2)) { if(diff == -2) diff = from_y + 1; else diff= from_y - 1; if(app->game.CellStatus[from_x][diff] == OCCUPIED) return TRUE; else return FALSE; } else return FALSE; } else {/* here check vertical move */ diff = from_x - to_x; if((diff == 2) || (diff == -2)) { if(diff == -2) diff = from_x + 1; else diff= from_x - 1; if(app->game.CellStatus[diff][from_y] == OCCUPIED) return TRUE; else return FALSE; } else return FALSE; } } else return FALSE;}/** * one_move_possible: * @app: * * function description: * * return values: TRUE if one move is possible, otherwise FALSE */gbooleanone_move_possible(GoalApp *app){ gint x, y; for(x = 0; x < NUMBER_CELLS; x++) { for(y = 0; y < NUMBER_CELLS; y++) { if(valid_move(app, x,y,x+2,y)) return TRUE; if(valid_move(app, x,y,x-2,y)) return TRUE; if(valid_move(app, x,y,x,y+2)) return TRUE; if(valid_move(app, x,y,x,y-2)) return TRUE; } } return FALSE;}/** * remove_piece: * @app: * @x: * @y: * * function description: * * return values: */voidremove_piece(GoalApp *app, gint x, gint y){ app->game.CellStatus[x][y] = EMPTY; gnome_canvas_item_hide(app->gui.PieceNormal[x][y]);}/** * make_move: * @app: * @from_x: * @from_y: * @to_x: * @to_y: * * function description: * * return values: */void make_move(GoalApp *app, gint from_x, gint from_y, gint to_x, gint to_y){ gint diff;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -