As the title suggests i have come up with 2 ways of doing something which is the better way of doing it considering program structior memory allocation and processing speed.
first way
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct
{
int position_x;
int position_y;
bool king;
bool taken;
}peice;
int main()
{
peice white[12], black[12];
int i, row = 0, collum = 0;
for (i = 0; i < 12; i++)
{
white[i].position_x = collum;
white[i].position_y = row;
white[i].king = false;
white[i].taken = false;
collum += 2;
if (collum == 8)//first row done
{
collum = 1;
row = 1;
}
if (collum == 9)//second row done
{
collum = 0;
row = 2;
}
//* turns lines 36-38 on and off by commenting out
printf("white's co-ordinates are: %d:%d\n", white[i].position_x, white[i].position_y);
printf("white is king: %d\n", white[i].king);
printf("white is taken:%d\n", white[i].taken);
//*/
}
collum = 1;
row = 7;
for (i = 0; i < 12; i++)
{
black[i].position_x = collum;
black[i].position_y = row;
black[i].king = false;
black[i].taken = false;
collum += 2;
if (collum == 9) //first row done
{
collum = 0;
row = 6;
}
if (collum == 8)//second row done
{
collum = 1;
row = 5;
}
//* turns lines 60-62 on or off by commenting out
printf("black's co-ordinates are: %d:%d\n", black[i].position_x, black[i].position_y);
printf("black's king is: %d\n", black[i].king);
printf("black taken is: %d\n", black[i].taken);
//*/
}
return 0;
}
2nd way
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct
{
int position_x;
int position_y;
bool king;
bool taken;
}peice;
int main()
{
peice white[12], black[12];
int i, white_row = 0, white_column = 0, black_row = 7, black_column = 1;
for (i = 0; i < 12; i++)
{
white[i].position_x = white_column;
white[i].position_y = white_row;
white[i].king = false;
white[i].taken = false;
black[i].position_x = black_column;
black[i].position_y = black_row;
black[i].king = false;
black[i].taken = false;
white_column += 2;
black_column += 2;
if (white_column == 8) // 1st rows done
{
white_column = 1;
white_row = 1;
black_column = 0;
black_row = 6;
}
if (white_column == 9) // 2nd rows done
{
white_column = 0;
white_row = 2;
black_column = 1;
black_row = 5;
}
//*
printf("whites co-ordinates: %d:%d blacks co-ordinates: %d:%d\n", white[i].position_x, white[i].position_y, black[i].position_x, black[i].position_y);
printf("white king: %d black king: %d\n",white[i].king, black[i].king);
printf("white taken: %d black taken: %d\n",white[i].taken, black[i].taken);
//*/
}
return 0;
}
all comments welcome
coop