Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "./dxlibp/dxlibp.h"
- #include "game.h"
- #include "pad.h"
- #include "main.h"
- #include "menu.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- static int game_state;
- static int game_playing_state;
- static int pause_cursor;
- #define BOARD_START_X 114
- #define BOARD_START_Y 10
- #define BOARD_DISTANCE 18
- static char board[15][15];
- enum BOARD_STATE {
- BOARD_STATE_NONE,
- BOARD_STATE_BLACK_STONE,
- BOARD_STATE_WHITE_STONE,
- };
- typedef struct _TAG_POSITION {
- int x;
- int y;
- } POSITION;
- static POSITION mycursor = { 0, 0 };
- static int turn;
- enum BOARD_DIRECTION {
- BOARD_DIRECTION_NONE = 0,
- BOARD_DIRECTION_UPPER = 1,
- BOARD_DIRECTION_UPPER_RIGHT = 2,
- BOARD_DIRECTION_RIGHT = 3,
- BOARD_DIRECTION_LOWER_RIGHT = 4,
- BOARD_DIRECTION_LOWER = 5,
- BOARD_DIRECTION_LOWER_LEFT = 6,
- BOARD_DIRECTION_LEFT = 7,
- BOARD_DIRECTION_UPPER_LEFT = 8,
- };
- int board_direction_list[3][3] = {
- {BOARD_DIRECTION_UPPER_LEFT, BOARD_DIRECTION_UPPER, BOARD_DIRECTION_UPPER_RIGHT},
- {BOARD_DIRECTION_LEFT , BOARD_DIRECTION_NONE , BOARD_DIRECTION_RIGHT },
- {BOARD_DIRECTION_LOWER_LEFT, BOARD_DIRECTION_LOWER, BOARD_DIRECTION_LOWER_RIGHT}
- };
- int board_direction_offset_x[9] = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
- int board_direction_offset_y[9] = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };
- enum GAME_FUNCTION_STATES {
- GAME_STATE_ERROR = 0,
- GAME_STATE_INIT = 1,
- GAME_STATE_PLAY = 2,
- GAME_STATE_RESULT = 3,
- GAME_STATE_EXIT = 4,
- };
- enum GAME_PLAYING_STATES {
- GAME_PLAYING_NORMAL = 0,
- GAME_PLAYING_PAUSE = 1,
- };
- //プロトタイプ
- int game_setting(void);
- int game_playing(void);
- int game_playing_main(void);
- int game_result(void);
- int game_exit(void);
- /*
- * 初期化
- * ---------------------------------------------------- */
- int GameInit(void)
- {
- pause_cursor = 0;
- game_state = GAME_STATE_INIT;
- return 0;
- }
- /*
- * ゲームメイン
- * ---------------------------------------------------- */
- int GameMain(void)
- {
- switch ( game_state ) {
- case GAME_STATE_INIT:
- return game_setting();
- case GAME_STATE_PLAY:
- return game_playing();
- case GAME_STATE_RESULT:
- return game_result();
- case GAME_STATE_EXIT:
- return game_exit();
- case GAME_STATE_ERROR:
- default:
- func_state = FUNC_STATE_ERROR;
- return -1;
- }
- return 0;
- }
- /*
- * ゲームを始める前の初期化
- * ---------------------------------------------------- */
- int game_setting(void)
- {
- PlaySoundMem( snd_menubgm, DX_PLAYTYPE_LOOP, TRUE );
- game_state = GAME_STATE_PLAY;
- game_playing_state = GAME_PLAYING_NORMAL;
- turn = 1;
- int i, j;
- for ( i = 0; i < 15; ++i ) {
- for ( j = 0; j < 15; ++j ) {
- board[i][j] = BOARD_STATE_NONE;
- }
- }
- board[7][7] = BOARD_STATE_BLACK_STONE;
- mycursor.x = 7;
- mycursor.y = 7;
- return 0;
- }
- /*
- * ゲームが終わった時の後始末
- * ---------------------------------------------------- */
- int game_exit(void)
- {
- StopSoundMem( snd_bgm );
- game_state = GAME_STATE_INIT;
- func_state = FUNC_STATE_MENU;
- pause_cursor = 0;
- return 0;
- }
- /*
- * リザルト画面
- * ---------------------------------------------------- */
- static int result_state;
- int game_result(void)
- {
- char *winner[2] = {
- "BLACK",
- "WHITE"
- };
- DrawString( 100, 100, winner[turn], DXP_COLOR_WHITE, 0 );
- DrawFormatString( 100, 140, DXP_COLOR_WHITE, "%d", result_state );
- if ( CheckNewKey(CIRCLE) )
- game_state = GAME_STATE_EXIT;
- return 0;
- }
- void game_pause_menu_returnGame(void)
- {
- game_playing_state = GAME_PLAYING_NORMAL;
- }
- void game_pause_menu_gotoMenu(void)
- {
- game_state = GAME_STATE_EXIT;
- game_playing_state = GAME_PLAYING_NORMAL;
- }
- static MENU_ITEM item[GAME_PAUSE_MENU_ITEM_MAX_NUMBER] = {
- { "Return Game", 200, 100, game_pause_menu_returnGame },
- { "Back to Menu", 200, 150, game_pause_menu_gotoMenu },
- };
- /*
- * ゲーム画面
- * ---------------------------------------------------- */
- int game_playing(void)
- {
- switch ( game_playing_state ) {
- /* ----------------------------------------
- * 通常のゲーム中
- * ---------------------------------------- */
- case GAME_PLAYING_NORMAL:
- {
- //ゲーム本体
- game_playing_main();
- //ポーズ画面への移行
- if ( CheckNewKey(START) ) game_playing_state = GAME_PLAYING_PAUSE;
- }
- break;
- /* ----------------------------------------
- * ポ-ズ画面
- * ---------------------------------------- */
- case GAME_PLAYING_PAUSE:
- {
- int i;
- for ( i = 0; i < GAME_PAUSE_MENU_ITEM_MAX_NUMBER; ++i ) {
- DrawString(item[i].x, item[i].y, item[i].name, pause_cursor == i ? DXP_COLOR_SKYBLUE : DXP_COLOR_PLUM, DXP_COLOR_WHITE);
- }
- //ゲームへ復帰
- if ( CheckNewKey(START) ) {
- game_playing_state = GAME_PLAYING_NORMAL;
- }
- if ( CheckNewKey(DOWN) ) {
- ++pause_cursor;
- pause_cursor = pause_cursor % GAME_PAUSE_MENU_ITEM_MAX_NUMBER;
- }
- else if ( CheckNewKey(UP) ) {
- if ( --pause_cursor == -1 ) pause_cursor = GAME_PAUSE_MENU_ITEM_MAX_NUMBER - 1;
- }
- if ( CheckNewKey(CIRCLE) ) {
- item[pause_cursor].func();
- return 1;
- }
- }
- break;
- }
- return 0;
- }
- /*
- * 五目並べのの処理本体
- * ---------------------------------------------------- */
- //5目並んだかチェック
- int check_stone(int x, int y, int num, int direction, int stoneknd)
- {
- if ( num >= 5 ) return direction;
- //初回は8つ全部調べる
- if ( num == 1 ) {
- int i, j, dx, dy;
- for ( i = x - 1, dx = 0; i < x + 1; ++i, ++dx ) {
- for ( j = y - 1, dy = 0; j < y + 1; ++j, ++dy ) {
- if ( i == -1 || i == 15 || j == -1 || j == 15 ) continue;
- if ( i == x && j == y ) continue;
- if ( board[i][j] == stoneknd ) {
- int ret = check_stone( i, j, 2, board_direction_list[dx][dy], stoneknd);
- if ( ret != 0 ) return ret;
- }
- }
- }
- //2回目以降は指定された方向だけ
- } else {
- x += board_direction_offset_x[direction];
- y += board_direction_offset_y[direction];
- if ( x + 1 == 0 || x == 15 || y + 1 == 0 || y == 15 ) return 0;
- /*
- switch ( direction ) {
- case BOARD_DIRECTION_UPPER:
- if ( --y == -1 ) return 0;
- break;
- case BOARD_DIRECTION_UPPER_RIGHT:
- if ( ++x == 15 ) return 0;
- if ( --y == -1 ) return 0;
- break;
- case BOARD_DIRECTION_RIGHT:
- if ( ++x == 15 ) return 0;
- break;
- case BOARD_DIRECTION_LOWER_RIGHT:
- if ( ++x == 15 ) return 0;
- if ( ++y == 15 ) return 0;
- break;
- case BOARD_DIRECTION_LOWER:
- if ( ++y == 15 ) return 0;
- break;
- case BOARD_DIRECTION_LOWER_LEFT:
- if ( --x == -1 ) return 0;
- if ( ++y == 15 ) return 0;
- break;
- case BOARD_DIRECTION_LEFT:
- if ( --x == -1 ) return 0;
- break;
- case BOARD_DIRECTION_UPPER_LEFT:
- if ( --x == -1 ) return 0;
- if ( --y == -1 ) return 0;
- break;
- }
- */
- if ( board[x][y] == stoneknd )
- return check_stone( x, y, ++num, direction, stoneknd );
- else
- return 0;
- }
- return 0;
- }
- //メイン処理
- int game_playing_main(void)
- {
- if ( CheckNewKey(DOWN) )
- mycursor.y = (++mycursor.y) % 15;
- else if ( CheckNewKey(UP) )
- if ( --mycursor.y == -1 ) mycursor.y = 14;
- if ( CheckNewKey(RIGHT) )
- mycursor.x = (++mycursor.x) % 15;
- else if ( CheckNewKey(LEFT) )
- if ( --mycursor.x == -1 ) mycursor.x = 14;
- //背景
- DrawGraph( 0, 0, img_background, TRUE );
- int i, j;
- for ( i = 0; i < 15; ++i ) {
- for ( j = 0; j < 15; ++j ) {
- if ( board[i][j] == BOARD_STATE_BLACK_STONE ) {
- DrawRotaGraph(
- BOARD_START_X + i * BOARD_DISTANCE,
- BOARD_START_Y + j * BOARD_DISTANCE,
- 1.0f,
- 0.0f,
- img_stone[0],
- 1,
- 0
- );
- } else if ( board[i][j] == BOARD_STATE_WHITE_STONE ) {
- DrawRotaGraph(
- BOARD_START_X + i * BOARD_DISTANCE,
- BOARD_START_Y + j * BOARD_DISTANCE,
- 1.0f,
- 0.0f,
- img_stone[1],
- 1,
- 0
- );
- }
- }
- }
- SetDrawBlendMode( DX_BLENDMODE_ALPHA, 100 );
- //石
- DrawRotaGraph(
- BOARD_START_X + mycursor.x * BOARD_DISTANCE,
- BOARD_START_Y + mycursor.y * BOARD_DISTANCE,
- 1.0f,
- 0.0f,
- img_stone[turn],
- 1,
- 0
- );
- SetDrawBlendMode( DX_BLENDMODE_NOBLEND, 255 );
- char *str[2] = {
- "BLACK",
- "WHITE"
- };
- DrawString( 20, 250, str[turn], DXP_COLOR_WHITE, 0 );
- DrawFormatString( 20, 200, DXP_COLOR_WHITE, "turn : %d", turn );
- if ( CheckNewKey(CIRCLE) ) {
- if ( board[mycursor.x][mycursor.y] == BOARD_STATE_NONE ) {
- int stone_knd = turn == 0 ? BOARD_STATE_BLACK_STONE : BOARD_STATE_WHITE_STONE;
- //石を置く
- board[mycursor.x][mycursor.y] = stone_knd;
- result_state = check_stone( mycursor.x, mycursor.y, 1, 0, stone_knd );
- if ( result_state != 0 ) {
- //終了!
- game_state = GAME_STATE_RESULT;
- } else {
- //ターンチェンジ!
- turn ^= 1;
- }
- }
- }
- DrawFormatString( 20, 220, DXP_COLOR_WHITE, "turn : %d", turn );
- return 0;
- }
Add Comment
Please, Sign In to add comment