#include "esc.h"
void display_guide(void)
{
printf("記号 @: ネズミ, ^ ^: ネコ, ( ): 穴\n");
printf("キー %c: 1歩戻る, %c: 1歩進む, %c: 3歩進む, %c: 5歩進む\n",
BACK, FWD1, FWD3, FWD5);
printf(" %c: 動かない, %c: 始めに戻る, %c: 中止\n",
PAUSE, RESET, STOP);
}
void display_scene(void)
{
char line[LENGTH + 3];
int i;
for (i = 1; i <= LENGTH; i++)
line[i] = '=';
line[0] = line[LENGTH + 1] = '|';
line[LENGTH + 2] = '\0';
line[HOLE - 1] = '('; line[HOLE] = ' '; line[HOLE + 1] = ')';
line[tom - 1] = line[tom + 1] = '^'; line[tom] = ' ';
line[jerry] = '@';
printf("%s\n", line);
}
void display_result(int game)
{
switch (game) {
case CAT_WINS:
printf("残念\n");
break;
case MOUSE_WINS:
printf("おめでとう\n");
break;
default:
printf("中止\n");
}
}
#include <stdio.h>
/* (1) ゲームの条件設定 */
#define LENGTH 54 /* 通路の長さ */
#define HOLE 50 /* 穴の位置 */
#define CAT 40 /* 初期のネコの位置 */
#define MOUSE 1 /* 初期のネズミの位置 */
/* (2) 状態変数の宣言 */
int tom, jerry; /* ネコ, ネズミの位置 */
/* (3) 状態コードの定義 */
#define IN_PROGRESS 0 /* 進行中 */
#define CAT_WINS 1 /* ネコの勝ち */
#define MOUSE_WINS 2 /* ネズミの勝ち */
/* (4) 入力キーの定義 */
#define BACK 'd' /* ネズミの動き 1歩後退 */
#define PAUSE 'f' /* 停止 */
#define FWD1 'j' /* 1歩前進 */
#define FWD3 'k' /* 3歩前進 */
#define FWD5 'l' /* 5歩前進 */
#define RESET 'r' /* 始めからやり直す */
#define STOP 's' /* ゲーム中止 */
/* (5) プログラムを構成する関数のプロトタイプ宣言 */
void display_guide(void); /* ガイドパネルを表示する */
void display_scene(void); /* ゲームの場面を表示する */
int mouse_runs_away(int); /* ネズミの移動 */
int runaway_distance(int); /* ネズミの移動距離を決める */
int cat_attacks(void); /* ネコの移動 */
int jump_distance(void); /* ネコの移動距離を決める */
void display_result(int); /* ゲーム結果の表示 */
main()
{
int cmd; /* キー入力を受ける */
int game; /* ゲームの状態コードを保持 */
display_guide();
tom = CAT; /* ネコと */
jerry = MOUSE; /* ネズミの位置の初期化 */
display_scene();
game = IN_PROGRESS; /* ゲームの状態を"進行中"に設定 */
while (game == IN_PROGRESS) {
cmd = getchar(); /* キー入力を受け付ける */
switch (cmd) { /* 入力されたキーに応じた処理を行う */
case RESET:
tom = CAT;
jerry = MOUSE;
game = IN_PROGRESS;
display_scene();
break;
case STOP:
goto stop;
break;
case BACK:
case PAUSE:
case FWD1:
case FWD3:
case FWD5:
game = mouse_runs_away(cmd);
if (game == IN_PROGRESS)
game = cat_attacks();
display_scene();
}
}
stop:
display_result(game);
}
/*
* ファイルを移動したので、その代り include を追加
*/
#include "display.c"
#include "jerry.c"
#include "tom.c"
int mouse_runs_away(int cmd)
{
int jerrys_aim;
jerrys_aim = jerry + runaway_distance(cmd);
if (jerrys_aim > LENGTH)
jerry = LENGTH;
else if (jerrys_aim < 1)
jerry = 1;
else
jerry = jerrys_aim;
if (jerry == tom)
return CAT_WINS;
else if (jerry == HOLE)
return MOUSE_WINS;
else
return IN_PROGRESS;
}
int runaway_distance(int cmd)
{
switch (cmd) {
case BACK:
return -1;
break;
case FWD1:
return 1;
break;
case FWD3:
return 3;
break;
case FWD5:
return 5;
break;
default:
return 0;
}
}
int cat_attacks(void)
{
int toms_aim;
toms_aim = tom + jump_distance();
if (toms_aim > LENGTH)
tom = LENGTH;
else if (toms_aim < 1)
tom = 1;
else
tom = toms_aim;
if (tom == jerry)
return CAT_WINS;
else
return IN_PROGRESS;
}
/* ネズミとネコの距離を計算し */
/* システム時間を用いてネコの飛びかかる距離をばらつかせ */
/* ネコの移動距離を決める関数 */
#include <time.h>
int jump_distance(void)
{
int d, rad;
{
time_t aft;
struct tm bdt;
aft = time(NULL);
bdt = *(localtime(&aft));
rad = 4 - bdt.tm_sec % 8;
}
d = (jerry - tom) + rad;
return d;
}