Tile merging algorithm 2048 game - c

I am trying to recreate the game 2048 in C, but I can't get the algorithms to move or merge tiles together to function properly.
In the original 2048 game you would move tiles together like this:
2 | 2 | 4 | 4 4 | 8 | |
---+---+---+--- *swipes to the left* -> ---+---+---+---
8 | | 8 | 16| | |
So two tiles that are the same can merge into one tile that is twice the size. My version is almost the same, but instead of using numbers I use characters that increment by one when they merge, so[A|A] would merge to [B], etc. I did that only to not have to deal with varying size tiles.
So my board is stored as a 4*4 char array inside a struct I called grid (I know probably a bit redundant)
typedef struct grid {
char tiles[4][4];
} Grid;
I have tried to make algorithms to move and merge up, down, left and right, but they don't work properly.
void pushLeft(Grid * grid)
{
int i, j, k;
for(i = 0; i < 4; i++) //Row number i
{
for(j = 1; j < 4; j++) //Column number j
{
if(grid->tiles[i][j] != ' ') //tile is not empty
{
int flag = 1; //flag to prevent merging more than one level at a time
//Starting on column k, push tile as far to the left as possible
for(k = j; k > 0; k--)
{
if(grid->tiles[i][k-1] == ' ') //neighbor tile is empty
{
grid->tiles[i][k-1] = grid->tiles[i][k];
grid->tiles[i][k] = ' ';
}
else if(grid->tiles[i][k-1] == grid->tiles[i][k] && flag) //neighbor equals
{
grid->tiles[i][k-1]++;
grid->tiles[i][k] = ' ';
flag = 0;
}
else //Can't push or merge
{
flag = 1;
break;
}
}
}
} // Done with row
}
}
void pushRight(Grid * grid)
{
int i, j, k;
for(i = 0; i < 4; i++) //Row number i
{
for(j = 2; j >= 0; j--) //Column number j
{
if(grid->tiles[i][j] != ' ') //tile is not empty
{
int flag = 1; //flag to prevent merging more than one level at a time
//Starting on column k, push tile as far to the right as possible
for(k = j; k < 3; k++)
{
if(grid->tiles[i][k+1] == ' ') //neighbor tile is empty
{
grid->tiles[i][k+1] = grid->tiles[i][k];
grid->tiles[i][k] = ' ';
}
else if(grid->tiles[i][k+1] == grid->tiles[i][k] && flag) //neighbor equals
{
grid->tiles[i][k+1]++;
grid->tiles[i][k] = ' ';
flag = 0;
}
else //Can't push or merge
{
flag = 1;
break;
}
}
}
} // Done with row
}
}
void pushUp(Grid * grid)
{
int i, j, k;
for(i = 0; i < 4; i++) //Column number i
{
for(j = 1; j < 4; j++) //Row number j
{
if(grid->tiles[j][i] != ' ') //tile is not empty
{
int flag = 1; //flag to prevent merging more than one level at a time
//Starting on row k, push tile as far upwards as possible
for(k = j; k > 0; k--)
{
if(grid->tiles[k-1][i] == ' ') //neighbor tile is empty
{
grid->tiles[k-1][i] = grid->tiles[i][k];
grid->tiles[k][i] = ' ';
}
else if(grid->tiles[k-1][i] == grid->tiles[i][k] && flag) //neighbor equals
{
grid->tiles[k-1][i]++;
grid->tiles[k][i] = ' ';
flag = 0;
}
else //Can't push or merge
{
flag = 1;
break;
}
}
}
} // Done with column
}
}
void pushDown(Grid * grid)
{
int i, j, k;
for(i = 0; i < 4; i++) //Column number i
{
for(j = 2; j >= 0; j--) //Row number j
{
if(grid->tiles[j][i] != ' ') //tile is not empty
{
int flag = 1; //flag to prevent merging more than one level at a time
//Starting on row k, push tile as far down as possible
for(k = j; k < 3; k++)
{
if(grid->tiles[k+1][i] == ' ') //neighbor tile is empty
{
grid->tiles[k+1][i] = grid->tiles[i][k];
grid->tiles[k][i] = ' ';
}
else if(grid->tiles[k+1][i] == grid->tiles[i][k] && flag) //neighbor equals
{
grid->tiles[k+1][i]++;
grid->tiles[k][i] = ' ';
flag = 0;
}
else //Can't push or merge
{
flag = 1;
break;
}
}
}
} // Done with column
}
}
I tested these algorithms with some hardcoded testdata. The algorithm to push the tiles to the left seems to be working correctly. pushRight almost works, but it merges two levels at the same time, so [B|A|A] merges into [C] but should merge into [B|B].
pushUp seems to be almost always just wiping the entire board with empty tiles (spaces).
pushDows seems to be removing some tiles.
Does anyone see the problem or know a way to do this? I have thought about using recursive algorithms, but I just can't wrap my head around it.

