はじめての C
ソースコード game.2.c を改良して 敵との距離を表示する関数を使ったプログラムを作成せよ。
- /* game.3 */
- #include
- #include
- EnemyCource (int cource) {
- printf("(enemy cource)\n");
- if (cource == 0) {
- printf("< 0....5 >\n");
- printf(" |E-----|\n");
- } else if (cource == 1) {
- printf("< 0....5 >\n");
- printf(" |-E----|\n");
- } else if (cource == 2) {
- printf("< 0....5 >\n");
- printf(" |--E---|\n");
- } else if (cource == 3) {
- printf("< 0....5 >\n");
- printf(" |---E--|\n");
- } else if (cource == 4) {
- printf("< 0....5 >\n");
- printf(" |----E-|\n");
- } else {
- printf("< 0....5 >\n");
- printf(" |-----E|\n");
- }
- }
- EnemyDistance (int clength, int distance, char enemy) {
- printf("(enemy distance)\n");
- printf("< 8 6 4 2 0>\n");
- printf("|");
- for ( clength = 10 ; clength > 0 ; clength-- ) {
- if (clength == distance) {
- printf("%c", enemy);
- } else {
- printf("-");
- }
- }
- printf("Y|\n\n");
- }
- main()
- {
- int seed, distance, cource, goal, round, lazer, speed = 0;
- int clength;
- char enemy = 'E';
- printf("Input seed = ");
- scanf("%d", &seed);
- srand(seed);
- printf("\n");
- for ( round = 1 ; round < 6 ; round ++ ) {
- printf("+++ stage %d +++\n\n", round);
- cource = rand() % 6;
- speed++;
- for ( distance = 10 ; ; distance -= speed ) {
- if (distance <= 0) {
- printf("=== you lose (>_<) ===\n");
- exit(0);
- }
- if (cource == 0) {
- cource += rand() % 2;
- } else if (cource == 5) {
- cource += rand() % 2 - 1;
- } else {
- cource += rand() % 3 - 1;
- }
- EnemyDistance(clength, distance, enemy);
- printf("select '1'(lazer) or '2'(fight)\n");
- printf("Input your choice = ");
- scanf("%d", &lazer);
- printf("\n");
- if (lazer == 1 ) {
- EnemyCource(cource);
- } else if (lazer == 2) {
- printf("select lazer cource 0 - 5\n");
- printf("Input your choice = ");
- scanf("%d", &goal);
- if (goal == cource) {
- printf("\n");
- printf("=== you win ! ===\n\n");
- break;
- }
- printf("\n");
- printf("=== miss the target ===\n\n");
- } else {
- printf("set '1' or '2' only\n");
- }
- }
- }
- }
text: 金山典世さん(稚内北星)のページ