不成型贪吃蛇

瞎写的试手,日后改进。

#include<iostream>
#include<queue>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<vector>
#include<conio.h>
#include<Windows.h>
using namespace std;
char yidong='d';
int board[20][20];

int appx=8, appy=8;
struct she {
	int x;
	int y;
};
she cao, head;
vector<she> vi;
void tprintf() {
	for (int i = 0; i < 20; i++) {
		for (int j = 0; j < 20; j++) {
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
	printf("\n\n\n\n");
}
bool peng(struct she cao) {
	if (cao.x == appx && cao.y == appy)
		return true;
	else
		return false;
}
int main() {
	vector<she>::iterator it;
	for (int i = 0; i < 20; i++)
		for (int j = 0; j < 20; j++)
			board[i][j] = 32;
	board[appx][appy] = 36;
	head.x = 5;
	head.y = 5;
	board[head.x][head.y] = 64;;
	tprintf();
	vi.push_back(head);
	while (1) {
		//fflush(stdin);
		if (kbhit()) {
			fflush(stdin);
			//printf("reete");
			yidong = getche();
		}
		//yidong = getche();
		//getchar();
		if (yidong == 'd') {
			cao.x = head.x;
			cao.y = head.y + 1;
		}
		else if (yidong == 'a') {
			cao.x = head.x;
			cao.y = head.y - 1;
		}
		else if (yidong == 's') {
			cao.x = head.x + 1;
			cao.y = head.y;
		}
		else if (yidong == 'w') {
			cao.x = head.x - 1;
			cao.y = head.y;
		}
		
		board[cao.x][cao.y] = 64;
		head = cao;
		//printf("dfgdg\n");
		vi.insert(vi.begin(), cao);
		if (!peng(cao)) {
			it = vi.end() - 1;
			board[it->x][it->y] = 32;
			vi.pop_back();
		}
		Sleep(2000);
		tprintf();
	}
	
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值