From 0191c68db777447883bf2c78014448a139a19e81 Mon Sep 17 00:00:00 2001 From: Crystal Date: Thu, 15 Feb 2024 00:02:48 +0100 Subject: Add stuff --- blog/c/game.html | 467 +++++++++++++++++++++++++------------------------------ 1 file changed, 212 insertions(+), 255 deletions(-) (limited to 'blog/c/game.html') diff --git a/blog/c/game.html b/blog/c/game.html index d79bbbf..ac14ee3 100644 --- a/blog/c/game.html +++ b/blog/c/game.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + The loneliness Game @@ -12,38 +12,6 @@ - - -
@@ -55,170 +23,164 @@

Hello !!, I hope you are doing great you amazing person whoever you are, and I really appreciate you reading my little C programming adventure. Soo basically I wanted to blog about a little game I made when bored, and figured out it would be a great way to optimize it, and learn new stuff too by documenting the process!

-
-

The concept :

-
+
+

The concept :

+

Basically the player is faced with a NxM field made up with the sign “-” and the player is denoted by the symbol “+”, there are also Bonuses “B” which add 1 to your score, Traps “T”, that remove one from your score, and Dead “D” which resets the score to 0. I will go into more of the specifics later but for now this is how it works, and the controls are Basic WASD bindings, though i may go for a HJKL style later.

-
-

The code :

-
-
-

-#include <stdio.h> -#include <stdlib.h> -int main(int argc, char *argv[]) { - char input,map[5][5] = { - {’-’, ’-’, ’-’, ’-’, ’-’}, - {’-’, ’-’, ’-’, ’-’, ’-’}, - {’-’, ’-’, ’-’, ’-’, ’-’}, - {’-’, ’-’, ’-’, ’-’, ’-’}, - {’-’, ’-’, ’-’, ’-’, ’-’} - }; - int stop=0,i=0,moves=0,score=0,pos[2] = {2, 2}; - int bonus[2]; - int trap[2] ; - int death[2]; - map[pos[0]][pos[1]] = ’’; - do{ - bonus[0] = arc4random_uniform(5); bonus[1] = arc4random_uniform(5); - trap[0] = arc4random_uniform(5); trap[1] = arc4random_uniform(5); - death[0] = arc4random_uniform(5); death[1] = arc4random_uniform(5); - }while((bonus[0] = trap[0] && bonus[1] = trap[1]) || (bonus[0] = death[0] && bonus[1] = death[1]) || (trap[0] = death[0] && trap[1] = death[1]) || (bonus[0] = pos[0] && bonus[1] = pos[1]) || (trap[0] = pos[0] && trap[1] = pos[1]) || (death[0] = pos[0] && death[1] = pos[1])); - map[bonus[0]][bonus[1]] = ’B’; - map[trap[0]][trap[1]] = ’T’; - map[death[0]][death[1]] = ’D’; - do{ - printf(“Map:\n”); - for (int i = 0; i < 5; i+) { - for (int j = 0; j < 5; j++) { - printf(“%c ”, map[i][j]); - } - printf(“\n”); - } - printf(“Score: %d\n”, score); - printf(“Moves: %d\n”, moves); - printf(“Enter a direction (w,a,s,d) or c to quit: ”); - scanf(“ %c”, &input); -// pos[0] updown pos[1] lr - if (input = 'w') { - printf("Moving up\n"); - map[pos[0]][pos[1]] = '-'; - if (pos[0] = 0) { - pos[0] = 4; - } - else { - pos[0]–; - } - } else if (input = 'a') { - printf("Moving left\n"); - map[pos[0]][pos[1]] = '-'; - if (pos[1] = 0) { - pos[1] = 4; - } - else { - pos[1]–; - } - } else if (input == ’s’) { -

- -

