CDIEM.cs
PHP Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace BT19_ransanmoidos
{
class CDIEM
{
public int x;
public int y;
public char c;
public CDIEM()
{
}
public CDIEM(int xx, int yy, char cc)
{
x = xx;
y = yy;
c = cc;
}
public bool KiemTraX(int xx)
{
if (xx <= 0 || xx >= Console.WindowWidth)
{
return false;
}
return true;
}
public bool KiemTraY(int yy)
{
if (yy <= 0 || yy >= Console.WindowHeight)
{
return false;
}
return true;
}
public void DichPhai(int k)
{
x += k;
}
public void DichTrai(int k)
{
x -= k;
}
public void DichLen(int k)
{
y -= k;
}
public void DichXuong(int k)
{
y += k;
}
public void Xuat()
{
Console.SetCursorPosition(x, y);
Console.Write(c);
}
public void Xoa()
{
Console.Clear();
}
public float KhoangCach(CDIEM M)
{
return 0;
}
public int KhoangCachX()
{
return x;
}
public int KhoangCachY()
{
return y;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace BT19_ransanmoidos
{
class CDIEM
{
public int x;
public int y;
public char c;
public CDIEM()
{
}
public CDIEM(int xx, int yy, char cc)
{
x = xx;
y = yy;
c = cc;
}
public bool KiemTraX(int xx)
{
if (xx <= 0 || xx >= Console.WindowWidth)
{
return false;
}
return true;
}
public bool KiemTraY(int yy)
{
if (yy <= 0 || yy >= Console.WindowHeight)
{
return false;
}
return true;
}
public void DichPhai(int k)
{
x += k;
}
public void DichTrai(int k)
{
x -= k;
}
public void DichLen(int k)
{
y -= k;
}
public void DichXuong(int k)
{
y += k;
}
public void Xuat()
{
Console.SetCursorPosition(x, y);
Console.Write(c);
}
public void Xoa()
{
Console.Clear();
}
public float KhoangCach(CDIEM M)
{
return 0;
}
public int KhoangCachX()
{
return x;
}
public int KhoangCachY()
{
return y;
}
}
}
Program.cs
PHP Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
namespace BT19_ransanmoidos
{
class Program
{
static ArrayList list = new ArrayList();
static CDIEM thucan;
static Random rnd = new Random();
static int TOCDO = 100;
static String huong = "Phai";
// ký tự đại diện cho rẳn
const char KYTU = '0';
static void Main(string[] args)
{
Console.CursorVisible = false;
list.Add(new CDIEM(0, 0, KYTU));
TaoThucAn();
RanChay();
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("YOU LOSE!!!");
Console.WriteLine("Press Any Key To Exit!!!");
Console.Read();
}
static void RanChay()
{
do
{
NhanPhimBam();
// Lấy đoạn cuối trong rắn
CDIEM doanCuoi = (CDIEM)list[list.Count - 1];
CDIEM c = new CDIEM(doanCuoi.x, doanCuoi.y, KYTU);
// Thực hiện dịch chuyển rắn trong đoạn cuối.
switch (huong)
{
case "Len":
c.DichLen(1);
break;
case "Xuong":
c.DichXuong(1);
break;
case "Trai":
c.DichTrai(1);
break;
case "Phai":
c.DichPhai(1);
break;
}
if (TimXTrongList(c))
{
break;
}
// Thêm một đoạn vào list.
list.Add(c);
// Remove phần tử đầu tiên khỏi list.
list.RemoveAt(0);
// kiểm tra có thức ăn chưa.
KiemTraThucAn();
try
{
VeLai();
}
catch (Exception)
{ }
Thread.Sleep(TOCDO);
} while (KiemTraTiepTuc());
}
static void VeLai()
{
Console.Clear();
// Vẽ rắn.
foreach (CDIEM c in list)
{
c.Xuat();
}
// Ve thuc an
thucan.Xuat();
}
static void TaoThucAn()
{
int hang = rnd.Next(1, Console.WindowWidth);
int cot = rnd.Next(1, Console.WindowHeight);
thucan = new CDIEM(hang, cot, 'G');
//thucan = new CDIEM(5, 5, 'G');
}
static void KiemTraThucAn()
{
CDIEM c = (CDIEM)list[list.Count - 1];
if (c.x == thucan.x && c.y == thucan.y)
{
CDIEM cDau = (CDIEM)list[0];
CDIEM cMoi = new CDIEM(cDau.x, cDau.y, KYTU);
list.Insert(0, cMoi);
TaoThucAn();
}
}
static bool KiemTraTiepTuc()
{
CDIEM c = (CDIEM)list[list.Count - 1];
if (c.y < 0 || c.y >= Console.WindowHeight - 1 ||
c.x < 0 || c.x >= Console.WindowWidth - 1)
{
return false;
}
return true;
}
static bool TimXTrongList(CDIEM x)
{
foreach (CDIEM c in list)
{
if (c.x == x.x && c.y == x.y)
{
return true;
}
}
return false;
}
static void NhanPhimBam()
{
while (Console.KeyAvailable)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow:
huong = "Len";
break;
case ConsoleKey.DownArrow:
huong = "Xuong";
break;
case ConsoleKey.LeftArrow:
huong = "Trai";
break;
case ConsoleKey.RightArrow:
huong = "Phai";
break;
}
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
namespace BT19_ransanmoidos
{
class Program
{
static ArrayList list = new ArrayList();
static CDIEM thucan;
static Random rnd = new Random();
static int TOCDO = 100;
static String huong = "Phai";
// ký tự đại diện cho rẳn
const char KYTU = '0';
static void Main(string[] args)
{
Console.CursorVisible = false;
list.Add(new CDIEM(0, 0, KYTU));
TaoThucAn();
RanChay();
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("YOU LOSE!!!");
Console.WriteLine("Press Any Key To Exit!!!");
Console.Read();
}
static void RanChay()
{
do
{
NhanPhimBam();
// Lấy đoạn cuối trong rắn
CDIEM doanCuoi = (CDIEM)list[list.Count - 1];
CDIEM c = new CDIEM(doanCuoi.x, doanCuoi.y, KYTU);
// Thực hiện dịch chuyển rắn trong đoạn cuối.
switch (huong)
{
case "Len":
c.DichLen(1);
break;
case "Xuong":
c.DichXuong(1);
break;
case "Trai":
c.DichTrai(1);
break;
case "Phai":
c.DichPhai(1);
break;
}
if (TimXTrongList(c))
{
break;
}
// Thêm một đoạn vào list.
list.Add(c);
// Remove phần tử đầu tiên khỏi list.
list.RemoveAt(0);
// kiểm tra có thức ăn chưa.
KiemTraThucAn();
try
{
VeLai();
}
catch (Exception)
{ }
Thread.Sleep(TOCDO);
} while (KiemTraTiepTuc());
}
static void VeLai()
{
Console.Clear();
// Vẽ rắn.
foreach (CDIEM c in list)
{
c.Xuat();
}
// Ve thuc an
thucan.Xuat();
}
static void TaoThucAn()
{
int hang = rnd.Next(1, Console.WindowWidth);
int cot = rnd.Next(1, Console.WindowHeight);
thucan = new CDIEM(hang, cot, 'G');
//thucan = new CDIEM(5, 5, 'G');
}
static void KiemTraThucAn()
{
CDIEM c = (CDIEM)list[list.Count - 1];
if (c.x == thucan.x && c.y == thucan.y)
{
CDIEM cDau = (CDIEM)list[0];
CDIEM cMoi = new CDIEM(cDau.x, cDau.y, KYTU);
list.Insert(0, cMoi);
TaoThucAn();
}
}
static bool KiemTraTiepTuc()
{
CDIEM c = (CDIEM)list[list.Count - 1];
if (c.y < 0 || c.y >= Console.WindowHeight - 1 ||
c.x < 0 || c.x >= Console.WindowWidth - 1)
{
return false;
}
return true;
}
static bool TimXTrongList(CDIEM x)
{
foreach (CDIEM c in list)
{
if (c.x == x.x && c.y == x.y)
{
return true;
}
}
return false;
}
static void NhanPhimBam()
{
while (Console.KeyAvailable)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow:
huong = "Len";
break;
case ConsoleKey.DownArrow:
huong = "Xuong";
break;
case ConsoleKey.LeftArrow:
huong = "Trai";
break;
case ConsoleKey.RightArrow:
huong = "Phai";
break;
}
}
}
}
}
Đây là bài viết mình sưu tằm, có vần đề gì các bạn thông cảm nhé