//控制台简单贪吃蛇小游戏,蛇的移动需要玩家控制,不能自己移动
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Collections;
using System.Threading;
namespace C#简单贪吃蛇
{
public class SnakeCoordinate
{
public int x;
public int y;
public SnakeCoordinate()
{
}
public void Labyrinth(int[,] tmp)
{
int[,] num = new int[10, 10];
for (int i = 0; i < num.GetLength(0); i++)
{
for (int j = 0; j < num.GetLength(0); j++)
{
num[i, j] = tmp[i, j];
}
}
for (int i = 0; i < num.GetLength(0); i++)
{
for (int j = 0; j < num.GetLength(0); j++)
{
if (num[i, j] == 1)
{
Console.Write("■");
}
if (num[i, j] == 0)
{
Console.Write(" ");
}
if (num[i, j] == 2)
{
Console.Write("♀");
}
if (num[i, j] == 3)
{
Console.Write("◆");
}
}
Console.WriteLine();
}
}
//随机生成方块
public SnakeCoordinate RandeDiamonds(int[,] num, List<SnakeCoordinate> list)
{
Random tmp = new Random();
Random tmps = new Random();
bool bo = false;
int tmp1 = 0;
int tmp2 = 0;
while (true)
{
tmp1 = tmp.Next(0, num.GetLength(0));
tmp2 = tmps.Next(0, num.GetLength(1));
for (int i = 0; i < num.GetLength(0); i++)
{
for (int j = 0; j < num.GetLength(1); j++)
{
if (num[tmp1, tmp2] == 1)
{
bo = false;
break;
}
if (j == num.GetLength(1) - 1)
{
bo = true;
}
}
if (i == num.GetLength(0) - 1 && bo == true)
{
for (int k = 0; k < list.Count; k++)
{
if (list[k].x == tmp1 && list[k].y == tmp2)
{
bo = false;
break;
}
if (k == list.Count - 1)
{
return new SnakeCoordinate() { x = tmp1, y = tmp2 };
break;
}
}
}
if (bo == false)
{
break;
}
}
}
return new SnakeCoordinate() { x = tmp1, y = tmp2 };
}
//是否包含方块
public bool IsContains(int[,] num)
{
for (int i = 0; i < num.GetLength(0); i++)
{
for (int j = 0; j < num.GetLength(0); j++)
{
if (num[i, j] == 3)
{
return false;
break;
}
if (i == num.GetLength(0) - 1 && j == num.GetLength(1) - 1)
{
return true;
break;
}
}
}
return true;
}
//是否包含蛇自己
public bool IsIsContainsSelf(List<SnakeCoordinate> list, SnakeCoordinate snake)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i].x == snake.x && list[i].y == snake.y)
{
return false;
}
if (i == list.Count - 1)
{
return true;
}
}
return true;
}
//是否包含墙壁
public bool ISContainsCube(int[,] num, SnakeCoordinate snake)
{
if (num[snake.x, snake.y] == 1)
{
return false;
}
else
{
return true;
}
}
}
class Program
{
public static void Remove()
{
List<SnakeCoordinate> list = new List<SnakeCoordinate>();
int a = 0;
int b = 0;
bool bo;
int[,] num = new int[10, 10]
{
{1,1,1,1,1,1,1,1,1,1 },
{1,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,2,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1 },
{1,0,0,0,0,0,0,0,0,1 },
{1,1,1,1,1,1,1,1,1,1 }
};
for (int i = 0; i < num.GetLength(0); i++)
{
for (int j = 0; j < num.GetLength(0); j++)
{
if (num[i, j] == 1)
{
Console.Write("■");
}
if (num[i, j] == 0)
{
Console.Write(" ");
}
if (num[i, j] == 2)
{
list.Add(new SnakeCoordinate() { x = i, y = j });
a = i;
b = j;
Console.Write("♀");
}
if (num[i, j] == 3)
{
Console.Write("◆");
}
}
Console.WriteLine();
}
while (true)
{
if (list.Count == 63)
{
Console.WriteLine("game over,player victory");
break;
}
bo = false;
if (new SnakeCoordinate().IsContains(num))
{
//SnakeCoordinate sna = new SnakeCoordinate().RandeDiamonds(num, list);
num[new SnakeCoordinate().RandeDiamonds(num, list).x, new SnakeCoordinate().RandeDiamonds(num, list).y] = 3;
}
int aa = 0;
int bb = 0;
Console.WriteLine("按英文格式下w,s,a,d控制♀移动吃掉◆!");
Console.WriteLine(" 现在:{0}分",list.Count-1);
char str = Console.ReadKey().KeyChar;
Console.WriteLine();
//Thread.Sleep(1000);
if (str == 'w')
{
aa = -1;
}
if (str == 's')
{
aa = 1;
}
if (str == 'a')
{
bb = -1;
}
if (str == 'd')
{
bb = 1;
}
if (num[a + aa, b + bb] == 1)
{
Console.WriteLine("game over,player death");
RemoveAgain();
break;
}
else
{
if (num[a + aa, b + bb] == 3)
{
list.Add(new SnakeCoordinate() { x = a + aa, y = b + bb });
num[a + aa, b + bb] = 2;
new SnakeCoordinate().Labyrinth(num);
Console.Clear();
new SnakeCoordinate().Labyrinth(num);
bo = true;
}
else
{
if (new SnakeCoordinate().IsIsContainsSelf(list, new SnakeCoordinate() { x = a + aa, y = b + bb }) == true)
{
list.Add(new SnakeCoordinate() { x = a + aa, y = b + bb });
num[a + aa, b + bb] = 2;
SnakeCoordinate snake = list[0];
num[snake.x, snake.y] = 0;
list.RemoveAt(0);
new SnakeCoordinate().Labyrinth(num);
Console.Clear();
new SnakeCoordinate().Labyrinth(num);
bo = true;
}
else
{
Console.WriteLine("game over,player death");
RemoveAgain();
break;
}
}
if (bo == true)
{
a += aa;
b += bb;
}
}
}
}
static void Main(string[] args)
{
Inter();
Console.ReadKey();
}
public static void Inter()
{
Console.WriteLine("**************");
Console.WriteLine("* 请选择 *");
Console.WriteLine("* 1.开始游戏 *");
Console.WriteLine("* 2.退出 *");
Console.WriteLine("**************");
for (int i = 0; true; i++)
{
string str = Console.ReadLine();
int n=0;
if (int.TryParse(str,out n))
{
switch (n)
{
case 1:Remove();break;
case 2:Quit(); break;
default:
Console.WriteLine("输入不正确,请重新输入");
break;
}
}
else
{
Console.WriteLine("输入不正确,请重新输入");
}
}
}
public static void RemoveAgain()
{
Console.WriteLine("**************");
Console.WriteLine("* 请选择 *");
Console.WriteLine("* 1.重新开始 *");
Console.WriteLine("* 2.退出 *");
Console.WriteLine("**************");
for (int i = 0; true; i++)
{
string str = Console.ReadLine();
int n = 0;
if (int.TryParse(str, out n))
{
switch (n)
{
case 1: Remove(); break;
case 2: Quit(); break;
default:
Console.WriteLine("输入不正确,请重新输入");
break;
}
}
else
{
Console.WriteLine("输入不正确,请重新输入");
}
}
}
public static void Quit()
{
Console.WriteLine("退出游戏成功!");
}
}
}