- printf("Moving down\n”); - map[pos[0]][pos[1]] = ’-’; - if (pos[0] = 4) { - pos[0] = 0; - } - else { - pos[0]++; - } - } else if (input = ’d’) { - printf(“Moving right\n”); - map[pos[0]][pos[1]] = ’-’; - if (pos[1] = 4) { - pos[1] = 0; - } - else { - pos[1]++; - } - } else if (input = ’c’) { - printf(“Quitting\n”); -} else { - printf(“Invalid input\n”); -} -map[pos[0]][pos[1]] = ’’; -if (pos[0] = bonus[0] && pos[1] = bonus[1]) { - score+; - do{ - bonus[0]= arc4random_uniform(5); - bonus[1]= arc4random_uniform(5); - }while((bonus[0] = trap[0] && bonus[1] = trap[1]) || (bonus[0] = death[0] && bonus[1] = death[1]) || (bonus[0] = pos[0] && bonus[1] = pos[1])); -} -if (pos[0] = trap[0] && pos[1] = trap[1]) { - score–; - do{ - trap[0]= arc4random_uniform(5); - trap[1]= arc4random_uniform(5); - }while((trap[0] = bonus[0] && trap[1] = bonus[1]) || (trap[0] = death[0] && trap[1] = death[1]) || (trap[0] = pos[0] && trap[1] = pos[1])); - } -if (pos[0] = death[0] && pos[1] = death[1]) { - score = 0; - do{ - death[0]= arc4random_uniform(5); - death[1]= arc4random_uniform(5); - }while((death[0] = bonus[0] && death[1] = bonus[1]) || (death[0] = trap[0] && death[1] = trap[1]) || (death[0] = pos[0] && death[1] = pos[1])); -} -if (score % 3 = 0 && score ! 0 && stop = 0) { - map[death[0]][death[1]] = '-'; - do{ - death[0] arc4random_uniform(5); - death[1]= arc4random_uniform(5); - }while((death[0] = bonus[0] && death[1] = bonus[1]) || (death[0] = trap[0] && death[1] = trap[1]) || (death[0] = pos[0] && death[1] = pos[1])); - stop = 1; -} -else if (score % 3 != 0) { -stop = 0; -} -if (moves % 5 = 0 && moves ! 0) { - do{ - map[trap[0]][trap[1]] = ’-’; - trap[0]= arc4random_uniform(5); - trap[1]= arc4random_uniform(5); - }while((trap[0] = bonus[0] && trap[1] = bonus[1]) || (trap[0] = death[0] && trap[1] = death[1]) || (trap[0] = pos[0] && trap[1] = pos[1])); -

- -

- } - map[bonus[0]][bonus[1]] = ’B’; - map[trap[0]][trap[1]] = ’T’; - map[death[0]][death[1]] = ’D’; - moves++; - }while(input != ’c’); - return 0; -} -

-
+
+

The code :

+
+
+
  1: #include <stdio.h>