I would personally break the swipe into two steps as the swipe left and swipe right are actually functionally the same regarding tile combination. The only difference is that the remaining tiles are bunched to either the left or the right depending on direction.
Below is a quick algorithm to replace two tiles with the a new one. I scan left->right and replace the left tile with the new tile, zero the right tile and then make sure I exclude this new tile from comparison:
typedef struct grid {
char tiles[4][4];
} Grid;
void eliminateHoriz (Grid* g)
{
int row, col, col2;
for (row=0; row<4; row++)
{
for (col=0; col<4; col++)
{
if (g->tiles[row][col])
{
for (col2=col+1; col2<4; col2++)
{
if (g->tiles[row][col2])
{
if (g->tiles[row][col] == g->tiles[row][col2])
{
g->tiles[row][col++] *= 2;
g->tiles[row][col2] = 0;
}
break;
}
}
}
}
}
}
void showGrid (Grid* g)
{
int row, col;
for (row=0; row<4; row++)
for (col=0; col<4; col++)
printf ("%4d%c",
g->tiles[row][col],
col == 3 ? '\n' : ' ');
printf ("\n");
}
int main()
{
Grid g = {{2,2,4,4,
8,0,8,0,
8,8,8,4,
2,2,2,2}};
showGrid (&g);
eliminateHoriz (&g);
showGrid (&g);
system ("pause");
return 0;
}
Output of this:
2 2 4 4
8 0 8 0
8 8 8 4
2 2 2 2
4 0 8 0
16 0 0 0
16 0 8 4
4 0 4 0
After this a simple compaction step could be made, or output realtime to a second buffer, or which ever. Less duplication.

