推棋子

发布时间 2023-08-11 17:18:45作者: 陈若麟
#include <iostream>
#include <iomanip>
#include <Windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;
int main(){
	int h,l,a,b,c[3],d[3],e;
	bool t;
	h=16;
	l=16;
	a=10;b=10;
	char w;
	srand(time(0));
		for(int i=0;i<3;i++){
			c[i]=rand()%16;
			d[i]=rand()%16;
			cout<<c[i]<<"  "<<d[i]<<endl;
		}
		c[0]=3;
		d[0]=4;
	
	while(1){
		for(int i=0;i<h;i++){
			for(int j=0;j<l;j++){
				if(i==a&&j==b){
					cout<<setw(3)<<"@";
				}else{
					t=true;
					for(int i=0;i<3;i++){
						if(i==c[i]&&j==d[i]){
							cout<<setw(3)<<"口";
							t=false;
						}
					}
						if(t){
							cout<<setw(3)<<"O";
						}
						
				}
			}
			cout<<endl;
		}
		cout<<"wasd 移动棋子 q 退出"<<endl;
		cin>>w;
		//箱子移动
		for(int i=0;i<3;i++){
			if(a==c[i]&&b==d[i]){
				if(w=='w'||w=='W'){
					c[i]--;	
					if(c[i]<=0){
						c[i]=0;
					}
				}else if(w=='a'||w=='A'){
					d[i]--;
					if(d[i]<=0){
						d[i]=0;
					}
				}else if(w=='s'||w=='S'){
					c[i]++;		
					if(c[i]>=h){
						c[i]=h-1;
					}
				}else if(w=='d'||w=='D'){
					d[i]++;	
					if(d[i]>=h){
						d[i]=h-1;
					}
				}
			}
		}
		
		//棋子移动 
		if(w=='w'||w=='W'){
			a--;		
			if(a<=0){
				a=0;
			}
		}else if(w=='a'||w=='A'){
			b--;
			if(b<=0){
				b=0;
			}
		}else if(w=='s'||w=='S'){
			a++;		
			if(a>=h){
				a=h-1;
			}
		}else if(w=='d'||w=='D'){
			b++;	
			if(b>=h){
				b=h-1;
			}
		}else if(w=='q'||w=='Q'){
			break;
		}
		system("cls");
	}
	return 0;
}