C# 行方向
我正在研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仅在一个方向上迭代,即从左到右。您的第一个 for 循环需要在向上和向下迭代列之间交替。
您想要一个沿行呈之字形排列的游戏板。向下迭代行的逻辑很好,从最高行开始然后向下移动。现在您只需要处理列即可。您声明列中的值在立即出现时始终从左到右。也就是说,我假设它们看起来像:
这是因为你的列值总是从 0->max,但你还没有覆盖 max->0。
一些伪代码:
一些未经测试的实际代码:
您显然可以将其中一些代码分解为其他方法,因为有很多重复的东西。这个想法是,根据您位于奇数行还是偶数行来完成之字形。
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:
This is because your column value always goes from 0->max, but you havent covered max->0.
some peusdo code:
some untested actual code:
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.
你只需要做一些数学计算。这是循环所有单元格(按单元格编号)并获取行和列的数学运算。剩下的就交给你了。注意:我没有测试...可能是拼写错误或语法错误。
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.