#include <bits/stdc++.h> using namespace std; int maxH = 8,maxL = 8,l = maxH/2,h = maxL/2; void v(int h1,int l1){ for(int i = 0;i<maxH;i++){ for(int j = 0;j<maxL;j++){ if(i==h && j==l){ cout<<setw(3)<<"A"; }else if(i==h1 && j==l1){ cout<<setw(3)<<"B"; }else{ cout<<setw(3)<<"."; } } cout<<endl; } } int main(){ srand(time(0)); int h1 = rand()%8,l1 = rand()%8; char a; while(1){ v(h1,l1); cout<<"请输入方向 (w,a,s,d)"<<endl; cin>>a; if(a=='a'){ l--; if(l-1<0){ l = 0; } if(l==l1 && h==h1){ l1--; } }else if(a=='d'){ l++; if(l+1>maxH){ l = maxL-1; } if(l==l1 && h==h1){ l1++; } }else if(a=='w'){ h--; if(h-1<0){ h = 0; } if(h==h1 && l==l1){ h1--; } }else if(a=='s'){ h++; if(h+1>maxH){ h = maxH-1; } if(h==h1 && l==l1){ h1++; } } if(l1<0 || l1+1>maxL || h1<0 || h1+1>maxH){ system("cls"); v(h1,l1); break; } system("cls"); } return 0; }
https://demo.lanrenzhijia.com/demo/55/5518/demo/
标签:int,demo,h1,maxH,111,maxL,l1 From: https://www.cnblogs.com/wangyihang-xh/p/17615329.html