I only have done the case of pushing the lines to the left, but it the same method for every direction. I took the code of the answer and modify it; take a look:
typedef struct grid {
int tiles[4][4];
} Grid;
/* Functions prototypes */
void pushLeft(Grid* grid);
void showGrid (Grid* g);
void find_great_tile(Grid* grid);
/* Main function */
int main()
{
Grid g = {{4,2,2,8,
2,8,2,2,
16,2,0,2,
128,128,64,64}};
/*
The sequence is:
--> Show the grid
--> PushLeft
--> Find great tile
--> PushLeft
--> Show the grid
*/
printf("\n\n\n\n");
showGrid (&g);
printf("\n\n\n\n");
pushLeft(&g);
showGrid (&g);
printf("\n\n\n\n");
find_great_tile(&g);
showGrid(&g);
printf("\n\n\n\n");
pushLeft(&g);
showGrid(&g);
printf("\n\n\n\n");
return 0;
}
/* Functions definitions */
void pushLeft(Grid* grid){
int row, col, col2;
for (row = 0; row < 4; row++)
{
for (col = 0; col < 4; col++)
{
if (!grid->tiles[row][col])
{
for (col2 = col+1; col2 < 4; col2++)
{
if (grid->tiles[row][col2])
{
/*
if (grid->tiles[row][col] == grid->tiles[row][col2])
{
grid->tiles[row][col++] *= 2;
grid->tiles[row][col2] = 0;
}
break;
*/
grid->tiles[row][col] = grid->tiles[row][col2];
grid->tiles[row][col2] = 0;
break;
}
}
}
}
}
}
void showGrid (Grid* grid){
int row, col;
for(row = 0; row < 4; row++){
fprintf(stdout, "\t\t |");
for(col = 0; col < 4; col++)
{
/*
In case there's any number in the matrix, it will print those numbers, otherwise, it'll print a space (it is the alternative of putting a 0)
*/
if(grid->tiles[row][col])
{
printf("%4d |", grid->tiles[row][col]);
}else
printf("%4c |", ' ');
}
fprintf(stdout, "\n\n");
}
}
void find_great_tile(Grid* grid){
int row, col, col2;
for(row = 0; row < 4; row++)
{
for(col = 0; col < 4; col++)
{
if(grid->tiles[row][col])
{
col2 = col+1;
if(grid->tiles[row][col2])
{
if(grid->tiles[row][col] == grid->tiles[row][col2])
{
grid->tiles[row][col++] *= 2;
grid->tiles[row][col2] = 0;
}
}
}
}
}
}
Output of this:
| 4 | 2 | 2 | 8 |
| 2 | 8 | 2 | 2 |
| 16 | 2 | | 2 |
| 128 | 128 | 64 | 64 |
| 4 | 2 | 2 | 8 |
| 2 | 8 | 2 | 2 |
| 16 | 2 | 2 | |
| 128 | 128 | 64 | 64 |
| 4 | 4 | | 8 |
| 2 | 8 | 4 | |
| 16 | 4 | | |
| 256 | | 128 | |
| 4 | 4 | 8 | |
| 2 | 8 | 4 | |
| 16 | 4 | | |
| 256 | 128 | | |
Of course, you can compress the steps doing:
--> PushLeft
--> FindGreatTile
--> PushLeft

Related

Trying to sort a struct array in Descending Order

