Help with Variable Length Array C language Chess Board program

Hi

The following code is for a ‘tour’ of a knight piece around a chess board. The knight’s moves are randomly generated from an array of acceptable knights’ moves.

The moves are generated and continued for each game until a move is no longer available.

The number of moves taken by the knight are put into an array and printed out.

The #define games at the top determines the number of games run. (Here finite and set at 15)

I would like to turn my gameNum[i] array into a variable length array, the length of which will only be determined at compile time.

I believe this is called a variable length array and is now allowed in C.

Right now, the length of the array is determined by #define GAME 15.

Here, I would like to change it to a brute force attempt so that my game will continue to run until all 64 moves are obtained.

So, I would like to insert a while statement.

However, I am not sure how to create a variable length array to accept the game result data?

Any thoughts?

Happy to provide further explanation of the code.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 8
#define GAME 15
#define COLUMN 8

//GLOBAL
// functions
void printBoard(void);
void sortArray(void);
void printArray(void);
void game(void);

// board & move combination arrays
int board[ROW][COLUMN] = {0};
int vertical[ROW] =      {-1,-2,-2,-1,1,2,2,1};
int horizontal[COLUMN] = {2,1,-1,-2,-2,-1,1,2};
int gameNum[GAME] = {0};

//co-ordinates variables
int y =3; // row
int x =3; // column
int move = 0; // the move we will make (here using vertical /horizontal array above to make legal moves)
int numMove = 1; // number of moves legally performed this game

//MAIN
int main(void)
{

	//seed the randomizer
	srand(time(NULL));
	//let's play a game = here we will play 'GAME' number of times
	size_t i = 0;
	for (i = 0; i < GAME; ++i) {
	//for each game, put co-ordinates back in the middle of board and reset all moves to zero
	y,x = 3;
	for (size_t j = 0; j < ROW; ++j) {
		for (size_t k = 0; k < COLUMN; ++k) {
			board[j][k] = 0;
		}
	}
	numMove = 1; // starting in the middle
	board[y][x] = numMove; // put the 1 in the middle

	// begin game
	game();
	//put the game results (numberf of legal piece moves into an array)
	gameNum[i] = numMove;
	}
	//sort the array top down
	sortArray();
	//print final game results
	printArray();
} // end main

void game (void) {
	//1. generate new random move value
	numMove = 1;
	size_t i = 0;
	//2. try to complete 64 moves across the board
	while (i < 64) {
	//3. generate new random move value
	move = 1 + rand()%7;
	//2. generate new y,x co-ordinates for move
	y += vertical[move];
	x += horizontal[move];
	i++;
	//3. test
if ((y < 0 || y > 7 || x < 0 || x > 7) || (board[y][x] != 0 )) {
	//4. if it doesn't work, put back to where you were before and tray again
		y-=vertical[move];
		x-=horizontal[move];
		continue;
	}
	//5. increment move number
	numMove++;
	board[y][x] = numMove;
	} // end while
}

void printBoard(void) {
	for (size_t i = 0; i < ROW; ++i) {
		for (size_t j = 0; j < COLUMN; ++j) {
			printf(" %d ", board[i][j]);
		}
	puts("");
	}
}

void printArray(void) {
	for (size_t k = 0; k < GAME; ++k) {
		printf("%d %d \n", k, gameNum[k]);
	}
}

void sortArray(void) {
	for (size_t pass = 0; pass < GAME; ++pass) {
		for (size_t pass1 = 0; pass1 < GAME; ++pass1) {
			if (gameNum[pass1+1] > gameNum[pass1]) {
				int a = gameNum[pass1];
				gameNum[pass1] = gameNum[pass1 + 1];
				gameNum[pass1 + 1] = a;
			}
		}
	}
}

Start off by creating the array:

structName ** sarray = (structName **) malloc(0 * sizeof(structName *));

Always keep track of the size separately:

long sarray_len = 0;

To increase or truncate:

sarray = (structName **) realloc(sarray, (sarray_len + offset) * sizeof(structName *));

Then set the size:

sarray_len += offset;

But if you have access to c++ use std::vector instead

1 Like

Hi thx

Do you need structs for this? I have not yet learned them :frowning:

No, they don’t need to be structs. Where he allocated for a struct *, you could instead allocate for an int, since your game board array are ints.

Great thanks😃

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.