I am attempting to write a simple chess program (starting with just pawns), and am currently at the stage of initialising the board.

The initial board is declared using an array of numbers, with '0' indicating a white piece, '1' indicating a black piece and '2' indicating an empty square.

I'm then trying to get the initial state of the board printed back to me, but currently all squares are indicated as white (0), so it appears the values are not being assigned in the array correctly.

The code I currently have is:

Main.c

/*
 *	main.c
 *	P Chess
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "defs.h"
#include "boardrep.h"

void init_board()
{
	int i;

	for (i = 0; i < 64; ++i) {
		colour[i] = init_colour[i];
	}
}
void print_board()
{
	int i;
	printf("\n8 ");
	for (i = 0; i < 64; ++i) {
		switch (colour[i]) {
			case EMPTY:
				printf(" -");
				break;
			case BLACK:
				printf(" b");
				break;
			case WHITE:
				printf(" w");
				break;
		}
		if ((i + 1) % 8 == 0 && i != 63)
			printf("\n%d ", 7 - ROW(i));
	}
	printf("\n\n   a b c d e f g h\n\n");
}
int main()
{
	char s[256];

	printf("\n");
	printf("Pawn Chess - Board representation\n");
	printf("version 0.01, 26/10/09\n");
	printf("\n");
	printf("\"d\" displays the current board state\n");
	printf("\n");
	return 0;
}

Boardrep.c

/*
 *	boardrep.c
 *	P Chess
 *	
 */

#include "defs.h"

/* the board representation */
int colour[64];  /* WHITE, BLACK, or EMPTY */

int mailbox[120] = {
	 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	 -1,  0,  1,  2,  3,  4,  5,  6,  7, -1,
	 -1,  8,  9, 10, 11, 12, 13, 14, 15, -1,
	 -1, 16, 17, 18, 19, 20, 21, 22, 23, -1,
	 -1, 24, 25, 26, 27, 28, 29, 30, 31, -1,
	 -1, 32, 33, 34, 35, 36, 37, 38, 39, -1,
	 -1, 40, 41, 42, 43, 44, 45, 46, 47, -1,
	 -1, 48, 49, 50, 51, 52, 53, 54, 55, -1,
	 -1, 56, 57, 58, 59, 60, 61, 62, 63, -1,
	 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};

int mailbox64[64] = {
	21, 22, 23, 24, 25, 26, 27, 28,
	31, 32, 33, 34, 35, 36, 37, 38,
	41, 42, 43, 44, 45, 46, 47, 48,
	51, 52, 53, 54, 55, 56, 57, 58,
	61, 62, 63, 64, 65, 66, 67, 68,
	71, 72, 73, 74, 75, 76, 77, 78,
	81, 82, 83, 84, 85, 86, 87, 88,
	91, 92, 93, 94, 95, 96, 97, 98
};

int init_colour[64] = {
	1, 1, 1, 1, 1, 1, 1, 1,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	2, 2, 2, 2, 2, 2, 2, 2,
	0, 0, 0, 0, 0, 0, 0, 0
};

boardrep.h

/*
 *	boardrep.h
 *	p chess
 */

extern int colour[64];
extern int piece[64];
extern int mailbox[120];
extern int mailbox64[64];
extern int init_colour[64];

defs.h

/*
 *	DEFS.H
 *	P Chess
 */

#define WHITE			0
#define BLACK			1
#define EMPTY			2

#define ROW(x)			(x >> 3)

It currently just outputs a grid of W's.

Any help much appreciated. Ta. :)

I'm looking at you main function I can't see where your calling any function to initialize your array.

shouldn't you be calling
void init_board();
void print_board()

commented: A good and fast response. +2

It's always something simple, eh!

The above code for main.c is incorrect, and print_board does get called, but init_board does not.

I've now got a lovely chess grid printing out.

Cheers dude. Needed a fresh pair of eyes to look on it..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.