首页 > 解决方案 > C语言10x10板上的替代棋子运动

问题描述

我试图让棋盘上的棋子在棋盘上向上移动时从左向右移动,然后从右向左移动。我使用二维数组来表示坐标和指针算法来操作它,因为我们不能在函数本身上使用二维数组。

while (k <= 11) { //I used K as a marker for the 11 vertical line Im going to make
  if (lineOne == 1) {
    printf("|");
    lineOne = 0;
    lineTwo = 1;
    k++;
  } else if (lineTwo == 1) {
    if (row % 2 == 0) { //pointer arithmetic to go through each part of the array
      printf("%c%c%c%c", *( * (arrA + row) + col), 
                         *( * (arrB + row) + col), 
                         *( * (arrC + row) + col),
                         *( * (arrD + row) + col));
      col++;
    } else if (row % 2 != 0) {
      printf("%c%c%c%c", *( * (arrA + row) + col),
                         *( * (arrB + row) + col),
                         *( * (arrC + row) + col),
                         *( * (arrD + row) + col));
      col--;
    }
    lineOne = 1;
    lineTwo = 0;
  }
}

row++;
divider = 1;
thirdLine = 0;
printf("\n");

这些是我的初始化

int row = 0, col = 0;

在我的脑海中,我的 2D 网格也是这样排列的

0
1
2
3
4
5
6
7
8
9
 0 1 2 3 4 5 6 7 8 9 

这是我的代码的输出。不要介意S,它是另一天的错误:(

+----+----+----+----+----+----+----+----+----+----+
|#   |S   |S   |S   |S   |S   |S   |S   |S   |S   |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|-   |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
a's turn.
a, press <Enter> to roll.

You rolled a 5

95

95
+----+----+----+----+----+----+----+----+----+----+
|#   |S   |S   |S   |S   |S   |S   |S   |S   |S   |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |a   |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+

95

get out
b's turn.
b, press <Enter> to roll.

这里 9 5 是我拥有的那个玩家的坐标

正如你所看到的,当它应该像这样从左到右进行时,它向左移动了 5 步

+----+----+----+----+----+----+----+----+----+----+
|#   |   S|    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |l   |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |l   |   S|    |    |    |    |    |   s|    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |L   |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |l   |    |    |L   |S   |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |   s|    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |   s|L   |    |    |    |    |    |    |    |
|abcd|    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+

a's turn.
a, press <Enter> to roll.

+----+----+----+----+----+----+----+----+----+----+
|#   |   S|    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |l   |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |l   |   S|    |    |    |    |    |   s|    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |L   |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |l   |    |    |L   |S   |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |    |    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |    |    |    |    |   s|    |    |    |    |
|    |    |    |    |    |    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+
|    |   s|L   |    |    |    |    |    |    |    |
| bcd|    |    |    |   a|    |    |    |    |    |
+----+----+----+----+----+----+----+----+----+----+

a rolls a 4!
b's turn
b, press <Enter> to roll.

我只想让我的作品在每次向上移动 Y 轴时交替移动。

标签: c

解决方案


推荐阅读