For Loop running infinite C - c

I am making a checksum error verifying program using C language but I have encountered an infinite loop while executing the program and I don't have any clue why I am getting this.
So what I have done in this program is that I am going to first convert characters of a string into respective ASCII codes and then checking for checksum error and for this I have made this program below.
#include <stdio.h>
int a[2], i, dec[10][8], de[2][8], j, k, add[8], carry = 0, n = 5;
int u[] = {0, 0, 0, 0, 0, 0, 0, 1};
char b[] = {'H', 'e', 'l', 'l', 'o'};
int main()
{
for (k = 0; k < 2; k++)
{
for (j = 0; j < 8; j++)
{
dec[k][j] = 0;
}
}
for (k = 0; k < 2; k++)
{
a[k] = b[k];
}
for (k = 0; k < 2; k++)
{
i = 0;
while (a[k] > 0)
{
dec[k][i] = a[k] % 2;
a[k] = a[k] / 2;
i++;
}
}
i = 0;
s:
for (k = 0; k < 2; k++)
{
for (j = 7; j >= 0; j--)
{
de[k][i] = dec[k][j];
i++;
}
i = 0;
}
for (k = 0; k < 2; k++)
{
for (j = 0; j < 8; j++)
{
printf("%d", de[k][j]);
}
printf("\n");
}
i = 0;
for (j = 7; j >= 0; j--)
{
if (de[i][j] == 1 && de[i + 1][j] == 1 && carry == 0)
{
add[j] = 0;
carry = 1;
}
else if (de[i][j] == 1 && de[i + 1][j] == 1 && carry == 1)
{
add[j] = 1;
carry = 1;
}
else if (de[i][j] == 0 && de[i + 1][j] == 1 && carry == 1)
{
add[j] = 0;
carry = 1;
}
else if (de[i][j] == 0 && de[i + 1][j] == 1 && carry == 0)
{
add[j] = 1;
carry = 0;
}
else if (de[i][j] == 0 && de[i + 1][j] == 1 && carry == 1)
{
add[j] = 0;
carry = 1;
}
else if (de[i + 1][j] == 0 && de[i][j] == 1 && carry == 0)
{
add[j] = 1;
carry = 0;
}
else if (de[i + 1][j] == 0 && de[i][j] == 0 && carry == 0)
{
add[j] = 0;
carry = 0;
}
else if (de[i + 1][j] == 0 && de[i][j] == 0 && carry == 1)
{
add[j] = 1;
carry = 0;
}
}
i = i + 2;
for (k = 0; k < 8; k++)
{
de[i][j] = add[j];
}
if (carry == 1 && i < n)
{
for (k = 0; k < 8; k++)
{
de[i + 1][j] = u[j];
}
goto s;
}
else if (carry == 0 && i < n)
{
for (k = 0; k < 8; k++)
{
de[i + 1][j] = u[j];
}
goto s;
}
for (i = 0; i < 8; i++)
printf("%d", add[i]);
return 0;
}

Related

Counting number of neighbors Conway's Game of Life

