Guest User

Untitled

a guest
Apr 26th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.88 KB | None | 0 0
  1. #include "./dxlibp/dxlibp.h"
  2. #include "game.h"
  3. #include "pad.h"
  4. #include "main.h"
  5. #include "menu.h"
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. static int game_state;
  11. static int game_playing_state;
  12. static int pause_cursor;
  13.  
  14. #define BOARD_START_X   114
  15. #define BOARD_START_Y   10
  16. #define BOARD_DISTANCE  18
  17.  
  18. static char board[15][15];
  19.  
  20. enum BOARD_STATE {
  21.     BOARD_STATE_NONE,
  22.     BOARD_STATE_BLACK_STONE,
  23.     BOARD_STATE_WHITE_STONE,
  24. };
  25.  
  26. typedef struct _TAG_POSITION {
  27.     int x;
  28.     int y;
  29. } POSITION;
  30.  
  31. static POSITION mycursor = { 0, 0 };
  32.  
  33. static int turn;
  34.  
  35. enum BOARD_DIRECTION {
  36.     BOARD_DIRECTION_NONE            = 0,
  37.     BOARD_DIRECTION_UPPER           = 1,
  38.     BOARD_DIRECTION_UPPER_RIGHT     = 2,
  39.     BOARD_DIRECTION_RIGHT           = 3,
  40.     BOARD_DIRECTION_LOWER_RIGHT     = 4,
  41.     BOARD_DIRECTION_LOWER           = 5,
  42.     BOARD_DIRECTION_LOWER_LEFT      = 6,
  43.     BOARD_DIRECTION_LEFT            = 7,
  44.     BOARD_DIRECTION_UPPER_LEFT      = 8,
  45. };
  46.  
  47. int board_direction_list[3][3] = {
  48.     {BOARD_DIRECTION_UPPER_LEFT, BOARD_DIRECTION_UPPER, BOARD_DIRECTION_UPPER_RIGHT},
  49.     {BOARD_DIRECTION_LEFT      , BOARD_DIRECTION_NONE , BOARD_DIRECTION_RIGHT      },
  50.     {BOARD_DIRECTION_LOWER_LEFT, BOARD_DIRECTION_LOWER, BOARD_DIRECTION_LOWER_RIGHT}
  51. };
  52.  
  53. int board_direction_offset_x[9] = { 0, 0, 1, 1, 1, 0, -1, -1, -1 };
  54. int board_direction_offset_y[9] = { 0, -1, -1, 0, 1, 1, 1, 0, -1 };
  55.  
  56. enum GAME_FUNCTION_STATES {
  57.     GAME_STATE_ERROR        = 0,
  58.     GAME_STATE_INIT         = 1,
  59.     GAME_STATE_PLAY         = 2,
  60.     GAME_STATE_RESULT       = 3,
  61.     GAME_STATE_EXIT         = 4,
  62. };
  63.  
  64. enum GAME_PLAYING_STATES {
  65.     GAME_PLAYING_NORMAL     = 0,
  66.     GAME_PLAYING_PAUSE      = 1,
  67. };
  68.  
  69. //プロトタイプ
  70.  
  71. int game_setting(void);
  72. int game_playing(void);
  73. int game_playing_main(void);
  74. int game_result(void);
  75. int game_exit(void);
  76.  
  77. /*
  78.  *  初期化
  79.  * ---------------------------------------------------- */
  80.  
  81. int GameInit(void)
  82. {
  83.     pause_cursor = 0;
  84.     game_state = GAME_STATE_INIT;
  85.     return 0;
  86. }
  87.  
  88. /*
  89.  *  ゲームメイン
  90.  * ---------------------------------------------------- */
  91.  
  92. int GameMain(void)
  93. {
  94.     switch ( game_state ) {
  95.     case GAME_STATE_INIT:
  96.         return game_setting();
  97.  
  98.     case GAME_STATE_PLAY:
  99.         return game_playing();
  100.  
  101.     case GAME_STATE_RESULT:
  102.         return game_result();
  103.  
  104.     case GAME_STATE_EXIT:
  105.         return game_exit();
  106.  
  107.     case GAME_STATE_ERROR:
  108.     default:
  109.         func_state = FUNC_STATE_ERROR;
  110.         return -1;
  111.     }
  112.  
  113.     return 0;
  114. }
  115.  
  116. /*
  117.  *  ゲームを始める前の初期化
  118.  * ---------------------------------------------------- */
  119. int game_setting(void)
  120. {
  121.     PlaySoundMem( snd_menubgm, DX_PLAYTYPE_LOOP, TRUE );
  122.  
  123.     game_state = GAME_STATE_PLAY;
  124.     game_playing_state = GAME_PLAYING_NORMAL;
  125.     turn = 1;
  126.  
  127.     int i, j;
  128.     for ( i = 0; i < 15; ++i ) {
  129.         for ( j = 0; j < 15; ++j ) {
  130.             board[i][j] = BOARD_STATE_NONE;
  131.         }
  132.     }
  133.  
  134.     board[7][7] = BOARD_STATE_BLACK_STONE;
  135.  
  136.     mycursor.x = 7;
  137.     mycursor.y = 7;
  138.  
  139.     return 0;
  140. }
  141.  
  142. /*
  143.  *  ゲームが終わった時の後始末
  144.  * ---------------------------------------------------- */
  145. int game_exit(void)
  146. {
  147.     StopSoundMem( snd_bgm );
  148.     game_state = GAME_STATE_INIT;
  149.     func_state = FUNC_STATE_MENU;
  150.     pause_cursor = 0;
  151.     return 0;
  152. }
  153.  
  154. /*
  155.  *  リザルト画面
  156.  * ---------------------------------------------------- */
  157.  
  158.  
  159. static int result_state;
  160.  
  161. int game_result(void)
  162. {
  163.     char *winner[2] = {
  164.         "BLACK",
  165.         "WHITE"
  166.     };
  167.     DrawString( 100, 100, winner[turn], DXP_COLOR_WHITE, 0 );
  168.     DrawFormatString( 100, 140, DXP_COLOR_WHITE, "%d", result_state );
  169.  
  170.     if ( CheckNewKey(CIRCLE) )
  171.         game_state = GAME_STATE_EXIT;
  172.  
  173.     return 0;
  174. }
  175.  
  176. void game_pause_menu_returnGame(void)
  177. {
  178.     game_playing_state = GAME_PLAYING_NORMAL;
  179. }
  180.  
  181. void game_pause_menu_gotoMenu(void)
  182. {
  183.     game_state = GAME_STATE_EXIT;
  184.     game_playing_state = GAME_PLAYING_NORMAL;
  185. }
  186.  
  187. static MENU_ITEM item[GAME_PAUSE_MENU_ITEM_MAX_NUMBER] = {
  188.     { "Return Game", 200, 100, game_pause_menu_returnGame },
  189.     { "Back to Menu", 200, 150, game_pause_menu_gotoMenu },
  190. };
  191.  
  192. /*
  193.  *  ゲーム画面
  194.  * ---------------------------------------------------- */
  195.  
  196. int game_playing(void)
  197. {
  198.     switch ( game_playing_state ) {
  199.  
  200.         /* ----------------------------------------
  201.          *  通常のゲーム中
  202.          * ---------------------------------------- */
  203.         case GAME_PLAYING_NORMAL:
  204.         {
  205.             //ゲーム本体
  206.             game_playing_main();
  207.  
  208.             //ポーズ画面への移行
  209.             if ( CheckNewKey(START) ) game_playing_state = GAME_PLAYING_PAUSE;
  210.         }
  211.         break;
  212.                                  
  213.         /* ----------------------------------------
  214.          *  ポ-ズ画面
  215.          * ---------------------------------------- */
  216.         case GAME_PLAYING_PAUSE:
  217.         {
  218.             int i;
  219.             for ( i = 0; i < GAME_PAUSE_MENU_ITEM_MAX_NUMBER; ++i ) {
  220.                 DrawString(item[i].x, item[i].y, item[i].name, pause_cursor == i ? DXP_COLOR_SKYBLUE : DXP_COLOR_PLUM, DXP_COLOR_WHITE);
  221.             }
  222.  
  223.             //ゲームへ復帰
  224.             if ( CheckNewKey(START) ) {
  225.                 game_playing_state = GAME_PLAYING_NORMAL;
  226.             }
  227.  
  228.             if ( CheckNewKey(DOWN) ) {
  229.                 ++pause_cursor;
  230.                 pause_cursor = pause_cursor % GAME_PAUSE_MENU_ITEM_MAX_NUMBER;
  231.             }
  232.  
  233.             else if ( CheckNewKey(UP) ) {
  234.                 if ( --pause_cursor == -1 ) pause_cursor = GAME_PAUSE_MENU_ITEM_MAX_NUMBER - 1;
  235.             }
  236.  
  237.             if ( CheckNewKey(CIRCLE) ) {
  238.                 item[pause_cursor].func();
  239.                 return 1;
  240.             }
  241.         }
  242.         break;
  243.     }
  244.    
  245.     return 0;
  246. }
  247.  
  248.  
  249. /*
  250.  *  五目並べのの処理本体
  251.  * ---------------------------------------------------- */
  252.  
  253. //5目並んだかチェック
  254. int check_stone(int x, int y, int num, int direction, int stoneknd)
  255. {
  256.     if ( num >= 5 ) return direction;
  257.  
  258.     //初回は8つ全部調べる
  259.     if ( num == 1 ) {
  260.         int i, j, dx, dy;
  261.         for ( i = x - 1, dx = 0; i < x + 1; ++i, ++dx ) {
  262.             for ( j = y - 1, dy = 0; j < y + 1; ++j, ++dy ) {
  263.                 if ( i == -1 || i == 15 || j == -1 || j == 15 ) continue;
  264.                 if ( i == x && j == y ) continue;
  265.                
  266.                 if ( board[i][j] == stoneknd ) {
  267.                     int ret = check_stone( i, j, 2, board_direction_list[dx][dy], stoneknd);
  268.                     if ( ret != 0 ) return ret;
  269.                 }
  270.             }
  271.         }
  272.     //2回目以降は指定された方向だけ
  273.     } else {
  274.  
  275.         x += board_direction_offset_x[direction];
  276.         y += board_direction_offset_y[direction];
  277.  
  278.         if ( x + 1 == 0 || x == 15 || y + 1 == 0 || y == 15 ) return 0;
  279.  
  280.         /*
  281.         switch ( direction ) {
  282.         case BOARD_DIRECTION_UPPER:
  283.             if ( --y == -1 ) return 0;
  284.             break;
  285.         case BOARD_DIRECTION_UPPER_RIGHT:
  286.             if ( ++x == 15 ) return 0;
  287.             if ( --y == -1 ) return 0;
  288.             break;
  289.         case BOARD_DIRECTION_RIGHT:
  290.             if ( ++x == 15 ) return 0;
  291.             break;
  292.         case BOARD_DIRECTION_LOWER_RIGHT:
  293.             if ( ++x == 15 ) return 0;
  294.             if ( ++y == 15 ) return 0;
  295.             break;
  296.         case BOARD_DIRECTION_LOWER:
  297.             if ( ++y == 15 ) return 0;
  298.             break;
  299.         case BOARD_DIRECTION_LOWER_LEFT:
  300.             if ( --x == -1 ) return 0;
  301.             if ( ++y == 15 ) return 0;
  302.             break;
  303.         case BOARD_DIRECTION_LEFT:
  304.             if ( --x == -1 ) return 0;
  305.             break;
  306.         case BOARD_DIRECTION_UPPER_LEFT:
  307.             if ( --x == -1 ) return 0;
  308.             if ( --y == -1 ) return 0;
  309.             break;
  310.         }
  311.         */
  312.        
  313.         if ( board[x][y] == stoneknd )
  314.             return check_stone( x, y, ++num, direction, stoneknd );
  315.         else
  316.             return 0;
  317.     }
  318.    
  319.     return 0;
  320. }
  321.  
  322. //メイン処理
  323. int game_playing_main(void)
  324. {
  325.     if ( CheckNewKey(DOWN) )
  326.         mycursor.y = (++mycursor.y) % 15;
  327.    
  328.     else if ( CheckNewKey(UP) )
  329.         if ( --mycursor.y == -1 ) mycursor.y = 14;
  330.    
  331.     if ( CheckNewKey(RIGHT) )
  332.         mycursor.x = (++mycursor.x) % 15;
  333.    
  334.     else if ( CheckNewKey(LEFT) )
  335.         if ( --mycursor.x == -1 ) mycursor.x = 14;
  336.    
  337.     //背景
  338.     DrawGraph( 0, 0, img_background, TRUE );
  339.  
  340.     int i, j;
  341.     for ( i = 0; i < 15; ++i ) {
  342.         for ( j = 0; j < 15; ++j ) {
  343.             if ( board[i][j] == BOARD_STATE_BLACK_STONE ) {
  344.                 DrawRotaGraph(
  345.                     BOARD_START_X + i * BOARD_DISTANCE,
  346.                     BOARD_START_Y + j * BOARD_DISTANCE,
  347.                     1.0f,
  348.                     0.0f,
  349.                     img_stone[0],
  350.                     1,
  351.                     0
  352.                 );
  353.             } else if ( board[i][j] == BOARD_STATE_WHITE_STONE ) {
  354.                 DrawRotaGraph(
  355.                     BOARD_START_X + i * BOARD_DISTANCE,
  356.                     BOARD_START_Y + j * BOARD_DISTANCE,
  357.                     1.0f,
  358.                     0.0f,
  359.                     img_stone[1],
  360.                     1,
  361.                     0
  362.                 );
  363.             }
  364.         }
  365.     }
  366.  
  367.     SetDrawBlendMode( DX_BLENDMODE_ALPHA, 100 );
  368.  
  369.     //石
  370.     DrawRotaGraph(
  371.         BOARD_START_X + mycursor.x * BOARD_DISTANCE,
  372.         BOARD_START_Y + mycursor.y * BOARD_DISTANCE,
  373.         1.0f,
  374.         0.0f,
  375.         img_stone[turn],
  376.         1,
  377.         0
  378.     );
  379.    
  380.     SetDrawBlendMode( DX_BLENDMODE_NOBLEND, 255 );
  381.    
  382.     char *str[2] = {
  383.         "BLACK",
  384.         "WHITE"
  385.     };
  386.  
  387.     DrawString( 20, 250, str[turn], DXP_COLOR_WHITE, 0 );
  388.  
  389.     DrawFormatString( 20, 200, DXP_COLOR_WHITE, "turn : %d", turn );
  390.  
  391.     if ( CheckNewKey(CIRCLE) ) {
  392.         if ( board[mycursor.x][mycursor.y] == BOARD_STATE_NONE ) {
  393.             int stone_knd = turn  == 0 ? BOARD_STATE_BLACK_STONE : BOARD_STATE_WHITE_STONE;
  394.  
  395.             //石を置く
  396.             board[mycursor.x][mycursor.y] = stone_knd;
  397.  
  398.             result_state = check_stone( mycursor.x, mycursor.y, 1, 0, stone_knd );
  399.             if ( result_state != 0 ) {
  400.                 //終了!
  401.                 game_state = GAME_STATE_RESULT;
  402.             } else {
  403.                 //ターンチェンジ!
  404.                 turn ^= 1;
  405.             }
  406.         }
  407.     }
  408.  
  409.     DrawFormatString( 20, 220, DXP_COLOR_WHITE, "turn : %d", turn );
  410.  
  411.     return 0;
  412. }
Add Comment
Please, Sign In to add comment