Arrays in C and Prime Numbers - c

I need help with my programming assignment, I have everything done except for one part! Here is the assignment:
Problem 1: String Operations and Data Security: To improve data security in the transmission of data and information, dynamic character coding is being practiced. The modification of the original characters can be using the first 8 prime members [1, 2, 3, 5, 7, 11, 13, 17]: First character enhanced by 1; second character by 2, third by 3, .. 8th character by 17. Next 8 characters use the prime numbers in the reverse order 17..1, and decrease the values. Use a total message of at least 64 characters [in quantities of 8 characters] and repeat the process of modifying 1-17 for the first 8; modifying by 17 -1 for next 8, and so on. Make your own message. After the message is coded, decoding should also be done, to restore the original message. You may want to change the lower case and upper case transitions as well.
Example: Original Message A B C D. …..
Normal ASCII 65 66 67 68 ….
Prime Numbers 1 2 3 5 ….
Enhanced ASCII 66 68 70 73 ….
Coded Message B D F I ……
I'm having trouble printing out the prime numbers without messing up the code afterwards such as the encoded and decoded ASCII codes and the encoded and decoded codes. This is my code so far:
If you could help me somehow that would be great! I would really appreciate it, thank you.
size_t x;
int i, c;
i = 1;
c = 0;
char text[65];
int s[8] = {1, 2, 3, 5, 7, 11, 13, 17};
printf("Enter a line of text: "); // prompts user to enter text
fgets(text, 65, stdin); // reads input from user
printf("\nOriginal Message: ");
for(x = 0; x < strlen(text); ++x) // for loop to print original message
{
printf("%c", text[x]);
} // end for
// ASCII Code
printf("\nASCII Code: ");
for(x = 0; x < strlen(text) - 1; ++x)
{
printf("%d ", text[x]);
}
// prime numbers
// my issue is here
printf("\n\nPrime Numbers: ");
for(x = 0; x <= 8 ; ++x)
{
if(c == 0)
{
if(i == 1)
{
printf("1 ");
++i;
}
else if(i == 2)
{
printf("2 ");
++i;
}
else if(i == 3)
{
printf("3 ");
++i;
}
else if(i == 4)
{
printf("5 ");
++i;
}
else if(i == 5)
{
printf("7 ");
++i;
}
else if(i == 6)
{
printf("11 ");
++i;
}
else if(i == 7)
{
printf("13 ");
++i;
}
else if(i == 8)
{
printf("17 ");
++c;
++x;
}
}
if(c == 1)
{
if(i == 8)
{
printf("17 ");
--i;
}
else if(i == 7)
{
printf("13 ");
--i;
}
else if(i == 6)
{
printf("11 ");
--i;
}
else if(i == 5)
{
printf("7 ");
--i;
}
else if(i == 4)
{
printf("5 ");
--i;
}
else if(i == 3)
{
printf("3 ");
--i;
}
else if(i == 2)
{
printf("2 ");
--i;
}
else if(i == 1)
{
printf("1 ");
--c;
}
} // end outer if
} // issue ends here
for(x = 0; x < strlen(text) - 1; ++x)
{
if(c == 0) // outer if statement increasing
{
if(i == 1) // inner if statement
{
text[x] = text[x] + 1;
++i;
}
else if (i == 2)
{
text[x] = text[x] + 2;
++i;
}
else if (i == 3)
{
text[x] = text[x] + 3;
++i;
}
else if(i == 4)
{
text[x] = text[x] + 5;
++i;
}
else if(i == 5)
{
text[x] = text[x] + 7;
++i;
}
else if(i == 6)
{
text[x] = text[x] + 11;
++i;
}
else if(i == 7)
{
text[x] = text[x] + 13;
++i;
}
else if(i == 8)
{
text[x] = text[x] + 17;
++c;
++x;
} // end if statement
} // end outer if statement
if(c == 1) // outer if statement decreasing
{
if(i == 8)
{
text[x] = text[x] + 17;
--i;
}
else if(i == 7)
{
text[x] = text[x] + 13;
--i;
}
else if(i == 6)
{
text[x] = text[x] + 11;
--i;
}
else if(i == 5)
{
text[x] = text[x] + 7;
--i;
}
else if(i == 4)
{
text[x] = text[x] + 5;
--i;
}
else if(i == 3)
{
text[x] = text[x] + 3;
--i;
}
else if(i == 2)
{
text[x] = text[x] + 2;
--i;
}
else if(i == 1)
{
text[x] = text[x] + 1;
--c;
}
} // end outer if
} // end for
printf("\n\nEncrypted Message: ");
for(x = 0; x <= strlen(text) - 1; ++x)
{
printf("%c", text[x]);
}
printf("\nEncrypted ASCII: ");
for(x = 0; x < strlen(text) - 1; x++)
{
printf("%d ", text[x]);
}
c = 0;
i = 1;
for(x = 0; x < strlen(text) - 1; ++x)
{
if(c == 0)
{
if(i == 1)
{
text[x] = text[x] - 1;
++i;
}
else if(i == 2)
{
text[x] = text[x] - 2;
++i;
}
else if(i == 3)
{
text[x] = text[x] - 3;
++i;
}
else if(i == 4)
{
text[x] = text[x] - 5;
++i;
}
else if(i == 5)
{
text[x] = text[x] - 7;
++i;
}
else if(i == 6)
{
text[x] = text[x] - 11;
++i;
}
else if(i == 7)
{
text[x] = text[x] - 13;
++i;
}
else if(i == 8)
{
text[x] = text[x] - 17;
++c;
++x;
} // end if statement
} // end outer if statement
if(c == 1)
{
if(i == 8)
{
text[x] = text[x] - 17;
--i;
}
else if(i == 7)
{
text[x] = text[x] - 13;
--i;
}
else if(i == 6)
{
text[x] = text[x] - 11;
--i;
}
else if(i == 5)
{
text[x] = text[x] - 7;
--i;
}
else if(i == 4)
{
text[x] = text[x] - 5;
--i;
}
else if(i == 3)
{
text[x] = text[x] - 3;
--i;
}
else if(i == 2)
{
text[x] = text[x] - 2;
--i;
}
else if(i == 1)
{
text[x] = text[x] - 1;
--c;
} // end inner if statements
} // end outer if statements
} // end for
printf("\n\nDecrypted Message: ");
for(x = 0; x < strlen(text); ++x)
{
printf("%c", text[x]);
}
printf("\nDecrypted ASCII: ");
for(x = 0; x < strlen(text) - 1; ++x)
{
printf("%d ", text[x]);
}
puts(" ");
} // end main