To preface what I'm trying to do - I'm trying to make a Shortest Job First (SJF), the OS algo in C.
And to handle the sort-by-burst-time, I'm pushing all the ready processes in a stack, and then sorting that stack in a descending order. So for example - if the stack looks like 3,5,1,2 with "2" on top, I'm trying to sort it as: 5,3,2,1 with "1" on top. So when I start using pop, I'll get elements in the order I require.
But the problem with the code I'm going to show is, it orders 1,1,5,3 as 5,1,1,3. The last process "3" is never sorted.
Code for Sorting:
void sortByBurst() {
int size = top;
struct fcfs temp;
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < (size - 1 - i); j++) {
if (stack[j].burst < stack[j + 1].burst) {
temp = stack[j];
stack[j] = stack[j + 1];
stack[j + 1] = temp;
}
}
}
}
And in case you want to see where I'm handling the rest of the logic i.e making decisions on what to push - this is the for loop for that:
for (i = 0; i < numOfProc; i++) {
if (first_process == 1 && p[i].arrival <= current_time && p[i].completed == 0) {
min_index = i;
} else if (first_process == 0 && last_index != -1) {
for (int k = 0; k < numOfProc; k++) {
if (p[k].completed == 0 && p[k].arrival <= p[last_index].max_slot && p[k].inQ != 1) {
push(p[k]);
p[k].inQ = 1;
critical_case = 1;
} else if (p[k].completed == 0 && p[k].arrival > finalp[last_index].max_slot && p[k].inQ != 1) {
min_index = k;
}
}
if (critical_case == 1) {
sortByBurst();
struct fcfs compare = pop();
for (int l = 0; l < numOfProc; l++) {
if (compare.pid == p[l].pid) {
min_index = l;
break;
}
}
}
break;
}
}
The input I'm testing my code with is:
1. Process 1 | 2 (Arrival) | 1 (Burst)
2. Process 2 | 1 (Arrival) | 5 (Burst)
3. Process 3 | 4 (Arrival) | 1 (Burst)
4. Process 4 | 0 (Arrival) | 6 (Burst)
5. Process 5 | 2 (Arrival) | 3 (Burst)
Required Output:
1. Process 4 | 0 (Arrival) | 6 (Burst)
2. Process 1 | 2 (Arrival) | 1 (Burst)
3. Process 3 | 4 (Arrival) | 1 (Burst)
4. Process 5 | 2 (Arrival) | 3 (Burst)
5. Process 2 | 1 (Arrival) | 5 (Burst)
Output I'm Getting:
1. Process 4 | 0 (Arrival) | 6 (Burst)
2. Process 5 | 2 (Arrival) | 3 (Burst)
3. Process 3 | 4 (Arrival) | 1 (Burst)
4. Process 1 | 2 (Arrival) | 1 (Burst)
5. Process 2 | 1 (Arrival) | 5 (Burst)
As you can see, the Process 5 should be at 4th place in the list. But because in Stack - it's still on Top even after sorting, the algo is not working as it should. Kindly look into what I'm doing wrong here.
Complete Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct fcfs {
int pid, arrival, burst, min_slot, max_slot, wait_time, completed, inQ;
};
struct fcfs stack[10];
int top = -1;
void push(struct fcfs process);
struct fcfs pop();
void sortByBurst();
void pline(int x);
void main() {
int i, numOfProc, j;
int counter = 0;
int current_time = 0;
int completed = 0;
struct fcfs p[10], finalp[10];
int first_process = 1;
int prevMaxSlot = 0;
int last_index = -1;
int critical_case = 0;
printf("Enter total number of Processes \n");
scanf("%d", &numOfProc);
for (i = 0; i < numOfProc; i++) {
printf("Enter Arrival Time & Burst Time for Process %d: \n", i + 1);
scanf("%d %d", &p[i].arrival, &p[i].burst);
p[i].pid = i + 1;
p[i].wait_time = 0;
p[i].completed = 0;
p[i].inQ = 0;
}
int prev = 0;
while (completed != numOfProc) {
int min_index = -1;
for (i = 0; i < numOfProc; i++) {
if (first_process == 1 && p[i].arrival <= current_time && p[i].completed == 0) {
min_index = i;
} else if (first_process == 0 && last_index != -1) {
for (int k = 0; k < numOfProc; k++) {
if (p[k].completed == 0 && p[k].arrival <= p[last_index].max_slot && p[k].inQ != 1) {
push(p[k]);
p[k].inQ = 1;
critical_case = 1;
} else if (p[k].completed == 0 && p[k].arrival > finalp[last_index].max_slot && p[k].inQ != 1) {
min_index = k;
}
}
if (critical_case == 1) {
sortByBurst();
struct fcfs compare = pop();
for (int l = 0; l < numOfProc; l++) {
if (compare.pid == p[l].pid) {
min_index = l;
break;
}
}
}
break;
}
}
if (min_index == -1) {
current_time++;
} else {
if (p[min_index].arrival == prevMaxSlot) {
p[min_index].min_slot = prevMaxSlot;
p[min_index].max_slot = p[min_index].arrival + p[min_index].burst;
} else if (p[min_index].arrival < prevMaxSlot || p[min_index].arrival == prevMaxSlot) {
p[min_index].min_slot = prevMaxSlot;
p[min_index].max_slot = p[min_index].min_slot + p[min_index].burst;
} else if (p[min_index].arrival > prevMaxSlot) {
p[min_index].min_slot = p[min_index].arrival;
p[min_index].max_slot = p[min_index].arrival + p[min_index].burst;
}
p[min_index].wait_time = (first_process == 1) ? 0 : abs(p[i].min_slot - prev);
p[min_index].completed = 1;
prev = current_time;
prevMaxSlot = p[min_index].max_slot;
finalp[counter++] = p[min_index];
first_process = 0;
last_index = min_index;
completed++;
}
}
pline(44);
printf("Slot\tPID\tArrival\t\tBurst\n");
pline(44);
for (i = 0; i < numOfProc; i++) {
if ((finalp[i].min_slot - finalp[i - 1].max_slot) > 0 && i > 0) {
printf("%d - %d\tNONE\tNONE\t\tNONE\n", finalp[i - 1].max_slot, finalp[i].min_slot);
} else if ((abs(0 - finalp[i].min_slot) > 0 && i == 0)) {
printf("0 - %d\tNONE\tNONE\t\tNONE\n", finalp[i].min_slot);
}
printf("%d - %d\t%d\t%d\t\t%d\n", finalp[i].min_slot, finalp[i].max_slot, finalp[i].pid, finalp[i].arrival,
finalp[i].burst);
}
pline(44);
}
void pline(int x) {
for (int i = 0; i < x; i++) {
printf("-");
}
printf("\n");
}
void push(struct fcfs process) {
if (top == 10)
printf("\n Overflow");
else {
top = top + 1;
stack[top] = process;
}
}
struct fcfs pop() {
struct fcfs process;
if (top == -1)
printf("Underflow");
else {
process = stack[top];
top = top - 1;
}
return process;
}
void sortByBurst() {
int size = top + 1;
struct fcfs temp;
for (int i = 1; i < size - 1; i++) {
for (int j = 0; j < (size - 1 - i); j++) {
if (stack[j].burst < stack[j + 1].burst) {
temp = stack[j];
stack[j] = stack[j + 1];
stack[j + 1] = temp;
}
}
}
}
The issue was with top & i = 1;
In the sorting function, int size = top + 1 and initializing the for loop with i = 0 fixed it.
Thanks a lot to all who contributed.

