最初はポインタを使ってなにかしたいなーーと思った。
特に文字コードで何かやりたかった。
しかし気がついたら黒い画面の上で戦ってた。
構造体とかswitchとか色々と詰め込みたいものを詰め込んでいた。
途中で設定に飽きた。
色々とやりたかったことはある。
そのうち気力がでたら綺麗にしたい。
というわけで体力、攻撃力、防御力、回復力というステータスを設定して、
敵と戦っている気になれるものをつくった。
なお途中で疲れたため能力値の調整が不十分、よってたまにマイナスが出る。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
typedef struct{
char name[20];
int life;
int attack;
int defense;
int cure;
}status;
status cha();
status ene();
void battle(status *prof, char*);
int main(void){
status profile;
char name[20];
int command;
printf("▶名前を入力してください\n");
scanf("%s", name);
printf("あなたの名前は%sです\n",name);
profile = cha();
battle(&profile, name);
if (profile.life <= 0){
printf("%sの体力が0になった\nYou lose\n", name);
}
else{
printf("You win\n");
}
return 0;
}
void battle(status *prof, char *player_name){
int command;
int damage;
int attack;
double power;
status enemy;
enemy = ene();
while(prof->life > 0 && enemy.life >0){
damage = 0;
printf("現在の体力:%d\n", prof->life);
printf("敵の体力:%d\n", enemy.life);
printf("▶どうする?\n攻撃:1,突進:2,全力を出す:3,休む:4\n");
scanf("%d", &command);
switch(command){
case 1:
printf("%sの攻撃!\n", player_name);
srand((unsigned int)time(NULL));
power = 0.8 + (rand()*(2.2)/(1.0+RAND_MAX));
attack = (int)prof->attack*power*0.6-(int)enemy.defense*0.1;
enemy.life -= attack;
printf("敵に%dのダメージ\n", attack);
break;
case 2:
attack = (int)prof->attack*1.2-(int)enemy.defense*0.1;
enemy.life -= attack;
printf("敵に%dのダメージ\n", attack);
prof->life -= (int)attack / 2;
printf("反動で%dのダメージを受けた!\n", (int)enemy.attack/2);
break;
case 3:
prof->attack *= 1.5;
prof->defense *= 1.5;
prof->cure *= 1.5;
printf("%sは本気を出した\n", player_name);
break;
case 4:
prof->life += (int)prof->cure*0.8;
printf("体力が%d回復した\n", (int)(prof->cure*0.8));
break;
default:
continue;
}
if(enemy.life>0){
int act;
srand((unsigned int)time(NULL));
act = 1+(int)(rand()*4.0/(1.0+RAND_MAX));
switch(act){
case 1:
power=0.8+(rand()*(1.4)/(1.0+ RAND_MAX));
damage += (int)enemy.attack*power-(int)prof->defense*0.1;
prof->life -= damage;
printf("敵の攻撃!!\n%sは%dのダメージを受けた\n", player_name,damage);
break;
case 2:
enemy.defense *= 1.2;
printf("敵が固くなった!!\n敵の防御力が上がった\n");
break;
case 3:
enemy.life += (int)enemy.cure * 0.8;
printf("敵の休息!!\n敵の体力が%d回復した\n", (int)(enemy.cure * 0.8));
break;
case 4:
printf("敵は様子を見ている\n");
break;
}
}
else{
printf("敵の体力が0になった\n");
}
}
}
status cha(){
int character;
int i=0;
while(i==0){
printf("▶どのキャラクターにしますか?\natacker:1,shielder:2,priest:3\n");
scanf("%d", &character);
status chara;
switch(character){
case 1:
strcpy(chara.name, "power");
chara.life = 120;
chara.attack = 65;
chara.defense = 36;
chara.cure = 40;
i++;
break;
case 2:
strcpy(chara.name, "shielder");
chara.life = 150;
chara.attack = 30;
chara.defense = 80;
chara.cure = 50;
i++;
break;
case 3:
strcpy(chara.name, "priest");
chara.life = 200;
chara.attack = 30;
chara.defense = 40;
chara.cure = 60;
i++;
break;
default:
printf("誤った入力です\n");
continue;
}
return chara;
}
}
status ene(){
double power;
static int flag;
if (flag == 0){
srand((unsigned int)time(NULL));
flag = 1;
}
power = 0.8+(rand()*2.0/(1.0+RAND_MAX));
status enemy;
strcpy(enemy.name, "enemy");
enemy.life = 140*power;
enemy.attack = 30*power;
enemy.defense = 30*power;
enemy.cure = 10*power;
return enemy;
}