Characters are represented by numbers. For example, for ASCII, z is represented by the number 122. If you add something to that value then you get a new value (e.g. 122 + 17 = 139). That new value may not be valid ASCII (e.g. 128 or larger), and might be a control code (e.g. 127 is the "delete" control character).
Because the values may no longer represent valid characters, you can't print them as characters. You need to print them as values (e.g. for(x = 0; text[x] != 0; x++) { printf("%d ", text[x]); }).
Worse, for "signed char", the addition can cause overflow, which is undefined behaviour in C. You'd need to use a different data type (e.g. unsigned char or uint8_t) to ensure the code has defined/expected behaviour.
Also note that your long chain of "if .. else if ... else if" statements should be a "switch()" with cases; more like this:
switch(i) {
case 1:
text[x] = text[x] + 1;
++i;
break;
case 2:
text[x] = text[x] + 2;
++i;
break;
...
}
However; by using int s[16] = {1, 2, 3, 5, 7, 11, 13, 17, 17, 13, 11, 7, 5, 3, 2, 1}; you'd be able replace many lines of similar code with something like this:
for(x = 0; text[x] != 0; x++) {
text[x] += s[x % 16]);
}

Related

How do I add power ups to my space invader game in C language like, double shooting, adding firing speed?

I'm having trouble with adding power ups. I don't know where to put it. I'm a first year college; can somebody help?
This is what I did. I want to add power ups in this game where whenever I destroy enemy, it drops power ups like firing speed and double shooting.
I don't know what to put inside to have the enemy drop some power up.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
int main()
{
int sizey = 23;
int sizex = 40;
int x, y, yi;
char world[sizey][sizex];
char player = 'A';
char playerLaser = '^';
char enemy = 'M';
char enemyShielded = 'O';
char enemyLaser = 'U';
char explosion = 'X';
int score = 0;
int victory = 1;
int laserReady = 1;
int enemyReady = 0;
srand(time(NULL));
/*welcome screen*/
printf("\n \n Welcome soldier! \n \n \n \n");
Sleep(1000);
printf(" Brave the COMMAND PROMPT INVADERS and come back a hero. \n \n \n \n");
Sleep(2500);
printf(" Your operating system is depending upon you. \n \n \n \n");
Sleep(2500);
printf(" Good luck.");
Sleep(1000);
printf("\n \n \n \n Press any key to start.");
getch();
/*initialise world*/
int totalEnemies = 0;
for (x = 0; x < sizex; x ++) {
for (y = 0; y < sizey; y ++) {
if ((y+1) % 2 == 0 && y < 7 && x > 4
&& x < sizex - 5 && x % 2 ==0) {
world[y][x] = enemy;
totalEnemies ++;
}
else if ((y+1) % 2 == 0 && y >= 7 && y < 9 && x > 4
&& x < sizex - 5 && x % 2 ==0){
world[y][x] = enemyShielded;
totalEnemies = totalEnemies + 2;
}
else {
world[y][x] = ' ';
}
}
}
world[sizey - 1][sizex / 2] = player;
int i = 1;
char direction = 'l';
char keyPress;
int currentEnemies = totalEnemies;
while(currentEnemies > 0 && victory) {
int drop = 0;
int enemySpeed = 1 + 10 * currentEnemies / totalEnemies;
laserReady ++;
/*display world*/
system("cls");
printf(" SCORE: %d", score);
printf("\n");
for (y = 0; y < sizey; y ++) {
printf("|");
for (x = 0; x < sizex; x ++) {
printf("%c",world[y][x]);
}
printf("|");
printf("\n");
}
/*laser time*/
for (x = 0; x < sizex; x ++) {
for (y = sizey-1; y >= 0; y --) {
if (i%2 == 0 && world[y][x] == enemyLaser
&& (world[y+1][x] != enemy & world[y+1][x] != enemyShielded)){
world[y+1][x] = enemyLaser;
world[y][x] = ' ';
}
else if (i%2 == 0 && world[y][x] == enemyLaser
&& (world[y+1][x] == enemy | world[y+1][x] == enemyShielded)){
world[y][x] = ' ';
}
}
}
for (x = 0; x < sizex; x ++) {
for (y = 0; y < sizey; y ++) {
if ((i % 5) == 0 && (world[y][x] == enemyShielded
| world[y][x] == enemy) && (rand() % 15) > 13
&& world[y+1][x] != playerLaser) {
for (yi = y+1; yi < sizey; yi ++) {
if (world[yi][x] == enemy
| world[yi][x] == enemyShielded) {
enemyReady = 0;
break;
}
enemyReady = 1;
}
if (enemyReady) {
world[y+1][x] = enemyLaser;
}
}
if (world[y][x] == playerLaser && world[y-1][x] == enemy) {
world[y][x] = ' ';
world[y-1][x] = explosion;
currentEnemies --;
score = score + 50;
}
else if (world[y][x] == playerLaser
&& world[y-1][x] == enemyShielded) {
world[y][x] = ' ';
world[y-1][x] = enemy;
currentEnemies --;
score = score + 50;
}
else if (world[y][x] == playerLaser
&& world[y-1][x] == enemyLaser) {
world[y][x] = ' ';
}
else if (world[y][x] == explosion) {
world[y][x] = ' ';
}
else if ((i+1) % 2 == 0 && world[y][x] == enemyLaser
&& world[y+1][x] == player) {
world[y+1][x] = explosion;
world[y][x] = ' ';
victory = 0;
}
else if (world[y][x] == playerLaser
&& world[y-1][x] != enemyLaser) {
world[y][x] = ' ';
world[y-1][x] = playerLaser;
}
}
}
/*update enemy direction*/
for (y = 0; y < sizey; y ++) {
if (world[y][0] == enemy) {
direction = 'r';
drop = 1;
break;
}
if (world[y][sizex-1] == enemy){
direction = 'l';
drop = 1;
break;
}
}
/*update board*/
if (i % enemySpeed == 0) {
if (direction == 'l') {
for (x = 0; x < sizex - 1; x ++) {
for (y = 0; y < sizey; y ++) {
if (drop && (world[y-1][x+1] == enemy
|| world[y-1][x+1] == enemyShielded)){
world[y][x] = world[y-1][x+1];
world[y-1][x+1] = ' ';
}
else if (!drop && (world[y][x+1] == enemy
|| world[y][x+1] == enemyShielded)) {
world[y][x] = world[y][x+1];
world[y][x+1] = ' ';
}
}
}
}
else {
for (x = sizex; x > 0; x --) {
for (y = 0; y < sizey; y ++) {
if (drop && (world[y-1][x-1] == enemy
|| world[y-1][x-1] == enemyShielded)) {
world[y][x] = world[y-1][x-1];
world[y-1][x-1] = ' ';
}
else if (!drop && (world[y][x-1] == enemy
|| world[y][x-1] == enemyShielded)) {
world[y][x] = world[y][x-1];
world[y][x-1] = ' ';
}
}
}
}
for (x = 0; x < sizex; x ++) {
if (world[sizey - 1][x] == enemy) {
victory = 0;
}
}
}
/*control player*/
if(kbhit()){
keyPress = getch();
}
else {
keyPress = ' ';
}
if (keyPress == 'a') {
for (x = 0; x < sizex; x = x+1) {
if ( world[sizey-1][x+1] == player) {
world[sizey-1][x] = player;
world[sizey-1][x+1] = ' ';
}
}
}
if (keyPress == 'd') {
for (x = sizex - 1; x > 0; x = x-1) {
if ( world[sizey-1][x-1] == player) {
world[sizey-1][x] = player;
world[sizey-1][x-1] = ' ';
}
}
}
if (keyPress == 'm' && laserReady > 2) {
for (x = 0; x < sizex; x = x+1) {
if ( world[sizey-1][x] == player) {
world[sizey - 2][x] = playerLaser;
laserReady = 0;
}
}
}
i ++;
Sleep(50);
}
system("cls");
printf(" SCORE: %d", score);
printf("\n");
for (y = 0; y < sizey; y ++) {
printf("|");
for (x = 0; x < sizex; x ++) {
printf("%c",world[y][x]);
}
printf("|");
printf("\n");
}
Sleep(1000);
system("cls");
if (victory != 0) {
printf("\n \n \n \n \n \n CONGRATULATIONS! \n \n \n \n \n");
Sleep(1000);
printf("\n \n Score: %d", score);
Sleep(1000);
int bonus = totalEnemies*20 - i;
printf("\n \n Bonus: %d", bonus);
Sleep(1000);
printf("\n \n Total Score: %d", score + bonus);
printf("\n \n \n \n Well done");
Sleep(1000);
printf(", Hero.");
Sleep(1000);
getch();
}
else {
printf("\n \n \n \n \n \n You have failed.");
Sleep(1000);
printf("\n \n \n \n \n \n Windows is doomed.");
Sleep(1000);
printf("\n \n Final Score: %d", score);
getch();
}
}

