Skip to content

Instantly share code, notes, and snippets.

@Coocla33
Created November 28, 2019 12:55
Show Gist options
  • Save Coocla33/fc62590cd05e5f6973164d6d897735f9 to your computer and use it in GitHub Desktop.
Save Coocla33/fc62590cd05e5f6973164d6d897735f9 to your computer and use it in GitHub Desktop.
Alsjeblieft knakker
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Snake
{
class Program
{
static readonly int gridX = 61;
static readonly int gridY = 21;
static readonly int speed = 1;
static Cell[, ] grid = new Cell[gridX, gridY];
static Cell lastCell;
static Snake snake;
static void Main(string[] args)
{
restart();
}
static void restart() {
snake = new Snake();
lastCell = new Cell();
populateGrid();
generateFood();
snake.start();
getInput();
}
static void getInput() {
ConsoleKeyInfo input;
while (!Console.KeyAvailable)
{
snake.move();
updateScreen();
Thread.Sleep(speed * 100);
}
input = Console.ReadKey();
doInput(input.KeyChar);
}
static void updateScreen() {
Console.SetCursorPosition(0, 0);
printGrid();
Console.WriteLine("Current direction: " + snake.direction + " ");
Console.WriteLine("Score: " + snake.score + " ");
Console.WriteLine("Length: " + snake.length + " ");
Console.WriteLine("Food Eaten: " + (snake.length - 3) + " ");
}
static void doInput(char input) {
switch (input) {
case 'w':
if (snake.direction != "down") {
changeDirection("up");
}
break;
case 'a':
if (snake.direction != "right") {
changeDirection("left");
}
break;
case 's':
if (snake.direction != "up") {
changeDirection("down");
}
break;
case 'd':
if (snake.direction != "left") {
changeDirection("right");
}
break;
}
getInput();
}
static void changeDirection(string type) {
if (type != snake.direction) {
snake.direction = type;
}
}
static void generateFood() {
Random r = new Random();
Cell cell;
while (true) {
// Get random cell in grid
cell = grid[r.Next(gridX), r.Next(gridY)];
if (cell.value == ' ') {
cell.value = 'O';
grid[cell.x, cell.y] = cell;
break;
}
}
}
static void populateGrid() {
// Generate Grid
for (int x = 0; x < gridX; x++)
{
for (int y = 0; y < gridY; y++)
{
// Create and setup cell
Cell cell = new Cell();
cell.x = x;
cell.y = y;
cell.visited = false;
// Check if cell is wall
if (cell.x == 0 || cell.x == gridX - 1 || cell.y == 0 || cell.y == gridY - 1)
{
cell.value = '#';
}
else
{
cell.value = ' ';
}
// Set cell in grid
grid[x, y] = cell;
}
}
}
static void printGrid() {
string toPrint = "";
// Go trough each x and y coordinate
for (int y = 0; y < gridY; y++)
{
for (int x = 0; x < gridX; x++)
{
// Decay the remaining snake parts
grid[x, y].decayCell();
// add to toprint value of current x,y
toPrint += grid[x, y].value;
}
// Add new line on new y
toPrint += "\n";
}
Console.WriteLine(toPrint);
}
public class Cell
{
public int x { get; set; }
public int y { get; set; }
public char value { get; set; }
public int decay { get; set; }
public bool visited { get; set; }
public void set(int width, int height) {
x = width;
y = height;
}
public void decayCell() {
decay -= 1;
if (decay == 0) {
value = ' ';
visited = false;
}
}
}
public class Snake
{
public int x { get; set; }
public int y { get; set; }
public int length { get; set; }
public int score { get; set; }
public string direction { get; set; }
public void start() {
setPos(gridX / 2, gridY / 2);
direction = "up";
length = 3;
score = 0;
}
private void setPos(int newX, int newY, char head = '^') {
Cell cell = new Cell();
cell.x = x;
cell.y = y;
cell.value = ' ';
grid[x, y] = cell;
x = newX;
y = newY;
// Check if new position = wall
if (grid[x, y].value == '#')
{
restart();
}
else if (grid[x, y].value == 'O')
{
score += (2 * length);
length += 1;
generateFood();
}
else if (grid[x, y].visited)
{
restart();
}
if (lastCell.x != 0 && lastCell.y != 0) {
lastCell.value = '+';
lastCell.decay = length;
grid[lastCell.x, lastCell.y] = lastCell;
}
cell = new Cell();
cell.x = x;
cell.y = y;
cell.value = head;
cell.visited = true;
grid[x, y] = cell;
lastCell = cell;
}
public void move() {
switch (direction) {
case "up":
setPos(x, y - 1, '^');
break;
case "left":
setPos(x - 1, y, '<');
break;
case "down":
setPos(x, y + 1, 'v');
break;
case "right":
setPos(x + 1, y, '>');
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment