C# 行方向

发布于 2024-11-08 23:43:25 字数 881 浏览 0 评论 0原文

我正在研究 GUI。我有一个使用 TableLayoutPanel 显示的游戏板。它有 6 列和 7 行。正方形数字的顺序正确,并且在某些行上它们应该从右到左。 例如;


  • 23-22-21-20-19-18
  • 12-13-14-15-16-17
  • 11-10-9-8-7-6
  • Start-1-2-3-4-5

但在我的代码中方形数字总是从左到右。你能帮忙吗?

    private void SetupGameBoard() {
    int counter = 0;

    for (int row = NUM_OF_ROWS-1; row > -1; row--) {
        for (int column = 0; column < NUM_OF_COLUMNS; column++) {
            SquareControl squareControl = new SquareControl
                (hareAndTortoiseGame.Board.Squares[counter], hareAndTortoiseGame.Players);
            if ((row == 6 && column == 0) || (row == 0 && column == 5)){
                squareControl.BackColor = Color.BurlyWood;
            }
            GameTableLayout.Controls.Add(squareControl, column, row);
            counter++;
        }
    }
}

I am working on GUI. I have a game board which is displayed by using a TableLayoutPanel. It has 6 columns and 7 rows. The square numbers are in the correct sequence and they should go right to left on some rows.
For example;


  • 23-22-21-20-19-18
  • 12-13-14-15-16-17
  • 11-10-9-8-7-6
  • Start-1-2-3-4-5

But in my code square numbers are always left to right. Could you please help?

    private void SetupGameBoard() {
    int counter = 0;

    for (int row = NUM_OF_ROWS-1; row > -1; row--) {
        for (int column = 0; column < NUM_OF_COLUMNS; column++) {
            SquareControl squareControl = new SquareControl
                (hareAndTortoiseGame.Board.Squares[counter], hareAndTortoiseGame.Players);
            if ((row == 6 && column == 0) || (row == 0 && column == 5)){
                squareControl.BackColor = Color.BurlyWood;
            }
            GameTableLayout.Controls.Add(squareControl, column, row);
            counter++;
        }
    }
}

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

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

发布评论

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

