summary refs log tree commit diff stats
path: root/blog
diff options
context:
space:
mode:
authorCrystal <crystal@wizard.tower>2024-02-14 23:58:49 +0100
committerCrystal <crystal@wizard.tower>2024-02-14 23:58:49 +0100
commit663bbf0173c7e48e9d50b75a15b1b6014beaa135 (patch)
tree2aa266573ba31843114c6d8c1f594c3f6d930328 /blog
parent881ff00e4140350fa74fa256733fdcdf92ef94c4 (diff)
downloadwww-663bbf0173c7e48e9d50b75a15b1b6014beaa135.tar.gz
Add stuff
Diffstat (limited to 'blog')
-rw-r--r--blog/c/game.html331
1 files changed, 191 insertions, 140 deletions
diff --git a/blog/c/game.html b/blog/c/game.html
index 18e530c..d79bbbf 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">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
-<!-- 2024-02-14 Wed 23:56 -->
+<!-- 2024-02-14 Wed 23:58 -->
 <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1" />
 <title>The loneliness Game</title>
@@ -12,6 +12,38 @@
 <link rel="stylesheet" type="text/css" href="../../src/css/colors.css"/>
 <link rel="stylesheet" type="text/css" href="../../src/css/style.css"/>
 <link rel="icon" type="image/x-icon" href="../../../favicon.png">
+<script>
+  window.MathJax = {
+    tex: {
+      ams: {
+        multlineWidth: '85%'
+      },
+      tags: 'ams',
+      tagSide: 'right',
+      tagIndent: '.8em'
+    },
+    chtml: {
+      scale: 1.0,
+      displayAlign: 'center',
+      displayIndent: '0em'
+    },
+    svg: {
+      scale: 1.0,
+      displayAlign: 'center',
+      displayIndent: '0em'
+    },
+    output: {
+      font: 'mathjax-modern',
+      displayOverflow: 'overflow'
+    }
+  };
+</script>
+
+<script
+  id="MathJax-script"
+  async
+  src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
+</script>
 </head>
 <body>
 <div id="org-div-home-and-up">
@@ -23,161 +55,170 @@
 <p>
 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!
 </p>
-<div id="outline-container-org4e3dd7a" class="outline-2">
-<h2 id="org4e3dd7a">The concept :</h2>
-<div class="outline-text-2" id="text-org4e3dd7a">
+<div id="outline-container-org494cba6" class="outline-2">
+<h2 id="org494cba6">The concept :</h2>
+<div class="outline-text-2" id="text-org494cba6">
 <p>
 Basically the player is faced with a NxM field made up with the sign <b>&ldquo;-&rdquo;</b> and the player is denoted by the symbol <b>&ldquo;+&rdquo;</b>, there are also Bonuses <b>&ldquo;B&rdquo;</b> which add 1 to your score, Traps <b>&ldquo;T&rdquo;</b>, that remove one from your score, and Dead <b>&ldquo;D&rdquo;</b> 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.
 </p>
 </div>
 </div>
-<div id="outline-container-orgfc70f38" class="outline-2">
-<h2 id="orgfc70f38">The code :</h2>
-<div class="outline-text-2" id="text-orgfc70f38">
-<pre class="example" id="org047cb86">
+<div id="outline-container-org490b019" class="outline-2">
+<h2 id="org490b019">The code :</h2>
+<div class="outline-text-2" id="text-org490b019">
+<blockquote>
+<p>
 #include &lt;stdio.h&gt;
 #include &lt;stdlib.h&gt;
 int main(int argc, char *argv[]) {
     char input,map[5][5] = {
-        {'-', '-', '-', '-', '-'},
-        {'-', '-', '-', '-', '-'},
-        {'-', '-', '-', '-', '-'},
-        {'-', '-', '-', '-', '-'},
-        {'-', '-', '-', '-', '-'}
+        {&rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;},
+        {&rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;},
+        {&rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;},
+        {&rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;},
+        {&rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;, &rsquo;-&rsquo;}
     };
     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]] = '+';
