//名前 学籍番号 //潜水艦ゲームプログラム #include #include //srand() rand()関数のため #include //time()関数のため int main(void) { int cx,cy; int bx,by; int hit; int count; int error; //潜水艦の座標を乱数で生成 srand((unsigned int) time(NULL)); //現在時刻を元に乱数シードを生成 cx = rand()%10; cy = rand()%10; //rand()は 0からRAND_MAXの間の疑似乱数を返す hit = 0; //相手は沈んでいない //ループ開始 f##(count = 0; count < ##; count++){ //forで10回ループ printf("攻撃のx座標を入力してください。(0-9)"); scanf("%d", &bx); printf("攻撃のy座標を入力してください。(0-9)"); scanf("%d", &by); printf("攻撃回数 %d\n", (count+1)); //終了判定(あたり判定) i#((bx ## cx) && (by ## cy)){ //bx,by cx,cyは同じ座標の場合 printf("攻撃(%d,%d)は命中しました!\n", bx, by); printf("相手の潜水艦を撃沈しました。【YOU WIN!】\n"); hit = 1; //相手を撃沈 break;//ループを抜ける } //誤差表示 printf("攻撃(%d,%d)は外れました・・・\n", bx, by); error = abs(bx-cx) + abs(by-cy); //abs()は括弧の中が負の値の場合、正の値にして返す printf("相手潜水艦との誤差は%d\n", error); }//forループ終わり i#(#hit){ //相手が沈んでいなければ(not hit) printf("相手からの攻撃でこちらの戦艦が撃沈されました。【YOU LOSE!】\n"); } }//main終わり