评论(2

ゞ记忆︶ㄣ 2024-11-15 23:43:25

您仅在一个方向上迭代,即从左到右。您的第一个 for 循环需要在向上和向下迭代列之间交替。

您想要一个沿行呈之字形排列的游戏板。向下迭代行的逻辑很好,从最高行开始然后向下移动。现在您只需要处理列即可。您声明列中的值在立即出现时始终从左到右。也就是说,我假设它们看起来像:

  • 23-22-21-20-19-18
  • 17-16-15-14-13-12
  • 11-10-9-8-7-6-5
  • 4-3-2- 1-start

这是因为你的列值总是从 0->max,但你还没有覆盖 max->0。

一些伪代码:

for number of rows
  if row % 2 == 0 //if we are on an even row
    iterate up
  else
    iterate down

一些未经测试的实际代码:

private void SetupGameBoard() {
        int counter = 0;

        for (int row = NUM_OF_ROWS-1; row > -1; row--) {
            if(row % 2 == 0){
               for (int column = NUM_OF_COLUMNS; column > -1; column--) {
                SquareControl squareControl = new SquareControl
                    (hareAndTortoiseGame.Board.Squares[counter], hareAndTortoiseGame.Players);
                if ((row == 6 && column == 0) || (row == 0 && column == 5)){
                    squareControl.BackColor = Color.BurlyWood;
                }
                GameTableLayout.Controls.Add(squareControl, column, row);
                counter++;
               }
            }
            else{
               for (int column = 0; column < NUM_OF_COLUMNS; column++) {
                SquareControl squareControl = new SquareControl
                    (hareAndTortoiseGame.Board.Squares[counter], hareAndTortoiseGame.Players);
                if ((row == 6 && column == 0) || (row == 0 && column == 5)){
                    squareControl.BackColor = Color.BurlyWood;
                }
                GameTableLayout.Controls.Add(squareControl, column, row);
                counter++;
               }
            }
        }

您显然可以将其中一些代码分解为其他方法,因为有很多重复的东西。这个想法是,根据您位于奇数行还是偶数行来完成之字形。

You are only iterating in one direction and that is from left to right. Your first for loop needs to alternate between iterating columns up and down.

You want a game board that sort of zig-zags down the rows. Your logic for iterating down the rows is fine, you start at the highest row and move down. Now you just need to tackle the columns. You state that the values in the columns are always left to right when they come out right now. That is, I assume they look like:

  • 23-22-21-20-19-18
  • 17-16-15-14-13-12
  • 11-10-9-8-7-6-5
  • 4-3-2-1-start

This is because your column value always goes from 0->max, but you havent covered max->0.

some peusdo code:

for number of rows
  if row % 2 == 0 //if we are on an even row
    iterate up
  else
    iterate down

some untested actual code:

private void SetupGameBoard() {
        int counter = 0;

        for (int row = NUM_OF_ROWS-1; row > -1; row--) {
            if(row % 2 == 0){
               for (int column = NUM_OF_COLUMNS; column > -1; column--) {
                SquareControl squareControl = new SquareControl
                    (hareAndTortoiseGame.Board.Squares[counter], hareAndTortoiseGame.Players);
                if ((row == 6 && column == 0) || (row == 0 && column == 5)){
                    squareControl.BackColor = Color.BurlyWood;
                }
                GameTableLayout.Controls.Add(squareControl, column, row);
                counter++;
               }
            }
            else{
               for (int column = 0; column < NUM_OF_COLUMNS; column++) {
                SquareControl squareControl = new SquareControl
                    (hareAndTortoiseGame.Board.Squares[counter], hareAndTortoiseGame.Players);
                if ((row == 6 && column == 0) || (row == 0 && column == 5)){
                    squareControl.BackColor = Color.BurlyWood;
                }
                GameTableLayout.Controls.Add(squareControl, column, row);
                counter++;
               }
            }
        }

you can obviously break some of that out into other methods because there is a lot of repeating stuff. The idea is that you complete the zig-zagging based on if you are on an odd row or an even row.

大海や 2024-11-15 23:43:25

你只需要做一些数学计算。这是循环所有单元格(按单元格编号)并获取行和列的数学运算。剩下的就交给你了。注意:我没有测试...可能是拼写错误或语法错误。

const int maxCols = 6;
const int maxRows = 4;

int numberCells = maxCols * maxRows;  // zero based in main array.

for (int cellNumber = 0; cellNumber < numberCells; cellNumber)
{
     int row;
     int col;

     row = maxRows - floor(cellNumber / maxRows);

     bool oddRow = (row % 2) == 1;

     col = oddRow ? (cellNumber % maxCols) + 1 : maxCols - (cellNumber % maxCols);

     // I believe the following is what you want to do in your proc.
     SquareControl squareControl = new SquareControl(hareAndTortoiseGame.Board.Squares[cellNumber], hareAndTortoiseGame.Players);
     if ((row == 6 && col == 0) || (row == 0 && col == 5))
     // if ((cellNumber == 0) || (cellNumber == (numberCells-1)))
     {
        squareControl.BackColor = Color.BurlyWood;
     }
     GameTableLayout.Controls.Add(squareControl, col, row);

}

You just have to do some math. Here is the math to loop over all the cells (by cell number) and get the row and column. I leave the rest to you. NOTE: I did not test... might be typos or syntax errors.

const int maxCols = 6;
const int maxRows = 4;

int numberCells = maxCols * maxRows;  // zero based in main array.

for (int cellNumber = 0; cellNumber < numberCells; cellNumber)
{
     int row;
     int col;

     row = maxRows - floor(cellNumber / maxRows);

     bool oddRow = (row % 2) == 1;

     col = oddRow ? (cellNumber % maxCols) + 1 : maxCols - (cellNumber % maxCols);

     // I believe the following is what you want to do in your proc.
     SquareControl squareControl = new SquareControl(hareAndTortoiseGame.Board.Squares[cellNumber], hareAndTortoiseGame.Players);
     if ((row == 6 && col == 0) || (row == 0 && col == 5))
     // if ((cellNumber == 0) || (cellNumber == (numberCells-1)))
     {
        squareControl.BackColor = Color.BurlyWood;
     }
     GameTableLayout.Controls.Add(squareControl, col, row);

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