【问题标题】:how to repair move function如何修复移动功能
【发布时间】:2016-08-20 15:04:21
【问题描述】:

大家好,我正在尝试构建一个动态的两人井字游戏,我的移动功能有问题,需要让玩家移动并检查棋盘,如果要求的位置清晰,如果清晰打印棋盘与移动并告诉其他玩家投入移动......

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

#define SIGN '_'
#define FIRST 'X'
#define SECOND 'O'

void isValid(void);
int turn();
void printMsg();

int main()
{
  printMsg();
  int i, j;//indexs
  int row = 1, column = 1;//first set for the size of the rows\columns.
  char board[row][column];
 do
  {
    scanf("%d", &row);
    column = row;
  }while(row <= 0 || row > 10);

  for(i = 0; i < row; i++)//for loop to build the board for the game

   {

     for(j = 0; j < column; j++)

      {
          board[i][j] = SIGN;
          printf("%c", board[i][j]);

      }

      printf("\n");
  }
  turn();

  return 0;
  }

 void printMsg() //prints openning massage.
 {
   printf("Enter board size (1 to 10): \n");
 }

int turn(int i, int j)
{
  int count = 0;
  int player = 0;
  do
   {
     printf("\nplayer %d, enter next move: \n", count % 2 + 1);
       scanf("%d%*c%d", row, column);
       count++;
     }while(i > 0 && j > 0);
    return player;
 }

【问题讨论】:

    标签: c ansi-c


    【解决方案1】:

    你可能需要为char board[row][column]动态分配内存,可能是这样的:

    char**board=malloc(col_size); 
      for (int i=0; i<size; i++)
            *(board+i)=malloc(row_size);
    

    因此,你可以用同样的方法检查单元格是否为空;

    for (int i=0; i<col_size; i++)
            for (int c=0; c<row_size; c++)
                 if board[i][c]==filled
                           ...
                 else ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多