+    map[pos[0]][pos[1]] = &rsquo;<del>&rsquo;;
     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] &amp;&amp; bonus[1] == trap[1]) || (bonus[0] == death[0] &amp;&amp; bonus[1] == death[1]) || (trap[0] == death[0] &amp;&amp; trap[1] == death[1]) || (bonus[0] == pos[0] &amp;&amp; bonus[1] == pos[1]) || (trap[0] == pos[0] &amp;&amp; trap[1] == pos[1]) || (death[0] == pos[0] &amp;&amp; death[1] == pos[1]));
-    map[bonus[0]][bonus[1]] = 'B';
-    map[trap[0]][trap[1]] = 'T';
-    map[death[0]][death[1]] = 'D';
+    }while((bonus[0] <code>= trap[0] &amp;&amp; bonus[1] =</code> trap[1]) || (bonus[0] <code>= death[0] &amp;&amp; bonus[1] =</code> death[1]) || (trap[0] <code>= death[0] &amp;&amp; trap[1] =</code> death[1]) || (bonus[0] <code>= pos[0] &amp;&amp; bonus[1] =</code> pos[1]) || (trap[0] <code>= pos[0] &amp;&amp; trap[1] =</code> pos[1]) || (death[0] <code>= pos[0] &amp;&amp; death[1] =</code> pos[1]));
+    map[bonus[0]][bonus[1]] = &rsquo;B&rsquo;;
+    map[trap[0]][trap[1]] = &rsquo;T&rsquo;;
+    map[death[0]][death[1]] = &rsquo;D&rsquo;;
     do{
-    printf("Map:\n");
-    for (int i = 0; i &lt; 5; i++) {
+    printf(&ldquo;Map:\n&rdquo;);
+    for (int i = 0; i &lt; 5; i+</del>) {
         for (int j = 0; j &lt; 5; j++) {
-            printf("%c ", map[i][j]);
+            printf(&ldquo;%c &rdquo;, map[i][j]);
         }
-        printf("\n");
+        printf(&ldquo;\n&rdquo;);
     }
-    printf("Score: %d\n", score);
-    printf("Moves: %d\n", moves);
-    printf("Enter a direction (w,a,s,d) or c to quit: ");
-    scanf(" %c", &amp;input);
+    printf(&ldquo;Score: %d\n&rdquo;, score);
+    printf(&ldquo;Moves: %d\n&rdquo;, moves);
+    printf(&ldquo;Enter a direction (w,a,s,d) or c to quit: &rdquo;);
+    scanf(&ldquo; %c&rdquo;, &amp;input);
 // pos[0] updown pos[1] lr
-    if (input == 'w') {
+    if (input <code>= 'w') {
         printf("Moving up\n");
         map[pos[0]][pos[1]] = '-';
-        if (pos[0] == 0) {
+        if (pos[0] =</code> 0) {
             pos[0] = 4;
         }
         else {
-            pos[0]--;
+            pos[0]&#x2013;;
         }
-    } else if (input == 'a') {
+    } else if (input <code>= 'a') {
         printf("Moving left\n");
         map[pos[0]][pos[1]] = '-';
-        if (pos[1] == 0) {
+        if (pos[1] =</code> 0) {
             pos[1] = 4;
         }
         else {
-            pos[1]--;
+            pos[1]&#x2013;;
         }
-    } else if (input == 's') {
+    } else if (input == &rsquo;s&rsquo;) {
+</p>
 
-        printf("Moving down\n");
-        map[pos[0]][pos[1]] = '-';
-        if (pos[0] == 4) {
+<p>
+    printf("Moving down\n&rdquo;);
+    map[pos[0]][pos[1]] = &rsquo;-&rsquo;;
+    if (pos[0] <code>= 4) {
             pos[0] = 0;
         }
         else {
             pos[0]++;
         }
-    } else if (input == 'd') {
-        printf("Moving right\n");
-        map[pos[0]][pos[1]] = '-';
-        if (pos[1] == 4) {
+    } else if (input =</code> &rsquo;d&rsquo;) {
+    printf(&ldquo;Moving right\n&rdquo;);
+    map[pos[0]][pos[1]] = &rsquo;-&rsquo;;
+    if (pos[1] <code>= 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] &amp;&amp; pos[1] == bonus[1]) {
-        score++;
-        do{
-        bonus[0]= arc4random_uniform(5);
-        bonus[1]= arc4random_uniform(5);
-        }while((bonus[0] == trap[0] &amp;&amp; bonus[1] == trap[1]) || (bonus[0] == death[0] &amp;&amp; bonus[1] == death[1]) || (bonus[0] == pos[0] &amp;&amp; bonus[1] == pos[1]));
-    }
-    if (pos[0] == trap[0] &amp;&amp; pos[1] == trap[1]) {
-        score--;
-        do{
-        trap[0]= arc4random_uniform(5);
-        trap[1]= arc4random_uniform(5);
-        }while((trap[0] == bonus[0] &amp;&amp; trap[1] == bonus[1]) || (trap[0] == death[0] &amp;&amp; trap[1] == death[1]) || (trap[0] == pos[0] &amp;&amp; trap[1] == pos[1]));
-        }
-    if (pos[0] == death[0] &amp;&amp; pos[1] == death[1]) {
-        score = 0;
-        do{
-        death[0]= arc4random_uniform(5);
-        death[1]= arc4random_uniform(5);
-        }while((death[0] == bonus[0] &amp;&amp; death[1] == bonus[1]) || (death[0] == trap[0] &amp;&amp; death[1] == trap[1]) || (death[0] == pos[0] &amp;&amp; death[1] == pos[1]));
+    } else if (input =</code> &rsquo;c&rsquo;) {
+    printf(&ldquo;Quitting\n&rdquo;);
+} else {
+    printf(&ldquo;Invalid input\n&rdquo;);
+}
+map[pos[0]][pos[1]] = &rsquo;<del>&rsquo;;
+if (pos[0] <code>= bonus[0] &amp;&amp; pos[1] =</code> bonus[1]) {
+    score+</del>;
+    do{
+    bonus[0]= arc4random_uniform(5);
+    bonus[1]= arc4random_uniform(5);
+    }while((bonus[0] <code>= trap[0] &amp;&amp; bonus[1] =</code> trap[1]) || (bonus[0] <code>= death[0] &amp;&amp; bonus[1] =</code> death[1]) || (bonus[0] <code>= pos[0] &amp;&amp; bonus[1] =</code> pos[1]));
+}
+if (pos[0] <code>= trap[0] &amp;&amp; pos[1] =</code> trap[1]) {
+    score&#x2013;;
+    do{
+    trap[0]= arc4random_uniform(5);
+    trap[1]= arc4random_uniform(5);
+    }while((trap[0] <code>= bonus[0] &amp;&amp; trap[1] =</code> bonus[1]) || (trap[0] <code>= death[0] &amp;&amp; trap[1] =</code> death[1]) || (trap[0] <code>= pos[0] &amp;&amp; trap[1] =</code> pos[1]));
     }
-    if (score % 3 == 0 &amp;&amp; score != 0 &amp;&amp; stop == 0) {
+if (pos[0] <code>= death[0] &amp;&amp; pos[1] =</code> death[1]) {
+    score = 0;
+    do{
+    death[0]= arc4random_uniform(5);
+    death[1]= arc4random_uniform(5);
+    }while((death[0] <code>= bonus[0] &amp;&amp; death[1] =</code> bonus[1]) || (death[0] <code>= trap[0] &amp;&amp; death[1] =</code> trap[1]) || (death[0] <code>= pos[0] &amp;&amp; death[1] =</code> pos[1]));
+}
+if (score % 3 <code>= 0 &amp;&amp; score !</code> 0 &amp;&amp; stop <code>= 0) {
         map[death[0]][death[1]] = '-';
         do{
-        death[0]= arc4random_uniform(5);
-        death[1]= arc4random_uniform(5);
-        }while((death[0] == bonus[0] &amp;&amp; death[1] == bonus[1]) || (death[0] == trap[0] &amp;&amp; death[1] == trap[1]) || (death[0] == pos[0] &amp;&amp; death[1] == pos[1]));
-        stop = 1;
-    }
-    else if (score % 3 != 0) {
-    stop = 0;
-    }
-    if (moves % 5 == 0 &amp;&amp; moves != 0) {
-        do{
-            map[trap[0]][trap[1]] = '-';
-        trap[0]= arc4random_uniform(5);
-        trap[1]= arc4random_uniform(5);
-        }while((trap[0] == bonus[0] &amp;&amp; trap[1] == bonus[1]) || (trap[0] == death[0] &amp;&amp; trap[1] == death[1]) || (trap[0] == pos[0] &amp;&amp; trap[1] == pos[1]));
+        death[0]</code> arc4random_uniform(5);
+    death[1]= arc4random_uniform(5);
+    }while((death[0] <code>= bonus[0] &amp;&amp; death[1] =</code> bonus[1]) || (death[0] <code>= trap[0] &amp;&amp; death[1] =</code> trap[1]) || (death[0] <code>= pos[0] &amp;&amp; death[1] =</code> pos[1]));
+    stop = 1;
+}
+else if (score % 3 != 0) {
+stop = 0;
+}
+if (moves % 5 <code>= 0 &amp;&amp; moves !</code> 0) {
+    do{
+        map[trap[0]][trap[1]] = &rsquo;-&rsquo;;
+    trap[0]= arc4random_uniform(5);
+    trap[1]= arc4random_uniform(5);
+    }while((trap[0] <code>= bonus[0] &amp;&amp; trap[1] =</code> bonus[1]) || (trap[0] <code>= death[0] &amp;&amp; trap[1] =</code> death[1]) || (trap[0] <code>= pos[0] &amp;&amp; trap[1] =</code> pos[1]));
+</p>
 
+<p>
     }
-    map[bonus[0]][bonus[1]] = 'B';
-    map[trap[0]][trap[1]] = 'T';
-    map[death[0]][death[1]] = 'D';
+    map[bonus[0]][bonus[1]] = &rsquo;B&rsquo;;
+    map[trap[0]][trap[1]] = &rsquo;T&rsquo;;
+    map[death[0]][death[1]] = &rsquo;D&rsquo;;
     moves++;
-    }while(input != 'c');
+    }while(input != &rsquo;c&rsquo;);
     return 0;
 }
-
-</pre>
+</p>
+</blockquote>
 
 
 <p>
 Let&rsquo;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)
 </p>
-<pre class="example" id="orgc476b5a">
-char input,map[5][5] = {'-'};
-</pre>
+<blockquote>
+<p>
+char input,map[5][5] = {&rsquo;-&rsquo;};
+</p>
+</blockquote>
 <p>
 Much better&#x2026;but can be even more better, why stop at 5 when we can give the choice to the user !!
 </p>
-<pre class="example" id="orgcdc65be">
+<blockquote>
+<p>
 int n=5,m=5;
-char input,map[n][m]={'-'};
-</pre>
+char input,map[n][m]={&rsquo;-&rsquo;};
+</p>
+</blockquote>
 
 <p>
 For now at least, n and m are hardcoded to 5, but this will change later.
@@ -186,9 +227,11 @@ For now at least, n and m are hardcoded to 5, but this will change later.
 <p>
 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
 </p>
-<pre class="example" id="orge2ff0f5">
-    int stop=0,i=0,moves=0,score=0,pos[2] = {n/2,m/2};
-</pre>
+<blockquote>
+<p>
+int stop=0,i=0,moves=0,score=0,pos[2] = {n/2,m/2};
+</p>
+</blockquote>
 
 <p>
 This is getting better, of course we then initialize the coordinates of bonus, trap, and death, and set the player as a <b>&rsquo;+&rsquo;</b> in the field.
@@ -198,11 +241,13 @@ This is getting better, of course we then initialize the coordinates of bonus, t
 <p>
 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.
 </p>
-<pre class="example" id="org75ddb11">
-    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);
-</pre>
+<blockquote>
+<p>
+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);
+</p>
+</blockquote>
 
 <p>
 Looking good so far!!, We then have line 21-23 which also shows the pickups as their respective symbols in the map.
@@ -212,15 +257,16 @@ Looking good so far!!, We then have line 21-23 which also shows the pickups as t
 <p>
 The main interactive program starts here, which will learn at least one time and stop if the received input is a <b>&rsquo;c&rsquo;</b>, 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!
 </p>
-<pre class="example" id="org968765c">
-  for (int i = 0; i &lt; n; i++) {
-        for (int j = 0; j &lt; m; j++) {
-            printf("%c ", map[i][j]);
-        }
-        printf("\n");
-    }
-
-</pre>
+<blockquote>
+<p>
+for (int i = 0; i &lt; n; i++) {
+      for (int j = 0; j &lt; m; j++) {
+          printf(&ldquo;%c &rdquo;, map[i][j]);
+      }
+      printf(&ldquo;\n&rdquo;);
+  }
+</p>
+</blockquote>
 <p>
 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 <b>ww</b> will make the player move twice up.
 </p>
@@ -228,50 +274,55 @@ We show the score and the moves too, which at the start of the game are set to 0
 <p>
 After that we have some logic which should also be changed to account for the n and m changes yet again
 </p>
-<pre class="example" id="org112ebc5">
-    if (input == 'w') {
+
+<blockquote>
+<p>
+if (input <code>= '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') {
+        if (pos[0] =</code> 0) {
+        pos[0] = n-1;
+    }
+    else {
+        pos[0]&#x2013;;
+    }
+} else if (input <code>= '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') {
+        if (pos[1] =</code> 0) {
+        pos[1] = m-1;
+    }
+    else {
+        pos[1]&#x2013;;
+    }
+} else if (input == &rsquo;s&rsquo;) {
+</p>
 
-        printf("Moving down\n");
-        map[pos[0]][pos[1]] = '-';
-        if (pos[0] == n-1) {
+<p>
+    printf("Moving down\n&rdquo;);
+    map[pos[0]][pos[1]] = &rsquo;-&rsquo;;
+    if (pos[0] <code>= 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) {
+    } else if (input =</code> &rsquo;d&rsquo;) {
+    printf(&ldquo;Moving right\n&rdquo;);
+    map[pos[0]][pos[1]] = &rsquo;-&rsquo;;
+    if (pos[1] <code>= m-1) {
             pos[1] = 0;
         }
         else {
             pos[1]++;
         }
-    } else if (input == 'c') {
-        printf("Quitting\n");
-    } else {
-        printf("Invalid input\n");
-    }
-</pre>
+    } else if (input =</code> &rsquo;c&rsquo;) {
+    printf(&ldquo;Quitting\n&rdquo;);
+} else {
+    printf(&ldquo;Invalid input\n&rdquo;);
+}
+</p>
+</blockquote>
 
 <p>
 What this achieves is the &ldquo;teleportation effect&rdquo; whenever you are at the border of the screen!
@@ -281,7 +332,7 @@ What this achieves is the &ldquo;teleportation effect&rdquo; whenever you are at
 </div>
 <div id="postamble" class="status">
 <p class="author">Author: Crystal</p>
-<p class="date">Created: 2024-02-14 Wed 23:56</p>
+<p class="date">Created: 2024-02-14 Wed 23:58</p>
 </div>
 </body>
 </html>