Wrong output (C)

I'm getting the wrong output. The black screen is my output and the white screen is the right output. In my output the "V" is always in the right placement but the symbols under that line is always in the wrong placement.
: (hyphen) represents undiscovered locations
! : represents bombs
$ : represents treasure
& : represents both a bomb and treasure
. : represents a visited location that had neither a bomb nor a treasure
Check for BOTH a bomb AND a treasure
Check both the bomb and treasure member arrays to see if a value of 1 is set for both at the same
location
Update the player’s counters accordingly (bomb: reduce lives, treasure: increase treasure counter)
Display an appropriate message (use symbol: [&] to denote a bomb AND treasure, the treasure is
considered a “life insurance payout”)
If there is no bomb or treasure at the location entered by the user
Display an appropriate message (use symbol: [.] to denote nothing found)
This is my code:
do
{
printf(" ");
for (lives = 1; lives < (playerData.currentMove + 1); lives++)
{
if (lives != playerData.currentMove)
{
printf(" ");
}
else if (playerData.currentMove == 0)
{
printf(" ");
}
else
{
printf("%c\n", playerData.playerSym);
}
}
if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '!';
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '$';
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
gameSettings.revealedPosition[playerData.currentMove] = '&';
}
else if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '.';
}
if (playerData.currentMove == 0)
{
for (lives = 0; lives < (gameSettings.gameLength); lives++)
{
printf("%c", hiddenPosition);
gameSettings.revealedPosition[lives] = hiddenPosition;
}
}
else {
printf(" ");
for (lives = 0; lives < (gameSettings.gameLength); lives++)
{
printf("%c", gameSettings.revealedPosition[lives]);
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("%d", z);
z++;
}
else {
printf("|");
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("0");
z = 1;
}
else
{
printf("%d", z);
z++;
}
}
printf("\n");
printf("+---------------------------------------------------+\n");
printf(" Lives: %2d | Treasures: %2d | Moves Remaining: %2d\n", playerData.playerLives, playerData.treasureNum, gameSettings.gameMoves);
printf("+---------------------------------------------------+\n");
do
{
lives = 0;
printf("Next Move [1-%d]: ", gameSettings.gameLength);
scanf("%d", &playerData.currentMove);
if (playerData.currentMove >= 1 && playerData.currentMove <= gameSettings.gameLength)
{
printf("\n");
lives++;
}
else
{
printf(" Out of Range!!!\n");
}
} while (lives < 1);
if (gameSettings.revealedPosition[playerData.currentMove] == hiddenPosition)
{
if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
printf("===============> [.] ...Nothing found here... [.]\n\n");
gameSettings.gameMoves -= 1;
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
printf("===============> [!] !!! BOOOOOM !!! [!]\n\n");
playerData.playerLives -= 1;
gameSettings.gameMoves -= 1;
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
printf("===============> [$] $$$ Found Treasure! $$$ [$]\n\n");
playerData.treasureNum += 1;
gameSettings.gameMoves -= 1;
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
printf("===============> [&] !!! BOOOOOM !!! [&]\n");
printf("===============> [&] $$$ Life Insurance Payout!!! [&]\n\n");
gameSettings.gameMoves -= 1;
playerData.treasureNum += 1;
playerData.playerLives -= 1;
}
}
else
{
printf("===============> Dope! You've been here before!\n\n");
}
} while (playerData.playerLives != 0 && gameSettings.gameMoves != 0);
printf("No more LIVES remaining!\n\n");
printf(" ");
for (lives = 1; lives < (playerData.currentMove + 1); lives++)
{
if (lives != playerData.currentMove)
{
printf(" ");
}
else if (playerData.currentMove == 0)
{
printf(" ");
}
else
{
printf("%c\n", playerData.playerSym);
}
}
if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '!';
}
else if (gameSettings.treasureInput[playerData.currentMove] == 1 && gameSettings.bombInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '$';
}
else if (gameSettings.bombInput[playerData.currentMove] == 1 && gameSettings.treasureInput[playerData.currentMove] == 1)
{
gameSettings.revealedPosition[playerData.currentMove] = '&';
}
else if (gameSettings.bombInput[playerData.currentMove] == 0 && gameSettings.treasureInput[playerData.currentMove] == 0)
{
gameSettings.revealedPosition[playerData.currentMove] = '.';
}
if (playerData.currentMove == 0)
{
for (lives = 1; lives < (gameSettings.gameLength + 1); lives++)
{
printf("%c", hiddenPosition);
gameSettings.revealedPosition[lives] = hiddenPosition;
}
}
else {
printf(" ");
for (lives = 1; lives < (gameSettings.gameLength + 1); lives++)
{
printf("%c", gameSettings.revealedPosition[lives]);
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("%d", z);
z++;
}
else {
printf("|");
}
}
printf("\n ");
for (lives = 1, z = 1; lives <= gameSettings.gameLength; lives++)
{
if (lives % 10 == 0)
{
printf("0");
z = 1;
}
else {
printf("%d", z);
z++;
}
}
printf("\n");
printf("+---------------------------------------------------+\n");
printf(" Lives: %2d | Treasures: %2d | Moves Remaining: %2d\n", playerData.playerLives, playerData.treasureNum, gameSettings.gameMoves);
printf("+---------------------------------------------------+\n\n");
printf("##################\n# Game over! #\n##################\n\n");
printf("You should play again and try to beat your score!!\n");
return 0;

