贪吃蛇(AI)

写了一个贪吃蛇AI版的,是我写的基础,让AI改写出来的

#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

bool gameover;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;

void StartGame()
{
	gameover = false;
	dir = STOP;
	x = width / 2;
	y = height / 2;
	fruitX = rand() % width;
	fruitY = rand() % height;
	score = 0;
}

void Draw()
{
	system("cls");
	for (int i = 0; i < width + 2; i++)
		cout << "#";
	cout << endl;
	
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			if (j == 0)
				cout << "#";
			if (i == y && j == x)
				cout << "O";
			else if (i == fruitY && j == fruitX)
				cout << "F";
			else
			{
				bool printTail = false;
				for (int k = 0; k < nTail; k++)
				{
					if (tailX[k] == j && tailY[k] == i)
					{
						cout << "o";
						printTail = true;
					}
				}
				if (!printTail)
					cout << " ";
			}
			
			if (j == width - 1)
				cout << "#";
		}
		cout << endl;
	}
	
	for (int i = 0; i < width + 2; i++)
		cout << "#";
	cout << endl;
	cout << "Score:" << score << endl;
}

void Input()
{
	if (_kbhit())
	{
		switch (_getch())
{
case 'a':
	dir = LEFT;
	break;
case 'd':
	dir = RIGHT;
	break;
case 'w':
	dir = UP;
	break;
case 's':
	dir = DOWN;
	break;
case 'x':
	gameover = true;
	break;
}
	}
}

void Logic()
{
	int prevX = tailX[0];
	int prevY = tailY[0];
	int prev2X, prev2Y;
	tailX[0] = x;
	tailY[0] = y;
	
	for (int i = 1; i < nTail; i++)
	{
		prev2X = tailX[i];
		prev2Y = tailY[i];
		tailX[i] = prevX;
		tailY[i] = prevY;
		prevX = prev2X;
		prevY = prev2Y;
	}
	
	switch (dir)
{
case LEFT:
	x--;
	break;
case RIGHT:
	x++;
	break;
case UP:
	y--;
	break;
case DOWN:
	y++;
	break;
default:
	break;
}
	
	if (x >= width)
		x = 0;
	else if (x < 0)
		x = width - 1;
	
	if (y >= height)
		y = 0;
	else if (y < 0)
		y = height - 1;
	
	for (int i = 0; i < nTail; i++)
	{
		if (tailX[i] == x && tailY[i] == y)
		{
			gameover = true;
			break;
		}
	}
	
	if (x == fruitX && y == fruitY)
	{
		score += 10;
		fruitX = rand() % width;
		fruitY = rand() % height;
		nTail++;
	}
}

int main()
{
	StartGame();
	while (!gameover)
	{
		Draw();
		Input();
		Logic();
		Sleep(10);
	}
	return 0;
}

 

 

  • 16
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
作为一个 AI贪吃蛇需要具备以下能力: 1. 寻找食物:贪吃蛇需要知道当前场景中食物的位置,并能够计算出到达食物的最短路径。 2. 避免碰壁:贪吃蛇需要知道当前场景的边界,并且在移动时需要避免碰到边界。 3. 避免自杀:贪吃蛇需要知道自己的身体位置,并在移动时需要避免撞到自己的身体。 下面是一个简单的贪吃蛇 AI 实现: ```python import random class SnakeAI: def __init__(self, board, snake): self.board = board self.snake = snake def find_food(self): # 从当前位置出发,寻找最近的食物 food_pos = None min_distance = float('inf') for i in range(self.board.width): for j in range(self.board.height): if self.board.grid[i][j] == 'food': distance = abs(i - self.snake.head.x) + abs(j - self.snake.head.y) if distance < min_distance: min_distance = distance food_pos = (i, j) return food_pos def avoid_wall(self): # 判断当前位置是否靠近边界 if self.snake.head.x < 2 or self.snake.head.x > self.board.width - 3 \ or self.snake.head.y < 2 or self.snake.head.y > self.board.height - 3: return True else: return False def avoid_self(self): # 判断当前位置是否与自己的身体相撞 for part in self.snake.body[1:]: if self.snake.head.x == part.x and self.snake.head.y == part.y: return True return False def get_move(self): food_pos = self.find_food() if food_pos is None: # 如果没有食物,则随机移动 return random.choice(['up', 'down', 'left', 'right']) if self.snake.head.x < food_pos[0]: if not self.avoid_wall() and not self.avoid_self(): return 'right' elif self.snake.head.x > food_pos[0]: if not self.avoid_wall() and not self.avoid_self(): return 'left' elif self.snake.head.y < food_pos[1]: if not self.avoid_wall() and not self.avoid_self(): return 'down' elif self.snake.head.y > food_pos[1]: if not self.avoid_wall() and not self.avoid_self(): return 'up' # 如果无法直接到达食物,则随机移动 return random.choice(['up', 'down', 'left', 'right']) ``` 在上面的代码中,SnakeAI 类接受两个参数:board 和 snake,分别表示当前场景和贪吃蛇的状态。它实现了三个方法:find_food、avoid_wall 和 avoid_self。 find_food 方法用于寻找最近的食物,它遍历整个场景,找到距离当前位置最近的食物,返回其坐标。 avoid_wall 方法用于判断当前位置是否靠近边界,如果靠近边界则返回 True,否则返回 False。 avoid_self 方法用于判断当前位置是否与自己的身体相撞,如果相撞则返回 True,否则返回 False。 最后,get_move 方法根据当前位置和食物位置,决定贪吃蛇下一步的移动方向。如果能直接到达食物,则选择向食物移动;否则随机移动。在移动时,还需要检查是否靠近边界或与自己的身体相撞,如果是,则选择随机移动。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文宇炽筱

有一个打赏就多写十篇文章

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值