0
已采纳
臧启亚
初级光能
初级光能
0
0
0
邵逸儒
中级天翼
中级天翼
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main(){
int times = 0;
int mousey = 0, mousex= 0;
int num = 0;
srand(time(0));
int posy = 0,posx = 0;
int hits = 0,missed = 0;
int row = 0,col = 0;
//获取游戏次数
printf("请输入游戏次数:");
scanf("%d",&mousex);
//打印地图
printf("* * *\n* * *\n* * *\n");
//游戏过程
for(num = 1;num <= times;num++){
//获得老鼠和锤子的位置
mousey = rand() % 3 + 1;
mousex = rand() % 3 + 1;
do{
printf("请输入锤子的位置:");
scanf("%d %d",&posy,&posx);
}while(posy < 1 || posy > 3 || posx < 1 || posx > 3);
//修改打中和错过的个数
if(mousey == posy && mousex == posx){
hits++;
}else{
missed++;
}
//打印地图
for(row = 1;row <= 3;row++){
for(col = 1;col <= 3;col++){
if(row == posy && col == posx){
printf("O ");
}
else if(row == mousey && col == mousex){
printf("X ");
}
else{
printf("* ");
}
}
printf("\n");
}
//提示是否打中
if(mousey == posy && mousex == posx){
printf("恭喜你,打中了!\n");
}
else{
printf("很遗憾,没有打中\n");
}
//打印总成绩
printf("打中%d次,错过%d次\n",hits,missed);
}
return 0;
}
编译如下:
0