I have an error somewhere in this code.
The number of neighbors is not being counted correctly, as per my understanding. The neighbors function is probably where the issue is. My field variable is a 12x12 char array, the '#' is an alive cell and '-' is a dead one.
I am relatively new to programming and would appreciate some help with this.
int neighbors(int l, int c)
{
int num = 0;
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
if ((l+i < 0 || l+i > 12) && (c+j < 0 || c+j > 12))
{
continue;
}
else if ((i != 0 || j != 0) && field[(l + i)][(c + j)] == '#')
{
num++;
}
}
}
return num;
}
//game logic
void logic()
{
char temp[12][12];
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
temp[i][j] = field[i][j];
}
}
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
if (field[i][j] == '#')
{
if (neighbors(i, j) < 1 || neighbors(i, j) > 3)
{
temp[i][j] = '-';
}
else
{
temp[i][j] = '#';
}
}
if (field[i][j] = '-')
{
if (neighbors(i, j) == 3)
{
temp[i][j] = '#';
}
else
{
temp[i][j] = '-';
}
}
field[i][j] = temp[i][j];
}
}
}
If the array has dimensions 12x12, the maximum index value is 11, hence the test if ((l+i < 0 || l+i > 12) && (c+j < 0 || c+j > 12)) is incorrect. It should be:
if (l+i < 0 || l+i >= 12 || c+j < 0 || c+j >= 12)
continue;
Another major problem is you update field[i][j] = temp[i][j]; inside the update loop: this corrupts the computation for the neighbors of the adjacent cells. You should first compute the whole temp array and update field in a subsequent loop, or with a single call to memcpy().
Furthermore, the standard rules for Conway's Game of Life are somewhat different from your implementation: if (neighbors(i, j) < 1 || neighbors(i, j) > 3) keeps a cell with a single neighbour alive whereas under the standard rules it should die. Change this test to:
if (neighbors(i, j) < 2 || neighbors(i, j) > 3)
temp[i][j] = '-';
Here is a simplified version:
int neighbors(int l, int c) {
int num = 0;
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if (l+i >= 0 && l+i < 12 && c+j >= 0 && c+j < 12
&& (i != 0 || j != 0) && field[l+i][c+j] == '#') {
num++;
}
}
}
return num;
}
//game logic
void logic() {
char temp[12][12];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
temp[i][j] = field[i][j];
}
}
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
int nb = neighbors(i, j);
if (nb < 2 || nb > 3) {
temp[i][j] = '-';
} else
if (nb == 3) {
temp[i][j] = '#';
}
}
}
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
field[i][j] = temp[i][j];
}
}
}

SetConsoleCursorPosition : Getting black rows in console upon use

