C - stack around the variable 'ch1' was corrupted - arrays
I was trying to make a chess game in c. It is basic. It is for a friend of mine. But I am getting this error when I try to play the game: The error is "stack around the variable 'ch1' was corrupted". I think this is about overloading this ch1 array which is char array(The array in the player1() function). But I don't know where I did overload this variable. Can you guys help me solve this problem ?
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int pwstatus[8] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
int pbstatus[8] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
struct piece {
int color; //0 if white, 1 if black, 2 if empty
int type;//1:pawn, 2:rook, 3:knight, 4:bishop, 5:queen, 6:king, 0 if empty
};
char* pieceToChar(piece piece) {
char *ch = (char*)malloc(5);
if (piece.color == 0)
ch[0] = 'W';
if (piece.color == 1)
ch[0] = 'B';
if (piece.color == 2) {
ch[0] = '0';
ch[1] = ' ';
ch[2] = ' ';
ch[3] = ' ';
ch[4] = '\0';
return ch;
}
if (piece.type == 1)
ch[1] = 'P';
if (piece.type == 2)
ch[1] = 'R';
if (piece.type == 3)
ch[1] = 'K';
if (piece.type == 4)
ch[1] = 'B';
if (piece.type == 5)
ch[1] = 'Q';
if (piece.type == 6)
ch[1] = 'G';
ch[2] = ' ';
ch[3] = ' ';
ch[4] = '\0';
return ch;
}
void assignChar(char* char1, char* char2, int n) {
for (int i = 0; i < n; i++) {
char1[i] = char2[i];
}
}
void charToInt(char* char1, int* int1) {
if (char1[0] == 'A') {
int1[0] = 0;
}
if (char1[0] == 'B') {
int1[0] = 1;
}
if (char1[0] == 'C') {
int1[0] = 2;
}
if (char1[0] == 'D') {
int1[0] = 3;
}
if (char1[0] == 'E') {
int1[0] = 4;
}
if (char1[0] == 'F') {
int1[0] = 5;
}
if (char1[0] == 'G') {
int1[0] = 6;
}
if (char1[0] == 'H') {
int1[0] = 7;
}
if (char1[1] == '1') {
int1[1] = 0;
}
if (char1[1] == '2') {
int1[1] = 1;
}
if (char1[1] == '3') {
int1[1] = 2;
}
if (char1[1] == '4') {
int1[1] = 3;
}
if (char1[1] == '5') {
int1[1] = 4;
}
if (char1[1] == '6') {
int1[1] = 5;
}
if (char1[1] == '7') {
int1[1] = 6;
}
if (char1[1] == '8') {
int1[1] = 7;
}
}
void intToChar(int* int1, char* char1) {
if (int1[0] == 0) {
char1[0] = 'A';
}
if (int1[0] == 1) {
char1[0] = 'B';
}
if (int1[0] == 2) {
char1[0] = 'C';
}
if (int1[0] == 3) {
char1[0] = 'D';
}
if (int1[0] == 4) {
char1[0] = 'E';
}
if (int1[0] == 5) {
char1[0] = 'F';
}
if (int1[0] == 6) {
char1[0] = 'G';
}
if (int1[0] == 7) {
char1[0] = 'H';
}
if (int1[1] == 0) {
char1[1] = '1';
}
if (int1[1] == 1) {
char1[1] = '2';
}
if (int1[1] == 2) {
char1[1] = '3';
}
if (int1[1] == 3) {
char1[1] = '4';
}
if (int1[1] == 4) {
char1[1] = '5';
}
if (int1[1] == 5) {
char1[1] = '6';
}
if (int1[1] == 6) {
char1[1] = '7';
}
if (int1[1] == 7) {
char1[1] = '8';
}
}
char intToCharDif(int integer) {
char ch = '0';
if (integer == 0) {
ch = 'A';
}
if (integer == 1) {
ch = 'B';
}
if (integer == 2) {
ch = 'C';
}
if (integer == 3) {
ch = 'D';
}
if (integer == 4) {
ch = 'E';
}
if (integer == 5) {
ch = 'F';
}
if (integer == 6) {
ch = 'G';
}
if (integer == 7) {
ch = 'H';
}
return ch;
}
struct Board{
piece board[8][8];
Board() {
piece piece;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (i < 2)
piece.color = 0;
if (i > 5)
piece.color = 1;
if (j == 0)
piece.type = 2;
if (j == 1)
piece.type = 3;
if (j == 2)
piece.type = 4;
if (j == 3)
piece.type = 5;
if (j == 4)
piece.type = 6;
if (j == 5)
piece.type = 4;
if (j == 6)
piece.type = 3;
if (j == 7)
piece.type = 2;
if (i > 1 && i < 6) {
piece.color = 2;
piece.type = 0;
}
if (i == 1 || i == 6) {
piece.type = 1;
}
board[i][j] = piece;
}
}
}
void print() {
printf("Chess Board: \n");
char ch[5] = "0000";
for (int i = 8; i >= 0; i--) {
if (i == 8)
printf("8 ");
else if (i == 7)
printf("7 ");
else if (i == 6)
printf("6 ");
else if (i == 5)
printf("5 ");
else if (i == 4)
printf("4 ");
else if (i == 3)
printf("3 ");
else if (i == 2)
printf("2 ");
else if (i == 1)
printf("1 ");
else
printf(" ");
for (int j = 0; j < 8; j++) {
if (i == 0)
break;
assignChar(&ch[0], pieceToChar(board[i - 1][j]), 5);
printf("%s", ch);
}
printf("\n");
}
printf(" A B C D E F G H\n");
}
int pawnMove(int r1, int c1) {
char ch;
int mCount = 0;
printf("Press: ");
if (board[r1][c1].color == 0) {
if (pwstatus[c1] == 1)
{
if (board[r1 + 1][c1].color == 2) {
ch = intToCharDif(c1);
printf("%c%d , ", ch, r1 + 1);
mCount++;
}
if (board[r1 + 2][c1].color == 2) {
ch = intToCharDif(c1);
printf("%c%d , ", ch, r1 + 2);
mCount++;
}
}
else
{
if (board[r1 + 1][c1].color == 2) {
ch = intToCharDif(c1);
printf("%c%d, ", ch, r1 + 1);
mCount++;
}
if (board[r1 + 1][c1 + 1].color == 1) {
ch = intToCharDif(c1 + 1);
printf("%c%d, ", ch, r1 + 1);
mCount++;
}
if (board[r1 + 1][c1 - 1].color == 1) {
ch = intToCharDif(c1 - 1);
printf("%c%d, ", ch, r1 + 1);
mCount++;
}
}
}
else {
if (pbstatus[c1] == 1)
{
if (board[r1 - 1][c1].color == 2) {
ch = intToCharDif(c1);
printf("%c%d , ", ch, r1 - 1);
mCount++;
}
if (board[r1 - 2][c1].color == 2) {
ch = intToCharDif(c1);
printf("%c%d , ", ch, r1 - 2);
mCount++;
}
}
else
{
if (board[r1 - 1][c1].color == 2) {
ch = intToCharDif(c1);
printf("%c%d , ", ch, r1 - 1);
mCount++;
}
if (board[r1 - 1][c1 - 1].color == 0) {
ch = intToCharDif(c1 - 1);
printf("%c%d, ", ch, r1 - 1);
mCount++;
}
if (board[r1 - 1][c1 + 1].color == 0) {
ch = intToCharDif(c1 + 1);
printf("%c%d, ", ch, r1 - 1);
mCount++;
}
}
}
if (mCount == 0)
{
printf(" There is no available move!\nTry different piece!\n");
}
return mCount;
}
int rookMove(int r1, int c1) {
int n;
int mCount = 0;
printf("Press: ");
int color = board[r1][c1].color;
n = c1;
char ch;
while (board[r1][n - 1].color == 2)
{
if (n == 0) { break; }
ch = intToCharDif(n - 1);
printf("%c%d , ", ch, r1);
n--;
mCount++;
}
n = c1;
while (board[r1][n + 1].color == 2 && (n + 1) <= 7)
{
ch = intToCharDif(n + 1);
printf("%c%d , ", ch, r1);
++n;
mCount++;
}
n = r1;
while (board[n - 1][c1].color == 2 && n > -1)
{
ch = intToCharDif(c1);
printf("%c%d , ", ch, n - 1);
--n;
mCount++;
}
n = r1;
while ((board[n + 1][c1].color == 2) && ((n) <= 7))
{
ch = intToCharDif(c1);
printf("%c%d , ", ch, n + 1);
++n;
mCount++;
}
if (mCount == 0)
{
printf(" There is no available move!\nTry different piece!\n");
}
return mCount;
}
int knightMove(int r1, int c1) {
printf("Press: ");
int color = board[r1][c1].color;
char ch;
int mCount = 0;
if (board[r1 + 2][c1 + 1].color == 2) {
ch = intToCharDif(c1 + 1);
printf("%c%d, ", ch, r1 + 2);
mCount++;
}
if (board[r1 + 2][c1 - 1].color != color) {
ch = intToCharDif(c1 - 1);
if ((c1 - 1) > -1) printf("%c%d, ", ch, r1 + 2);
mCount++;
}
if (board[r1 + 1][c1 + 2].color != color) {
ch = intToCharDif(c1 + 2);
if ((c1 + 2) != 8) printf("%c%d, ", ch, r1 + 1);
mCount++;
}
if (board[r1 - 1][c1 + 2].color != color) {
ch = intToCharDif(c1 + 2);
printf("%c%d, ", ch, r1 - 1);
mCount++;
}
if (board[r1 - 2][c1 - 1].color != color){
ch = intToCharDif(c1 - 1);
if ((c1 - 1) != -1)
printf("%c%d, ", ch, r1 - 2);
mCount++;
}
if (board[r1 - 2][c1 + 1].color != color) {
ch = intToCharDif(c1 + 1);
printf("%c%d, ", ch, r1 - 2);
mCount++;
}
if (board[r1 + 1][c1 - 2].color != color) {
ch = intToCharDif(c1 - 2);
printf("%c%d, ", ch, r1 + 1);
mCount++;
}
if (board[r1 - 1][c1 - 2].color != color){
ch = intToCharDif(c1 - 2);
if ((c1 - 2) != -1)
printf("%c%d, ", ch, r1 - 1);
mCount++;
}
if (mCount == 0)
{
printf(" There is no available move!\nTry different piece!\n");
}
return mCount;
}
int bishopMove(int r1, int c1) {
int a, b;
printf("Press: ");
int color = board[r1][c1].color;
char ch;
int mCount = 0;
a = 1, b = 1;
while (board[r1 - a][c1 + b].color != color)
{
ch = intToCharDif(c1 + b);
if ((r1 - a) == -1 || (c1 + b) == 8) break;
printf("%c%d , ", ch, r1 - a);
a++;
b++;
mCount++;
}
a = 1, b = 1;
while (board[r1 + a][c1 - b].color != color)
{
ch = intToCharDif(c1 - b);
if ((r1 + a) == 8 || (c1 - b) == -1) break;
printf("%c%d , ", ch, r1 + a);
a++;
b++;
mCount++;
}
a = 1, b = 1;
while (board[r1 + a][c1 + b].color != color)
{
ch = intToCharDif(c1 + b);
if ((r1 + a) == 8 || (c1 + b) == 8) break;
printf("%c%d , ", ch, r1 + a);
a++;
b++;
mCount++;
}
a = 1;
b = 1;
while (board[r1 - a][c1 - b].color != color)
{
ch = intToCharDif(c1 - b);
if ((r1 - a) == -1 || (c1 - b) == -1) break;
printf("%c%d , ", ch, r1 - a);
a++;
b++;
mCount++;
}
if (mCount == 0)
{
printf(" There is no available move!\nTry different piece!\n");
}
return mCount;
}
int queenMove(int r1, int c1) {
char ch;
int mCount = 0;
int x = 1, y = 1, a, b;
printf("Press: ");
int color = board[r1][c1].color;
while (board[r1][c1 - y].color != color)
{
ch = intToCharDif(c1 - y);
if ((c1 - y) == -1) break;
printf("%c%d , ", ch, r1);
y++;
mCount++;
}
y = 1;
while (board[r1][c1 + y].color != color)
{
ch = intToCharDif(c1 + y);
if ((c1 + y) == 8) break;
printf("%c%d , ", ch, r1);
y++;
mCount++;
}
x = 1;
while (board[r1 - x][c1].color != color)
{
ch = intToCharDif(c1);
if ((r1 - x) == -1) break;
printf("%c%d , ", ch, r1 - x);
x++;
mCount++;
}
x = 1;
while (board[r1 + x][c1].color != color)
{
ch = intToCharDif(c1);
if ((r1 + x) == 8) break;
printf("%c%d , ", ch, r1 + x);
x++;
mCount++;
}
a = 1, b = 1;
while (board[r1 - a][c1 + b].color != color)
{
ch = intToCharDif(c1 + b);
if ((r1 - a) == -1 || (c1 + b) == 8) break;
printf("%c%d , ", ch, r1 - a);
a++;
b++;
mCount++;
}
a = 1, b = 1;
while (board[r1 + a][c1 - b].color != color)
{
ch = intToCharDif(c1 - b);
if ((r1 + a) == 8 || (c1 - b) == -1) break;
printf("%c%d , ", ch, r1 + a);
a++;
b++;
mCount++;
}
a = 1, b = 1;
while (board[r1 + a][c1 + b].color != color)
{
ch = intToCharDif(c1 + b);
if ((r1 + a) == 8 || (c1 + b) == 8) break;
printf("%c%d , ", ch, r1 + a);
a++;
b++;
mCount++;
}
a = 1;
b = 1;
while (board[r1 - a][c1 - b].color != color)
{
ch = intToCharDif(c1 - b);
if ((r1 - a) == -1 || (c1 - b) == -1) break;
printf("%c%d , ", ch, r1 - a);
a++;
b++;
mCount++;
}
if (mCount == 0)
{
printf(" There is no available move!\nTry different piece!\n");
}
return mCount;
}
int kingMove(int r1, int c1) {
char ch;
int mCount = 0;
int color = board[r1][c1].color;
printf("Press: ");
if (board[r1][c1 + 1].color != color) {
ch = intToCharDif(c1 + 1);
printf("%c%d , ", ch, r1);
mCount++;
}
if (board[r1][c1 - 1].color != color) {
ch = intToCharDif(c1 - 1);
printf("%c%d , ", ch, r1);
mCount++;
}
if (board[r1 + 1][c1].color != color) {
ch = intToCharDif(c1);
printf("%c%d , ", ch, r1 + 1);
mCount++;
}
if (board[r1 - 1][c1].color != color) {
ch = intToCharDif(c1);
printf("%c%d , ", ch, r1 - 1);
mCount++;
}
if (board[r1 + 1][c1 + 1].color != color) {
ch = intToCharDif(c1 + 1);
printf("%c%d , ", ch, r1 + 1);
mCount++;
}
if (board[r1 - 1][c1 - 1].color != color) {
ch = intToCharDif(c1 - 1);
printf("%c%d , ", ch, r1 - 1);
mCount++;
}
if (board[r1 - 1][c1 + 1].color != color) {
ch = intToCharDif(c1 + 1);
printf("%c%d , ", ch, r1 - 1);
mCount++;
}
if (board[r1 + 1][c1 - 1].color != color) {
ch = intToCharDif(c1 - 1);
printf("%c%d , ", ch, r1 + 1);
mCount++;
}
if (mCount == 0)
{
printf(" There is no available move!\nTry different piece!\n");
}
return mCount;
}
void change(char* ch1, char* ch2)
{
int temp1[2] = { 0, 0 };
int temp2[2] = { 0, 0 };
charToInt(ch1, &temp1[0]);
charToInt(ch2, &temp2[0]);
piece tempPiece;
tempPiece.color = 2;
tempPiece.type = 0;
board[temp2[1]][temp2[0]] = board[temp1[1]][temp1[0]];
board[temp1[1]][temp1[0]] = tempPiece;
}
void player1()
{
int p1[2];
char ch1[2];
char ch2[2];
printf("\nPLAYER 1 (W)\n");
again1:
int withMove = 1; //if selected piece has no move this will be 0
printf("Select Piece: \n");
scanf("%s", &ch1[0]);
charToInt(&ch1[0], &p1[0]);
switch (board[p1[1]][p1[0]].type)
{
case 1:
withMove = pawnMove(p1[1], p1[0]);
break;
case 2:
withMove = rookMove(p1[1], p1[0]);
break;
case 3:
withMove = knightMove(p1[1], p1[0]);
break;
case 4:
withMove = bishopMove(p1[1], p1[0]);
break;
case 5:
withMove = queenMove(p1[1], p1[0]);
break;
case 6:
withMove = kingMove(p1[1], p1[0]);
break;
default: printf("Incorrect selection! "); goto again1;
}
if (withMove == 0)
goto again1;
scanf("%s", &ch2[0]);
change(&ch1[0], &ch2[0]);
}
void player2() {
int p1[2];
char ch1[2];
char ch2[2];
printf("\nPLAYER 2 (B)\n");
again1:
int withMove = 1; //if selected piece has no move this will be 0
printf("Select Piece: \n");
scanf("%s", &ch1[0]);
charToInt(&ch1[0], &p1[0]);
switch (board[p1[1]][p1[0]].type)
{
case 1:
withMove = pawnMove(p1[1], p1[0]);
break;
case 2:
withMove = rookMove(p1[1], p1[0]);
break;
case 3:
withMove = knightMove(p1[1], p1[0]);
break;
case 4:
withMove = bishopMove(p1[1], p1[0]);
break;
case 5:
withMove = queenMove(p1[1], p1[0]);
break;
case 6:
withMove = kingMove(p1[1], p1[0]);
break;
default: printf("Incorrect selection! "); goto again1;
}
if (withMove == 0)
goto again1;
scanf("%s", &ch2[0]);
change(&ch1[0], &ch2[0]);
}
};
int main()
{
Board board = Board();
char ch;
printf("\n\tWELCOME TO CHESS GAME");
printf("\n\n\t By Berkay ");
_getch();
system("cls");
int x = 0;
do
{
x++;
system("cls");
board.print();
if ((x % 2) == 0)
{
board.player2();
}
else
{
board.player1();
}
printf(" \n\nPress Enter To Continue ! \n\n ");
ch = _getch();
} while (ch == 13);
}
I am not sure where I did the mistake which causes this error. Is this about the references ?
The code you posted is way too long for a detailed analysis, but I can see that in player1() and player2() you have something like this:
char ch1[2];
...
scanf("%s", &ch1[0]);
I'm just speculating but, if by mistake you input more than just one letter, then ch1 will overflow since it can just accept one letter and the ending \0.
Instead of dealing with char[] or char * for this information where only one character is relevant, maybe should you use only a char?
Or at least should you input this character with "%1s" in order to prevent overflow of char ch1[2]?
Moreover, in charToInt() (which is called a bit after the input) you expect two characters (char1[0] and char1[1]), so maybe you need to declare char ch1[3] and input with "%2s" in order to store two characters and the ending \0.
However, it is difficult to give more precise advice without knowing exactly what happens (and where) when you experience the problem.
Related
Movement for game character doesn't work at all
Here is the .c code from start to finish. I suspect it's an order of operations issue or something else. The code is supposed to have a title screen where you can exit or play. If you hit play it renders a new char array in which you can move either left, right, or down (a/d/s). You get points for going down a row each time as well as going over 'P' spots on the array. Once you hit y-coordinate 202 on the array as well as have positive points, you win and it renders a congrats char array. If you go negative in points (by hitting the '#' paces or the 'L' wall that goes down a row every 1/2 second) then you will lose and be told you lost in a new char array as well as be asked if you want to exit or play again. And as mentioned before, the movement for the 'L's and the play character 'O' simply doesn't move. #include <stdlib.h> #include <stdio.h> #include <time.h> #include <fcntl.h> #include <unistd.h> char quickchar(float delay); void render(); void titleRender(); void renderLoss(); void renderWin(); int score(); char titlescreen[20][59] = { }; char diffdiff[20][46] = { (Grid removed due to char limit) }; char gameover[20][50] = { (Grid removed due to char limit) }; char gameboard[203][26] = { (Grid removed due to char limit) }; int playerx; int playery; int pts; void entryRender(); void gameRender(); int main() { int input; char replay; int lx; int ly; char walkin; int gaming; char titlechoice; int diff; int running; lx = 0; ly = 0; running = 1; playerx = 13; playery = 5; pts = 25; gaming = 0; while (gaming == 0) { titleRender(); printf("\033[1;32m"); printf("\nPress 'p' to play or 'x' to exit!"); printf("\033[0m"); scanf(" %c", &titlechoice); if (titlechoice == 'p') { gaming += 4; } if (titlechoice == 'x') { return 0; } } while (gaming == 4) { system("stty -echo raw"); render(); printf("\033[1;35m"); printf("Choose a direction: s/Down, a/Left, d/Right.\n\r"); printf("\033[0m"); walkin = quickchar(0.5); scanf("%c", &walkin); //obj behaviour if (walkin == 's' && gameboard[playery +1][playerx] != '#' && gameboard[playery + 1][playerx] != '+' && gameboard[playery + 1][playerx] != '|' && gameboard[playery + 1][playerx] != '-') { playery++; pts += 5; if (gameboard[playery + 1][playerx] == '#') { pts -= 25; } if (gameboard[playery + 1][playerx] == 'P') { printf("Points increased!\n\r"); pts += 50; gameboard[playery + 1][playerx] = '_'; } } else if (walkin == 'a' && gameboard[playery][playerx - 1] != '#' && gameboard[playery][playerx - 1] != '+' && gameboard[playery][playerx - 1] != '|' && gameboard[playery][playerx - 1] != '-') { playerx--; if (gameboard[playery][playerx - 1] == '#') { pts -= 25; } if (gameboard[playery][playerx - 1] == 'P') { printf("Points increased!\n\r"); pts += 50; gameboard[playery][playerx - 1] = '_'; } } else if (walkin == 'd' && gameboard[playery][playerx + 1] != '#' && gameboard[playery][playerx + 1] != '+' && gameboard[playery][playerx + 1] != '|' && gameboard[playery][playerx + 1] != '-') { playerx++; if (gameboard[playery][playerx + 1] == '#') { pts -= 25; } if (gameboard[playery + 1][playerx] == 'P) { printf("Points increased!\n\r"); pts += 50; gameboard[playery][playerx + 1] = '_'; } } if (walkin == 'a' || walkin == 's' || walkin == 'd' || walkin != 'a' || walkin != ' ' || walkin != 'd' ) { for (ly = 0; ly = 202; ly++) { for (lx = 1; lx = 25; lx++) { gameboard[ly][lx] = 'L'; } } } if (pts <= 0) { system("clear"); renderLoss(); scanf("%c", replay); if (replay == 'Y') { gaming = 3; } if (replay == 'N') { gaming = 5; } } if (playery == 202) { renderWin(); printf(""); printf("Congrats! You got a score of %d !", pts); printf(""); scanf("%c", &replay); if (replay == 'Y') { gaming = 4; } if (replay == 'N') { gaming = 5; } } //player postion == 203y, then congrta screen //ask player to set gaming to 4(replay) or none (exit program) } system("stty echo -raw"); return 0; } void render() { int y,x,k; system("clear"); printf("\033[01;33m"); printf("||POINTS: %d||\r\n", pts); printf("\033[0m"); for (y = 0; y < 203; y++) { for (x = 0; x < 26; x++) { if (y == playery && x == playerx) { printf("\033[0;32m"); printf("O"); printf("\033[0m"); } else { printf("\033[1;36m"); printf("%c", gameboard[y][x]); printf("\033[0m"); } } printf("\r\n"); } } void titleRender() { int y, x; system("clear"); for (y = 0; y < 20; y++) { for (x = 0; x < 59; x++) { printf("\033[0;32m"); printf(" %c", titlescreen[y][x]); printf("\033[0m"); } printf("\r\n"); } } void renderLoss() { int y, x; system("clear"); for (y = 0; y < 26; y++) { for (x = 0; x < 50; x++) { printf("\033[0;31m"); printf(" %c", gameover[y][x]); printf("\033[0m"); } printf("\r\n"); } } void renderWin() { int y, x; system("clear"); for (y = 0; y < 20; y++) { for (x = 0; x < 46; x++) { printf("\033[0;33m"); printf(" %c", diffdiff); printf("\033[0m"); } printf("\r\n"); } } char quickchar(float delay) { int flags; char c; usleep((int)(delay*1000000)); flags = fcntl(0, F_GETFL, 0); fcntl(0, F_SETFL, flags | O_NONBLOCK); c = getchar(); fcntl(0, F_SETFL, flags ^ O_NONBLOCK); return c; }
Connect four game, if statement
Here is code for my connect four game for exam. There are seven columns, if a number above 7 is entered it should say "Move not allowed", but if 0 is entered it should save the game. When I enter 0 it says "Move not allowed". There is a code to save the game when 0 is entered but it says "Move not allowed" and doesn't go there. Can someone help? #include <stdio.h> #include <string.h> typedef struct gameState{ int id; char board[6][7]; int numberOfMoves; char player1Name[20]; char player2Name[20]; }GameState; void ShowMenu() { printf("\n\n\n1. New Game \n"); printf("2. Load Game \n"); printf("3. Exit \n\n"); printf("Choose: "); } void ReadPlayerNames(char player1Name[20], char player2Name[20]) { printf("\nName of first player:"); scanf("%s", player1Name); printf("\nName of second player:"); scanf("%s", player2Name); } void PrintBoard(char board[6][7]) { char header[] = " 1 2 3 4 5 6 7 "; char border[] = "|---|---|---|---|---|---|---|"; printf("%s\n", header); printf("%s\n", border); for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { printf("| %c ", board[i][j]); } printf("|\n"); printf("%s\n", border); } } void ClearBoard(char board[6][7]) { for (int i = 0; i < 6; i++){ for (int j = 0; j < 7; j++) { board[i][j] = ' '; } } } // 1 - X wins, 2 - O wins, 0 - still playing int CheckDiagonals(char board[6][7], int i, int j, int goUpRight){ int connectedO = 0; int connectedX = 0; while(i >= 0){ if (board[i][j] != ' '){ if (board[i][j] == 'X'){ connectedX++; connectedO = 0; if (connectedX == 4){ if (goUpRight = 0){ board[i][j] = 'Y'; //checking if x won, putting Y on places of x board[i + 1][j + 1] = 'Y'; board[i + 2][j + 2] = 'Y'; board[i + 3][j + 3] = 'Y'; } else { board[i][j] = 'Y'; board[i + 1][j - 1] = 'Y'; board[i + 2][j - 2] = 'Y'; board[i + 3][j - 3] = 'Y'; } return 1; } } else { connectedO++; connectedX = 0; if (connectedO == 4){ if (goUpRight = 0){ board[i][j] = 'Y'; board[i + 1][j + 1] = 'Y'; //checking if o won, putting Y on places of o board[i + 2][j + 2] = 'Y'; board[i + 3][j + 3] = 'Y'; } else { board[i][j] = 'Y'; board[i + 1][j - 1] = 'Y'; board[i + 2][j - 2] = 'Y'; board[i + 3][j - 3] = 'Y'; } return 2; } } } else { connectedO = 0; connectedX = 0; } i--; if (goUpRight == 1){ j++; }else{ j--; } } return 0; } // 1 - X wins, 2 - O wins, 0 - still playing int CheckRowsOrCols(char board[6][7], int rows){ int connectedO = 0; int connectedX = 0; int brI = 6; int brJ = 7; if (rows == 0){ brI = 7; brJ = 6; } for (int i = 0; i < brI; i++){ for (int j = 0; j < brJ; j++) { int pI = i, pJ = j; if (rows == 0){ pI = j; pJ = i; } if (board[pI][pJ] != ' '){ if (board[pI][pJ] == 'X'){ connectedX++; connectedO = 0; if (connectedX == 4){ if (rows == 0){ board[pI][pJ] = 'Y'; board[pI - 1][pJ] = 'Y'; board[pI - 2][pJ] = 'Y'; board[pI - 3][pJ] = 'Y'; } else { board[pI][pJ] = 'Y'; board[pI][pJ - 1] = 'Y'; board[pI][pJ - 2] = 'Y'; board[pI][pJ - 3] = 'Y'; } return 1; } } else { connectedO++; connectedX = 0; if (connectedO == 4){ if (rows == 0){ board[pI][pJ] = 'Y'; board[pI - 1][pJ] = 'Y'; board[pI - 2][pJ] = 'Y'; board[pI - 3][pJ] = 'Y'; } else { board[pI][pJ] = 'Y'; board[pI][pJ - 1] = 'Y'; board[pI][pJ - 2] = 'Y'; board[pI][pJ - 3] = 'Y'; } return 2; } } } else { connectedO = 0; connectedX = 0; } } } return 0; } // 1 - X wins, 2 - O wins, 0 - still playing int CheckForWinner(char board[6][7]) { int rezultat = CheckRowsOrCols(board, 1); if (rezultat != 0){ return rezultat; } rezultat = CheckRowsOrCols(board, 0); if (rezultat != 0){ return rezultat; } for (int i = 0; i < 6; i++){ rezultat = CheckDiagonals(board, i, 0, 1); if (rezultat != 0){ return rezultat; } } for (int j = 0; j < 7; j++){ rezultat = CheckDiagonals(board, 5, j, 1); if (rezultat != 0){ return rezultat; } rezultat = CheckDiagonals(board, 5, j, 0); if (rezultat != 0){ return rezultat; } } for (int i = 0; i < 6; i++){ rezultat = CheckDiagonals(board, i, 6, 0); if (rezultat != 0){ return rezultat; } } return 0; } void SaveGame(char board[6][7], int movesPlayed, char player1Name[20], char player2Name[20]){ printf("\n\n\nEnter ID for your game: "); int id; scanf("%d", &id); GameState state; state.id = id; for (int i = 0; i < 6; i++){ for (int j = 0; j < 7; j++){ state.board[i][j] = board[i][j]; } } state.numberOfMoves = movesPlayed; strcpy(state.player1Name, player1Name); strcpy(state.player2Name, player2Name); FILE *filePointer; filePointer = fopen("SavedGames.dat", "ab"); if (filePointer == NULL){ printf("\nGames not found!"); return; } fwrite(&state, sizeof(state), 1, filePointer); fclose(filePointer); printf("\nGame with ID:%d saved!", id); } int MakeMove(char board[6][7], int movesPlayed, char player1Name[20], char player2Name[20]) { char sign = 'X'; if (movesPlayed % 2 == 1){ sign = 'O'; } int column; while (1){ printf("\nChoose the column player %c(0 for save and exit): ", sign); column; scanf("%d", &column); if (column >= 0 && column <= 7 && board[0][column-1] == ' '){ break; } printf("\nMove not allowed!\n"); } if (column != 0){ for (int i = 6; i >= 0; i--) { if (board[i][column-1] == ' ') { board[i][column-1] = sign; printf("\n\n\n"); break; } } }else { SaveGame(board, movesPlayed, player1Name, player2Name); return 1; } return 0; } void PlayGame(char board[6][7], char player1Name[20], char player2Name[20], int movesPlayed){ while (1){ PrintBoard(board); if (MakeMove(board, movesPlayed, player1Name, player2Name) == 1){ break; } movesPlayed++; int result = CheckForWinner(board); if (result != 0){ PrintBoard(board); if (result == 1){ printf("\nX wins\n\n\n"); } else { printf("\nO wins\n\n\n"); } break; } if (movesPlayed == 42){ PrintBoard(board); printf("\nTie!\n\n\n"); break; } } } void ListAllSavedGames(){ FILE *filePointer; filePointer = fopen("SavedGames.dat", "rb"); if (filePointer == NULL){ printf("\nGames not played yet!"); return; } GameState state; while(fread(&state, sizeof(state), 1, filePointer) == 1){ printf("\n%d, X: %s, O: %s, %d", state.id, state.player1Name, state.player2Name, (42 - state.numberOfMoves)); } fclose(filePointer); } void ListAllPlayerGames(){ char playerName[20]; printf("\nName of player: "); scanf("%s", playerName); FILE *filePointer; filePointer = fopen("SavedGames.dat", "rb"); if (filePointer == NULL){ printf("\nGames not played yet!"); return; } GameState state; while(fread(&state, sizeof(state), 1, filePointer) == 1){ if (strcmp(playerName, state.player1Name) == 0 || strcmp(playerName, state.player2Name) == 0){ printf("\n%d, X: %s, O: %s, %d", state.id, state.player1Name, state.player2Name, (42 - state.numberOfMoves)); } } fclose(filePointer); } int ShowTheBoard(){ int ID; printf("\nEnter ID: "); scanf("%d", &ID); FILE *filePointer; filePointer = fopen("SavedGames.dat", "rb"); if (filePointer == NULL){ printf("\nGames not played yet!"); return; } int IDfound = 0; GameState state; while(fread(&state, sizeof(state), 1, filePointer) == 1){ if (ID == state.id){ IDfound = 1; printf("\nX: %s, O: %s", state.player1Name, state.player2Name); PrintBoard(state.board); } } fclose(filePointer); if (IDfound == 0){ return 1; } return 0; } int LoadAGame(){ int ID; printf("\nEnter ID: "); scanf("%d", &ID); FILE *filePointer; filePointer = fopen("SavedGames.dat", "rb"); if (filePointer == NULL){ printf("\nGames not played yet!"); return; } int IDfound = 0; GameState state; while(fread(&state, sizeof(state), 1, filePointer) == 1){ if (ID == state.id){ IDfound = 1; PlayGame(state.board, state.player1Name, state.player2Name, state.numberOfMoves); } } fclose(filePointer); if (IDfound == 0){ return 1; } return 0; } void ShowLoadMenu(){ int returnToMainMenu = 0; while (returnToMainMenu == 0){ printf("\n\n\n1. List all saved games\n"); printf("2. List all saved games for a particular player\n"); printf("3. Show the board of one of the saved games\n"); printf("4. Load a game\n"); printf("5. Return to main menu\n"); printf("Choose: "); int choice; scanf("%d", &choice); switch(choice){ case 1: ListAllSavedGames(); break; case 2: ListAllPlayerGames(); break; case 3: while (ShowTheBoard() == 1){ printf("ID not valid!"); } break; case 4: while (LoadAGame() == 1){ printf("ID not valid!"); } returnToMainMenu = 1; break; case 5: returnToMainMenu = 1; break; default: printf("Wrong choice, try again!"); } } } int main(){ int endOfProgram = 0; while (endOfProgram == 0){ char board[6][7]; char player1Name[20]; char player2Name[20]; int movesPlayed = 0; ShowMenu(); int choice; scanf("%d", &choice); switch(choice){ case 1: ClearBoard(board); ReadPlayerNames(player1Name, player2Name); PlayGame(board, player1Name, player2Name, movesPlayed); break; case 2: ShowLoadMenu(); break; case 3: printf("Goodbye!"); endOfProgram = 1; break; default: printf("Wrong choice, try again!"); } } }
Culprit is likely to be inside MakeMove: if (column >= 0 && column <= 7 && board[0][column-1] == ' ') { break; } if you pass 0 as column, the first 2 tests will indeed succeed (both 0 >= 0 and 0 <= 7 are true). But third one tries to use board[0][-1]. -1 is not a valid index and you are invoking UB. If column is 0, you should not test anything else, so your test should be: if (column == 0 || (column > 0 && column <= 7 && board[0][column-1] == ' ')) { break; }
How to fix filling the struct with these data in c
I have struct input parameter and array of it called input_arr. I want to fill the array with the text as it gives the wrong value for the id and it work correctly with the name and nothing appear in visible. #include <stdio.h> #include <stdlib.h> #include <string.h> struct input_parameter { int id; char name[30]; int position; char visible[5]; char required[5]; char parameter_type; char client_id[5]; int min_length; int max_length; char confirm_required[5]; }; struct input_parameter input_arr[10]; char text[2*1024]="{\"success\": true,\"language\": \"en\",\"action\": \"GetServiceInputParameterList\",\"version\": 1,\"data\": { \"input_parameter_list\": [{\"id\": 1489,\"service_id\": 12102,\"name\": \"Customer Number\",\"position\": 1,\"visible\": true,\"required\": true,\"parameter_type\": \"N\",\"client_id\": true,\"min_length\": 11, \"max_length\": 11,\"confirm_required\": false } ] }}"; Fill an array of structs with the text: int main() { int i = 0; int Wstart = 0; int Wend = 0; char name[19] = {0x20}; char name1[19] = {0x20}; int menunum = 0; int len = strlen(text); while (1) // while ALL { if (i >= len) { break; } if (text[i] == 'i' && text[i + 1] == 'd') { while (1) { // while id if (text[i] == ':') { Wstart = i + 1; Wend = 0; i++; } else if (text[i] == ',' || text[i] == '}') { Wend = i; strncpy(name, text + Wstart, Wend - Wstart); input_arr[menunum].id = atoi(name); memset(name, 0, sizeof(name)); i++; break; } else { i = i + 1; } } // while id } else if (text[i] == 'n' && text[i + 1] == 'a' && text[i + 2] == 'm' && text[i + 3] == 'e') { while (1) { // while name if (text[i] == ':') { Wstart = i + 3; Wend = 0; i++; } else if (text[i] == ',' || text[i] == '}') { Wend = i - 1; strncpy(name, text + Wstart, Wend - Wstart); // name[Wend-Wstart] = '\0'; // memset(name1, 0, sizeof(name1)); if ((name[1] >= 'a' && name[1] <= 'z') || (name[1] >= 'A' && name[1] <= 'Z')) { // printf("%c is an alphabet.",c); strcpy(name1, name); } else { int vc = 0; int ia = strlen(name) - 1; for (ia = strlen(name) - 1; ia >= 0; ia--) { name1[vc] = name[ia]; vc++; } } strcpy(input_arr[menunum].name, name1); menunum++; memset(name, 0, sizeof(name)); i++; break; } else { i = i + 1; } } // while name } else if (text[i] == 'v' && text[i + 1] == 'i' && text[i + 2] == 's' && text[i + 3] == 'i' && text[i + 4] == 'b' && text[i + 5] == 'l' && text[i + 6] == 'e') { while (1) { // while visible if (text[i] == ':') { Wstart = i + 3; Wend = 0; i++; } else if (text[i] == ',' || text[i] == '}') { Wend = i - 1; strncpy(name, text + Wstart, Wend - Wstart); // name[Wend-Wstart] = '\0'; memset(name1, 0, sizeof(name1)); if ((name[1] >= 'a' && name[1] <= 'z') || (name[1] >= 'A' && name[1] <= 'Z')) { // printf("%c is an alphabet.",c); strcpy(name1, name); } else { int vc = 0; int ia = strlen(name) - 1; for (ia = strlen(name) - 1; ia >= 0; ia--) { name1[vc] = name[ia]; vc++; } } strcpy(input_arr[menunum].visible, name1); menunum++; // memset(name, 0, sizeof(name)); i++; break; } else { i = i + 1; } } // while visible } else { i++; } } printf("id:%d \n name: %s \n visible: %s \n",&input_arr[0].id,&input_arr[0].name,&input_arr[0].visible); return 0; }
Well you are printing address of id instead of its value using %d format specifier. printf("id:%d\nname: %s\n visible: %d\n",&input_arr[0].id,&input_arr[0].name,&input_arr[0].visible); should be printf("id:%d \n name: %s \n visible: %d\n",input_arr[0].id,input_arr[0].name,input_arr[0].visible);
loop not completing its iteration in C
I am however having trouble with my game loop! Currently when an invalid move is made, the loop iterates correctly. However when a valid move is made, my program displays the changed board, however appears to freeze in the middle of the loop! i have the location where it freezes (Test Spot 2 in the code), but i have no clue why. The desired output of my code is to keep on iterating until there are no moves available. (I haven't created the check for no moves yet as I cannot get my code to run in a normal counted loop!) The code working when it is Invalid and not when it is valid is shown below. here Main Function where the issue occurs int main(void) { int n; int p; char oppChar; char playChar; int i; int pTurn; int deltaRow; int deltaCol; int endGame = 999; printf("Enter the board dimension: "); fflush(stdout); scanf("%d", &n); printf("Computer plays (B/W) "); fflush(stdout); scanf(" %c", &oppChar); if (oppChar == 'B') { pTurn = 0; playChar = 'W'; } else { pTurn = 1; playChar = 'B'; } char board[n][26]; int movesAvalB[n][26]; int movesAvalW[n][26]; int AIBoard[n][26]; for (i = 0; i < n; i++) { for (p = 0; p < n; p++) { board[i][p] = 'U'; } } board[(n / 2) - 1][(n / 2) - 1] = 'W'; board[(n / 2) - 1][(n / 2)] = 'B'; board[(n / 2)][(n / 2) - 1] = 'B'; board[n / 2][n / 2] = 'W'; printBoard(board, n); int q = 0; do { q++; // testing printf(" \n"); printf("Turn Number %d \n", pTurn); // variable decleration int k = 0; int y = 0; int x = 0; int max = 0; int temp = 0; char c[3] = " "; // arrays are all 0 for (i = 0; i < n; i++) { for (p = 0; p < n; p++) { AIBoard[i][p] = 0; movesAvalB[i][p] = 0; movesAvalW[i][p] = 0; } } // moves avaliable to W for (y = 0; y < n; y++) { for (x = 0; x < n; x++) { if (moves(n, x, y, board, 'W')) { movesAvalW[y][x] = 1; } } } printf("Test Spot 1\n"); // moves avaliable to B for (y = 0; y < n; y++) { for (x = 0; x < n; x++) { if (moves(n, x, y, board, 'B')) { printf("Test Spot 1.5\n"); movesAvalB[y][x] = 1; } } } fflush(stdout); printf("Test Spot 2\n"); // pTurn = pTurn%2; fflush(stdout); printf("Enter a move for colour %c (RowCol): \n", playChar); fflush(stdout); scanf("%s", &c); if (positionInBounds(n, c[0], c[1])) { int y = c[0] - 97; int x = c[1] - 97; if (playChar == 'B') { if (movesAvalB[y][x] == 1) { for (deltaRow = -1; deltaRow <= 1; deltaRow++) { for (deltaCol = -1; deltaCol <= 1; deltaCol++) { if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) { i = 1; while ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) && (board[y + (i * deltaRow)][x + (i * deltaCol)] == 'W')) { i++; if ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) && (board[y + (i * deltaRow)][x + (i * deltaCol)] == 'B')) { while (i != 0) { i--; board[y + (i * deltaRow)][x + (i * deltaCol)] = 'B'; } } } } } } for (deltaRow = -1; deltaRow <= 1; deltaRow++) { for (deltaCol = -1; deltaCol <= 1; deltaCol++) { if (board[y + deltaRow][x + deltaCol] == 'W') { board[y + deltaRow][x + deltaCol] == 'B'; } } } printBoard(board, n); } else { printf("Invalid Move."); } } if (playChar == 'W') { if (movesAvalW[y][x] == 1) { for (deltaRow = -1; deltaRow <= 1; deltaRow++) { for (deltaCol = -1; deltaCol <= 1; deltaCol++) { if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) { i = 1; while ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) && (board[y + (i * deltaRow)][x + (i * deltaCol)] == 'B')) { i++; if ((positionIntBounds(n, (y + (i * deltaRow)), (x + (i * deltaCol)))) && (board[y + (i * deltaRow)][x + (i * deltaCol)] == 'W')) { while (i != 0) { i--; board[y + (i * deltaRow)][x + (i * deltaCol)] = 'W'; } } } } } } for (deltaRow = -1; deltaRow <= 1; deltaRow++) { for (deltaCol = -1; deltaCol <= 1; deltaCol++) { if (board[y + deltaRow][x + deltaCol] == 'B') { board[y + deltaRow][x + deltaCol] == 'W'; } } } printBoard(board, n); } else { printf("Invalid Move."); } } } else { printf("Invalid Move."); } pTurn++; } while (q <= 5); printf("were out"); } Secondary Functions which you probably don't need but maybe #include <stdio.h> #include <string.h> #include <stdbool.h> void printBoard(char board[][26], int n) { int i; char alpha[27] = "abcdefghijklmnopqrstuvwxyz"; printf(" "); for (i = 0; i < n; i++) { printf("%c", alpha[i]); } int p; int q; for (p = 0; p < n; p++) { printf("\n"); printf("%c", alpha[p]); for (q = 0; q < n; q++) { printf("%c", board[p][q]); } } } bool positionInBounds(int n, char row, char col) { int p = row - 97; int d = col - 97; if (p > n) { return false; } if (d > n) { return false; } if (0 > p) { return false; } if (0 > d) { return false; } return true; } bool positionIntBounds(int n, int row, int col) { if (row > n) { return false; } if (col > n) { return false; } if (0 > row) { return false; } if (0 > col) { return false; } return true; } bool checkLegalInDirection(char board[][26], int n, char row, char col, char colour, int deltaRow, int deltaCol) { int i = 0; while ((positionIntBounds(n, (row + (i * deltaRow)), (col + (i * deltaCol)))) && (board[row + (i * deltaRow)][col + (i * deltaCol)] != colour) && (board[row + (i * deltaRow)][col + (i * deltaCol)] != 'U')) { i++; if ((positionIntBounds(n, (row + (i * deltaRow)), (col + (i * deltaCol)))) && (board[row + (i * deltaRow)][col + (i * deltaCol)] == colour)) { return true; } } return false; } bool moves(int n, int x, int y, char board[][26], char colour) { int deltaRow; int deltaCol; if (board[y][x] == 'U') { for (deltaRow = -1; deltaRow <= 1; deltaRow++) { for (deltaCol = -1; deltaCol <= 1; deltaCol++) { if (positionIntBounds(n, (x + deltaRow), (y + deltaCol))) { if (checkLegalInDirection(board, n, (x + deltaRow), (y + deltaCol), colour, deltaRow, deltaCol)) { return true; } } } } } return false; } (I was told to put fflush(stdout) before my scanf statements but this didnt fix the problem)
Converting 2D array of numbers into chars
I am making a maze using Depth-First search algorithm as a school project. The maze is working pretty much as I want but I have a little problem. I have to use # as a wall and . as a path when printing the maze but I used 0 as a wall and 1 as a path at first thinking that it is gonna be easy to change it later but I was wrong. How can I change 0 and 1 into # and .? Code: #include <stdio.h> #include <stdlib.h> #include <time.h> void nahodne_suradnice(int *r, int *s, int n) { srand(time(NULL)); *r = ((rand() % n) - 1) + 2; if (*r == 1 || *r == n) { *s = (rand() % n) + 2; } else { if (rand() % 2 == 1) *s = 1; else *s = n; } } int main() { int i, j, n, r, s,smer,posledny_smer[1500]; int maze[500][500]; scanf_s("%d", &n); if (n < 10 || n > 100) { return 0; } //vynulovanie pola/bludiska for (i = 1; i < n + 1; i++) { for (j = 1; j < n + 1; j++) { maze[i][j] = 0; } } //nahodny vyber zaciatku bludiska nahodne_suradnice(&r, &s, n); //generovanie bludiska j = 0; maze[r][s] = 2; for (i = 0 ;; i++) { //backtracking if ((maze[r - 1][s] == 1 || maze[r - 2][s] == 1 || r - 2 <=1 || s==n || s==1) && (maze[r][s + 1] == 1 || maze[r][s + 2] == 1 || s + 2 >= n || r == n || r==1) && (maze[r + 1][s] == 1 || maze[r + 2][s] == 1 || r + 2 >= n || s == n || s==1) && (maze[r][s - 1] == 1 || maze[r][s - 2] == 1 || s - 2 <=1 || r == n || r==1)) { if (posledny_smer[j-1] == 1) if (maze[r + 1][s] == 1 && maze[r + 2][s] == 1) { r += 2; j--; continue; } else { j--; continue; } if (posledny_smer[j-1] == 2) if (maze[r][s - 1] == 1 && maze[r][s - 2] == 1) { s -= 2; j--; continue; } else { j--; continue; } if (posledny_smer[j-1] == 3) if (maze[r - 1][s] == 1 && maze[r - 2][s] == 1) { r -= 2; j--; continue; } else { j--; continue; } if (posledny_smer[j-1] == 4) if (maze[r][s + 1] == 1 && maze[r][s + 2] == 1) { s += 2; j--; continue; } else { j--; continue; } if (j == 0) { if (r == n) { nahodne_suradnice(&r, &s,n); maze[1][s] = 3; maze[2][s] = 3; } if (r == 1) { nahodne_suradnice(&r, &s, n); maze[n][s] = 3; maze[n - 1][s] = 3; } if (s == n-2) { nahodne_suradnice(&r, &s, n); maze[r][1] = 3; maze[r][2] = 3; } if (s == 3) { nahodne_suradnice(&r, &s, n); maze[r][n] = 3; maze[r][n-1] = 3; } break; } } //buranie stien smer = (rand() % 4) + 1; if (smer == 1) { if (r - 2 >1 && s<n && s>1) { if (maze[r - 1][s] == 1 || maze[r - 2][s] == 1) continue; maze[r - 1][s] = 1; maze[r - 2][s] = 1; r -= 2; posledny_smer[j] = smer; j++; continue; } } if (smer == 2) { if (s + 2 < n && r < n && r>1) { if (maze[r][s+1] == 1 || maze[r][s+2] == 1) continue; maze[r][s + 1] = 1; maze[r][s + 2] = 1; s += 2; posledny_smer[j] = smer; j++; continue; } } if (smer == 3) { if (r + 2 < n && s < n && s>1) { if (maze[r + 1][s] == 1 || maze[r + 2][s] == 1) continue; maze[r + 1][s] = 1; maze[r + 2][s] = 1; r += 2; posledny_smer[j] = smer; j++; continue; } } if (smer == 4) { if (s - 2 >1 && r < n && r>1) { if (maze[r][s-1] == 1 || maze[r][s-2] == 1) continue; maze[r][s - 1] = 1; maze[r][s - 2] = 1; s -= 2; posledny_smer[j] = smer; j++; continue; } } } //vypis bludiska for (i = 1; i < n + 1; i++) { for (j = 1; j < n + 1; j++) { printf("%d", maze[i][j]); } printf("\n"); } system("PAUSE"); return 0; } ` Thanks.
Change the definition of maze to: char maze[500][500] and change all references to use char instead of int. Then return '#' and '.' instead of 0 and 1.
You can change from 0 and 1 to # and . (or any other representation) when printing. For instance you can change: printf("%d", maze[i][j]); to switch(maze[i][j]) { case 0: printf("#"); break; case 1: printf("."); break; default: printf("X"); /* in case you have some value other than 0 or 1 in the maze */ } If you do it like this, you can change which letters you want to display the maze as without changing other parts of your code.