+  2: #include <stdlib.h>
+  3: int main(int argc, char *argv[]) {
+  4:     char input,map[5][5] = {
+  5:         {'-', '-', '-', '-', '-'},
+  6:         {'-', '-', '-', '-', '-'},
+  7:         {'-', '-', '-', '-', '-'},
+  8:         {'-', '-', '-', '-', '-'},
+  9:         {'-', '-', '-', '-', '-'}
+ 10:     };
+ 11:     int stop=0,i=0,moves=0,score=0,pos[2] = {2, 2};
+ 12:     int bonus[2];
+ 13:     int trap[2] ;
+ 14:     int death[2];
+ 15:     map[pos[0]][pos[1]] = '+';
+ 16:     do{
+ 17:     bonus[0] = arc4random_uniform(5); bonus[1] = arc4random_uniform(5);
+ 18:     trap[0] = arc4random_uniform(5); trap[1] = arc4random_uniform(5);
+ 19:     death[0] = arc4random_uniform(5); death[1] = arc4random_uniform(5);
+ 20:     }while((bonus[0] == trap[0] && bonus[1] == trap[1]) || (bonus[0] == death[0] && bonus[1] == death[1]) || (trap[0] == death[0] && trap[1] == death[1]) || (bonus[0] == pos[0] && bonus[1] == pos[1]) || (trap[0] == pos[0] && trap[1] == pos[1]) || (death[0] == pos[0] && death[1] == pos[1]));
+ 21:     map[bonus[0]][bonus[1]] = 'B';
+ 22:     map[trap[0]][trap[1]] = 'T';
+ 23:     map[death[0]][death[1]] = 'D';
+ 24:     do{
+ 25:     printf("Map:\n");
+ 26:     for (int i = 0; i < 5; i++) {
+ 27:         for (int j = 0; j < 5; j++) {
+ 28:             printf("%c ", map[i][j]);
+ 29:         }
+ 30:         printf("\n");
+ 31:     }
+ 32:     printf("Score: %d\n", score);
+ 33:     printf("Moves: %d\n", moves);
+ 34:     printf("Enter a direction (w,a,s,d) or c to quit: ");
+ 35:     scanf(" %c", &input);
+ 36: // pos[0] updown pos[1] lr
+ 37:     if (input == 'w') {
+ 38:         printf("Moving up\n");
+ 39:         map[pos[0]][pos[1]] = '-';
+ 40:         if (pos[0] == 0) {
+ 41:             pos[0] = 4;
+ 42:         }
+ 43:         else {
+ 44:             pos[0]--;
+ 45:         }
+ 46:     } else if (input == 'a') {
+ 47:         printf("Moving left\n");
+ 48:         map[pos[0]][pos[1]] = '-';
+ 49:         if (pos[1] == 0) {
+ 50:             pos[1] = 4;
+ 51:         }
+ 52:         else {
+ 53:             pos[1]--;
+ 54:         }
+ 55:     } else if (input == 's') {
+ 56: 
+ 57:         printf("Moving down\n");
+ 58:         map[pos[0]][pos[1]] = '-';
+ 59:         if (pos[0] == 4) {
+ 60:             pos[0] = 0;
+ 61:         }
+ 62:         else {
+ 63:             pos[0]++;
+ 64:         }
+ 65:     } else if (input == 'd') {
+ 66:         printf("Moving right\n");
+ 67:         map[pos[0]][pos[1]] = '-';
+ 68:         if (pos[1] == 4) {
+ 69:             pos[1] = 0;
+ 70:         }
+ 71:         else {
+ 72:             pos[1]++;
+ 73:         }
+ 74:     } else if (input == 'c') {
+ 75:         printf("Quitting\n");
+ 76:     } else {
+ 77:         printf("Invalid input\n");
+ 78:     }
+ 79:     map[pos[0]][pos[1]] = '+';
+ 80:     if (pos[0] == bonus[0] && pos[1] == bonus[1]) {
+ 81:         score++;
+ 82:         do{
+ 83:         bonus[0]= arc4random_uniform(5);
+ 84:         bonus[1]= arc4random_uniform(5);
+ 85:         }while((bonus[0] == trap[0] && bonus[1] == trap[1]) || (bonus[0] == death[0] && bonus[1] == death[1]) || (bonus[0] == pos[0] && bonus[1] == pos[1]));
+ 86:     }
+ 87:     if (pos[0] == trap[0] && pos[1] == trap[1]) {
+ 88:         score--;
+ 89:         do{
+ 90:         trap[0]= arc4random_uniform(5);
+ 91:         trap[1]= arc4random_uniform(5);
+ 92:         }while((trap[0] == bonus[0] && trap[1] == bonus[1]) || (trap[0] == death[0] && trap[1] == death[1]) || (trap[0] == pos[0] && trap[1] == pos[1]));
+ 93:         }
+ 94:     if (pos[0] == death[0] && pos[1] == death[1]) {
+ 95:         score = 0;
+ 96:         do{
+ 97:         death[0]= arc4random_uniform(5);
+ 98:         death[1]= arc4random_uniform(5);
+ 99:         }while((death[0] == bonus[0] && death[1] == bonus[1]) || (death[0] == trap[0] && death[1] == trap[1]) || (death[0] == pos[0] && death[1] == pos[1]));
+100:     }
+101:     if (score % 3 == 0 && score != 0 && stop == 0) {
+102:         map[death[0]][death[1]] = '-';
+103:         do{
+104:         death[0]= arc4random_uniform(5);
+105:         death[1]= arc4random_uniform(5);
+106:         }while((death[0] == bonus[0] && death[1] == bonus[1]) || (death[0] == trap[0] && death[1] == trap[1]) || (death[0] == pos[0] && death[1] == pos[1]));
+107:         stop = 1;
+108:     }
+109:     else if (score % 3 != 0) {
+110:     stop = 0;
+111:     }
+112:     if (moves % 5 == 0 && moves != 0) {
+113:         do{
+114:             map[trap[0]][trap[1]] = '-';
+115:         trap[0]= arc4random_uniform(5);
+116:         trap[1]= arc4random_uniform(5);
+117:         }while((trap[0] == bonus[0] && trap[1] == bonus[1]) || (trap[0] == death[0] && trap[1] == death[1]) || (trap[0] == pos[0] && trap[1] == pos[1]));
+118: 
+119:     }
+120:     map[bonus[0]][bonus[1]] = 'B';
+121:     map[trap[0]][trap[1]] = 'T';
+122:     map[death[0]][death[1]] = 'D';
+123:     moves++;
+124:     }while(input != 'c');
+125:     return 0;
+126: }
+127: 
+
+

Let’s go step by step and see what we can fix or improve, to start off, line 4 to 10 can be reduced to a single line (which will be beneficial later too)

-
-

-char input,map[5][5] = {’-’}; -

-
+
+
4: char input,map[5][5] = {'-'};
+
+

Much better…but can be even more better, why stop at 5 when we can give the choice to the user !!

-
-

-int n=5,m=5; -char input,map[n][m]={’-’}; -

-
+
+
4: int n=5,m=5;
+5: char input,map[n][m]={'-'};
+
+

For now at least, n and m are hardcoded to 5, but this will change later. @@ -227,11 +189,10 @@ For now at least, n and m are hardcoded to 5, but this will change later.

Of course we have the usual inits on line 11, though since we are using variables instead of hardcoding 5, we will have to find the center by ourselves

-
-

-int stop=0,i=0,moves=0,score=0,pos[2] = {n/2,m/2}; -

-
+
+
11:     int stop=0,i=0,moves=0,score=0,pos[2] = {n/2,m/2};
+
+

