コード例 #1
0
ファイル: mineSweeper.c プロジェクト: nvurgaft/minesweeper
bool initBoard(GameBoard *g, int rows, int cols, int level) {
   	if (rows<4 || cols<4 || rows>MAX_BOARD_DIM || cols>MAX_BOARD_DIM) {
   		printf("ERROR: invalid board size.\nSize should be at least 4 by 4 up to 20 by 20!\n");
   		return 1;
	}

   	if (level<1 || level>3) {
   		printf("ERROR: invalid level! should be a value between 1 to 3\n");
   		return 1;
   	}
   	g->rows=rows;
   	g->cols=cols;
   
   //dynamically allocate the board tiles
   	g->board = (Tile **) malloc(rows*sizeof(Tile *));
   	int i, j;
   	for (i=0; i<rows; i++)
   	{
    	g->board[i] = (Tile *) malloc(cols*sizeof(Tile));
   	}
   	
   	//zero the values
	for (i=0; i<rows; i++) {
   		for (j=0; j<cols; j++) {
   			g->board[i][j].numOfMines = 0;
   			g->board[i][j].isMine = 0;
   			g->board[i][j].isVisible = 0;
   		}
   	}
   	g->totalMines=0;
   	g->isMineClicked=0;
   	g->hiddenTiles = rows*cols; // if hidden tiles == #mines then victory condition

   	if (level==1) populateMines(g, EASY_LEVEL_PERCENT);
   	if (level==2) populateMines(g, MEDIUM_LEVEL_PERCENT);
   	if (level==3) populateMines(g, HARD_LEVEL_PERCENT);

   	markNumbers (g);
   	
   	return 0;
}
コード例 #2
0
ファイル: mineSweeper.c プロジェクト: guybi/mineSweeper_c
bool initBoard(GameBoard *g, int rows, int cols, int level)
{
	int i;
	g->rows = rows;
	g->cols = cols;
	g->isMineClicked = FALSE;
	g->hiddenTiles = rows*cols;

	g->board = (Tile**)malloc(rows * sizeof(Tile*));
	if(g->board == NULL)
		return FALSE;
	for(i=0; i<rows; i++)
	{
		*(g->board+i) = (Tile*)(malloc(cols * sizeof(Tile)));
		if( *(g->board+i) == NULL)
			return FALSE;
	}
	
	populateMines(g, level);
	markNumbers(g);
	
	return TRUE;
}