CODE CHO LỚP CDIEM BẰNG NGÔN NGỮ C# { private int x, y; #region Nhom khoi tao public CDIEM() //CDIEM d = new CDIEM(); { x = 0; y = 0; } public CDIEM(int x, int y) //CDIEM d = new CDIEM (5,10); { this.x = x; this.y = y; } public CDIEM(CDIEM d) //CDIEM d = new CDIEM (5,10); CDIEM d2 = new CDIEM (d); { this.x = d.x; this.y = d.y; } public void KhoiTao() //d.KhoiTao(); { x = 0; y = 0; } public void KhoiTao(int x, int y)// d.KhoiTao(5,10); { this.x = x; this.y = y; } public void KhoiTao(CDIEM d)// d2.KhoiTao(d); { this.x = d.x; this.y = d.y; } public void Nhap() //d.Nhap(); { do { Console.Write("Nhap x: "); x = int.Parse(Console.ReadLine()); } while (!KiemTrax(x)); do { Console.Write("Nhap y: "); y = int.Parse(Console.ReadLine()); }while (!KiemTray(y)); } public void PhatSinh() //d.PhatSinh(); { Random rd = new Random(); x = rd.Next(0, Console.WindowWidth - 1); y = rd.Next(0, Console.WindowHeight - 1); } #endregion #region Nhom Kiem Tra Rang Buoc 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; } #endregion #region Nhom cung cap thong tin public void Xuat() //d.Xuat(); { Console.SetCursorPosition(x,y); Console.WriteLine("{{0}:{1}}",x,y); } public void Xoa() //d.Xoa(); { x=0; y=0; } public int X //int x=d.X get ; d.X=5 set { get { return x; } set { x = value; } } public int Y//int y=d.Y get ; d.Y=15 set { get { return y; } set { y = value; } } #endregion #region Nhom Cap Nhat public void DichPhai() //d.DichPhai(); { x++; if (!KiemTrax(x)) x = 0; } public void DichPhai(int k) //d.DichPhai(5); { x=x+k; if (!KiemTrax(x)) x = x-Console.WindowWidth; } public void DichTrai() //d.DichTrai(); { x ; if (!KiemTrax(x)) x = Console.WindowWidth; } public void DichTrai(int k) //d.DichTrai(5); { x = x - k; if (!KiemTrax(x)) x = Console.WindowWidth + x; } public void DichLen () { y ; if(!KiemTray(y)) y=Console.WindowHeight; } public void DichLen (int k) { y=y-k; if(!KiemTray(y)) y=y-Console.WindowHeight; } public void DichXuong () { y++; if(!KiemTray(y)) y=0; } public void DichXuong (int k) { y=y+k; if(!KiemTray(y)) y=Console.WindowHeight+y; } #endregion } } class CHCN { private CDIEM A; private int ngang, doc; #region Nhom khoi tao public CHCN() { A = new CDIEM(); ngang=1; doc=1; } public CHCN(CDIEM cd, int n, int d) { A=new CDIEM(cd); ngang=n; doc = d; } public CHCN(CHCN cn) { this.A = new CDIEM(cn.A); this.ngang = cn.ngang; this.doc = cn.doc; } public void KhoiTao() { ngang = 1; doc = 1; } public void KhoiTao(CDIEM cd, int n, int d) { A = new CDIEM(cd); ngang = n; doc = d; } public void KhoiTao(CHCN cn) { this.A = new CDIEM(cn.A); this.ngang = cn.ngang; this.doc = cn.doc; } public void Nhap() { do { Console.Write("Nhap ngang: "); ngang = int.Parse(Console.ReadLine()); } while (!KiemTraNgang(ngang)); do { Console.Write("Nhap doc: "); doc = int.Parse(Console.ReadLine()); }while (!KiemTraDoc(doc)); } public void PhatSinh() { A.PhatSinh(); Random rd = new Random(); ngang = rd.Next(1, Console.WindowWidth - A.X); doc = rd.Next(1, Console.WindowHeight - A.Y); } #endregion #region Nhom Kiem Tra Rang Buoc public bool KiemTraNgang(int ng) { if (ng <= 0 || ng > Console.WindowWidth-A.X) return false; return true; } public bool KiemTraDoc(int d) { if (d <= 0 || d >= Console.WindowHeight - A.Y) return false; return true; } #endregion #region Nhom cung cap thong tin public void Xuat() { for (int i = A.X; i <= A.X + ngang; i++) { Console.SetCursorPosition(i, A.Y); Console.WriteLine("-"); Console.SetCursorPosition(i, A.Y + doc); Console.WriteLine("-"); } for (int i = A.Y; i <= A.Y + doc; i++) { Console.SetCursorPosition(A.X,i); Console.WriteLine("+"); Console.SetCursorPosition(A.X + ngang,i); Console.WriteLine("+"); } } public int NGANG { get { return ngang; } set { ngang = value; } } public int DOC { get { return doc; } set { doc = value; } } public CDIEM Dinh { get { return A; } set { A = value; } } #endregion #region Nhom Cap Nhat public void DichPhai() { if (KiemTraNgang(ngang + 1)) A.DichPhai(); } public void DichPhai(int k) { if (KiemTraNgang(ngang + k)) A.DichPhai(k); } public void DichTrai() { if (A.X > 0) A.DichTrai(); } public void DichTrai(int k) { if (A.X >= k) A.DichPhai(k); } public void DichLen() { if (A.Y > 0) A.DichLen(); } public void DichLen(int k) { if (A.Y >= k) A.DichLen(k); } public void DichXuong() { if (KiemTraDoc(doc + 1)) A.DichXuong(); } public void DichXuong(int k) { if (KiemTraDoc(doc + k)) A.DichXuong(k); } public void TangNgang() { if (KiemTraNgang(ngang + 1)) ngang++; } public void TangNgang(int k) { if (KiemTraNgang(ngang + k)) ngang+=k; } public void GiamNgang() { if (KiemTraNgang(ngang - 1)) ngang ; } public void GiamNgang(int k) { if (KiemTraNgang(ngang - k)) ngang -= k; } public void TangDoc() { if(KiemTraDoc(doc + 1)) doc++; } public void TangDoc(int k) { if(KiemTraDoc(doc + k)) doc+=k; } public void GiamDoc() { if(KiemTraDoc(doc - 1)) doc ; } public void GiamDoc(int k) { if(KiemTraDoc(doc - k)) doc-=k; } #endregion #region Nhom Xu Ly Tinh Toan public int XetViTri(CDIEM M) { if (M.X > A.X && M.X < A.X + ngang && M.Y > A.Y && M.Y < A.Y + doc) return 1; //nam trong if ((M.X >= A.X && M.X <= A.X + ngang && M.Y == A.Y) || (M.X >= A.X && M.X <= A.X + ngang && M.Y == A.Y + doc) || (M.Y >= A.Y && M.Y <= A.Y + doc && M.X == A.X) || (M.Y >= A.Y && M.Y <= A.Y + doc && M.X == A.X + doc)) return 0; //nam tren canh return -1;//nam ngoai } #endregion } } class Program . CODE CHO LỚP CDIEM BẰNG NGÔN NGỮ C# { private int x, y; #region Nhom khoi tao public CDIEM( ) / /CDIEM d = new CDIEM( ); { x = 0; y = 0; } public CDIEM( int x, int y) / /CDIEM. CDIEM( int x, int y) / /CDIEM d = new CDIEM (5,10); { this.x = x; this.y = y; } public CDIEM( CDIEM d) / /CDIEM d = new CDIEM (5,10); CDIEM d2 = new CDIEM (d); { this.x = d.x; this.y. private CDIEM A; private int ngang, doc; #region Nhom khoi tao public CHCN() { A = new CDIEM( ); ngang=1; doc=1; } public CHCN (CDIEM cd, int n, int d) { A=new CDIEM( cd);