Following my previous question about making a snake program more fluid, I went and tried ANSI escape sequences and console functions to put back the text cursor on the top left corner.
I want to get the cursor on the top left corner of the screen, but while it does so I get black stripes across the screen every other line.
I am working in a windows environment making this program for the windows console.
Here is the painting function :
void paint(int tab[28][120], int ligneMax, int colonneMax, HANDLE hconsole)
{
//system("cls");
//printf("\033[1;1H");
COORD destCoord;
destCoord.X = 0;
destCoord.Y = 0;
SetConsoleCursorPosition(hconsole, destCoord);
for (int i = 0; i < ligneMax; i++)
{
for (int j = 0; j < colonneMax; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
I tried putting the escape code and console function in the main right before calling the paint function but I got the same results.
here is the whole program if someone wants to test :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
void paint(int tab[28][120], int ligneMax, int colonneMax, HANDLE hconsole)
{
//system("cls");
//printf("\033[1;1H");
COORD destCoord;
destCoord.X = 0;
destCoord.Y = 0;
SetConsoleCursorPosition(hconsole, destCoord);
for (int i = 0; i < ligneMax; i++)
{
for (int j = 0; j < colonneMax; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
void create(int tab[28][120], int Nbligne, int Nbcolonne,
int randligne, int randcols)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
tab[i][j] = ' ';
if (i == 0 || i == Nbligne - 1)
tab[i][j] = 205;
if (j == 0 || j == Nbcolonne - 1)
tab[i][j] = 186;
if (i == 0 && j == 0)
tab[i][j] = 201;
if (i == 0 && j == Nbcolonne - 1)
tab[i][j] = 187;
if (i == Nbligne - 1 && j == 0)
tab[i][j] = 200;
if (i == Nbligne - 1 && j == Nbcolonne - 1)
tab[i][j] = 188;
if (i == 14 && j == 60)
tab[i][j] = 219;
if (i == 14 && j == 59)
tab[i][j] = 79;
if (i == 14 && j == 58)
tab[i][j] = 35;
if (i == randligne && j == randcols)
tab[i][j] = 176;
}
}
}
void destroyTail(int tab[28][120], int Nbligne, int Nbcolonne)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if (tab[i][j] == 35)
{
tab[i][j] = ' ';
if (tab[i][j + 1] == 79)
tab[i][j + 1] = 35;
else if (tab[i][j - 1] == 79)
tab[i][j - 1] = 35;
else if (tab[i + 1][j] == 79)
tab[i + 1][j] = 35;
else if (tab[i - 1][j] == 79)
tab[i - 1][j] = 35;
goto stop;
}
}
}
stop: NULL;
}
void translate(int tab[28][120], char direction, int Nbligne, int Nbcolonne)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if (tab[i][j] == 219)
{
if (direction == 'R')
{
tab[i][j] = 79;
tab[i][j + 1] = 219;
}
if (direction == 'D')
{
tab[i][j] = 79;
tab[i + 1][j] = 219;
}
if (direction == 'L')
{
tab[i][j] = 79;
tab[i][j - 1] = 219;
}
if (direction == 'U')
{
tab[i][j] = 79;
tab[i - 1][j] = 219;
}
goto stop;
}
}
}
stop: NULL;
}
int checkExpand(int tab[28][120], int Nbligne, int Nbcolonne, char direction)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if ((direction == 'R' && tab[i][j] == 219 && tab[i][j + 1] == 176) ||
(direction == 'L' && tab[i][j] == 219 && tab[i][j - 1] == 176) ||
(direction == 'U' && tab[i][j] == 219 && tab[i - 1][j] == 176) ||
(direction == 'D' && tab[i][j] == 219 && tab[i + 1][j] == 176))
return 1;
}
}
return 0;
}
int checkDeath(int tab[28][120], int Nbligne, int Nbcolonne, char direction)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if ((direction == 'R' && tab[i][j] == 219 && (tab[i][j + 1] == 186 || tab[i][j + 1] == 79)) ||
(direction == 'L' && tab[i][j] == 219 && (tab[i][j - 1] == 186 || tab[i][j - 1] == 79)) ||
(direction == 'U' && tab[i][j] == 219 && (tab[i - 1][j] == 205 || tab[i - 1][j] == 79)) ||
(direction == 'D' && tab[i][j] == 219 && (tab[i + 1][j] == 205 || tab[i + 1][j] == 79)))
return 1;
}
}
return 0;
}
int main()
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 241);
int tab[28][120];
int randligne = rand() % 26 + 1;
int randcols = rand() % 118 + 1;
create(tab, 28, 120, randligne, randcols);
paint(tab, 28, 120, hConsole);
char i = '1';
char direction = 'R';
int eaten = 0;
int expand = 0;
int death = 0;
while(i != 'k')
{
if (kbhit())
i = getch();
switch(i) {
case 'z':
if (direction != 'D')
direction = 'U';
break;
case 's':
if (direction != 'U')
direction = 'D';
break;
case 'd':
if (direction != 'L')
direction = 'R';
break;
case 'q':
if (direction != 'R')
direction = 'L';
break;
}
randligne = rand() % 26 + 1;
randcols = rand() % 118 + 1;
death = checkDeath(tab, 28, 120, direction);
if (death == 1)
break;
translate(tab, direction, 28, 120);
expand = checkExpand(tab, 28, 120, direction);
if (expand == 0)
destroyTail(tab, 28, 120);
else
{
while (tab[randligne][randcols] != ' ')
{
randligne = rand() % 26 + 1;
randcols = rand() % 118 + 1;
}
tab[randligne][randcols] = 176;
eaten++;
}
printf("Number of biscuits eaten : %d ; direction : %c ; expand : %d", eaten, direction, expand);
paint(tab, 28, 120, hConsole);
Sleep(100);
}
while(i != 'k')
{
if (kbhit())
i = getch();
}
}
Too much code to take in quickly. If you have Windows console ouput for the snake, can't you simply printf the next line and leave it at that?
If you want to hide the cursor (instead of trying to park it out of sight) you can make it invisible by calling SetConsoleCursorInfo and the struct passed is shown here.

15 Puzzle Program, stuck on up left right down commands

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (void){
//Function used to set up your game board
int disp[4][4];
int i, j;
int game_over = 0;
for(i = 0; i < 4; i++) {
for(j = 0;j < 4; j++) {
printf("Enter a value from 1 - 15 for %d %d: ", i, j);
scanf("%d", &disp[i][j]);
}
}
//Function used to display the game board
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
printf(" _ ");
else if (disp[i][j] < 10)
printf(" %d ",disp[i][j]);
else
printf("%d ",disp[i][j]);
}
}
// Function to show where cursor is
for(i = 0; i < 4; i++){
printf("\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
printf("");
}
}
//Skeleton to function to controller
do{
char move[10];
printf("Enter a move: ");
scanf("%s", move);
if (strcmp("up", move) == 0){
for(i = 0; i < 4; i++){
printf("");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i >= 1){
int temp = disp[i][j];
disp[i][j] = disp[i - 1][j];
disp[i - 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("Up", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 1){
int temp = disp[i][j];
disp[i][j] = disp[i - 1][j];
disp[i - 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("UP", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 1){
int temp = disp[i][j];
disp[i][j] = disp[i - 1][j];
disp[i - 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("down", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i < 4){
int temp = disp[i][j];
disp[i][j] = disp[i + 1][j];
disp[i + 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("Down", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i < 4){
int temp = disp[i][j];
disp[i][j] = disp[i + 1][j];
disp[i + 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("DOWN", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i < 4){
int temp = disp[i][j];
disp[i][j] = disp[i + 1][j];
disp[i + 1][j] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("left", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (j >= 1){
int temp = disp[i][j];
disp[i][j] = disp[i][j - 1];
disp[i][j - 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("Left", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 1){
int temp = disp[i][j];
disp[i][j] = disp[i][j + 1];
disp[i][j + 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("LEFT", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 1){
int temp = disp[i][j];
disp[i][j] = disp[i][j + 1];
disp[i][j + 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("right", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (j <= 4){
int temp = disp[i][j];
disp[i][j] = disp[i][j + 1];
disp[i][j + 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("Right", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 4){
int temp = disp[i][j];
disp[i][j] = disp[i][j - 1];
disp[i][j - 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else if (strcmp("RIGHT", move) == 0){
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i > 4){
int temp = disp[i][j];
disp[i][j] = disp[i][j - 1];
disp[i][j - 1] = temp;
}
else {
printf("Invalid move!");
}
}
}
}
else
printf("Invalid Move!");
//Update the board function
for(i = 0; i < 4; i++){
printf("\n\n");
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
printf(" _ ");
else if (disp[i][j] < 10)
printf(" %d ",disp[i][j]);
else
printf("%d ",disp[i][j]);
}
}
}
while (game_over == 0);
return 0;
}
I am having trouble making the cursor go right or down? I can go left and up, but not the other two. I'm not looking for someone to answer the entire program its just I have been stuck on this for hours and every other code I look at sees similar to mine.I can get it to correctly do up and left but right and down cause a problem and not sure why.
The bug is the checking logic is wrong and the bad practice you followed is lots of repetitive code.
Well I have boiled the code to something simple.
...
else if (strcmp("down", move) == 0 || strcmp("Down", move) == 0 || strcmp("DOWN", move) == 0){
int d = 0;
for(i = 0; i < 4; i++){
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if (i+1 < 4){ // <--- notice the change
int temp = disp[i][j];
disp[i][j] = disp[i + 1][j];
disp[i + 1][j] = temp;
d = 1;
break; // <-- and this
}
else {
printf("%s","Invalid move!");
}
}
if( d == 1)
break;
}
}
else if (strcmp("right", move) == 0 || strcmp("Right", move) == 0 || strcmp("RIGHT", move) == 0){
for(i = 0; i < 4; i++){
for(j = 0; j < 4; j++){
if (disp[i][j] == -1)
if ( j+1 < 4){ // <--- notice the change
int temp = disp[i][j];
disp[i][j] = disp[i][j + 1];
disp[i][j + 1] = temp;
break; // <-- and this
}
else {
printf("%s","Invalid move!");
}
}
}
}
...
You can check that there is a break statement in case of down and right. The reason is, when you swap -1 to the right or down then you will meet up again in the loop iteration.
So without the break the result would be
_ 1 2 3
4 5 6 7
8 9 1 2
3 4 5 6
>>> right
1 2 3 _
4 5 6 7
8 9 1 2
3 4 5 6
Another thing is don't repeat code. The readability is worse when you do that. Maintaining the code is even harder. Even better here you can write swap() logic and use it. And also the movement of the tiles can also be done with another function.
Tricky downlogic (Edit-1).
In case of downlogic the simple breaking up the loop wont work because in case you do that there will be a chance that in next iteration it will again get -1 and try to swap it with next number.

by using scanf stopped working

here is my code a part of a project when i initialize CHAR_1,CHAR_2 it works but when i read them by scanf it stopped working.
int repair_beta(char beta[11][11])
{
int i = 0, j = 0;
int count = 0, n = 0;
int fail = 0;
for(i = 1; i < 11; i++)
{
for(j = 1; j < 11; j++)
{
if(beta[i][j] == stronghold)
{
count++;
}
}
}
if(count == 4)
{
return 1;
}
else if(count < 4)
{
char CHAR_1 = 'A', CHAR_2 = '5';
for(n = 0; n < 3; n++)
{
printf("\n\t\t <<choose to repair>>\n\t\t\t ");
scanf("%c", &CHAR_1);
scanf("\n%c", &CHAR_2);
for(i = 1; i < 11; i ++)
{
if(CHAR_1 == 'A' + i - 1)
{
break;
}
}
for(j = 1; j < 11; j++)
{
if(CHAR_2 == '0' + j - 1)
{
break;
}
}
if(beta[i][j] == LAND || beta[i][j] == land)
{
beta[i][j] = stronghold;
prize_beta--;
printf("\n\t\t\t <<repaired>>");
printf("\n****************************************************************************\n");
return 1;
}
else
{
fail++;
}
}
if(fail == 3)
{
printf("\n\t\t <<you failed to repair>>");
prize_beta;
printf("\n****************************************************************************\n");
}
}
}
it is my firs time i use stackoverflow so excuse me if i ask my question in bad way.

C algorithm to try out all the possible combinations of 12 knights in a chess board

I have been writing this program that tries to find how to put 12 knights on a chess board so all squares are either taken up by a knight or one of 12 knights can reach them in one move. So far I have created 2 functions: one takes a board with 12 knights and fills all the squares they can reach with 'T' and another that takes a filled chess board and checks if there are left any squares not taken up by a knight nor dominated by it (meaning no knight can reach it in one move).
Now I am dealing with the problem of how can I try out all the possible combinations of knights. My idea is that after every single combination of 12 knights on a board, I will send the board with 12 knights to be filled with 'T's for all the squares they can reach, and then send it to another function to check if all squares are dominated.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int filling(char array[8][8])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (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 checking(char array[8][8])
{
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;
}
}
return 1;
}
int main()
{
int i, j;
char board[8][8];
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
board[i][j] = '0';
}
// The following lines are here to test if the checking and filling work
/*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 (" \n Works");
else printf ("\n Doesnt work");
for(i = 0; i < 8; i++)
{
printf("\n");
for(j = 0; j < 8; j++)
printf("%c ", board[i][j]);
}
return 0;
}
What kind of algorithm I could use to try out every combo? Thank you for your answers.
You need these things:
A sorting rule that puts all possible combinations in a well-defined order.
An initialization rule that defines the board's first such combination.
An increment rule that transitions a board from its current combination to the next combination in the well-defined order.
A rule that detects when the board is in the last combination.
Then you just use algorithm 2 to put the board in the first state. Then check with algorithm 4 to see if you're in the last state. If not, use algorithm 3 to go to the next state.
Algorithm 1 is probably the hard one. One simple rule is just to convert the board's position to a binary number with a zero for an empty square and a one for a full square in a well-defined order, say starting from the top left and going across, then moving to the next row.
If you're on decent, or even semi-decent hardware, you've got a 64-bit unsigned integral type available to you. Let's call that uint64_t.
You can store a chessboard with knight positions as a single uint64_t. You can also store a "dominated" mask in the same type. You simply establish a correlation between the 64 bits in the type and the 64 spaces on the chessboard.
Since there are 64 possible locations, you can pre-compute the possible threat masks for each position, storing that as an array of 64 uint64_t values.
You need to set the position of each knight. But you can safely do them in a certain order, so that knight #1 is always the "highest" knight - his bit position is always highest, or lowest, or whatever. So you could write code like this:
for (k0 = 64; k0 > 11; --k0)
for (k1 = k0 - 1; k1 > 10; --k1)
for (k2 = k1 - 1; k2 > 9; --k2)
...
for (k11 = k10 - 1; k11 >= 0; --k11)
/* do stuff */
But that's horrible, because there are a squillion possibilities (squillion = 1573144097507348889600, thanks #jwd!).
That said, the threat mask for each knight can be computed "incrementally" from the masks of the outer knights, so it might be faster to perform all the computations than to try to store/mark/compute the rotations and flips of the board.
Something like this:
for (kXX = kYY - 1; kXX > (11-XX); --kXX) {
threat_XX = threat_YY | pre_computed_threat[kXX];
for (kZZ = KXX - 1; kZZ > (11-ZZ); --kZZ) {
threat_ZZ = threat_XX | pre_computed_threat[kZZ];
The nice thing about this approach is that your objective is total coverage by threats - all 1 bits in the threat_11 map, in other words. You can test just by inverting the bits and comparing with zero.
Well thanks to my questions of today I got myself blocked from asking more until my rep gets better, but for what its worth I managed to solve the problem myself. I had to take 3 positions in each quarter of the board that can only be reached by seperate horses, that way making my 12 for cycles shorter, as they only had to go through specific locations, instead of trying every single of of them. The execution times is just above one second and it finds two different layouts for 12 knights, marking them 'H' and the positions they can reach in one turn 'T'. Here is the code if anyone ever comes to a problem like this:
#include <stdio.h>
#include <stdlib.h>
struct horses
{
int x;
int y;
};
void fill(struct horses arkliai[12], char array[8][8])
{
int i,j;
for(i = 0; i < 8; i++)
{
for(j = 0; j < 8; j++)
array[i][j] = '0';
}
for(i = 0; i < 12; i++)
{
array[arkliai[i].x][arkliai[i].y] = 'H';
}
pildymas(array);
}
void startingpositions(struct horses arkliai[12])
{
int i = 0;
int j = 0;
int a = 0;
for(a = 0; a < 12; a++)
{
if(i > 7)
{
i = 0;
j++;
}
arkliai[a].x = i;
arkliai[a].y = j;
i++;
}
}
void combinacijos(struct horses h[12], char array[8][8])
{
int a,b,c,d,e,f,g,hi,ii,ji,k,l;
for(a = 0; a < 2; a++)
{
if(a == 0)
{
h[0].x = 1;
h[0].y = 2;
}
if(a == 1)
{
h[0].x = 2;
h[0].y = 1;
}
for(b = 0; b < 2; b++)
{
if(b == 0)
{
h[1].x = 5;
h[1].y = 1;
}
if(b == 1)
{
h[1].x = 6;
h[1].y = 2;
}
for(c = 0; c < 2; c++)
{
if(c == 0)
{
h[2].x = 1;
h[2].y = 5;
}
if(c == 1)
{
h[2].x = 2;
h[2].y = 6;
}
for(d = 0; d <2;d++)
{
if(d == 0)
{
h[3].x = 5;
h[3].y = 6;
}
if(d == 1)
{
h[3].x = 6;
h[3].y = 5;
}
for(e = 0; e < 3; e++)
{
if(e == 0)
{
h[4].x = 2;
h[4].y = 0;
}
if(e == 1)
{
h[4].x = 2;
h[4].y = 2;
}
if(e == 2)
{
h[4].x = 1;
h[4].y = 3;
}
for (f = 0; f < 3; f++)
{
if(f == 0)
{
h[5].x = 1;
h[5].y = 4;
}
if(f == 1)
{
h[5].x = 2;
h[5].y = 5;
}
if(f == 2)
{
h[5].x = 2;
h[5].y = 7;
}
for (g = 0; g < 3; g++)
{
if(g == 0)
{
h[6].x = 5;
h[6].y = 7;
}
if(g == 1)
{
h[6].x = 5;
h[6].y = 5;
}
if(g == 2)
{
h[6].x = 6;
h[6].y = 4;
}
for(hi = 0; hi < 3; hi++)
{
if(hi == 0)
{
h[7].x = 5;
h[7].y = 0;
}
if(hi == 1)
{
h[7].x = 5;
h[7].y = 2;
}
if(hi == 2)
{
h[7].x = 6;
h[7].y = 3;
}
for(ii = 0; ii < 4; ii++)
{
if (ii == 0)
{
h[8].x = 3;
h[8].y = 0;
}
if (ii == 1)
{
h[8].x = 3;
h[8].y = 2;
}
if (ii == 2)
{
h[8].x = 0;
h[8].y = 3;
}
if (ii == 3)
{
h[8].x = 2;
h[8].y = 3;
}
for(ji = 0; ji < 4; ji++)
{
if (ji == 0)
{
h[9].x = 3;
h[9].y = 7;
}
if (ji == 1)
{
h[9].x = 3;
h[9].y = 5;
}
if (ji == 2)
{
h[9].x = 2;
h[9].y = 4;
}
if (ji == 3)
{
h[9].x = 0;
h[9].y = 4;
}
for(k = 0; k < 4; k++)
{
if (k == 0)
{
h[10].x = 4;
h[10].y = 7;
}
if (k == 1)
{
h[10].x = 4;
h[10].y = 5;
}
if (k == 2)
{
h[10].x = 5;
h[10].y = 4;
}
if (k == 3)
{
h[10].x = 7;
h[10].y = 4;
}
for(l = 0;l < 64;l++)
{
if(h[11].x == 7)
{
if(h[11].y == 7)
{
h[11].x = 0;
h[11].y = 0;
break;
}
h[11].x = 0;
h[11].y = h[11].y +1;
}
else { h[11].x= h[11].x+1; }
fill(h, array);
}
}
}
}
}
}
}
}
}
}
}
}
}
int pildymas(char array[8][8])
{
int i, j;
for (i = 0;i < 8;i++)
{
for (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';
}
}
}
tikrinimas(array);
}
int tikrinimas(char array[8][8])
{
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;
}
}
printas(array);
}
int printas(char array[8][8])
{
int i,j;
for(j = 0; j <8; j++)
{
printf("\n");
for(i = 0; i <8 ; i++)
printf("%c ", array[i][7-j]);
}
printf("\n");
}
int main()
{
struct horses hr[12];
char board[8][8];
startingpositions(hr);
combinacijos(hr, board);
return 0;
}

Resources