使用C在板上订购瓷砖的位置

时间:2017-03-08 21:31:21

标签: c

假设我在n x n维的板上有黑色('B')和白色('W')。 n是用户输入,可以从“a”变为“z”。当用户输入n时,它将打印中间有2个白人和2个黑人的“板”。然后,用户将输入许多步骤。假设n始终是偶数,用户输入将仅在'!!!'时停止存储进入。因此,在一系列用户输入之后,瓷砖占据我需要的位置以打印出电路板的配置,如下所示:

Enter a board configuration:
Bbd
Bad
Wde
Wcb
!!!

然后,它应该打印:

  abcdef 
a UUUBUU
b UUUBUU
c UWWBUU
d UUBWWU
e UUUUUU
f UUUUUU

然而,我意识到除非我通过增加顺序重新排序磁贴,所以从前面的例子:

Enter a board configuration:
Bad
Bbd
Wcb
Wde
!!!

它只打印出一个或几个,但不是所有的瓷砖。 它适用于具有相同行号但列号不同的图块,反之亦然:

Enter a board configuration:
 Baa
 Bab
 Wbc
 Wbd
 !!!

可行,但

Enter a board configuration:
 Bab
 Baa
 Wbd
 Wbc
 !!!

不会。

总共有多少动作没有上限(好吧,绝对最大值是板子的大小所以26 * 26 - 4,但我觉得效率太低),只是它停止一次“ !!!”被读了。

如何编写按行和列排序这些图块的函数?

谢谢!

以下是我这部分的代码(它实际上是一个辅助函数,但是我在main()中的另一个文件中写了它,这样它就更容易测试。

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int sizeBoard = 6;

    int i, j; //row and col, a is the number of moves - 1
    int numMove = 0; 
    char color[numMove], row[numMove], col[numMove];
    char board[sizeBoard][sizeBoard];

    printf("Enter board configuration: \n");
    scanf(" %c%c%c", &color[numMove], &row[numMove], &col[numMove]); 
        //color[a] = board[row + 1][col + 2];

    while ((color[numMove] != '!') && (row[numMove] != '!') && (col[numMove] != '!')) {
        numMove++;
        scanf(" %c%c%c", &color[numMove], &row[numMove], &col[numMove]); 
    }

    printf("  "); //two spaces before the column header (row 0)
    for (j = 2; j < sizeBoard + 2; j++) {
        board[0][j] = 'a' - 2 + j;
        printf("%c", board[0][j]);
    }
    printf("\n");
    numMove = 0; //set it back to initial value that gives first input of color[], row[] and col[]
    for (i = 1; i < sizeBoard + 1; i++) {
        board[i][0] = 'a' - 1 + i;
        printf("%c ", board[i][0]); 
        for (j = 2; j < sizeBoard + 2; j++) {

            //initial placement of the tiles in center
            if (((j == ((sizeBoard + 2) / 2)) && (i == ((sizeBoard + 1) / 2))) || ((j == ((sizeBoard + 2) / 2) + 1) && (i == ((sizeBoard + 1) / 2) + 1)))
                board[i][j] = 'W';
            else if (((j == ((sizeBoard + 2) / 2) + 1) && (i == ((sizeBoard + 1) / 2))) || ((j == ((sizeBoard + 2) / 2)) && (i == ((sizeBoard + 1) / 2) + 1)))
                board[i][j] = 'B';
            else  
                board[i][j] = 'U'; //if not centered, set it as unoccupied

            //mid-game placement of tiles
            if ((i == ((row[numMove] - 'a') + 1)) && (j == ((col[numMove] - 'a') + 2))) {
                board[i][j] = color[numMove];
                numMove++;
            }
            printf("%c", board[i][j]);
        }
        printf("\n");
    }
    return (EXIT_SUCCESS);
}

1 个答案:

答案 0 :(得分:0)

试试这个

#include <stdio.h>
#include <stdlib.h>
// Maximum size
#define Maximum_size 26
// board initial size
#define Ini_Row_size 6
#define Ini_Col_size 6
/* test input
Bbd
Bad
Wde
Wcb
!!!
*/
int main(void) {
    // Maximum size
    int sizeMax = Maximum_size;
    // board size
    int sizeRow = Ini_Row_size - 1;
    int sizeCol = Ini_Row_size - 1;

    int i, j;
    char color, row, col;
    char board[sizeMax][sizeMax];
    // board initial
    for ( i=0 ; i<sizeMax ; i++ ) for ( j=0 ; j<sizeMax ; j++ ) board[i][j] = 'U';

    printf("Enter board configuration: \n");

    // moves
    while (true) {

        // read the moves
        scanf(" %c%c%c", &color, &row, &col); 
        if( (color == '!') && (row == '!') && (col == '!') ) break;

        // turn char to int (index)
        int temp_row = row - 'a';
        int temp_col = col - 'a';

        // if the move is out-of the sizeMax then ignore the move
        if( temp_row>=sizeMax || temp_row<0 || temp_col>=sizeMax || temp_col<0) continue;

        // save the color
        board[temp_row][temp_col] = color;

        // update the board size when the move is out-of the board range
        sizeRow = sizeRow > temp_row ? sizeRow : temp_row;
        sizeCol = sizeCol > temp_col ? sizeCol : temp_col;
    }

    // the size have to +1 because for-loop is i<size
    sizeRow++;
    sizeCol++;

    // set the center
        // center index
    int ci[4][2]={  {(sizeRow-1)/2,(sizeCol-1)/2},
                    {(sizeRow)/2,(sizeCol)/2},
                    {(sizeRow-1)/2,(sizeCol)/2},
                    {(sizeRow)/2,(sizeCol-1)/2} };
        // center color
    char cc[4]={ 'W', 'W', 'B', 'B'};

    for( i=0 ; i<4 ; i++) {
        // if there is no any move to center point then set center color
        if( board[ (ci[i][0]) ][ (ci[i][1]) ] == 'U')
            board[ (ci[i][0]) ][ (ci[i][1]) ] = cc[i];
    }

    // show the board

    // first row
    printf("  ");
    for ( i=0 ; i<sizeCol ; i++ ) printf("%c", 'a'+i );
    printf("\n");

    // first col & board
    for ( i=0 ; i<sizeRow ; i++ ) {
        // first col
        printf("%c ", 'a'+i);
        for ( j=0 ; j<sizeCol ; j++ ) {
            // board
            printf("%c", board[i][j] );
        }
        printf("\n");
    }

    return 0;
}

这是游戏吗? 如果是这样,我认为它应该改变运作方式。 现在就像:

-- user input
move-1
move-2
move-3
...
-- board output
board

也许改变这种方式:

-- user input
move-1
-- board output
board
-- user input
move-2
-- board output
board
...
相关问题