#include <string.h> #include "member.h" STRUCT_MEMBER_LIST member_list; void init_member_list() { //member_list.number=0; memset(&member_list, 0, sizeof(STRUCT_MEMBER_LIST)); } int regist_member(STRUCT_MEMBER member) { if(member_list.number > MAX_MEMBER_NUM) return -1; if(isExist(member) == 0) return -1; member_list.members[member_list.number] = member; member_list.number++; return 0; } int member_unregist(STRUCT_MEMBER member) { if(member_list.number <= 0) return -1; if(isExist(member) != 0) return -1; member_list.members[member_list.number].type=0; member_list.members[member_list.number].name=""; member_list.number--; return 0; } STRUCT_MEMBER_LIST get_members() { return member_list; } int isExist(STRUCT_MEMBER member) { int i=0; if(isListEmpty() == 0) return -1; for(i=0; i< member_list.number; i++) { if(compare_member(member_list.members[i], member) == 0) { return 0; } } return -1; } int compare_member(STRUCT_MEMBER pre, STRUCT_MEMBER next) { if(pre.type == next.type && strcmp(pre.name, next.name) == 0) return 0; return -1; } int isListEmpty() { if(member_list.number ==0) return 0; return -1; }
#ifndef __MEMBER_H #define __MEMBER_H #define MAX_MEMBER_NUM 100 typedef struct tag_member { int type; char* name; }STRUCT_MEMBER; typedef struct tag_list { int number; STRUCT_MEMBER members[MAX_MEMBER_NUM]; }STRUCT_MEMBER_LIST; void init_member_list(); int member_regist(STRUCT_MEMBER member); STRUCT_MEMBER_LIST get_members(); int isExist(STRUCT_MEMBER member); int compare_member(STRUCT_MEMBER pre, STRUCT_MEMBER next); int isListEmpty(); #endif
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include "game.h" #include "engine.h" #include "message.h" #include "member.h" int gqid=0; void game_init() { engine_init(); gqid=message_init(); init_member_list(); game_show_frame(); signal(SIGINT, signal_handle); signal(EXIT_SIGNAL, signal_handle); } void game_start() { //create_thread((void*) game_snd_thread); //create_thread((void*) game_rcv_thread); //sleep(20); } void game_show_frame() { int x=0; int y=2; mvprintw(y,x,"+--------------------------------------------------------------------+"); mvprintw(y+20,x," +--------------------------------------------------------------------+"); mvprintw(y+22,x," [ ]"); } void game_refresh_member() { int i=0; int y =2; int x=75; STRUCT_MEMBER_LIST list; list = get_members(); mvprintw(y,x,"MEMBER %d", list.number); refresh(); // members for(i=0;i<list.number;i++) { mvprintw(++y,x, "[name= %s] [type= %d]",list.members[i].name, list.members[i].type); refresh(); } move(25,3); refresh(); } void game_run() { int count =0; int ch=0; for(;;) { STRUCT_MSG_BUF msg={0}; // game_receive_msg(&msg); game_refresh_member(); STRUCT_MEMBER member; member.type=count; member.name="test"; regist_member(member); ch=getch(); printw("%c", ch); raise(EXIT_SIGNAL); // sleep(2); count++; if(count > 5) { game_over(); } } } int game_abort(char* msg) { engine_shut(); fprintf(stderr, "%s ", msg); exit(EXIT_FAILURE); } // void game_over() { engine_shut(); exit(EXIT_SUCCESS); } void game_send_msg(char* pmsg_content) { STRUCT_MSG_BUF msg={0}; memset(&msg, 0, sizeof(STRUCT_MSG_BUF)); msg.length=5; strncpy(msg.data, pmsg_content, strlen(pmsg_content)); message_send(gqid, &msg, 0); } void game_receive_msg(STRUCT_MSG_BUF* pmsg) { if(pmsg != NULL) { memset(pmsg, 0, sizeof(STRUCT_MSG_BUF)); message_receive(gqid, pmsg, 0); } } void print_info() { } void signal_handle(int signal) { switch(signal) { case SIGINT: game_over(); break; case EXIT_SIGNAL: game_over(); break; default: break; } }
#ifndef __GAME_H #define __GAME_H #include "message.h" #define FRAME_ROW 0 #define FRAME_COL 0 #define MSG_BEGIN_COL 3 #define MSG_END_COL #define EXIT_SIGNAL 1818 typedef enum tag_mode { GAME_INIT, GAME_RUN, GAME_ABORT, GAME_OVER }ENUM_GAME_MODE; typedef struct tag_game { ENUM_GAME_MODE mode; int length; }STRUCT_GAME; void game_init(); void game_start(); int game_abort(char* msg); void game_over(); void game_run(); void game_send_msg(); void game_receive_msg(STRUCT_MSG_BUF* pmsg); void game_show_frame(); void signal_handle(int signal); #endif