예제 #1
0
        public void load()         //used to load a previously saved game
        {
            Controls.Clear();
            var projectFolder = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
            var file          = Path.Combine(projectFolder, @"SaveData\save.txt");

            Char[] loadData = File.ReadAllText(file).ToCharArray();
            int    i        = 0;
            int    j        = 0;

            ChessPiece[] pcs = new ChessPiece[32];
            while (i < loadData.Length)  //starts from the first char in the save file which is the type of the first piece in the list
            //creates new pieces from the data in the save file and adds them to a list of chesspieces to be passed to the board creation
            {
                if (loadData[i] == '1')
                {
                    i++;
                    pcs[j] = new Pawn(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    if (loadData[i] != '0')
                    {
                        ((Pawn)pcs[j]).setFirstMove(loadData[i] - 48);
                        i++;
                    }
                    else
                    {
                        i++;
                    }
                    j++;
                }
                if (loadData[i] == '2')
                {
                    i++;
                    pcs[j] = new Knight(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    j++;
                }
                if (loadData[i] == '3')
                {
                    i++;
                    pcs[j] = new Bishop(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    j++;
                }
                if (loadData[i] == '4')
                {
                    i++;
                    pcs[j] = new Rook(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    if (loadData[i] != '0')
                    {
                        ((Rook)pcs[j]).setFirstMove(loadData[i] - 48);
                        i++;
                    }
                    else
                    {
                        i++;
                    }
                    j++;
                }
                if (loadData[i] == '5')
                {
                    i++;
                    pcs[j] = new Queen(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    j++;
                }
                if (loadData[i] == '6')
                {
                    i++;
                    pcs[j] = new King(j, loadData[i] - 48);
                    i++;
                    if (loadData[i] == '-')
                    {
                        pcs[j].setLocation(-1, -1);
                        i += 4;
                    }
                    else
                    {
                        pcs[j].setLocation(loadData[i] - 48, loadData[i + 1] - 48);
                        i += 2;
                    }
                    if (loadData[i] != '0')
                    {
                        ((King)pcs[j]).setFirstMove(loadData[i] - 48);
                        i++;
                    }
                    else
                    {
                        i++;
                    }
                    j++;
                }
                if (loadData[i] == ':')//: means the next char is the color selection
                {
                    i++;
                    colorSel = ((int)loadData[i] - 48);
                    i++;
                    turn = loadData[i] - 48;
                    break;
                }
            }

            game = new Board(colorSel, pcs);
            initializeGame();
        }
예제 #2
0
        //Lê Thanh Tâm + Nguyễn Quang Thạch + Huỳnh Viết Thám
        //Khởi tạo vị trí quân cờ khi load lại game, đọc từ chuỗi 128 kí tự được lưu trên file từ trước
        public void BuildChessBoardFromString(string t)
        {
            RestartBoard();
            string str1 = t.Substring(0, 128);

            t = t.Remove(0, str1.Length + 1);
            int n = 0;

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    string             s = str1.Substring(n, 2);
                    int                c = 0;
                    AbstractChessPiece p = new AbstractChessPiece();
                    if (s[1] == '0')
                    {
                        p = new AbstractChessPiece(i, j);
                    }
                    else if (s[0] == 'b')
                    {
                        c = 1;
                    }
                    switch (s[1])
                    {
                    case 'R': p = new Rook(i, j, c); break;

                    case 'N': p = new Knight(i, j, c); break;

                    case 'B': p = new Bishop(i, j, c); break;

                    case 'Q': p = new Queen(i, j, c); break;

                    case 'K': p = new King(i, j, c); break;

                    case 'P':
                    {
                        if (c == 0)
                        {
                            p = new WhitePawn(i, j);
                        }
                        else
                        {
                            p = new BlackPawn(i, j);
                        }
                    }; break;
                    }
                    if (cw[i, j] == null)
                    {
                        cw[i, j] = p;
                        Controls.Add(cw[i, j]);
                    }
                    else if (cw[i, j].Get_name != p.Get_name)
                    {
                        cw[i, j].Dispose();
                        cw[i, j] = p;
                        Controls.Add(cw[i, j]);
                    }
                    n += 2;
                }
            }
            string str2 = t.Substring(0, t.IndexOf("\n"));

            t = t.Remove(0, str2.Length + 1);
            Int32.TryParse(str2, out turn);
            turn             = turn % 2;
            pbDevil.Location = pt[turn];
            string str3 = t.Substring(0, t.IndexOf("\n"));

            t = t.Remove(0, str3.Length + 1);
            Int32.TryParse(str3, out player[0].time);

            string str4 = t.Substring(0, t.IndexOf("\n"));

            t = t.Remove(0, str4.Length + 1);
            Int32.TryParse(str4, out player[1].time);

            string str5 = t.Substring(0, t.IndexOf("\n"));

            t = t.Remove(0, str5.Length + 1);
            Int32.TryParse(str5, out player[0].score);

            string str6 = t.Substring(0, t.IndexOf("\n"));

            t = t.Remove(0, str6.Length + 1);
            Int32.TryParse(str6, out player[1].score);

            string str7 = t.Substring(0, t.IndexOf("\n"));

            player[0].eaten = str7;
            t = t.Remove(0, str7.Length + 1);
            string str8 = t.Substring(0, t.IndexOf("\n"));

            player[1].eaten = str8;
            t = t.Remove(0, str8.Length + 1);
            if (turn % 2 == 0)
            {
                Timer1.Start();
                Timer2.Stop();
            }
            else
            {
                Timer2.Start();
                Timer1.Stop();
            }
            cw[0, 0].GetAdvantage();
            Player.LoadPlayerInfo();
        }