Entering into the wrong If statement - c++

the program does not enter the if statement it suppose to enter.for example when the sentence1 is oguzhan and the sentence2 is bugrahan for first characters it should enter the first if statement end substitution should be 4 but it doesn't.
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <cmath>
using namespace std;
int main() {
char sentence1[50];
char sentence2[50];
int m, n, k, l;
int i, j, substitution;
cout << "Enter the first word:" << endl;
cin >> sentence1;
cout << "Enter the second word:" << endl;
cin >> sentence2;
m = strlen(sentence1);
n = strlen(sentence2);
int cost[m + 1][n + 1];
cost[0][0] = 0;
for (i = 1; i < m + 1; i++) {
cost[i][0] = cost[i - 1][0] + 2;
}
for (j = 1; j < n + 1; j++) {
cost[0][j] = cost[0][j - 1] + 2;
}
for (i = 1; i < m + 1; i++) {
for (j = 1; j < n + 1; j++) {
if ((sentence1[i - 1] == 'a' || sentence1[i - 1] == 'u' ||
sentence1[i - 1] == 'e' || sentence1[i - 1] == 'i' ||
sentence1[i - 1] == 'o') &&
(sentence2[j - 1] != 'a' || sentence2[j - 1] != 'u' ||
sentence2[j - 1] != 'e' || sentence2[j - 1] != 'i' ||
sentence2[j - 1] != 'o')) {
substitution = 4;
}
if ((sentence1[i - 1] != 'a' || sentence1[i - 1] != 'u' ||
sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' ||
sentence1[i - 1] != 'o') &&
(sentence2[j - 1] == 'a' || sentence1[i - 1] != 'u' ||
sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' ||
sentence1[i - 1] != 'o')) {
substitution = 4;
}
if (sentence1[i - 1] == sentence2[j - 1]) {
substitution = 0;
}
if ((sentence1[i - 1] == 'a' || sentence1[i - 1] == 'u' ||
sentence1[i - 1] == 'e' || sentence1[i - 1] == 'i' ||
sentence1[i - 1] == 'o') &&
(sentence2[j - 1] == 'a' || sentence2[j - 1] == 'u' ||
sentence2[j - 1] == 'e' || sentence2[j - 1] == 'i' ||
sentence2[j - 1] == 'o')) {
substitution = 3;
}
if ((sentence1[i - 1] != 'a' || sentence1[i - 1] != 'u' ||
sentence1[i - 1] != 'e' || sentence1[i - 1] != 'i' ||
sentence1[i - 1] != 'o') &&
(sentence2[j - 1] != 'a' || sentence2[j - 1] != 'u' ||
sentence2[j - 1] != 'e' || sentence2[j - 1] != 'i' ||
sentence2[j - 1] != 'o')) {
substitution = 3;
}
cost[i][j] = min(min(cost[i - 1][j] + 2, cost[i][j - 1] + 2),
cost[i - 1][j - 1] + substitution);
}
}
for (i = 0; i < m + 1; i++) {
for (j = 0; j < n + 1; j++) {
cout << cost[i][j] << " ";
}
cout << endl;
}
cout << sentence1[0];
return 0;
}

A condition like: sentence2[j-1]!='a'||sentence2[j-1]!='u' is always true -- no single character can be equal to both a and u, so one of these has to be true.
If you're using !=, it must almost always be joined by &&, not ||, otherwise the result will always be true, regardless of input.

Related

Checking correctness of infix expression in c++

