実現したいこと
以下のソースから将棋のプログラムを作成しています。makeを実行後、画像のように、
実行ファイルを実行して、画像の黄色いところで、エンター釦を押すと、エラーになります。
画像のターミナルに書かれたエラーです。
shougi_highlight.cの以下の"歩"がutf-8のファイルのため、2バイト以上の文字データのために
エラーになりますが、ファイルを全てshift-jisにすれば、よろしいでしょうか。
if (strcmp(piece, "歩") == 0) {
PDCursesWをインストールすれば、utf-8のファイルでもいいと言われましたが、
なぜか/c/pdcursesw/wincon/pdcursesw.aが作れず、pdcurses.aしかできず、
まだその方法もうまくできません。
何か解決のアドバイスをいただいてもよろしいですか。
・Makefile
# コンパイラの指定 CC=gcc # コンパイル時のオプション CFLAGS=-I. -Wall # 最終的な実行ファイル名 TARGET=myapp.exe LIBS = -lpdcurses # 最終ターゲット $(TARGET): shougi_main.o shougi_input.o shougi_boad.o shougi_highlight.o $(CC) -o $(TARGET) shougi_main.o shougi_input.o shougi_boad.o shougi_highlight.o $(LIBS) # shougi_main.cからshougi_main.oを生成 shougi_main.o: shougi_main.c $(CC) -c shougi_main.c $(CFLAGS) # shougi_input.cからshougi_input.oを生成 shougi_input.o: shougi_input.c $(CC) -c shougi_input.c $(CFLAGS) # shougi_boad.cからshougi_boad.oを生成 shougi_boad.o: shougi_boad.c $(CC) -c shougi_boad.c $(CFLAGS) # shougi_highlight.cからshougi_highlight.oを生成 shougi_highlight.o: shougi_highlight.c $(CC) -c shougi_highlight.c $(CFLAGS) # 'make clean' を実行した時に実行ファイルとオブジェクトファイルを削除 clean: rm -f $(TARGET) shougi_main.o shougi_input.o shougi_boad.o shougi_highlight.o
shougi_shisaku.h
h
1#ifndef SHOUGI_SHISAKU_DEF_H 2#define SHOUGI_SHISAKU_DEF_H 3typedef struct koma_info { 4 int tate; // 位置_縦 5 int yoko; // 位置_横 6 char koma[10]; // 駒の種類 7} person; 8 9typedef struct koma_shurui { 10 char koma[10]; // 駒の種類 11} motikoma; 12 13#define BOARD_SIZE 9 // 将棋は9x9 14 15extern int start_x; 16extern int start_y; 17 18#define CELL_W 5 19#define CELL_H 2 20 21// 画面上の盤左上のオフセット 22#define ORIGIN_Y (start_y + 1) 23#define ORIGIN_X (start_x + 2) 24#endif
・shougi_boad.c ```c #include "shougi_shisaku.h" #include <string.h> #include <pdcurses.h> void draw_board(); void set_board_from_dataretu(); extern void highlight_moves(int y, int x, const char *piece, int is_gote); extern person dataretu[81]; // 駒のデータの読み込み extern int is_selected; // 駒を選択中かどうか extern int from_x; // 選択した位置x extern int from_y; // 選択した位置y // 盤面を管理する2次元配列 // NULLなら空マス、文字列なら駒 extern const char *board[BOARD_SIZE][BOARD_SIZE]; extern const char *selected_piece; // 動かす対象の駒の種類 int start_y = 2; // 盤面の開始位置(縦方向) int start_x = 4; // 盤面の開始位置(横方向) int cursor_x = 5; // 初期カーソル位置(ファイル) int cursor_y = 5; // 初期カーソル位置(段) void draw_board() { clear(); // タイトル const char *title = "将棋ゲーム"; mvprintw(0, (COLS - strlen(title)) / 2, "%s", title); const char *kansuji[] = {"一", "二", "三", "四", "五", "六", "七", "八", "九"}; // 枠を描画 for (int y = 0; y <= BOARD_SIZE; y++) { for (int x = 0; x <= BOARD_SIZE; x++) { int py = start_y + y * CELL_H; int px = start_x + x * CELL_W; mvaddch(py, px, '+'); // 交点 if (x < BOARD_SIZE) { // 横線 for (int i = 1; i < CELL_W; i++) { mvaddch(py, px + i, '-'); } } if (y < BOARD_SIZE) { // 縦線 for (int j = 1; j < CELL_H; j++) { mvaddch(py + j, px, '|'); } } } } // 筋(上) — 右から左に 9 〜 1 for (int x = 0; x < BOARD_SIZE; x++) { int px = start_x + x * CELL_W + CELL_W / 2; mvprintw(start_y - 1, px, "%d", 9 - x); } // 段(右) — 上から下に 一〜九 for (int y = 0; y < BOARD_SIZE; y++) { int py = start_y + y * CELL_H + CELL_H / 2; int px = start_x + BOARD_SIZE * CELL_W + 2; // 右側に表示 mvprintw(py, px, "%s", kansuji[y]); } // 駒描画 for (int y = 0; y < BOARD_SIZE; y++) { for (int x = 0; x < BOARD_SIZE; x++) { int sy = ORIGIN_Y + y * CELL_H; int sx = ORIGIN_X + x * CELL_W; if (cursor_y - 1 == y && cursor_x - 1 == x) { attrset(COLOR_PAIR(2)); // 黒字+白背景 } else { attrset(COLOR_PAIR(1)); // 白字+黒背景 } // もし選択中で、元のマスがここなら空白(持ち上げ済み) if (is_selected && (from_y - 1) == y && (from_x - 1) == x) { mvaddstr(sy, sx, " "); // 元マスは空に見せる } else { // 通常はボード上の文字を出す mvaddstr(sy, sx, board[y][x] ? board[y][x] : " "); } // 駒(または空マス)を描画 mvaddstr(sy, sx, board[y][x] ? board[y][x] : " "); } } // もし駒を選択中なら、カーソル位置に選択中の駒を描画して追従表示(選択の視認性向上) if (is_selected && selected_piece != NULL) { int sy = ORIGIN_Y + (cursor_y - 1) * CELL_H; int sx = ORIGIN_X + (cursor_x - 1) * CELL_W; // attrset(COLOR_PAIR(3)); // 選択中は別色で描く(例: 白文字+青背景) // mvaddstr(sy, sx, selected_piece); highlight_moves(from_y - 1, from_x - 1, selected_piece, /*is_gote=*/0); attrset(A_NORMAL); } attrset(A_NORMAL); // 属性をリセット refresh(); } void set_board_from_dataretu() { for (int y = 0; y < BOARD_SIZE; y++) { for (int x = 0; x < BOARD_SIZE; x++) { int index = y * BOARD_SIZE + x; // 0〜80の通し番号 if (strlen(dataretu[index].koma) > 0) { // dataretu[index].moji が駒文字(例:"歩")なら代入 board[y][x] = (const char *)&dataretu[index].koma; // malloc確保してコピー } else { board[y][x] = NULL; // 空マス } } } }
・shougi_highlight.c
c
1#include <string.h> 2#include <pdcurses.h> 3#include "shougi_shisaku.h" 4 5void highlight_moves(int y, int x, const char *piece, int is_gote); 6 7typedef struct { 8 int dx; 9 int dy; 10} Direction; 11 12const Direction HO[] = { {0, -1} }; // 歩 13const Direction KYO[] = { {0, -1} }; // 香 14const Direction KEI[] = { {-1, -2}, {1, -2} }; // 桂 15const Direction GIN[] = { {-1,-1}, {0,-1}, {1,-1}, {-1,1}, {1,1} }; 16const Direction KIN[] = { {-1,-1}, {0,-1}, {1,-1}, {-1,0}, {1,0}, {0,1} }; 17const Direction OU[] = { {-1,-1}, {0,-1}, {1,-1}, {-1,0}, {1,0}, {-1,1}, {0,1}, {1,1} }; 18const Direction KAKU[]= { {-1,-1}, {1,-1}, {-1,1}, {1,1} }; 19const Direction HI[] = { {0,-1}, {0,1}, {-1,0}, {1,0} }; 20 21int get_moves(const char *piece, int is_gote, Direction moves[], int *max_steps) { 22 if (strcmp(piece, "歩") == 0) { 23 *max_steps = 1; 24 memcpy(moves, HO, sizeof(HO)); 25 } else if (strcmp(piece, "香") == 0) { 26 *max_steps = 8; 27 memcpy(moves, KYO, sizeof(KYO)); 28 } else if (strcmp(piece, "桂") == 0) { 29 *max_steps = 1; 30 memcpy(moves, KEI, sizeof(KEI)); 31 } else if (strcmp(piece, "銀") == 0) { 32 *max_steps = 1; 33 memcpy(moves, GIN, sizeof(GIN)); 34 } else if (strcmp(piece, "金") == 0) { 35 *max_steps = 1; 36 memcpy(moves, KIN, sizeof(KIN)); 37 } else if (strcmp(piece, "王") == 0) { 38 *max_steps = 1; 39 memcpy(moves, OU, sizeof(OU)); 40 } else if (strcmp(piece, "角") == 0) { 41 *max_steps = 8; 42 memcpy(moves, KAKU, sizeof(KAKU)); 43 } else if (strcmp(piece, "飛") == 0) { 44 *max_steps = 8; 45 memcpy(moves, HI, sizeof(HI)); 46 } else { 47 return 0; 48 } 49 50 // 後手なら縦方向反転 51 if (is_gote) { 52 for (int i = 0; i < 8; i++) { 53 moves[i].dy = -moves[i].dy; 54 } 55 } 56 57 return 1; 58} 59 60void highlight_moves(int y, int x, const char *piece, int 61 is_gote) { 62 Direction dirs[8]; 63 int max_steps; 64 if (!get_moves(piece, is_gote, dirs, &max_steps)) return; 65 66 for (int i = 0; i < 8; i++) { 67 for (int step = 1; step <= max_steps; step++) { 68 int ny = y + dirs[i].dy * step; 69 int nx = x + dirs[i].dx * step; 70 if (ny < 0 || ny >= BOARD_SIZE || nx < 0 || nx >= BOARD_SIZE) 71 break; 72 73 // ここでハイライト表示(PDCursesなら背景色変更) 74 attron(COLOR_PAIR(4)); 75 mvaddstr(ORIGIN_Y + ny * CELL_H, ORIGIN_X + nx * CELL_W, "□"); 76 attroff(COLOR_PAIR(4)); 77 } 78 } 79} 80
・shougi_main.c
https://www.mycompiler.io/view/Ht5quTklMci
・shougi_input.c
https://www.mycompiler.io/view/8iKKHPXAs0W
発生している問題・分からないこと
質問に詳細を記載した。
該当のソースコード
特になし
特になし
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
質問に詳細を記載した。
補足
特になし
