Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 7 của 7

Chủ đề: Game Shuffle

  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Game Shuffle

    hiện tại mình đang làm 1 project về cái game shufle này đại khái là bạn có 1 ô vuông dãy số 3x3 từ 0-8 được xắp xếp hỗn độn sau đó bạn có 4 cách di chuyển để nó đc xắp xếp lại thành từ 0-8 theo thứ từ trên xuống từ trái qua mình đã viết xong nhưng code dài quá và hiện tại vẫn còn bị lỗi chưa chạy đc AE nào có thể viết ngắn hơn hoặc sửa lổi dùm mình đc ko?

    Mã:
    using System;
    using System.Collections.Generic;  
    using System.Text; 
    using System.IO; 
    
    namespace  Game 
    { 
        class Program 
        {  
            static void Main(string[] args)
            { 
                Console.WriteLine(" ABC"); 
                print ran = new print();
                ran.start();
                ran.rangame();
            }  
        }  
        class print 
        {  
           public int[,] myArr ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
           public int[,] panduan ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 0 } };
           public int[,] aa = new int[3, 3];
           public int bestmove, move;
           public string playername;
           public print()
           { 
               move = 0;
               bestmove = 999;
               playername = "NOBABY";
           } 
           public void start()
            { 
                string[] files = Directory.GetFiles("D:\\"); 
                if (files.Length == 0)
                { 
                    writetxt(bestmove, playername);
                    readtxt();
                }
                else
                { 
                    for (int i = 0; i < files.Length; i++)
                   { 
    
                        if (Path.GetFileNameWithoutExtension(files ) == "message")
                        { 
                            readtxt(); 
                            break;
                        } 
                        else
                        { 
                            writetxt(bestmove, playername);
                            readtxt();
                            break;
                        }    
                   }  
                }  
            } 
           public void rangame()
            { 
                Random suiji = new Random();
                for (int i = 0; i < 3; i++) 
                { 
                    for (int j = 0; j < 3; j++)
                    { 
                        int randId1 = suiji.Next(0, 3 - i);
                        int randId2 = suiji.Next(0, 3 - j);
                        aa[i, j] = myArr[randId1, randId2];
                        myArr[randId1, randId2] = myArr[2 - i, 2 - j];
                    } 
                } 
    
                printgame(aa);
                startgame(aa); 
            }  
    
           public void printgame(int[,] aa) 
           {
               Console.WriteLine("+===+===+===+");
               Console.WriteLine("+ {0} + {1} + {2} +", aa[0, 0], aa[0, 1], aa[0, 2]);
               Console.WriteLine("+ {0} + {1} + {2} +", aa[1, 0], aa[1, 1], aa[1, 2]);
               Console.WriteLine("+ {0} + {1} + {2} +", aa[2, 0], aa[2, 1], aa[2, 2]);
               Console.WriteLine("+===+===+===+");
               Console.WriteLine("Enter your Move [L,R,U,D,E]");
           } 
           public void startgame(int[,] aa)
           {  
                int aai, aaj, i, j; 
                int jishu, x, y, temp;
                string get;
                jishu = aai = aaj = i = j = 0;
                for (i = 0; i < 3; i++)
                {  
                    for (j = 0; j < 3; j++)
                    { 
                        if (aa[i, j] == 0) 
                        { 
                            aai = i;
                            aaj = j;
                            break;
                        }
                    } 
                }  
                get = Console.ReadLine();
                switch (get)
                    { 
                        case "L":
                        move++;
                        y = aaj - 1;
                        if (y < 0)
                        { 
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa); 
                        }
                        else
                        {  
                            temp = aa[aai, aaj];
                            aa[aai, aaj] = aa[aai, y];
                            aa[aai, y] = temp;
                            Console.Clear();
                            readtxt();
                            printgame(aa);
                            startgame(aa);
                        }  
                        break;
    
                        case "R":
                        move++;
                        y = aaj + 1;
                        if (y > 2)
                        { 
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa); 
                        }  
                        else
                        {  
                            temp = aa[aai, aaj];
                            aa[aai, aaj] = aa[aai, y]; 
                            aa[aai, y] = temp;
                            Console.Clear();
                            readtxt(); 
                            printgame(aa);
                            startgame(aa);
                        } 
                        break;
    
                        case "U": 
                        move++;
                        x = aai - 1;
                        if (x < 0)
                        {
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa); 
                        }
                        else
                        {
                            temp = aa[aai, aaj];
                            aa[aai, aaj] = aa[x, aaj];
                            aa[x, aaj] = temp;
                            Console.Clear();
                            readtxt();
                            printgame(aa);
                            startgame(aa);
                        }
                        break;
    
                        case "D":
                        move++;
                        x = aai + 1;
                        if (x > 2)
                        { 
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa); 
                        } 
                        else 
                        { 
                            temp = aa[aai, aaj]; 
                            aa[aai, aaj] = aa[x, aaj]; 
                            aa[x, aaj] = temp;
                            Console.Clear();
                            readtxt();
                            printgame(aa);
                            startgame(aa);
                        } 
                        break;
    
                        case "E":
                        move++;
                        for (i = 0; i < 3; i++)
                        { 
                            for (j = 0; j < 3; j++)
                            {  
                                if (aa[i, j] == panduan[i, j])
                                {  
                                    jishu++;
                                } 
                            } 
                        } 
                        if (jishu == 9)
                        {  
                            Console.WriteLine("Xin chuc mung vuot qua!");
                            if (move < bestmove)
                            {  
                                Console.WriteLine("Ban da vuot qua ky luc!");
                                Console.WriteLine("Please input your name");
                                String str = Console.ReadLine();
                                bestmove = move;
                                writetxt(bestmove, str);
                                Console.WriteLine("If you play game again! [Y/N]");
                                YandN();
                            }
                            else
                            {
                                Console.WriteLine("If you play game again! [Y/N]");
                                YandN();
                            }
                        }  
                        else
                        {  
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa); 
                            break;
                        } 
                        break; 
    
                        case "l":
                        move++;
                        y = aaj - 1; 
                        if (y < 0)
                        { 
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa); 
                        } 
                        else 
                        {
                            temp = aa[aai, aaj];
                            aa[aai, aaj] = aa[aai, y];
                            aa[aai, y] = temp;
                            Console.Clear();
                            readtxt(); readtxt (); 
                            printgame(aa);
                            startgame(aa); 
                        }
                        break;
    
                        case "r":
                        move++;
                        y = aaj + 1; 
                        if (y > 2) 
                        { 
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa); 
                        } 
                        else 
                        { 
                            temp = aa[aai, aaj];
                            aa[aai, aaj] = aa[aai, y];
                            aa[aai, y] = temp;
                            Console.Clear(); 
                            readtxt();
                            printgame(aa);
                            startgame(aa); 
                        }  
                        break;
                        case "u": 
                        move++;
                        x = aai - 1;
                        if (x < 0)
                        { 
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa);
                        } 
                        else
                        {
                            temp = aa[aai, aaj];
                            aa[aai, aaj] = aa[x, aaj];
                            aa[x, aaj] = temp;
                            Console.Clear();
                            readtxt();
                            printgame(aa);
                            startgame(aa);
                        }  
                        break; 
    
                        case "d":
                        move++;  
                        x = aai + 1;
                        if (x > 2) 
                        { 
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa); 
                        } 
                        else 
                        { 
                        temp = aa[aai, aaj];
                        aa[aai, aaj] = aa[x, aaj];
                        aa[x, aaj] = temp;
                        Console.Clear();
                        readtxt();
                        printgame(aa);
                        startgame(aa);
                        }
                        break; 
    
                        case "e":
                        move++;
                        for (i = 0; i < 3; i++)
                        { 
                            for (j = 0; j < 3; j++)
                            { 
                                if (aa[i, j] == panduan[i, j])
                                { 
                                    jishu++; 
                                } 
                            }     
                        }  
                        if (jishu == 9) 
                        {  
                            Console.WriteLine("Xin chuc mung da qua!");
                            if (move < bestmove)
                            { 
                                Console.WriteLine("ban da doat ky luc moi!");
                                Console.WriteLine("Please input your name");
                                String str = Console.ReadLine();
                                bestmove = move;
                                writetxt(bestmove, str);
                                Console.WriteLine("If you play game again! [Y/N]"); 
                                YandN();
                            }  
                            else
                            {  
                                Console.WriteLine("If you play game again! [Y/N]");
                                YandN();
                            } 
                        }  
                        else 
                        {  
                            Console.WriteLine("error!Please enter key again!"); 
                            startgame(aa); 
                            break; 
                        }  
                        break;
     
                        default:
                        Console.WriteLine("error!Please enter key again!"); 
                        startgame(aa); 
                        break;  
                  }  
            }  
           public void YandN() 
            { 
                string again;
                again = Console.ReadLine();
                switch (again)
                { 
                    case "Y": 
                    { 
                        print ran = new print(); 
                        ran.start(); 
                        ran.rangame(); 
                        break; 
                    }  
                    case "N":
                    { 
                        return;
                    }  
                    case "y":
                    {  
                        print ran = new print(); ran.start(); ran.rangame(); break; 
                    }  
                    case "n":
                    {  
                        return;
                    }  
                    default: 
                    Console.WriteLine("error!Please enter key again!"); 
                    YandN(); 
                    break; 
                }  
            }  
           public void writetxt(int bastmove, string playername) 
           {  
               FileStream fs = new FileStream(@"d:\message.txt", FileMode.Create, FileAccess.Write); 
               StreamWriter sw = new StreamWriter(fs, Encoding.Default); 
               string str = playername;
               sw.WriteLine(str);
               sw.Flush();
               string newstr = bestmove.ToString();
               sw.WriteLine(newstr);
               sw.Flush();
               sw.Close(); 
               fs.Close(); 
            }  
           public void readtxt()
           { 
                FileStream fs = null;
                StreamReader sr = null;
                try 
                {  
                    fs = new FileStream(@"d:\message.txt", FileMode.Open, FileAccess.Read); 
                    sr = new StreamReader(fs, Encoding.Default);
                    string str = sr.ReadLine();
                    string a = str;
                    Console.WriteLine("Record Holder:{0}", a);
                    str = sr.ReadLine();
                    string b = str;
                    Console.WriteLine("Key Presses:{0}", b);
                    bestmove = Convert.ToInt32(b); 
                }  
                catch (Exception e) 
                {  
                    Console.WriteLine(e.Message);
                } 
                finally
                {  
                    sr.Close(); 
                    fs.Close(); 
                }  
            }  
        }  
    }
    nhớ cho code vào trong thẻ

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    hiện tại mình đang làm 1 project về cái game shufle này đại khái là bạn có 1 ô vuông dãy số 3x3 từ 0-8 được xắp xếp hỗn độn sau đó bạn có 4 cách di chuyển để nó đc xắp xếp lại thành từ 0-8 theo thứ từ trên xuống từ trái qua mình đã viết xong nhưng code dài quá và hiện tại vẫn còn bị lỗi chưa chạy đc AE nào có thể viết ngắn hơn hoặc sửa lổi dùm mình đc ko?
    Code này cậu tự viết ? hay là cậu lấy ở đây rồi sửa comment của người ta thành của mình nên không biết lỗi chỗ nào cả ?

    http://home.hackbase.com/space.php?u...=blog&id=43569

  3. #3
    sr code này là project trong nhóm mình cùng làm

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Trích dẫn Gửi bởi c_niit
    sr code này là project trong nhóm mình cùng làm
    tôi dám cá là cậu copy từ nơi khác về
    http://home.hackbase.com/space.php?u...=blog&id=43569
    trong link cậu window đưa đây này, lần sau đừng nhận vơ như thế nhé ![IMG]images/smilies/waiting.gif[/IMG][IMG]images/smilies/waiting.gif[/IMG]

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    sr nhé mình nói là nhóm mình cùng làm có những đoạn bí thì mình tham khảo thêm mình post lên chỉ mong ae trao đổi hoặc có những cách khác hay hơn để mình học hỏi kn thôi

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    haiza đã coppy code còn không biết đọc lỗi cơ =))
    chỉnh ở phần GetFile không dùng mảng mà ghi hẳn cái địa chỉ cứng ra là được =))

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Bạn sửa chỗ đấy ntnao hả b?[IMG]images/smilies/daydreaming.gif[/IMG]

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •