Do - while loop stops when condition is not finished - c

I need to copy a two-dimensional array into a second, identically-sized array, but the while loops finishes before I want it to, and I don't know why.
Here is my code.
memcpy( array2, array1, sizeof(array1) );
do
{
for( i = 0; i < rows; i++ )
{
for( j = 0; j < columns; j++ )
{
if( array1[i][j] == '!' )
{
if( array1[(i + 1)][j] == 'o' )
{
array2[(i + 1)][j] = '!';
condition1 = 1;
}
else
{
condition1 = 0;
}
if( array1[(i - 1)][j] == 'o' )
{
array2[(i - 1)][j] = '!';
condition2 = 1;
}
else
{
condition2 = 0;
}
if( array1[i][(j + 1)] == 'o' )
{
array2[i][(j + 1)] = '!';
contidion3 = 1;
}
else
{
condition3 = 0;
}
if( array1[i][(j - 1)] == 'o' )
{
array2[i][(j - 1)] = '!';
condition4 = 1;
}
else
{
condition4 = 0;
}
}
}
}
memcpy( array1, array2, sizeof(array2) );
count++;
}
while( condition1 != 0 && condition2 != 0 && condition3 != 0 && condition4 != 0 );
I've checked the state of each of the 4 condition variables, but every time I print them out I recieve 1 for all of them. I will be grateful for any advice.

Related

Segmentation fault C language strcat()

#include <stdio.h>
int main() {
char lexer_string[] = "echo Hello %a% %b%";
char temp_variable_name[128] = "...";
int variable_buff = 0;
int check = 0;
int detect_start = 0;
for (int i = 0; i < sizeof(lexer_string); i++) {
// printf("%c\n", lexer_string[i]);
if (lexer_string[i] == '%' && variable_buff == 0) {
check = 1;
variable_buff = 1;
}
if (variable_buff == 1) {
if (lexer_string[i] == '%' && detect_start == 0) {
detect_start = 1;
printf("%%\x20");
}
else if (lexer_string[i] == '%' && detect_start == 1) {
detect_start = 0;
printf(" \%\n");
}
else if (lexer_string[i] != '%') {
printf("%c", lexer_string[i]);
strcat(temp_variable_name, lexer_string[i]);
strcat(temp_variable_name, ' ');
}
}
if (lexer_string[i] == '%' && variable_buff == 1 && check != 1) {
variable_buff = 0;
}
if (lexer_string[i] == '\0') {
printf("Wynik:%s\n", temp_variable_name);
break;
}
check = 0;
}
return 0;
}
This is my simple code, when i use the function strcat() my code stops working. I tried change size of char array's, changed position of function in program (i move them to end but still doesn't work.)

