はじめての C

ソースコード game.2.c を改良して 敵との距離を表示する関数を使ったプログラムを作成せよ。

  1. /* game.3 */
  2. #include
  3. #include
  4. EnemyCource (int cource) {
  5. printf("(enemy cource)\n");
  6. if (cource == 0) {
  7. printf("< 0....5 >\n");
  8. printf(" |E-----|\n");
  9. } else if (cource == 1) {
  10. printf("< 0....5 >\n");
  11. printf(" |-E----|\n");
  12. } else if (cource == 2) {
  13. printf("< 0....5 >\n");
  14. printf(" |--E---|\n");
  15. } else if (cource == 3) {
  16. printf("< 0....5 >\n");
  17. printf(" |---E--|\n");
  18. } else if (cource == 4) {
  19. printf("< 0....5 >\n");
  20. printf(" |----E-|\n");
  21. } else {
  22. printf("< 0....5 >\n");
  23. printf(" |-----E|\n");
  24. }
  25. }
  26. EnemyDistance (int clength, int distance, char enemy) {
  27. printf("(enemy distance)\n");
  28. printf("< 8 6 4 2 0>\n");
  29. printf("|");
  30. for ( clength = 10 ; clength > 0 ; clength-- ) {
  31. if (clength == distance) {
  32. printf("%c", enemy);
  33. } else {
  34. printf("-");
  35. }
  36. }
  37. printf("Y|\n\n");
  38. }
  39. main()
  40. {
  41. int seed, distance, cource, goal, round, lazer, speed = 0;
  42. int clength;
  43. char enemy = 'E';
  44. printf("Input seed = ");
  45. scanf("%d", &seed);
  46. srand(seed);
  47. printf("\n");
  48. for ( round = 1 ; round < 6 ; round ++ ) {
  49. printf("+++ stage %d +++\n\n", round);
  50. cource = rand() % 6;
  51. speed++;
  52. for ( distance = 10 ; ; distance -= speed ) {
  53. if (distance <= 0) {
  54. printf("=== you lose (>_<) ===\n");
  55. exit(0);
  56. }
  57. if (cource == 0) {
  58. cource += rand() % 2;
  59. } else if (cource == 5) {
  60. cource += rand() % 2 - 1;
  61. } else {
  62. cource += rand() % 3 - 1;
  63. }
  64. EnemyDistance(clength, distance, enemy);
  65. printf("select '1'(lazer) or '2'(fight)\n");
  66. printf("Input your choice = ");
  67. scanf("%d", &lazer);
  68. printf("\n");
  69. if (lazer == 1 ) {
  70. EnemyCource(cource);
  71. } else if (lazer == 2) {
  72. printf("select lazer cource 0 - 5\n");
  73. printf("Input your choice = ");
  74. scanf("%d", &goal);
  75. if (goal == cource) {
  76. printf("\n");
  77. printf("=== you win ! ===\n\n");
  78. break;
  79. }
  80. printf("\n");
  81. printf("=== miss the target ===\n\n");
  82. } else {
  83. printf("set '1' or '2' only\n");
  84. }
  85. }
  86. }
  87. }

text: 金山典世さん(稚内北星)のページ