C Filling Chess board with T and H, somehow there are gaps and only 7 rows instead of 8 -
i had previous question closed broad, decided write code in parts , ask if had specific problem.
my code far sees parts of chess board filled knights (in code 'h') , fill parts dominate 't'. goal fill parts of chess board 'h' , 't' , have not dominated blocks left @ '0'. program tests if fields either 't' or 'h', if yes returns 1, if no 0. prints out chess board.
however prints out 7 rows (still 8 columns though), , though filled board 12 knights in position dominate entire board, somehow there still 0s left. here code:
#include <stdio.h> #include <stdlib.h> int filling(char array[7][7]) { int i, j; (i = 0;i < 8;i++) { (j = 0; j < 8;j++) { if(array[i][j] == 'h') { if(i-1>-1 && j+2<8) array[i-1][j+2]='t'; if(i+1<8 && j+2<8) array[i+1][j+2]='t'; if(i-2>-1 && j+1<8) array[i-2][j+1]='t'; if(i+2<8 && j+1<8) array[i+2][j+1]='t'; if(i-2>-1 && j-1>-1) array[i-2][j-1]='t'; if(i+2<8 && j-1>-1) array[i+2][j-1]='t'; if(i-1>-1 && j-2>-1) array[i-1][j-2]='t'; if(i+1<8 && j-2>-1) array[i+1][j-2]='t'; } } } } int checking(char array[7][7]) { int i, j; for(i = 0;i < 8;i++) { for(j = 0;j < 8;j++) { if(array[i][j] != 'h' && array[i][j] != 't') return 0; else return 1; } } } int main() { int i, j; char board[7][7]; for(i = 0; < 8; i++) { for(j = 0; j < 8; j++) board[i][j] = '0'; } board[2][1] = 'h'; board[2][2] = 'h'; board[3][2] = 'h'; board[5][2] = 'h'; board[6][2] = 'h'; board[5][3] = 'h'; board[2][4] = 'h'; board[1][5] = 'h'; board[2][5] = 'h'; board[4][5] = 'h'; board[5][5] = 'h'; board[5][6] = 'h'; filling(board); if(checking(board) == 1) printf ("works"); else printf ("doesnt work"); for(i = 0; < 8; i++) { printf("\n"); for(j = 0; j < 8; j++) printf("%c ", board[i][j]); } return 0; }
what think problem here? doing worng? thank answers.
edit: made mistake not making sure horses dont changed 't's. here fixed code:
#include <stdio.h> #include <stdlib.h> int pildymas(char array[7][7]) { int i, j; (i = 0;i < 8;i++) { (j = 0; j < 8;j++) { if(array[i][j] == 'h') { if(i-1>-1 && j+2<8 && array[i-1][j+2]!='h') array[i-1][j+2]='t'; if(i+1<8 && j+2<8 && array[i+1][j+2]!='h') array[i+1][j+2]='t'; if(i-2>-1 && j+1<8 && array[i-2][j+1]!='h') array[i-2][j+1]='t'; if(i+2<8 && j+1<8 && array[i+2][j+1]!='h') array[i+2][j+1]='t'; if(i-2>-1 && j-1>-1 && array[i-2][j-1]!='h') array[i-2][j-1]='t'; if(i+2<8 && j-1>-1 && array[i+2][j-1]!='h') array[i+2][j-1]='t'; if(i-1>-1 && j-2>-1 && array[i-1][j-2]!='h') array[i-1][j-2]='t'; if(i+1<8 && j-2>-1 && array[i+1][j-2]!='h') array[i+1][j-2]='t'; } } } } int tikrinimas(char array[7][7]) { int i, j; for(i = 0;i < 8;i++) { for(j = 0;j < 8;j++) { if(array[i][j] != 'h' && array[i][j] != 't') return 0; else return 1; } } } int main() { int i, j; char board[7][7]; for(i = 0; < 8; i++) { for(j = 0; j < 8; j++) board[i][j] = '0'; } board[2][1] = 'h'; board[2][2] = 'h'; board[3][2] = 'h'; board[5][2] = 'h'; board[6][2] = 'h'; board[5][3] = 'h'; board[2][4] = 'h'; board[1][5] = 'h'; board[2][5] = 'h'; board[4][5] = 'h'; board[5][5] = 'h'; board[5][6] = 'h'; for(i = 0; < 8; i++) { printf("\n"); for(j = 0; j < 8; j++) printf("%c ", board[i][j]); } pildymas(board); if(tikrinimas(board) == 1) printf ("veikia"); else printf ("neveikia"); for(i = 0; < 8; i++) { printf("\n"); for(j = 0; j < 8; j++) printf("%c ", board[i][j]); } return 0; }
i notice people array doesnt have enough size. under impression array a[2] have a[0] a[1] , a[2] spots, wrong?
thank help, pretty sure tonight though :)
extra question
i have working filling , testing, need find way fill board possible combinations of 12 horses, , way have in mind creating cycle 12 levels deep, meaning have 64^12 cycles. there other way try every possible positioning of 12 horses on 8x8 board? plan take position, test if fits, if save it, , try 1 until positions done. far know there 1\2 combinations of 12 horses dominate fields on board.
i have no idea type of dominance attempting create on board, indexes within correct bounds 7x7
array, need similar following:
#include <stdio.h> enum { dim = 7 }; void filling (char (*array)[dim]) { int i, j; (i = 0; < dim; i++) (j = 0; j < dim; j++) if (array[i][j] != 'h') array[i][j] = 't'; } int checking (char (*array)[dim]) { int i, j; (i = 0; < dim; i++) (j = 0; j < dim; j++) if (array[i][j] != 'h' && array[i][j] != 't') return 0; return 1; } int main (void) { int i, j; char board[dim][dim] = {{0}}; board[2][1] = 'h'; board[2][2] = 'h'; board[3][2] = 'h'; board[5][2] = 'h'; board[6][2] = 'h'; board[5][3] = 'h'; board[2][4] = 'h'; board[1][5] = 'h'; board[2][5] = 'h'; board[4][5] = 'h'; board[5][5] = 'h'; board[5][6] = 'h'; filling (board); if (checking (board) == 1) printf ("works"); else printf ("doesnt work"); (i = 0; < dim; i++) { printf ("\n"); (j = 0; j < dim; j++) printf ("%c ", board[i][j]); } putchar ('\n'); return 0; }
example use/output
$ ./bin/chess works t t t t t t t t t t t t h t t h h t h h t t t h t t t t t t t t t h t t t h h t h h t t h t t t t
or beauty in eliminating magic numbers code , using proper constants changing 1
number needed create 8x8
. e.g.
enum { dim = 8 };
and output is:
$ ./bin/chess works t t t t t t t t t t t t t h t t t h h t h h t t t t h t t t t t t t t t t h t t t t h h t h h t t t h t t t t t t t t t t t t t
Comments
Post a Comment