Sorting a 2D Array c++

I am bubble sorting a 2D array that looks like this. I am confuse on how to make my largest value as 1 and make the 2nd row's value follow to 1st row's counterpart.
Input:
13 9 1 8 5
1 2 3 4 1
Actual output:
1 5 8 9 13
1 2 3 4 1
This is the expected output that i am trying to make.
Output:
5 8 9 13 1
1 4 2 1 1
Here is my code for sorting the cards (col = 5 and row = 2):
void sortedCards(int card[][col])
{
int i, j, k, temp;
printf("\n\nSorted Cards\n");
for (k = 0; k < 10; k++)
{
for (i = 0; i < row - 1; i++)
{
for (j = 0; j < col - 1; j++)
{
if (card[i][j] > card[i][j + 1])
{
temp = card[i][j];
card[i][j] = card[i][j + 1];
card[i][j + 1] = temp;
}
}
}
}
for (i = 0; i < row; i++)
{
if (i == 1)
{
printf("\n");
}
for (j = 0; j < col; j++)
{
printf("%i ", card[i][j]);
}
}
}
If your sorting is only dependent on the first row, there is no need to iterate through the second row. Just set both rows at the same time while checking the first row.
Also, if you want 1 to be treated as larger than all other numbers, you need to add that to your Boolean logic. Adjusting your for loop like below should do it.
int j, k, temp, temp2;
for (k = 0; k < 10; k++)
{
for (j = 0; j < col-1; j++)
{
//here we only test row 0, and we check if the value is 1
if (card[0][j] == 1 || (card[0][j] > card[0][j+1] && card[0][j+1] != 1))
{
//all other reassignment is the same but you do both rows at the same time
temp = card[0][j];
temp2 = card[1][j];
card[0][j] = card[0][j + 1];
card[1][j] = card[1][j + 1];
card[0][j + 1] = temp;
card[1][j + 1] = temp2;
}
}
}

how to check if a matrix has the same value in a row or column in C

