i need the snake not to pass by the walls...can anyone help me?
here is the progam.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace TikTak
{
class TestCon
{
[DllImport("msvcrt")]
static extern int _getch();
static void Main()
{
ConsoleHelper ch = new ConsoleHelper();
char move;
short x = 1, y = 1;
string[,] maze = new string[15, 15];
short a, b;
for (a = 0; a <= 14; a++)
{
for (b = 0; b <= 14; b++)
{
maze[a, b] = "x";
Console.Write("{0}", maze[a, b]);
}
Console.Write("\n");
}
for (a = 1; a < 14; a++)
{
for (b = 1; b < 14; b++)
{
if (a == 1 || a == 3 || a == 5 || a == 7 || a == 9 || a == 11 || a == 13 || (a == 2 && b == 13)
|| (a == 4 && b == 1) || (a == 6 && b == 13) || (a == 8 && b == 1) ||
(a == 10 && b == 13) || (a == 12 && b == 1) || (a == 14 && b == 2))
{
ch.gotoxy(b, a);
maze[a, b] = "+";
Console.Write("{0}", maze[a, b]);
}
}
Console.Write("\n");
}
Console.Write("\n\nNOTE: Use your arrow keys to move");
do
{
move = (char)_getch();
if (move == 80 && y >= 1 && y <= 40)
{
ch.gotoxy(x, (short)(y + 1));
Console.Write(".\b");
y++;
}
else if (move == 72 && y > 1 && y <= 41)
{
ch.gotoxy(x, (short)(y - 1));
Console.Write(".\b");
y--;
}
else if (move == 77 && x >= 1 && x <= 60)
{
ch.gotoxy((short)(x + 1), y);
Console.Write(".\b");
x++;
}
else if (move == 75 && x > 1 && x <= 61)
{
ch.gotoxy((short)(x - 1), y);
Console.Write(".\b");
x--;
}
} while (move != 13);
Console.Read();
}
}
}
here is the class.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace TikTak
{
public class ConsoleHelper
{
[StructLayout(LayoutKind.Sequential)]
struct POSITION
{
public short x;
public short y;
}
[DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", EntryPoint = "SetConsoleCursorPosition", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int SetConsoleCursorPosition(int hConsoleOutput, POSITION dwCursorPosition);
public void gotoxy(short x, short y)
{
const int STD_OUTPUT_HANDLE = -11;
int hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
POSITION position;
position.x = x;
position.y = y;
SetConsoleCursorPosition(hConsoleHandle, position);
}
[StructLayout(LayoutKind.Sequential)]
struct CONSOLERECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}
[StructLayout(LayoutKind.Sequential)]
struct CONSOLEBUFFER
{
public POSITION size;
public POSITION position;
public int attrib;
public CONSOLERECT window;
public POSITION maxsize;
}
[DllImport("kernel32.dll", EntryPoint = "FillConsoleOutputCharacter", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int FillConsoleOutputCharacter(int handleConsoleOutput, byte fillchar, int len, POSITION writecord, ref int numberofbyeswritten);
[DllImport("kernel32.dll", EntryPoint = "GetConsoleScreenBufferInfo", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int GetConsoleScreenBufferInfo(int handleConsoleOutput, ref CONSOLEBUFFER bufferinfo);
public void ClearScreen()
{
const int STD_OUTPUT_HANDLE = -11;
int hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
int hWrittenChars = 0;
CONSOLEBUFFER strConsoleInfo = new CONSOLEBUFFER();
POSITION pos;
pos.x = pos.y = 0;
GetConsoleScreenBufferInfo(hConsoleHandle, ref strConsoleInfo);
FillConsoleOutputCharacter(hConsoleHandle, 32, strConsoleInfo.size.x * strConsoleInfo.size.y, pos, ref hWrittenChars);
SetConsoleCursorPosition(hConsoleHandle, pos);
}
}
}