Minesweeper in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
The game works perfectly, but I want to add a timer that measures and shows at the end of the game, the time the player's game lasted.
The code is a classic game of minesweeper where the user gives the coordinates of the point he wants to discover from the board, the game is made in c, not in C#.
The game consists of clearing all the squares of a two-dimensional arrangement that do not hide a mine. Some squares will have a random mine while others will not. The user is asked to position a box in the two-dimensional arrangement, if he does not find a mine he will continue the game until he wins, otherwise he will finish the game and the user will lose.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define FLUSH fflush(stdin)
void difficulty( void );
void beginner( void );
void intermediate( void );
void expert( void );
void minefield_generator( void );
void print_minefield( void );
void guess( void );
void boom( void );
void print_final_minefield( void );
void win( void );
void play_again( void );
void game_over( void );
int x, y;
int M, N;
float diff;
int total_mines = 0;
int mines = 0;
int minefield[30][30]; //This 2-D array contains all of the mines, numbers and blank spaces
int blank_minefield[30][30]; //This contains the minefield full of '|-|' characters
int final_minefield[30][30];
int main()
{
printf("\t\tWelcome to Minesweeper\n");
difficulty();
return 0;
}
void difficulty( void ) //Function for choosing the difficulty level
{
diff = 0;
while( (diff != 1) && (diff != 2) && (diff != 3) && (diff != 4))
{
printf("\t\tChoose a difficulty level(1-3) or 4 for a custom game:");
scanf("%f", &diff);
FLUSH;
if( (diff != 1) && (diff != 2) && (diff != 3) && (diff != 4))
{
printf("\t\tPlease enter either 1, 2, 3 or 4\n");
}
}
if( diff == 1 ) //If, else if and else statements that each go to the respective difficulty
{
beginner();
}
else if( diff == 2 )
{
intermediate();
}
else if( diff == 3 )
{
expert();
}
else if( diff == 4)
{
custom();
}
}
void beginner( void ) //Gives the minefield the 'beginner' grid and mines
{
M = 9;
N = 9;
total_mines = 10;
minefield_generator();
guess();
}
void intermediate( void ) //Gives the minefield the 'intermediate' grid and mines
{
M = 16;
N = 16;
total_mines = 40;
minefield_generator();
guess();
}
void expert( void ) //Gives the minefield the 'expert' grid size and mines
{
M = 16;
N = 30;
total_mines = 99;
minefield_generator();
guess();
}
void custom( void )
{
M = 0;
N = 0;
total_mines = 0;
printf("\t\tPlease enter the size of the dimensions you want\n");
printf("\t\tFirst value:\n");
scanf("%d", &M);
printf("\t\tSecond value:\n");
scanf("%d", &N);
printf("\t\tNumber of mines you want to assign to the board:\n");
scanf("%d", &total_mines);
minefield_generator();
guess();
}
void minefield_generator( void ) //Function that generates the minefield
{
int i = 0, j = 0;
srand( time( NULL ) ); //Starts the random no. generator
while( j < N ) //Nested loop for making the blank minefield and final minefield
{
while( i < M)
{
minefield[i][j] = '-';
blank_minefield[i][j] = minefield[i][j];
final_minefield[i][j] = minefield[i][j];
i++;
}
i = 0;
j++;
}
mines = 0;
while( mines < total_mines ) //Randomly generates the mines into the minefield
{
i = rand()%(M);
j = rand()%(N);
if( minefield[i][j] != '*') //If statement that checks if there is a mine already there and doesn't place a mine if there is
{
minefield[i][j] = '*';
final_minefield[i][j] = minefield[i][j];
mines++;
}
}
i = 0;
j = 0;
while( j < N ) //While loop that generates the numbers for any adjacent mines
{
while( i < M)
{
if( minefield[i][j] != '*')
{
minefield[i][j] = 0;
}
if((minefield[i-1][j-1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i-1][j] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i][j-1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i-1][j+1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i+1][j-1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i+1][j] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i][j+1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
if((minefield[i+1][j+1] == '*') && (minefield[i][j] != '*'))
{
minefield[i][j]++;
}
i++;
}
i = 0;
j++;
}
i = 0;
j = 0;
}
void print_minefield(void) // This function prints the minefield
{
int i = 0, j = 0, z = 0;
while( z < M ) // This while loop prints out the line of co-ordinates along the x axis of the minefield
{
if( z == 0 )
{
printf("\t");
}
printf("|%d|\t", z);
z++;
}
printf("\n\n");
while( j < N ) // Loop that prints out each character in the minefield
{
printf("|%d|\t", j);
while( i < M)
{
if( blank_minefield[i][j] == '-')
{
printf("|%c|\t", blank_minefield[i][j]);
}
else if( minefield[i][j] == 0 ) // This changes any spaces with values of zero to the character 'B'
{
blank_minefield[i][j] = 'B';
printf("|%c|\t", blank_minefield[i][j]);
}
else
{
printf("|%d|\t", blank_minefield[i][j]);
}
i++;
}
printf("\n");
i = 0;
j++;
}
}
void guess( void )
{
int q = 0, i=0, j=0, match=0;
print_minefield();
while( j < N ) // While loop for testing whether or not the user has cleared the minefield
{
while( i < M )
{
if(minefield[i][j] == blank_minefield[i][j])
{
match++;
}
i++;
}
i = 0;
j++;
}
if( match == (( M * N ) - total_mines)) // If the user has cleared the minefield, the win() function is run
{
win();
}
printf("\nEnter the x value, then a space, then the y value:");
scanf("%d %d", &x, &y); // Reading in the co-ordinates for the guess
FLUSH;
if( (x >= M) || (x < 0) || (y < 0) || (y >= N) )
{
printf("\nPlease enter a value inside the grid\n");
guess();
}
if( minefield[x][y] == '*' ) // Runs the boom() function if the user selects a mine
{
boom();
}
if( blank_minefield[x][y] != '-' )
{
printf("\nPlease enter a value that has not already been entered\n");
guess();
}
else // Checks if the adjacent spaces are blank, then changes the values in the blank_minefield array. Because they are changed, they will now print out in the print_minefield function
{
blank_minefield[x][y] = minefield[x][y];
if( minefield[x][y] == 0 )
{
if( minefield[x-1][y-1] == 0 )
{
blank_minefield[x-1][y] = minefield[x-1][y];
}
if( minefield[x-1][y] == 0 )
{
blank_minefield[x-1][y] = minefield[x-1][y];
}
if( minefield[x][y-1] == 0 )
{
blank_minefield[x][y-1] = minefield[x][y-1];
}
if( minefield[x-1][y+1] == 0 )
{
blank_minefield[x-1][y+1] = minefield[x-1][y+1];
}
if( minefield[x+1][y-1] == 0 )
{
blank_minefield[x+1][y-1] = minefield[x+1][y-1];
}
if( minefield[x+1][y] == 0 )
{
blank_minefield[x+1][y] = minefield[x+1][y];
}
if( minefield[x][y+1] == 0 )
{
blank_minefield[x][y+1] = minefield[x][y+1];
}
if( minefield[x+1][y+1] == 0 )
{
blank_minefield[x+1][y+1] = minefield[x+1][y+1];
}
}
guess();
}
}
void boom( void ) // Runs the print_final_minefield function, then the play_again function
{
print_final_minefield();
printf("\n\t\tYou hit a mine at %d,%d\n\t\tYOU LOSE!!!!", x, y);
play_again();
}
void print_final_minefield( void ) // Prints the minefield, showing where all of the mines are placed
{
int i = 0, j = 0, z = 0;
while( z < M )
{
if( z == 0 )
{
printf("\t");
}
printf("|%d|\t", z);
z++;
}
printf("\n\n");
while( j < N )
{
printf("|%d|\t", j);
while( i < M)
{
printf("|%c|\t", final_minefield[i][j]);
i++;
}
printf("\n");
i = 0;
j++;
}
}
void win( void ) // Runs the play_again function
{
printf("\n\n\n\t\t\tYOU WIN!!!!!\n\n\n");
play_again();
}
void play_again( void ) // Gives the user the option to play again
{
char option[2];
printf("\n\t\tWould you like to play again(Y/N)?:");
scanf("%c", &option[0]);
FLUSH;
if((option[0] == 'Y') || (option[0] == 'y')) // Restarts the program from after the welcome message
{
difficulty();
}
else if( (option[0] == 'N') || (option[0] == 'n'))
{
game_over();
}
else
{
printf("Please enter either Y or N");
play_again();
}
}
void game_over( void ) // Ends the program
{
printf("\n\n\t\tGame Over");
exit(1);
}
Use the function clock() to catch the moment’s clock, and then use the macro CLOCKS_PER_SEC to calculate how many second has gone by. Like this:
int timeInSeconds;
clock_t start = clock(), end,total;
...
end = clock();
total = end - start;
timeInSeconds = total/CLOCKS_PER_SEC;

Why does 'a' prematurely end this loop?

Pointers rather than straight answers if you will please.
This loop does some manipulation on chars and outputs a ciphertext c based on a key k and some plaintext p.
When 'a' or 'A' comes up in the plaintext, the program will output that letter as expected but then end the loop prematurely.
p suddenly becomes just one character long, this character being 1.
while (i < strlen(p))
{
char stdp = p[i];
char stdk = k[j];
if (isalpha(stdp))
{
if (islower(stdp))
{
p[i] = stdp - 'a';
Aa = 0;
}
else
{
p[i] = stdp - 'A';
Aa = 1;
}
if (islower(k[j]))
{
k[j] = stdk - 'a';
}
else
{
k[j] = stdk - 'A';
}
}
if (isalpha(stdp))
{
c[i] = ((p[i] + k[j]) % 26);
}
else
{
c[i] = p[i];
}
if (isalpha(stdp))
{
if (Aa == 1)
{
c[i] = c[i] + 'A';
}
else if (Aa == 0)
{
c[i] = c[i] + 'a';
}
}
if (isalpha(stdp))
{
if (j + 1 == kk)
{
j = (j + 1) % kk;
strcpy(k, argv[1]);
}
else
{
j++;
}
}
i++;
}
I have now solved the issues I was facing with the program terminating early.
Most of the issues seem to have been solved by stopping the constant refs to strlen(p) and just assigning that to a static variable. I have tried to tidy up the code as suggested.
int main(int argc, char *argv[])
{
if (argc != 2 || argv[1] == NULL)//no more than 1 arg and not empty
{
printf("Command requires one argument to run.\n");
return 1;
}
char *k = malloc(100);
strcpy(k, argv[1]);
int kInt = strlen(k);
for (int i = 0, n = kInt; i < n ; i++)// iterating though the key to check it is alphabetic
{
if (isalpha(k[i]) == false)
{
printf("Make sure key is alphabetic!\n");
return 1;
}
}
printf ("plaintext: ");
char *p = malloc(100);
strcpy(p, get_string());
int pInt = strlen(p);
int i = 0;
int j = 0;
int Aa = 0;
char *c = malloc(100);
while (j < kInt)
{
if (islower(k[j]))
{
k[j] = k[j] - 'a';
}
else
{
k[j] = k[j] - 'A';
}
j++;
}
j = 0;
while (i < pInt)
{
if (isalpha(p[i]))
{
if (islower(p[i]))
{
p[i] = p[i] - 'a';
Aa = 0;
}
else
{
p[i] = p[i] - 'A';
Aa = 1;
}
c[i] = ((p[i] + k[j]) % 26);
if (Aa == 1)
{
c[i] = c[i] + 'A';
}
else if (Aa == 0)
{
c[i] = c[i] + 'a';
}
if (j + 1 == kInt)
{
j = (j+1) % kInt;
}
else
{
j++;
}
}
else if (isalpha(p[i]) != true)
{
c[i] = p[i];
}
i++;
}
printf("ciphertext: %s\n", c);
free(c);
free(k);
free(p);
}

How to distinguish new line from EOF while reading from file

I am trying to build a program that could add up binary numbers, of arbitrary length, separated by a whitespace. I don't want to be limited by a maximum number length, therefore I'm doing everything without built-in number types, only chars. I read the numbers from a file. I perform everything on stack.
I can't loop the part where I read and add numbers. My guess is that it's because I can't find a way to distinguish new line from EOF. The program works this way: I read first number and save it into first stack, then I read second number and save in second stack. Then I add it up, save into third stack and transfer it to first stack. I read next number, save it into second stack, add up and so on. So I basically want to loop the second reading and summing. Here is the source code part:
while( !isspace(c=(char)getc(ifp))) {
if( c == '0' || c == '1'){
if(!push(&a , c)){
clearStack(&a);
printf("alloc error");
return 0;
}
}else{
clearStack(&a);
printf("error1");
return 0;
}
}
if( a.size == 0 ){
printf("error2");
return 0;
}
while (!feof(ifp)) {
while( !isspace(c=(char)getc(ifp))) {
if( c == '0' || c == '1'){
if(!push(&b , c)){
clearStack(&a);
clearStack(&b);
printf("alloc error");
return 0;
}
}
else{
//clearStack(&a);
//clearStack(&b);
printf("error3");
//return 0;
}
}
if( b.size == 0 ){
if (a.size == 0){
printf("error4");
}
}
topA = a.size;
topB = b.size;
carry = 0;
while( topA > 0 || topB > 0) {
if ( ( topA > 0 ) ){
topA--;
if(a.data[topA] == '1'){
x = 1;
}else{
x = 0;
}
}else{
x = 0;
}
if ( ( topB > 0 ) ){
topB--;
if(b.data[topB] == '1'){
y = 1;
}else{
y = 0;
}
}else{
y = 0;
}
sum = x + y + carry;
if (sum == 2 || sum == 3){
carry = 1;
}else{
carry = 0;
}
if (sum == 1 || sum == 3) {
c = '1';
}else{
c = '0';
}
if(!push(&output , c)){
clearStack(&a);
clearStack(&b);
clearStack(&output);
printf("alloc error");
return 0;
}
}
if (carry == 1) {
if(!push(&output , '1')){
clearStack(&a);
clearStack(&b);
clearStack(&output);
printf("alloc error");
return 0;
}
}
clearStack(&a);
for (i=0; i < output.size; i++){
push(&a , output.data[i]);
}
clearStack(&b);
clearStack(&output);
} /*end of EOF while*/
For some reason, although it reads multiple times, it reads wrong and works only for 2 numbers. The file looks like this: "1 1", but when its "1 1 1" it will just add first 2 numbers. I don't know where am I making a mistake. Below I attach the whole source code. I invoke the program in console like this: ./program file. I know that the case is poorly described but can't do it in a better manner, I will add comments if anything is needed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct stack {
char *data;
int size;
};
int push(struct stack * s, char c){
char * newData = (char *)realloc( s->data , (s->size + 1) * sizeof(char));
if( newData ){
s->data = newData;
s->data[s->size]=c;
s->size++;
return 1;
}else{
return 0;
}
}
/* not needed */
char pop(struct stack * s){
if(s->size > 0){
char result = s->data[s->size-1];
char * newData = (char *)realloc( s->data , s->size - 1);
if( newData ){
s->data = newData;
s->size--;
return result;
}else{
return 0;
}
}
else{
return 0;
}
}
struct stack newStack(){
struct stack s;
s.data = NULL;
s.size = 0;
return s;
}
void clearStack(struct stack * s){
free(s->data);
s->data = NULL;
s->size = 0;
}
int main (int argc, char const * argv[])
{
struct stack a = newStack();
struct stack b = newStack();
int topA, topB, x , y, carry, sum,i , start;
struct stack output = newStack();
char c;
FILE *ifp;
if(argc!=2){printf("Usage: ./a.out input_file."); return(1);}
if(!(ifp = fopen(argv[1], "r"))){printf("Unable to open input file!"); return(2);}
while( !isspace(c=(char)getc(ifp))) {
if( c == '0' || c == '1'){
if(!push(&a , c)){
clearStack(&a);
printf("alloc error");
return 0;
}
}else{
clearStack(&a);
printf("error1");
return 0;
}
}
if( a.size == 0 ){
printf("error2");
return 0;
}
while (!feof(ifp)) {
while( !isspace(c=(char)getc(ifp))) {
if( c == '0' || c == '1'){
if(!push(&b , c)){
clearStack(&a);
clearStack(&b);
printf("alloc error");
return 0;
}
}
else if (c == '\0' || c == ' ' || c == '\n'){
}
else{
//clearStack(&a);
//clearStack(&b);
printf("error3");
//return 0;
}
}
if( b.size == 0 ){
if (a.size == 0){
printf("error4");
//return 0;
}
}
topA = a.size;
topB = b.size;
carry = 0;
while( topA > 0 || topB > 0) {
if ( ( topA > 0 ) ){
topA--;
if(a.data[topA] == '1'){
x = 1;
}else{
x = 0;
}
}else{
x = 0;
}
if ( ( topB > 0 ) ){
topB--;
if(b.data[topB] == '1'){
y = 1;
}else{
y = 0;
}
}else{
y = 0;
}
sum = x + y + carry;
if (sum == 2 || sum == 3){
carry = 1;
}else{
carry = 0;
}
if (sum == 1 || sum == 3) {
c = '1';
}else{
c = '0';
}
if(!push(&output , c)){
clearStack(&a);
clearStack(&b);
clearStack(&output);
printf("alloc error");
return 0;
}
}
if (carry == 1) {
if(!push(&output , '1')){
clearStack(&a);
clearStack(&b);
clearStack(&output);
printf("alloc error");
return 0;
}
}
clearStack(&a);
for (i=0; i < output.size; i++){
push(&a , output.data[i]);
}
clearStack(&b);
clearStack(&output);
} /*end of EOF while*/
for (i=a.size - 1; i >= 0; i--){
if(a.data[i] == '1' || start){
start=1;
printf("%c", a.data[i]);
}
}
printf("\n");
clearStack(&a);
//clearStack(&b);
//clearStack(&output);
return 0;
}

C Recursive WordSearch solver in all directions

In the main file, I loop through each line of input until it hits the words, then I pass the word its searching for to startSearch with puzzleArray, the solvedArray I want to get back, the word as string, size as number of rows, and length as number of columns.
Currently, I keep getting segmentation faults/or endless loops. Any help over my algorithm/code would be greatly appreciated.
void startSearch(char** puzzleArray,char** solvedArray,char* string,int size,int length)
{
char* direction = "";
int solved = 1;
int j = 0;
while( j <= 7 && solved != 0)
{
if(j == 0)
{
direction = "up";
}
else if(j == 1)
{
direction = "upRight";
}
else if(j == 2)
{
direction = "right";
}
else if(j == 3)
{
direction = "downRight";
}
else if(j == 4)
{
direction = "down";
}
else if(j == 5)
{
direction = "downLeft";
}
else if(j == 6)
{
direction = "left";
}
else if(j == 7)
{
direction = "upLeft";
}
solved = recursiveSearch(puzzleArray,solvedArray,string,direction,size,length,0,0,0);
j++;
}
}
int recursiveSearch(char** puzzleArray,char** solvedArray,char* string,char* direction,int sizeOfPuzzle,int lengthOfArrayWithSpaces,int rowPos,int colPos,int stringPosition)
{
int lengthOfWord;
int i = rowPos;
int j = colPos;
int found = 0;
int empty = 1;
char c = string[stringPosition];
int position = stringPosition;
lengthOfWord = lengthOfArray(string);
if(string[position+1] == '\0')
{
return 0;
}
while(empty != 0)
{
if(string[stringPosition] == puzzleArray[i][j])
{
found = 1;
}
else if(rowPos < sizeOfPuzzle && colPos < lengthOfArrayWithSpaces)
{
stringPosition = 0;
for(i = rowPos; i < sizeOfPuzzle && found != 1; i++)
{
for(j = colPos; j < puzzleArray[rowPos][colPos] != '\0' && found != 1; j++)
{
if(string[stringPosition] == puzzleArray[i][j])
{
found = 1;
rowPos = i;
colPos = j;
stringPosition = 0;
}
}
}
if(found == 0)
{
empty = 1;
}
}
if(found == 1)
{
position = stringPosition + 1;
if(rowPos-1 >= 0)
{
//printf("\nString:%cPuzzleArray:%c",string[position],puzzleArray[rowPos-1][colPos]);
if(string[position] == puzzleArray[rowPos-1][colPos] && direction == "up")
{
//printf("UP");
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos,position+1))
{
solvedArray[rowPos-1][colPos] = puzzleArray[rowPos-1][colPos];
return 0;
}
}
else if(colPos+2 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos-1][colPos+2] && direction == "upRight")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos+2,position+1))
{
solvedArray[rowPos-1][colPos+2] = puzzleArray[rowPos-1][colPos+2];
return 0;
}
}
}
}
if(colPos+2 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos][colPos+2] && direction == "right")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos,colPos+2,position+1))
{
solvedArray[rowPos][colPos+2] = puzzleArray[rowPos][colPos+2];
return 0;
}
}
if(rowPos+1 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos+1][colPos+2] && direction == "downRight")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos+2,position+1))
{
solvedArray[rowPos+1][colPos+2] = puzzleArray[rowPos+1][colPos+2];
return 0;
}
}
}
}
if(rowPos+1 <= sizeOfPuzzle)
{
if(string[position] == puzzleArray[rowPos+1][colPos] && direction == "down")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos,position+1))
{
solvedArray[rowPos+1][colPos] = puzzleArray[rowPos+1][colPos];
return 0;
}
}
if(rowPos + 1 <= lengthOfArrayWithSpaces)
{
if(string[position] == puzzleArray[rowPos+1][colPos-2] && direction == "downLeft")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos-2,position+1))
{
solvedArray[rowPos+1][colPos-2] = puzzleArray[rowPos+1][colPos-2];
return 0;
}
}
}
}
if(colPos-2 >= 0)
{
if(string[position] == puzzleArray[rowPos][colPos-2] && direction == "left")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos+1,colPos+2,position+1))
{
solvedArray[rowPos+1][colPos+2] = puzzleArray[rowPos+1][colPos+2];
return 0;
}
}
if(rowPos - 1 >= 0)
{
if(string[position] == puzzleArray[rowPos-1][colPos-2] && direction == "upLeft")
{
if(recursiveSearch(puzzleArray,solvedArray,string,direction,sizeOfPuzzle,lengthOfArrayWithSpaces,rowPos-1,colPos-2,position+1))
{
solvedArray[rowPos-1][colPos-2] = puzzleArray[rowPos-1][colPos-2];
return 0;
}
}
}
}
}
}
return 1;
}
direction == "up"
This is not how you compare two strings to be equal. Use strcmp / strncmp for string comparison. This kind of comparison appears all over your code.
Also:
for(j = colPos; j < puzzleArray[rowPos][colPos] != '\0' && found != 1; j++)
This j < puzzleArray[rowPos][colPos] != '\0' looks dubious, what are you trying to do?

Resources