我无法刷新/重新打印 winform

发布于 2024-10-24 01:55:52 字数 6144 浏览 1 评论 0原文

基本上,我做了一个棋盘。 我有两节课。触发事件的部分类,以及执行国际象棋游戏代码的逻辑类(即MVC模型)。

基本上我现在要做的就是对游戏进行回复。当然,游戏必须自行重新启动(重画)。

首先,我将向您解释如何加载图片。

这是国际象棋加载时的算法。

    PictureBox[,] chessPics = new PictureBox[9, 9];// this is an array of pictureBox. each square is a picture box

    private void Chess_Load(object sender, EventArgs e)
    {        
        chessPics = Pattern();// pattern is a function that returns an array of 
        PrintPieces(codeFile.FirstTimeLoad());// this FirstTimeLoad method, goes to the chess code file an brings a new array of all the chess pieces (each chess piece is an object).        
    }


        public PictureBox[ , ] Pattern()
    {
        chessPics[1, 1] = a1;// each letter and number is an event that triggers the picture.
        chessPics[1, 2] = b1;
        chessPics[1, 3] = c1;
        chessPics[1, 4] = d1;
        chessPics[1, 5] = e1;
        chessPics[1, 6] = f1;
        chessPics[1, 7] = g1;
        chessPics[1, 8] = h1;
        chessPics[2, 1] = a2;
        chessPics[2, 2] = b2;
        chessPics[2, 3] = c2;
        chessPics[2, 4] = d2;
        chessPics[2, 5] = e2;
        chessPics[2, 6] = f2;
        chessPics[2, 7] = g2;
        chessPics[2, 8] = h2;
        chessPics[3, 1] = a3;
        chessPics[3, 2] = b3;
        chessPics[3, 3] = c3;
        chessPics[3, 4] = d3;
        chessPics[3, 5] = e3;
        chessPics[3, 6] = f3;
        chessPics[3, 7] = g3;
        chessPics[3, 8] = h3;
        chessPics[4, 1] = a4;
        chessPics[4, 2] = b4;
        chessPics[4, 3] = c4;
        chessPics[4, 4] = d4;
        chessPics[4, 5] = e4;
        chessPics[4, 6] = f4;
        chessPics[4, 7] = g4;
        chessPics[4, 8] = h44;
        chessPics[5, 1] = a5;
        chessPics[5, 2] = b5;
        chessPics[5, 3] = c5;
        chessPics[5, 4] = d5;
        chessPics[5, 5] = e5;
        chessPics[5, 6] = f5;
        chessPics[5, 7] = g5;
        chessPics[5, 8] = h5;
        chessPics[6, 1] = a6;
        chessPics[6, 2] = b6;
        chessPics[6, 3] = c6;
        chessPics[6, 4] = d6;
        chessPics[6, 5] = e6;
        chessPics[6, 6] = f6;
        chessPics[6, 7] = g6;
        chessPics[6, 8] = h6;
        chessPics[7, 1] = a7;
        chessPics[7, 2] = b7;
        chessPics[7, 3] = c7;
        chessPics[7, 4] = d7;
        chessPics[7, 5] = e7;
        chessPics[7, 6] = f7;
        chessPics[7, 7] = g7;
        chessPics[7, 8] = h7;
        chessPics[8, 1] = a8;
        chessPics[8, 2] = b8;
        chessPics[8, 3] = c8;
        chessPics[8, 4] = d8;
        chessPics[8, 5] = e8;
        chessPics[8, 6] = f8;
        chessPics[8, 7] = g8;
        chessPics[8, 8] = h8;

        return chessPics;

    }

将每张图片打印在图片框上的方法:

        public  void PrintPieces(Pieces [,] pieces)
    {      
        for (int i = 1; i < 9; i++)
        {             
            for (int j = 1; j < 9; j++)
            {
                if (pieces[i, j] is Object)
                {
                   chessPics[i, j].Load(pieces[i, j].print());    // in the pieces object there are objects with a print method which specifies from which location to load the pictures             
                }
                else
                {
                    chessPics[i, j].Image = null;
                }
            }             
        }
    }

我失败的方法是在游戏的中间或结束时将所有内容重置为开始。 我尝试创建新对象和新方法来复制我在加载函数形式中所做的算法,但没有任何效果。

有什么解决办法吗?


