SetConsoleCursorPosition : Getting black rows in console upon use - c

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

Too much code to take in quickly. If you have Windows console ouput for the snake, can't you simply printf the next line and leave it at that?
If you want to hide the cursor (instead of trying to park it out of sight) you can make it invisible by calling SetConsoleCursorInfo and the struct passed is shown here.

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;
}

Counting number of neighbors Conway's Game of Life

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

For Loop running infinite C

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

VM Placement with FF, BF and WF Algorithms

Please I am working on VM Placement using First-fit (FF), Best-fit (BF) and Worst-fit (WF) algorithms. The FF and WF seem to work pretty good but there's is a little bit of issue with the BF. The VM sequence are generated randomly for the placement.
The problem is that, sometimes when it sorts out the PMs and picks the one with the least capacity of resources and when it gets to checking if it has enough resource for hosting that particular VM it says "WAIT" instead of placing it on the next available PM with enough resources. It works well from the start but as the process continues it tends to get such error in the placements.
For example, a VM sequence of X, S, M, L, S, L, X, L, M, M the placement is supposed to be:
X 2
S 2
M 2
L 1
S 1
L 1
X 1
L 1
M WAIT
M WAIT
instead it displays:
X 2
S 2
M 2
L 1
S 1
L 1
X WAIT
L WAIT
M WAIT
M WAIT
I really don't know which part of the code is making such an error occur. I would be glad if assistance can be provided. Below is the code - thank you.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#define max 250
#define min 10
int main()
{
int i, k,g, c[max] = {0}, r[max] = {0}, d[max] = {0}, cv[max] = {0},
rv[max] = {0}, dv[max] = {0}, rw[max] = {0}; //declaration of variables
int nf=10; char ch;
float avgpwr=0; //, nb = 3
int m=2;
int b = 1; int ff[10] = {0};
int cm[max] = {0}, rm[max] = {0}, dm[max] = {0};
int a;
for (a=1; a<=b; a++)
{
printf("\n\tVirtual Machine Placement");
printf("\n\t**************************************"); printf("\n");
int flag, j, count=0;
char vm_type[] = {'S', 'M', 'L', 'X'};
printf("\n");
//loop throug array and print values
for (i=1; i<=nf; i++)
{
//generate random index number
int index = my_rand ();
printf("VM %d:",i); printf("%-5c", vm_type[index]);
switch(vm_type[index]) {
case 'S':
cv[i]= 2; rv[i]= 4; dv[i]= 50; rw[i] = 69;
break;
case 'M':
cv[i]= 3; rv[i]= 8; dv[i]= 100; rw[i] = 139;
break;
case 'L':
cv[i]= 5; rv[i]= 12; dv[i]= 150; rw[i] = 207;
break;
case 'X':
cv[i]= 6; rv[i]= 18; dv[i]= 200; rw[i] = 345;
break;
default:
printf("That was not a valid option :(");
}
printf("\n");
}
printf("\n1.First fit 2.Best fit 3.Worst fit\n");
do
{
int nc[10]={0}; int dc[10]={0};
int mc[10]={0}; int rd[10]={0};
int host[10] = {0};
for (i=1; i<=m; i++)
{
if (i==1){
c[i] = 64; r[i] = 64; d[i] = 700; ff[i] = i;
}
if (i==2){
c[i] = 32; r[i] = 32; d[i] = 500; ff[i] = i;
}
}
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nFirst Fit\n");
printf("\n\nVirtualM_No\tPhyMac_No\n");
for(i=1; i<=nf; i++)
{
flag = 1;
for(j=1; j<=m; j++)
{
if(cv[i] <= c[j] && rv[i]<= r[j] && dv[i] <= d[j]){
printf("%-15d\t%-15d\t%\n",i,ff[j]); //printf("\n");
count++;
if(ff[j]==1 || ff[j]==2 || ff[j]==3 || ff[j]==4 || ff[j]==5 || ff[j]==6 || ff[j]==7 || ff[j]== 8|| ff[j]==9 || ff[j]==10)
{
c[j] = c[j] - cv[i];
r[j] = r[j] - rv[i];
d[j] = d[j] - dv[i];
nc[j] = nc[j] + cv[i];
mc[j] = mc[j] + rv[i];
dc[j]= dc[j] + dv[i];
rd[j]= rd[j] + rw[i];
host[j] = host[j]+ 1;
}
break;
}
else
{
flag++;
}
}
if(flag > m) {
printf("%-15d\t%-15s\n",i,"WAIT..\n");
// rejectvm++;
}
}
printf("\n");
//printf("%d , %d , %d , %d \n", nc[i], rd[i], mc[i], dc[i]);
break;
case 2: printf("\nBest Fit\n");
int y1, z1, temp1,temp_r1, temp_d1;
//placing the vms
printf("\n\nVirtualM_No\tPhyMac_No\n");
for(i=1; i<=nf; i++)
{
flag = 1;
for(y1=1;y1<=m;y1++)
{
for(z1=y1;z1<=m;z1++)
{
if (c[y1] !=0 && r[y1] !=0 || d[y1] !=0)
{
if(c[y1] > c[z1] && r[y1] > r[z1] && d[y1] > d[z1])
{
temp1 = c[y1]; temp_r1 = r[y1]; temp_d1 = d[y1];
c[y1]= c[z1]; r[y1]= r[z1]; d[y1]= d[z1];
c[z1]=temp1; d[z1]=temp_r1; d[z1]=temp_d1;
temp1=ff[y1];
ff[y1]=ff[z1];
ff[z1]=temp1;
}
}
}
}
for(j=1; j<=m; j++)
{
if( c[j] >= cv[i] && r[j] >= rv[i] && d[j] >= dv[i]){
printf("%-15d\t%-15d\t%\n",i,ff[j]);
count++;
if(ff[j]==1 || ff[j]==2 || ff[j]==3 || ff[j]==4 ||
ff[j]==5 || ff[j]==6 || ff[j]==7 || ff[j]== 8|| ff[j]==9 || ff[j]==10)
{
c[j] = c[j] - cv[i];
r[j] = r[j] - rv[i];
d[j] = d[j] - dv[i];
//ctr[i] = ctr[i] + 1;
nc[j] = nc[j] + cv[i];
mc[j] = mc[j] + rv[i];
dc[j]= dc[j] + dv[i];
rd[j]= rd[j] + rw[i];
host[j] = host[j]+ 1;
}
break;
}
else {
flag++;
}
}
if(flag > m) {
printf("%-15d\t%-15s\n",i,"WAIT...\n");
}
}
printf("\n");
break;
case 3: printf("\nWorst Fit\n");
int y, z, temp,temp_r, temp_d;
//placing the vms
printf("\n\nVirtualM_No\tPhyMac_No\n");
for(i=1; i<=nf; i++)
{
flag = 1;
for(j=1; j<=m; j++)
{
for(y=1;y<=m;y++)
{
for(z=y;z<=m;z++)
{
if(c[y] < c[z] && r[y] < r[z] && d[y] < d[z])
{
temp = c[y]; temp_r = r[y]; temp_d = d[y];
c[y]= c[z]; r[y]= r[z]; d[y]= d[z];
c[z]=temp; r[z]=temp_r; d[z]=temp_d;
temp1=ff[y];
ff[y]=ff[z];
ff[z]=temp1;
}
}
}
if( c[j] >= cv[i] && r[j] >= rv[i] && d[j] >= dv[i]){
printf("%-15d\t%-15d\t%d\n",i,ff[j],j);
count++; g = ff[j];
if(ff[j]==1 || ff[j]==2 || ff[j]==3 || ff[j]==4 || ff[j]==5 || ff[j]==6 || ff[j]==7 || ff[j]== 8|| ff[j]==9 || ff[j]==10)
{
c[j] = c[j] - cv[i];
r[j] = r[j] - rv[i];
d[j] = d[j] - dv[i];
// ctr[i] = ctr[i] + 1;
nc[g] = nc[g] + cv[i];
mc[g] = mc[g] + rv[i];
dc[g]= dc[g] + dv[i];
rd[g]= rd[g] + rw[i];
host[g] = host[g]+ 1;
}
break;
}
else
{
flag++;
}
}
if(flag > m) {
printf("%-15d\t%-15s\n",i,"WAIT...\n");
}
}
printf("\n");
break;
default:
printf("That was not a valid option :(");
break;
}
}
while(ch<=3);
}
}
int my_rand ()
{
static int first = 0;
if (first == 0)
{
srand (time (NULL));
first = 1;
}
return (rand ()% 4);
}

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.

Resources