SetConsoleCursorPosition : Getting black rows in console upon use

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.

Moving characters in 2d array

I tried to create a 8 x 8 checkers game. I am trying to move the hyphen ' _ ' in the 2d array to select the character 'X' that I want. I have created the if statement for detecting the hyphen ' _ ' but seem that my code isn't working, I really need help. I am new to programming.
#include <stdio.h>
void gameboard(char board[8][8])
{
int x, y;
for(x=0; x<8; x++)
{
for(y=0; y<8; y++)
{
printf("=---=");
}
printf("\n\n");
for(y=0;y<8;y++)
{
printf("| %c |",board[x][y]);
}
printf("\n\n");
}
for(x=0;x<8;x++)
{
printf("=---=");
}
}
void character(char board[8][8])
{
int x,y;
for(x=0;x<8;x++){
for(y=0;y<8;y++){
if(x<3){
if(x%2 == 0){
if(x%2 == 0){
board[x][y] = 'O';
}
if(y%2==1){
board[x][y]= ' ';
}
}
if(x%2 == 1){
if(y%2 == 0){
board[x][y] = ' ';
}
if(y%2 ==1){
board[x][y]= 'O';
}
}
}
if((x==3) || (x==4)){
board[x][y] = ' ';
}
if(x>4)
{
if(x%2 == 0){
if(y%2 == 0){
board[x][y] = 'X';
}
if(y%2 ==1){
board[x][y]= ' ';
}
}
if(x%2 == 1){
if(y%2 == 0){
board[x][y] = ' ';
}
if(y%2 ==1){
board[x][y]= 'X';
}
}
if(x==5 && y ==1)
{
if(x%2 == 1){
if(y%2 == 1){
board[x][y] = '_';
}
}
}
}
}
}
}
void playgame(char board[8][8])
{
int x=0, y=0, a, b, c=0,input;
char token;
printf("\n\n---START GAME---\n");
if(token == '_')
{
printf("Please select your token : ");
}
for(a=0; a<8; a++)
{
for(b=0; b<8; b++)
{
if(board[a][b] == token & c == 0)
{
x = a;
y = b;
c++;
}
}
}
printf("1 to go right\n");
printf("2 to go left\n");
printf("3 to go up left\n");
printf("4 to go up right\n");
printf("5 to go down left\n");
printf("6 to go down right\n");
printf("7 to select token\n");
fflush(stdin);
scanf("%i", &input);
if(input == 1)
{
board[x][y+2] = token;
y++;
}
else if(input == 2)
{
board[x][y-2] = token;
y--;
}
else if(input == 3)
{
board[x-1][y-1] = token;
x--;
y--;
}
else if(input == 4)
{
board[x-1][y+1] = token;
x--;
y++;
}
else if(input == 5)
{
board[x+1][y-1] = token;
x++;
y--;
}
else if(input == 6)
{
board[x+1][y+1] = token;
x++;
y++;
}
else
{
board[x][y] = token;
}
}
int main()
{
char bx[8][8];
gameboard(bx);
playgame(bx);
return 0;
}
You have to initialize the array, then use switch statement to calculate the new position based on input.
Save the old position and swap the content of the new cell with that of the old cell in the saved position.
int main()
{
char board[8][8];
int x, y;
for (x = 0; x < 8; x++)
for (y = 0; y < 8; y++)
board[x][y] = '.';
board[0][0] = '_';
int xpos = 0;
int ypos = 0;
while (1)
{
system("cls||clear");
//print board
for (y = 0; y < 8; y++)
{
for (x = 0; x < 8; x++)
printf("%c", board[x][y]);
printf("\n");
}
printf("Menu\n 1 left \n2 right \n3 up \n4 down \n");
int savex = xpos;
int savey = ypos;
int move = 0;
scanf("%d", &move);
char c;
while ((c = getchar()) != '\n' && c != EOF);
switch (move)
{
case 1: if (xpos > 0) xpos--; break;
case 2: if (xpos < 7) xpos++; break;
case 3: if (ypos > 0) ypos--; break;
case 4: if (ypos < 7) ypos++; break;
}
//swap position:
board[savex][savey] = '.';
board[xpos][ypos] = '_';
}
return 0;
}

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