I have a homework problem. It requires us to make a matrix based on user's input. For example : if user input 4 so the matrix will be 4 X 4. After that, the program will check if the matrix has the same value in a row or column. and it will give yes or no output.
For example :
input :
2
1 2
2 1
Output :
Yes
(because that matrix doesnt has a same value in a row or a column.)
Input 2 :
3
4 5 6
7 8 9
7 3 3
Output :
No
(Because that matrix have same values in a row or column (3 & 3 and 7 & 7)
Input 3:
2
1 2
3 2
Output :
No
(because that matrix have same value on column 1.)
Input 4
2
1 1
3 4
Output :
No
(because that matrix has same value in first row(1 1)
I have tried to do that, but some 'cases' still doesnt work. For example, i tried to include a count in my code but some of the count is not true.
example :
input :
4
3 4 5 6
2 3 4 5
6 5 6 3
5 4 6 3
OUTPUT :
No
count : 2
(it supposed to be 3 because it has the same value which are 6 (on row 3), 6 on column 3, and 3 on column 4.)
#include "stdio.h"
int main()
{
int matrix[500][500];
int testcase;
int count = 0;
scanf("%d",&testcase); getchar();
for(unsigned i = 0; i < testcase; i++) {
for(unsigned j = 0; j < testcase; j++) {
scanf("%d",&matrix[i][j]); getchar();
}
}
// printf("[0,0] = %c",matrix[0][0]);
// printf("\n[0,1] = %c",matrix[0][1]);
// printf("\n[1,0] = %c",matrix[1][0]);
// printf("\n[1,1] = %c",matrix[1][1]);
for(unsigned i = 0; i < testcase; i++) {
for(unsigned j = 0; j < testcase; j++) {
if(matrix[i][j] == matrix[i][j+1]) {
count = count + 1;
}
else if(matrix[i][j] == matrix[i+1][j]) {
count = count + 1;
}
}
}
if(count > 0) {
printf("No\n");
} else{
printf("Yes\n");
}
printf("Count : %d\n",count );
getchar();
return 0;
}
As I see you check if 2 numbers of the same value differ only one column or one row here:
if(matrix[i][j] == matrix[i][j+1]) {
count = count + 1;
}
else if(matrix[i][j] == matrix[i+1][j]) {
count = count + 1;
}
I think that you might need a temp variable so that you can scan each line and then each column separately , for example:
temp = matrix[i][j];
if(checkRow(temp, i, j, matrix, testcase) == true) count++;
if(checkColumn(temp, i, j, matrix, testcase) == true) count++;
and the checkRow would be something like this:
bool checkRow(int temp, int row, int col, int matrix[][500], int size)
{
for(int i=col; i < size;)
{
if(temp == matrix[row][i]) return true;
}
return false;
}
and respectively you will build the checkColumn function.
EDIT:
Since you told me you haven't learned how to use functions yet, this would be your final program. It works and I might suggest that the final test case should output "count = 4" since there is a case that you might missed.
Here is the code:
#include "stdio.h"
int main()
{
int matrix[500][500];
int testcase;
int count = 0;
scanf("%d",&testcase); getchar();
int temp;
for(unsigned i = 0; i < testcase; i++) {
for(unsigned j = 0; j < testcase; j++) {
scanf("%d",&matrix[i][j]); getchar();
}
}
// printf("[0,0] = %c",matrix[0][0]);
// printf("\n[0,1] = %c",matrix[0][1]);
// printf("\n[1,0] = %c",matrix[1][0]);
// printf("\n[1,1] = %c",matrix[1][1]);
for(unsigned i = 0; i < testcase; i++) {
for(unsigned j = 0; j < testcase; j++) {
temp = matrix[i][j];
//Scan current row
for(unsigned k = j+1; k < testcase; k++)
{
if(temp == matrix[i][k])
{
count++;
break;
}
}
//Scan current column
for(unsigned k = i+1; k < testcase; k++)
{
if(temp == matrix[k][j])
{
count ++;
break;
}
}
}
}
if(count > 0) {
printf("No\n");
} else{
printf("Yes\n");
}
printf("Count : %d\n",count );
getchar();
return 0;
}
May I suggest that before you copy the code you must understand the algorithm that lies behind it. It's simple and brute force thinking.

Pattern is being printed weird when used in an array in c

I have created an array and some functions that print different patterns each. My problem is that they are printed inside the array in a weird way. The below is the main part of the code and just one function for the example.
int castle(int patternWidth, int doorStart, int doorEnd, int N, int i, int j, int row, int col, char** array)
{
if (N >= 3 && N <= 20)
{
for (i = 1; i <= (N + 1); i++)
{
array[row][col] = '*'; col++;
array[row][col] = ' '; col++;
}
row++;
patternWidth = (((N + 1) * 2) - 1);
doorStart = (patternWidth - 3) / 2;
doorEnd = doorStart + 3;
for (i = 0; i < N; i++)
{
for (j = 1; j <= patternWidth; j++)
{
if(N - i <= 2 && j > doorStart && j <= doorEnd)
{
array[row][col] = ' '; col++;
}
else
{
array[row][col] = '*'; col++;
}
}
row++;
}
}
return 0;
}
int main()
{
int N = 0, M = 0, i = 0, j = 0, a = 0, b = 0, s = 0, width = 0, height = 0, patternWidth = 0, doorStart = 0, doorEnd = 0, option = 0, num = 3, col = 0, row = 0;
char** array;
printf("Give height board size:");
scanf("%d", &height);
printf("Give width board size:");
scanf("%d", &width);
array = (char**)malloc(height * sizeof(char*));
for (i = 0; i < width; i++)
{
array[i] = (char*)malloc(height * sizeof(char));
}
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
array[i][j] = ' ';
}
}
while (option != 6)
{
printf("\nOption: 1-5, 6 to exit\n"
"1) Stairs and flag\n"
"2) Castle\n"
"3) Trap door\n"
"4) Platform\n"
"5) Obstacles\n"
"Option:");
scanf("%d", &option);
if (option == 1)
{
printf("Valid values 6 - 20\n");
printf("Size:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
stairs_flag(N, i, j, b, a, num, col, row, array);
}
else if (option == 2)
{
printf("Valid values 3 - 15\n");
printf("Size:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
castle(patternWidth, doorStart, doorEnd, N, i, j, row, col, array);
}
else if (option == 3)
{
printf("Valid values 3 - 18\n");
printf("Size of N:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
trap_door(N, patternWidth, i, j, col, row, array);
}
else if (option == 4)
{
printf("Valid values of N 3 - 20\n");
printf("Size of N:");
scanf("%d", &N);
printf("Valid values of M 6, 8, 10, 12, 14, 16, 18, 20\n");
printf("Size of M");
scanf("%d", &M);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
platform(N, M, i, j, col, row, array);
}
else if (option == 5)
{
printf("Valid values 2 - 10\n");
printf("Size of N:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
obstacles(N, i, s, j, patternWidth, row, col, array);
}
print_array(array, height, width);
}
free(array);
return 0;
}
The expected result and the correct one is this :
|------------------------------------------------------------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| * * * * * |
| ********* |
| ********* |
| *** *** |
| *** *** |
| |
| |
| |
| |
| |
|------------------------------------------------------------|
But the actual result is this:
|------------------------------------------------------------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| * * * * * |
| ********* |
| ********* |
| *** *** |
| *** *** |
| |
| |
| |
| |
| |
|------------------------------------------------------------|
For example, in the two arrays above, the board is 60 width and 20 height and for the pattern I gave the position 10 for both height and width. Every line of the pattern should be under the previous one and print a pattern but in reality it prints every line under the previous one but further more than the previous. How can I correct this? Is there any mistake in the function code. For every function I have done the same thing. I've put array[row][col] = '*'; col++; for the asterisks and array[row][col] = ' '; col++; for the spaces and row++; to print each line in a new line.
Thank you for your time
The problem seems to be that your column index col keeps increasing throughout the castle() function. If you want the asterisks to be aligned under each other, you need to set the column index back to its original value at the start of the for-loops.
For example, you could make it
int castle(int patternWidth, int doorStart, int doorEnd, int N, int i, int j, int row, int col, char** array)
{
int column_index = col;
if (N >= 3 && N <= 20)
{
for (i = 1; i <= (N + 1); i++)
{
array[row][col] = '*'; col++;
array[row][col] = ' '; col++;
}
row++;
patternWidth = (((N + 1) * 2) - 1);
doorStart = (patternWidth - 3) / 2;
doorEnd = doorStart + 3;
for (i = 0; i < N; i++)
{
for (j = 1; j <= patternWidth; j++)
{
col = column_index;
if(N - i <= 2 && j > doorStart && j <= doorEnd)
{
array[row][col] = ' '; col++;
}
else
{
array[row][col] = '*'; col++;
}
}
row++;
}
}
return 0;
}
where I added a command int column_index = col; which stores the original value at the start of the function, and a line col = column_index; at the start of the inner for-loop, putting the stored value back in the variable.

segmentation fault when creating connect 4 board c program

I'm creating a connect-4 game... I have a lot of it done; however, the way I was creating my board was static & it needed to be dynamic, so I've made a side program to fix this before implementing it in my main program. For some reason, the if & else-if conditionals in this chunk of code create a segmentation fault, and I can't figure out why...
// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
if(aPtr[row][col] == '0') {
printf("| X ");
}
else if(aPtr[row][col] == '1') {
printf("| O ");
}
else {
printf("| ");
}
}
puts("||");
}
when I comment these conditionals out the board prints just fine & looks like this
------ Connect *Four ------
Connect X Command Line Game
&&===================&&
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
&&===================&&
1 2 3 4 5
the entirety of this side-program is below, any insight as to why this segmentation fault is occurring will be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
void initialize(int num_rows, int num_cols, char **aPtr) {
int i, r, c;
// create the space for the board
aPtr = malloc(num_rows * sizeof(char*));
for (i = 0; i < num_rows; i++){
aPtr[i] = malloc(num_cols * sizeof (char));
}
// go through the board and set all values equal to -1
for (r = 0; r < num_rows; r++) {
for (c = 0; c < num_cols; c++) {
aPtr[r][c] = '9';
printf("%c", aPtr[r][c]);
}
printf("\n");
}
}
void printBoard(int num_rows, int num_columns, char **aPtr) {
int row, col;
printf("\n");
puts("------ Connect *Four ------");
puts("Connect X Command Line Game");
// for fancy top of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
// if(aPtr[row][col] == '0') {
// printf("| X ");
// }
// else if(aPtr[row][col] == '1') {
// printf("| O ");
// }
// else {
printf("| ");
// }
}
puts("||");
}
// for fancy bottom of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
printf(" ");
if (col < 100){
for(col = 0; col < num_columns; col++) {
if (col < 10) {
printf(" %d ", col + 1);
}
else {
printf("%d ", col + 1);
}
}
puts("\n");
}
}
// *******************************************************************************************************
// *******************************************************************************************************
int main (int argc, char *argv[]) {
char **aPtr;
int height = 10;
int width = 5;
int i;
initialize(height, width, aPtr);
printBoard(height, width, aPtr);
}
Here is the modification of your code, maybe it will help. Note that I'm passing &aPtr and *aPtr = (char*) malloc(...)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
void initialize(int num_rows, int num_cols, char **aPtr) {
int i, r, c;
// create the space for the board
*aPtr = (char*) malloc(num_rows * sizeof(char*));
if(*aPtr == NULL)
{
free(*aPtr);
printf("Memory allocation failed");
}
for (i = 0; i < num_rows; i++){
aPtr[i] = (char *) malloc(num_cols * sizeof (char));
}
// go through the board and set all values equal to -1
for (r = 0; r < num_rows; r++) {
for (c = 0; c < num_cols; c++) {
aPtr[r][c] = '9';
printf("%c", aPtr[r][c]);
}
printf("\n");
}
}
void printBoard(int num_rows, int num_columns, char **aPtr) {
int row, col;
printf("\n");
puts("------ Connect *Four ------");
puts("Connect X Command Line Game");
// for fancy top of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
if(aPtr[row][col] == '0') {
printf("| X ");
}
else if(aPtr[row][col] == '1') {
printf("| O ");
}
else {
printf("| ");
}
}
puts("||");
}
// for fancy bottom of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
printf(" ");
if (col < 100){
for(col = 0; col < num_columns; col++) {
if (col < 10) {
printf(" %d ", col + 1);
}
else {
printf("%d ", col + 1);
}
}
puts("\n");
}
}
// *******************************************************************************************************
// *******************************************************************************************************
int main (int argc, char *argv[]) {
char *aPtr;
int height = 10;
int width = 5;
int i;
initialize(height, width, &aPtr);
printBoard(height, width, &aPtr);
}
Note that you are doing: aPtr[r][c] = '9'; so your board is empty, but if you change it to say 0you would get something like:
------ Connect *Four ------
Connect X Command Line Game
&&===================&&
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
&&===================&&
1 2 3 4 5
I'm assuming that's what you expected?

Resources