CDIEM.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace bt18
{
class CDIEM
{
private int x;
private 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 bt18
{
class CDIEM
{
private int x;
private 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
namespace bt18
{
class Program
{
static ArrayList list = new ArrayList();
static Random rnd = new Random();
static int TOCDO = 700;
static void Main(string[] args)
{
Console.CursorVisible = false;
do
{
NhanPhimBam();
TaoChu();
RoiChu();
Thread.Sleep(TOCDO);
} while (KiemTraTiepTuc());
}
static void RoiChu()
{
Console.Clear();
for (int i = 0; i < list.Count; i++)
{
((CDIEM)list[i]).DichXuong(1);
((CDIEM)list[i]).Xuat();
}
}
static void TaoChu()
{
char c;
//do
//{
// c = (char)(rnd.Next(65, 90));
//} while(KiemTraChuTonTai(c));
c = (char)(rnd.Next(65, 90));
list.Add(new CDIEM(rnd.Next(1, Console.WindowWidth - 1), 0, c));
}
static bool KiemTraTiepTuc()
{
if (((CDIEM)list[0]).KhoangCachY() > Console.WindowHeight)
return false;
return true;
}
static void NhanPhimBam()
{
while (Console.KeyAvailable)
{
char cc = Console.ReadKey(true).KeyChar;
for (int i = 0; i < list.Count; i++)
{
if (((CDIEM)list[i]).c == cc)
{
list.RemoveAt(i);
}
}
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
namespace bt18
{
class Program
{
static ArrayList list = new ArrayList();
static Random rnd = new Random();
static int TOCDO = 700;
static void Main(string[] args)
{
Console.CursorVisible = false;
do
{
NhanPhimBam();
TaoChu();
RoiChu();
Thread.Sleep(TOCDO);
} while (KiemTraTiepTuc());
}
static void RoiChu()
{
Console.Clear();
for (int i = 0; i < list.Count; i++)
{
((CDIEM)list[i]).DichXuong(1);
((CDIEM)list[i]).Xuat();
}
}
static void TaoChu()
{
char c;
//do
//{
// c = (char)(rnd.Next(65, 90));
//} while(KiemTraChuTonTai(c));
c = (char)(rnd.Next(65, 90));
list.Add(new CDIEM(rnd.Next(1, Console.WindowWidth - 1), 0, c));
}
static bool KiemTraTiepTuc()
{
if (((CDIEM)list[0]).KhoangCachY() > Console.WindowHeight)
return false;
return true;
}
static void NhanPhimBam()
{
while (Console.KeyAvailable)
{
char cc = Console.ReadKey(true).KeyChar;
for (int i = 0; i < list.Count; i++)
{
if (((CDIEM)list[i]).c == cc)
{
list.RemoveAt(i);
}
}
}
}
}
}
Anh giai thích dùm em chỗ ((CDIEM)list[i]) là sao ?
ReplyDeleteCode theo cách binh thuong thì như thế nào?