这是我的 Class2:

   public class Class2
 {
    public static bool MATE;
    Pieces[,] pieces;
    Pieces[,] piece;

我创建了一个新对象

        Class2 codeFileReplay = new Class2();

,甚至分配了一个方法来重置和撤回新数组:

     public void resetBoard()
    {
        piece = null;
        pieces = null;
    }

我激活了该方法,并再次执行了整个过程,将新对象的新数组发送到打印我

        public static Pieces[,] ChessBoardDisplay()
    {
        Pieces[,] piece2 = new Pieces[9, 9];

        piece2[8, 1] = new Rook("WR");
        piece2[8, 2] = new Knight("WKN");
        piece2[8, 3] = new Bishop("WB");
        piece2[8, 5] = new Queen("WQ");
        piece2[8, 4] = new King("WK");
        piece2[8, 6] = new Bishop("WB");
        piece2[8, 7] = new Knight("WKN");
        piece2[8, 8] = new Rook("WR");


        piece2[7, 1] = new Pawn("WP");
        piece2[7, 2] = new Pawn("WP");
        piece2[7, 3] = new Pawn("WP");
        piece2[7, 4] = new Pawn("WP");
        piece2[7, 5] = new Pawn("WP");
        piece2[7, 6] = new Pawn("WP");
        piece2[7, 7] = new Pawn("WP");
        piece2[7, 8] = new Pawn("WP");

        piece2[1, 1] = new Rook("BR");
        piece2[1, 2] = new Knight("BKN");
        piece2[1, 3] = new Bishop("BB");
        piece2[1, 5] = new Queen("BQ");
        piece2[1, 4] = new King("BK");
        piece2[1, 6] = new Bishop("BB");
        piece2[1, 7] = new Knight("BKN");
        piece2[1, 8] = new Rook("BR");

        piece2[2, 1] = new Pawn("BP");
        piece2[2, 2] = new Pawn("BP");
        piece2[2, 3] = new Pawn("BP");
        piece2[2, 4] = new Pawn("BP");
        piece2[2, 5] = new Pawn("BP");
        piece2[2, 6] = new Pawn("BP");
        piece2[2, 7] = new Pawn("BP");
        piece2[2, 8] = new Pawn("BP");

        return piece2;
    }  

     public Pieces[,] FirstTimeLoad()
    {
        pieces = ChessBoardDisplay();
        piece = ChessBoardDisplay();
        return pieces;
    }

在部分类中激活了 FirstTimeLoad:

PrintPiecesReplay(codeFileReplay.FirstTimeLoad());
        PictureBox[,] chessPictures = new PictureBox[9, 9];
    public void PrintPiecesReplay(Pieces[,] pieces)
    {
        for (int i = 1; i < 9; i++)
        {
            for (int j = 1; j < 9; j++)
            {
                if (pieces[i, j] is Object)
                {
                    chessPictures[i, j].Load(pieces[i, j].print());
                }
                else
                {
                    chessPictures[i, j].Image = null;
                }
            }
        }
    }

基本上,我从类代码的新对象(Class2)开始执行整个过程。不会重画棋盘

Basically, i made a chessboard.
i have 2 classes. the partial class, that triggers the events, and the logic class where the chess game code is executed (i.e. MVC model).

basically, what i am working on now is to make a reply of the game. The game ,of course, has to restart (redraw) itself.

To start , i am going to explain to you how the pictures are loaded.

Here is the algorithm when the chess is loaded.

    PictureBox[,] chessPics = new PictureBox[9, 9];// this is an array of pictureBox. each square is a picture box

    private void Chess_Load(object sender, EventArgs e)
    {        
        chessPics = Pattern();// pattern is a function that returns an array of 
        PrintPieces(codeFile.FirstTimeLoad());// this FirstTimeLoad method, goes to the chess code file an brings a new array of all the chess pieces (each chess piece is an object).        
    }


        public PictureBox[ , ] Pattern()
    {
        chessPics[1, 1] = a1;// each letter and number is an event that triggers the picture.
        chessPics[1, 2] = b1;
        chessPics[1, 3] = c1;
        chessPics[1, 4] = d1;
        chessPics[1, 5] = e1;
        chessPics[1, 6] = f1;
        chessPics[1, 7] = g1;
        chessPics[1, 8] = h1;
        chessPics[2, 1] = a2;
        chessPics[2, 2] = b2;
        chessPics[2, 3] = c2;
        chessPics[2, 4] = d2;
        chessPics[2, 5] = e2;
        chessPics[2, 6] = f2;
        chessPics[2, 7] = g2;
        chessPics[2, 8] = h2;
        chessPics[3, 1] = a3;
        chessPics[3, 2] = b3;
        chessPics[3, 3] = c3;
        chessPics[3, 4] = d3;
        chessPics[3, 5] = e3;
        chessPics[3, 6] = f3;
        chessPics[3, 7] = g3;
        chessPics[3, 8] = h3;
        chessPics[4, 1] = a4;
        chessPics[4, 2] = b4;
        chessPics[4, 3] = c4;
        chessPics[4, 4] = d4;
        chessPics[4, 5] = e4;
        chessPics[4, 6] = f4;
        chessPics[4, 7] = g4;
        chessPics[4, 8] = h44;
        chessPics[5, 1] = a5;
        chessPics[5, 2] = b5;
        chessPics[5, 3] = c5;
        chessPics[5, 4] = d5;
        chessPics[5, 5] = e5;
        chessPics[5, 6] = f5;
        chessPics[5, 7] = g5;
        chessPics[5, 8] = h5;
        chessPics[6, 1] = a6;
        chessPics[6, 2] = b6;
        chessPics[6, 3] = c6;
        chessPics[6, 4] = d6;
        chessPics[6, 5] = e6;
        chessPics[6, 6] = f6;
        chessPics[6, 7] = g6;
        chessPics[6, 8] = h6;
        chessPics[7, 1] = a7;
        chessPics[7, 2] = b7;
        chessPics[7, 3] = c7;
        chessPics[7, 4] = d7;
        chessPics[7, 5] = e7;
        chessPics[7, 6] = f7;
        chessPics[7, 7] = g7;
        chessPics[7, 8] = h7;
        chessPics[8, 1] = a8;
        chessPics[8, 2] = b8;
        chessPics[8, 3] = c8;
        chessPics[8, 4] = d8;
        chessPics[8, 5] = e8;
        chessPics[8, 6] = f8;
        chessPics[8, 7] = g8;
        chessPics[8, 8] = h8;

        return chessPics;

    }

The method that prints each picture on a picture box:

        public  void PrintPieces(Pieces [,] pieces)
    {      
        for (int i = 1; i < 9; i++)
        {             
            for (int j = 1; j < 9; j++)
            {
                if (pieces[i, j] is Object)
                {
                   chessPics[i, j].Load(pieces[i, j].print());    // in the pieces object there are objects with a print method which specifies from which location to load the pictures             
                }
                else
                {
                    chessPics[i, j].Image = null;
                }
            }             
        }
    }

what i fail to do , is in the middle or end of the game to reset everything to the begining.
i tried to create new objects and new methods that will replicate the algorithm that i did when in the load function form, but nothing worked.

is there any solution?


here is my Class2:

   public class Class2
 {
    public static bool MATE;
    Pieces[,] pieces;
    Pieces[,] piece;

i created a new object

        Class2 codeFileReplay = new Class2();

and even assigned a method to reset and retreat a new array:

     public void resetBoard()
    {
        piece = null;
        pieces = null;
    }

i activated that method , and did the whole process again, of sending a new array of the new object to the method that prints the board

        public static Pieces[,] ChessBoardDisplay()
    {
        Pieces[,] piece2 = new Pieces[9, 9];

        piece2[8, 1] = new Rook("WR");
        piece2[8, 2] = new Knight("WKN");
        piece2[8, 3] = new Bishop("WB");
        piece2[8, 5] = new Queen("WQ");
        piece2[8, 4] = new King("WK");
        piece2[8, 6] = new Bishop("WB");
        piece2[8, 7] = new Knight("WKN");
        piece2[8, 8] = new Rook("WR");


        piece2[7, 1] = new Pawn("WP");
        piece2[7, 2] = new Pawn("WP");
        piece2[7, 3] = new Pawn("WP");
        piece2[7, 4] = new Pawn("WP");
        piece2[7, 5] = new Pawn("WP");
        piece2[7, 6] = new Pawn("WP");
        piece2[7, 7] = new Pawn("WP");
        piece2[7, 8] = new Pawn("WP");

        piece2[1, 1] = new Rook("BR");
        piece2[1, 2] = new Knight("BKN");
        piece2[1, 3] = new Bishop("BB");
        piece2[1, 5] = new Queen("BQ");
        piece2[1, 4] = new King("BK");
        piece2[1, 6] = new Bishop("BB");
        piece2[1, 7] = new Knight("BKN");
        piece2[1, 8] = new Rook("BR");

        piece2[2, 1] = new Pawn("BP");
        piece2[2, 2] = new Pawn("BP");
        piece2[2, 3] = new Pawn("BP");
        piece2[2, 4] = new Pawn("BP");
        piece2[2, 5] = new Pawn("BP");
        piece2[2, 6] = new Pawn("BP");
        piece2[2, 7] = new Pawn("BP");
        piece2[2, 8] = new Pawn("BP");

        return piece2;
    }  

     public Pieces[,] FirstTimeLoad()
    {
        pieces = ChessBoardDisplay();
        piece = ChessBoardDisplay();
        return pieces;
    }

i activated teh FirstTimeLoad in the partial class:

PrintPiecesReplay(codeFileReplay.FirstTimeLoad());
        PictureBox[,] chessPictures = new PictureBox[9, 9];
    public void PrintPiecesReplay(Pieces[,] pieces)
    {
        for (int i = 1; i < 9; i++)
        {
            for (int j = 1; j < 9; j++)
            {
                if (pieces[i, j] is Object)
                {
                    chessPictures[i, j].Load(pieces[i, j].print());
                }
                else
                {
                    chessPictures[i, j].Image = null;
                }
            }
        }
    }

Basically, i did the whole process from the beginning with a new object of the class code (Class2). wont redraw the board

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月野兔 2024-10-31 01:55:52

根据 PrintPieces 的说法,您需要担心重置的是 pieces 数组,而不是 chessPics 数组。

According to PrintPieces, it's the pieces array you need to worry about resetting, not the chessPics array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文