This is getting better, of course we then initialize the coordinates of bonus, trap, and death, and set the player as a ’+’ in the field. @@ -241,13 +202,12 @@ This is getting better, of course we then initialize the coordinates of bonus, t

Here comes the line 17-21, where it generates a random coordinate for the aforementioned pickups, and do that until there is no conflict between eachother and the player) here we will need to change it a tiny bit.

-
-

-bonus[0] = arc4random_uniform(n); bonus[1] = arc4random_uniform(m); -trap[0] = arc4random_uniform(n); trap[1] = arc4random_uniform(m); -death[0] = arc4random_uniform(n); death[1] = arc4random_uniform(m); -

-
+
+
17:     bonus[0] = arc4random_uniform(n); bonus[1] = arc4random_uniform(m);
+18:     trap[0] = arc4random_uniform(n); trap[1] = arc4random_uniform(m);
+19:     death[0] = arc4random_uniform(n); death[1] = arc4random_uniform(m);
+
+

Looking good so far!!, We then have line 21-23 which also shows the pickups as their respective symbols in the map. @@ -257,16 +217,16 @@ Looking good so far!!, We then have line 21-23 which also shows the pickups as t

The main interactive program starts here, which will learn at least one time and stop if the received input is a ’c’, it starts with a nested for loop on line 26 up to 31 to show the content of the map, nothing fancy, just some matrix stuff. we need to change the 5 though!

-
-

-for (int i = 0; i < n; i++) { - for (int j = 0; j < m; j++) { - printf(“%c ”, map[i][j]); - } - printf(“\n”); - } -

-
+
+
26:   for (int i = 0; i < n; i++) {
+27:         for (int j = 0; j < m; j++) {
+28:             printf("%c ", map[i][j]);
+29:         }
+30:         printf("\n");
+31:     }
+32: 
+
+

We show the score and the moves too, which at the start of the game are set to 0. and we prompt the user for a direction. Note here the space before the %c, this basically allows for the program to not choke on newlines and also even if the user writes multiple keys at the same time, they will still be done, like ww will make the player move twice up.

@@ -275,54 +235,51 @@ We show the score and the moves too, which at the start of the game are set to 0 After that we have some logic which should also be changed to account for the n and m changes yet again

-
-

-if (input = 'w') { - printf("Moving up\n"); - map[pos[0]][pos[1]] = '-'; - if (pos[0] = 0) { - pos[0] = n-1; - } - else { - pos[0]–; - } -} else if (input = 'a') { - printf("Moving left\n"); - map[pos[0]][pos[1]] = '-'; - if (pos[1] = 0) { - pos[1] = m-1; - } - else { - pos[1]–; - } -} else if (input == ’s’) { -

- -

- printf("Moving down\n”); - map[pos[0]][pos[1]] = ’-’; - if (pos[0] = n-1) { - pos[0] = 0; - } - else { - pos[0]++; - } - } else if (input = ’d’) { - printf(“Moving right\n”); - map[pos[0]][pos[1]] = ’-’; - if (pos[1] = m-1) { - pos[1] = 0; - } - else { - pos[1]++; - } - } else if (input = ’c’) { - printf(“Quitting\n”); -} else { - printf(“Invalid input\n”); -} -

-
+
+
37:     if (input == 'w') {
+38:         printf("Moving up\n");
+39:         map[pos[0]][pos[1]] = '-';
+40:         if (pos[0] == 0) {
+41:             pos[0] = n-1;
+42:         }
+43:         else {
+44:             pos[0]--;
+45:         }
+46:     } else if (input == 'a') {
+47:         printf("Moving left\n");
+48:         map[pos[0]][pos[1]] = '-';
+49:         if (pos[1] == 0) {
+50:             pos[1] = m-1;
+51:         }
+52:         else {
+53:             pos[1]--;
+54:         }
+55:     } else if (input == 's') {
+56: 
+57:         printf("Moving down\n");
+58:         map[pos[0]][pos[1]] = '-';
+59:         if (pos[0] == n-1) {
+60:             pos[0] = 0;
+61:         }
+62:         else {
+63:             pos[0]++;
+64:         }
+65:     } else if (input == 'd') {
+66:         printf("Moving right\n");
+67:         map[pos[0]][pos[1]] = '-';
+68:         if (pos[1] == m-1) {
+69:             pos[1] = 0;
+70:         }
+71:         else {
+72:             pos[1]++;
+73:         }
+74:     } else if (input == 'c') {
+75:         printf("Quitting\n");
+76:     } else {
+77:         printf("Invalid input\n");
+78:     }
+
+

What this achieves is the “teleportation effect” whenever you are at the border of the screen! @@ -332,7 +289,7 @@ What this achieves is the “teleportation effect” whenever you are at

Author: Crystal

-

Created: 2024-02-14 Wed 23:58

+

Created: 2024-02-15 Thu 00:02

-- cgit 1.4.1-2-gfad0