I read an infix expression from the file, how can I check if it is a true or false input? I can only do 1 such case. I can think many situations but I don't know how to check in c++. Everyone please help me. Thanks very much!
int32_t check_error(string& str)
{
unsigned len = str.length();
if (str[0] != '(' || str[len-1] != ')')
{
return -1;
}
for (unsigned i = 3; i < len-3; i++)
{
if (str[i] >='0' && str[i] <= '9' && str[i + 1] = ' ' && str[i + 2] >= '0' && str[i + 2] <= '9')
return -2;
if ((str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/' || str[i] == '%' || str[i] == '^') && (str[i - 2] < '0' || str[i - 2]>'9' || str[i + 2] < '0' || str[i + 2]>'9'))
return -3;
if (str[i] == ' ' && str[i + 1] == ' ')
return -4;
}
return 0;
}

How can I fix the "Invalid memory reference" error in my code?

I'm working on a maze-solver robot for my arduino project. I want my robot to memorize the maze and then find the shortest path. I keep having a problem when the char array's lenght is 3.
The problem appears when the lenght is <= 3, so I tried diffrent stuff to make a particular case out of that, that's why the if (strlen(a) > 3) is there.
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int main()
{
char a[] = "LLLBLLLRBLLBSRSRS";
char b[200];
while(strcmp(a, b) != 0) {
strcpy(b, a); //sa verific daca se schimba sirul, daca nu inseamna ca a ajuns la minim
for(int i = 0; i < strlen(a) - 2; i++)
{
if(a[i] == 'L' && a[i + 1] == 'B' && a[i + 2] == 'R') //if urile astea cauta combinatii de cate 3 miscari sa optimizezi drumul
{
a[i] = 'B';
if (strlen(a) > 3) strcpy(a + i + 1, a + i + 3);
else a[i + 1] = '\0';
}
else if(a[i] == 'L' && a[i + 1] == 'B' && a[i + 2] == 'S')
{
a[i] = 'R';
if (strlen(a) > 3) strcpy(a + i + 1, a + i + 3);
else a[i + 1] = '\0';
}
else if(a[i] == 'L' && a[i + 1] == 'B' && a[i + 2] == 'L')
{
a[i] = 'S';
if (strlen(a) > 3) strcpy(a + i + 1, a + i + 3);
else a[i + 1] = '\0';
}
else if(a[i] == 'S' && a[i + 1] == 'B' && a[i + 2] == 'L')
{
a[i] = 'R';
if (strlen(a) > 3) strcpy(a + i + 1, a + i + 3);
else a[i + 1] = '\0';
}
else if(a[i] == 'S' && a[i + 1] == 'B' && a[i + 2] == 'S')
{
a[i] = 'B';
if (strlen(a) > 3) strcpy(a + i + 1, a + i + 3);
else a[i + 1] = '\0';
}
else if(a[i] == 'R' && a[i + 1] == 'B' && a[i + 2] == 'L')
{
a[i] = 'B';
if (strlen(a) > 3) strcpy(a + i + 1, a + i + 3);
else a[i + 1] = '\0';
}
}
cout << a << endl;
}
return 0;
}
This is the output:
LLSLLBRRSRS
LLSLBRSRS
LLSBSRS
LLBRS
LBS
and then the error message Runtime error(Exit status:139(Invalid memory reference)).
The goal is to make the last output be R, because LBS means R.
Thanks for the attention!
The reason for invalid memory reference is in the loop consition:
for(int i = 0; i < strlen(a) - 2; i++)
you are accessing a[i + 2] inside loop so the last iteration must end at i < strlen(a) - 3:
for(int i = 0; i < strlen(a) - 3; i++)
this just fixes your memory problem. you still get LBS as the last output.

tictaktoe Array bound overflow

I am in need of help for this code that i am working on for a assignment. I am have the issue where if i have any X's on the board that is either in the left 2 columns it will display a X in the row above. I used my debugger and it seems that it is trying to access something outside the array bounds, but it shouldnt be. any advice on how to do this?
#include <iostream>
using namespace std;
void printTTT(char a[3][3]);
void insertX(/*PASS BY REFERENCE*/);
void insertO(char (&arr)[3][3]);
void checkForWin(/*PASS BY REFERENCE*/); // IGNORE THIS FOR NOW
int main() {
char TTTarray[3][3] = { { 'X','-','-' },
{ '-','-','-' },
{ 'X','-','-' } };
//char TTTarray[3][3] = { {'-','X','-'},
// {'-','X','-'},
// {'-','-','O'}};
//char TTTarray[3][3] = { {'-','-','-'},
// {'-','X','-'},
// {'-','O','-'}};
//char TTTarray[3][3] = { {'X','-','X'},
// {'-','-','-'},
// {'O','-','-'}};
//char TTTarray[3][3] = { {'X','-','X'},
// {'O','X','-'},
// {'O','-','O'}};
//insertX(/*CALL*/);
//OR
insertO(TTTarray);
printTTT(TTTarray);
/*****************
I have included the declaratoin of the array, initialized to - for each spot.
The '-' represents an empty position. You should fill it with either a
capital 'O' or a capital 'X'. I have also included a number of initialized arrays
to test; just comment out the ones you don't want for that moment
*****************/
return 0;
}
void printTTT(char a[3][3])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << a[i][j];
}
cout << endl;
}
}
void insertX(/*PASS BY REFERENCE*/) {
}
void insertO(char (&arr)[3][3])
{
int x1x;
int x1y;
//int x2x;
//int x2y;
for (int i = 0; i < 3; i++)
{
int go = 0;
for (int j = 0; j < 3; j++)
{
if (arr[i][j] == '-')
{
x1x = i;
x1y = j;
// looking for 2 x's for the block lol
if (x1x == 0 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x - 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1x == 1 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x - 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1x == 2 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x - 2] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1y == 0 && go == 0)
{
if (arr[x1x + 1][x1y] == 'X' && arr[x1x + 2][x1y] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x + 1][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x - 2][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1y == 1 && go == 0)
{
if (arr[x1x + 1][x1y] == 'X' && arr[x1x + 2][x1y] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x + 1][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x - 2][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
if (x1y == 2 && go == 0)
181,1-8 83%
{
if (arr[x1x + 1][x1y] == 'X' && arr[x1x + 2][x1y] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x + 1][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
if (arr[x1x - 1][x1y] == 'X' && arr[x1x - 2][x1x] == 'X')
{
arr[i][j] = 'O';
go = 1;
}
}
}
}
}
}
Take a look at these lines from your insertD function:
if (x1x == 0 && go == 0)
{
if (arr[x1x][x1y + 1] == 'X' && arr[x1x][x1y + 2] == 'X')
In this case you have checked that x1x is zero, but you haven't checked x1y. So in this case you will go out of bounds if x1y is non-zero.
A couple of lines below you have
if (arr[x1x][x1y - 1] == 'X' && arr[x1x][x1x + 1] == 'X')
This will go out of bounds too, when x1y is zero.
You need to add more checks, or rethink the logic.

Random unique number arrays comparison C++

I have two 2D arrays of type integer. I assign random values into both theses arrays. The values in the first column represent the x value for a and b, and the values in the second column represent the y value for a and b.
Declaring the arrays
int ROW(12), COLUMN(2);
int a[ROW][COLUMN];
int b[ROW][COLUMN];
Assigning random values to the arrays
for (int i(0); i < ROW; i++)
{
a[i][0] = Random(SIZEX - 2); //horizontal coordinate in range [1..(SIZEX - 2)]
a[i][1] = Random(SIZEY - 2); //vertical coordinate in range [1..(SIZEY - 2)]
}
for (int i(0); i < (ROW - 4); i++)
{
b[i][0] = Random(SIZEX - 2); //horizontal coordinate in range [1..(SIZEX - 2)]
b[i][1] = Random(SIZEY - 2); //vertical coordinate in range [1..(SIZEY - 2)]
}
I need to find a way in which I can compare the x and y value of an row, with the x and y value of each other row in each array aswell as with the other array and make sure each combination of x and y is unique for both of the arrays.
As the coordinates will relate to the position in another array.
I have a very messy solution which will work for one of the arrays.
for (int i(0); i < COLUMN; ++i)
{
if (i == 0)
{
while (b[i][0] == spot.x)
b[i][0] = Random(SIZEX - 2);
}
if (i == 1)
{
while ((b[i][0] == spot.x) || (b[i][0] == b[i - 1][0]))
b[i][0] = Random(SIZEX - 2);
}
if (i == 2)
{
while ((b[i][0] == spot.x) || (b[i][0] == b[i - 2][0]) || (b[i][0] == b[i - 1][0]))
b[i][0] = Random(SIZEX - 2);
}
if (i == 3)
{
while ((b[i][0] == spot.x) || (b[i][0] == b[i - 3][0]) || (b[i][0] == b[i - 2][0]) || (b[i][0] == b[i - 1][0]))
b[i][0] = Random(SIZEX - 2);
}
if (i == 4)
{
while ((b[i][0] == spot.x) || (b[i][0] == b[i - 4][0]) || (b[i][0] == b[i - 3][0]) || (b[i][0] == b[i - 2][0])
|| (b[i][0] == b[i - 1][0]))
b[i][0] = Random(SIZEX - 2);
}
if (i == 5)
{
while ((b[i][0] == spot.x) || (b[i][0] == b[i - 5][0]) || (b[i][0] == b[i - 4][0]) || (b[i][0] == b[i - 3][0])
|| (b[i][0] == b[i - 2][0]) || (b[i][0] == b[i - 1][0]))
b[i][0] = Random(SIZEX - 2);
}
if (i == 6)
{
while ((b[i][0] == spot.x) || (b[i][0] == b[i - 6][0]) || (b[i][0] == b[i - 5][0]) || (b[i][0] == b[i - 4][0])
|| (b[i][0] == b[i - 3][0]) || (b[i][0] == b[i - 2][0]) || (b[i][0] == b[i - 1][0]))
b[i][0] = Random(SIZEX - 2);
}
if (i == 7)
{
while ((b[i][0] == spot.x) || (b[i][0] == b[i - 7][0]) || (b[i][0] == b[i - 6][0]) || (b[i][0] == b[i - 5][0])
|| (b[i][0] == b[i - 4][0]) || (b[i][0] == b[i - 3][0]) || (b[i][0] == b[i - 2][0])
|| (b[i][0] == b[i - 1][0]))
b[i][0] = Random(SIZEX - 2);
}
}
But it only compares the x (first column). spot represents a struct variable.
May be you can try
for (int i=0;i<12;i++)
{for (int k=i+1;k<12;k++)
{if (a[i][0]==a[k][0])
cout<<"a["<<i<<"][0] same as a["<<k<<"][0]"<<endl;//you can assign a new number to a[k][0]
}};

'C' Program: multidimensional array issue

Please help me figure this out, I have tested it and tested it and re-read and re-wrote for the past 11 hours and I give up. I found a working code that someone else wrote, but it still doesn't explain to me why his works and mine doesn't because the problem that I am having works on his but not on mine
Got it people, code edited for anyone who has had a similiar problem...
The original code that I had is here http://pastebin.com/h7fXHKzf
the problem I was having was that it kept hanging up on the if(board[x][y - 1] == '.') checks.
Spoke too soon....The program will sometimes crash...it's rare but has crashed 3x in a row before...most of the time when I run it everything works.
// Chapter 8 Programming Project #9
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
#define PATH_SIZE ((int) (sizeof(brd_path)))
#define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))
int main(void)
{
char board[SIZE][SIZE] = {};
char brd_path[25] = {'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z'};
// 0 = Up, 1 = Down, 2 = Left, 3 = Right
bool path_dir_chk[4] = {false};
bool blocked = false;
unsigned short i, j, x = 0, y = 0;
// Generate a random number
srand((unsigned) time(NULL));
int dir = rand() % 4;
// Set all positions of board to '.'
for (x = 0; x < ROW_SIZE; x++) {
for (y = 0; y < ROW_SIZE; y++)
board[x][y] = '.';
}
x = 0;
y = 0;
board[0][0] = 'A';
// Generate the path
while (blocked != true && i != PATH_SIZE) {
for (i = 0; i < PATH_SIZE;) {
// Reset path_dir_chk values if empty
for (j = 0; j < 4; j++) {
if (board[x][y - 1] == '.')
path_dir_chk[0] = false;
if (board[x][y + 1] == '.')
path_dir_chk[1] = false;
if (board[x - 1][y] == '.')
path_dir_chk[2] = false;
if (board[x + 1][y] == '.')
path_dir_chk[3] = false;
}
// Check the direction and replace that char
switch (dir) {
case 0: if ((y - 1) >= 0 && board[x][y - 1] == '.') {
board[x][--y] = brd_path[i];
path_dir_chk[0] = true;
printf("I is now: %d\n", ++i);
} break;
case 1: if ((y + 1) >= 0 && board[x][y + 1] == '.') {
board[x][++y] = brd_path[i];
path_dir_chk[1] = true;
printf("I is now: %d\n", ++i);
} break;
case 2: if ((x - 1) >= 0 && board[x - 1][y] == '.') {
board[--x][y] = brd_path[i];
path_dir_chk[2] = true;
printf("I is now: %d\n", ++i);
} break;
case 3: if ((x + 1) >= 0 && board[x + 1][y] == '.') {
board[++x][y] = brd_path[i];
path_dir_chk[3] = true;
printf("I is now: %d\n", ++i);
} break;
}
// if all path's are true exit
if (path_dir_chk[0] == true &&
path_dir_chk[1] == true &&
path_dir_chk[2] == true &&
path_dir_chk[3] == true)
blocked = true;
// Reset the random direction
dir = rand() % 4;
}
}
// Print the board
for (x = 0; x < ROW_SIZE; x++) {
for (y = 0; y < ROW_SIZE; y++)
printf("%c ", board[x][y]);
printf("\n");
}
return 0;
}
OK I have made changes to reflect what I have so far, no it is printing 'I is now:' numbers 1 - 25 and then it starts over but it stops on 12 the second time around and freezes into some kind of loop
Below is the working code I found online, you can compare the two and see the similarity's but the lines of code on his that are exactly like mine do not work on mine.....
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define COLS 10
int main (void)
{
int i, j, k, direction;
char board[ROWS][COLS];
const char letters[] = {'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z'};
srand ((unsigned) time(NULL));
for (i = 0; i < ROWS; i++)
for (j = 0; j < COLS; j++)
board[i][j] = '.';
i = 0;
j = 0;
k = 1;
//set array[0][0] to first element
board[i][j] = letters[0];
while (k < 26) {
direction = rand() % 4;
if (board[i][j] == '.')
board[i][j] = letters[k++];
if ((board[i][j + 1] != '.' || j == ROWS - 1 )&&
(board[i + 1][j] != '.' || i == COLS -1) &&
(board[i - 1][j] != '.' || i == 0) &&
(board[i][j - 1] != '.' || j == 0))
break;
switch (direction) {
case 0: if (j < ROWS - 1 && board[i][j + 1] == '.'){ //move right
j++;
break; }
case 1: if (i < COLS -1 && board[i + 1][j] == '.') { //move down
i++;
break; }
case 2: if (i > 0 && board[i - 1][j] == '.'){ //move up
i--;
break; }
case 3: if (j > 0 && board[i][j - 1] == '.') { //move left
j--;
break; }
}
}
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++)
printf ("%4c", board[i][j]);
printf ("\n");
}
return 0;
}
You aren't setting x and y back to 0 after this code
for (x = 0; x < ROW_SIZE; x++) {
for (y = 0; y < ROW_SIZE; y++)
board[x][y] = '.';
}
Thus x and y will start at 10. Also you aren't range checking x and y, which means that x and y might wander off the board.
This code
if ((board[x][y - 1] != '.' || y - 1 < 0) &&
(board[x][y + 1] != '.' || y + 1 > ROW_SIZE) &&
(board[x - 1][y] != '.' || x - 1 < 0) &&
(board[x + 1][y] != '.' || x + 1 > ROW_SIZE))
should be this
if ((y - 1 < 0 || board[x][y - 1] != '.') &&
(y + 1 >= ROW_SIZE || board[x][y + 1] != '.') &&
(x - 1 < 0 || board[x - 1][y] != '.') &&
(x + 1 >= ROW_SIZE || board[x + 1][y] != '.'))
There are two subtle differences.
First y+1 and x+1 are not allowed to be equal to ROW_SIZE, since ROW_SIZE is 10, but the valid array indices are 0 to 9.
Second, order is important. When evaluating a logical OR, the left side is evaluated first, and if it's true, then the right side is not evaluated. This is important, since on some machines, even reading outside of the array bounds will cause a crash.
for (x = 0; x < ROW_SIZE; x++) {
for (y = 0; y < ROW_SIZE; y++)
board[x][y] = '.';
}
After the initialization you are not resetting the value of x and y. While value of X= ROW_SIZE, you are trying to access board[x + 1][y] and board[x][y + 1].
x = 0;
y = 0;
Thanks user3386109 your input was invaluable as was the rest of y'alls help I appreciate it, the working code is below :)
// Chapter 8 Programming Project #9
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
#define PATH_SIZE 25
#define ROW_SIZE ((int) (sizeof(board) / sizeof(board[0])))
int main(void)
{
char board[SIZE][SIZE];
// 0 = Up, 1 = Down, 2 = Left, 3 = Right
unsigned short i = 0, x, y;
// Generate a random number
srand((unsigned) time(NULL));
int dir = rand() % 4;
// Set all positions of board to '.'
for (x = 0; x < ROW_SIZE; x++) {
for (y = 0; y < ROW_SIZE; y++)
board[x][y] = '.';
}
x = 0;
y = 0;
board[0][0] = 'A';
// Generate the path
for (i = 0; i < PATH_SIZE;) {
// Check that the last character has not been cornered
if ((board[x][y - 1] != '.' || y - 1 < 0) &&
(board[x][y + 1] != '.' || y + 1 > ROW_SIZE) &&
(board[x - 1][y] != '.' || x - 1 < 0) &&
(board[x + 1][y] != '.' || x + 1 > ROW_SIZE))
break;
// Check the direction and replace that char
switch (dir) {
case 0: if ((y - 1) >= 0
&& board[x][y - 1] == '.') {
board[x][--y] = i + 'B';
++i;
} break;
case 1: if ((y + 1) < ROW_SIZE
&& board[x][y + 1] == '.') {
board[x][++y] = i + 'B';
++i;
} break;
case 2: if ((x - 1) >= 0
&& board[x - 1][y] == '.') {
board[--x][y] = i + 'B';
++i;
} break;
case 3: if ((x + 1) < ROW_SIZE
&& board[x + 1][y] == '.') {
board[++x][y] = i + 'B';
++i;
} break;
default: if (board[x][y] == '.')
board[x][y] = i + 'B';
break;
}
// Reset the random directions
dir = rand() % 4;
}
// Print the board
for (x = 0; x < ROW_SIZE; x++) {
for (y = 0; y < ROW_SIZE; y++)
printf("%4c ", board[x][y]);
printf("\n");
}
return 0;
}