diff options
-rw-r--r-- | blog/c/game.html | 90 | ||||
-rwxr-xr-x | src/org/blog/c/a.out | bin | 0 -> 9976 bytes | |||
-rw-r--r-- | src/org/blog/c/game.c | 126 | ||||
-rw-r--r-- | src/org/blog/c/game.org | 64 |
4 files changed, 254 insertions, 26 deletions
diff --git a/blog/c/game.html b/blog/c/game.html index 6543e5a..90c1965 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-15 Thu 00:09 --> +<!-- 2024-02-22 Thu 19:09 --> <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> @@ -23,17 +23,17 @@ <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-orgbfcec8f" class="outline-2"> -<h2 id="orgbfcec8f">The concept :</h2> -<div class="outline-text-2" id="text-orgbfcec8f"> +<div id="outline-container-org26dcb46" class="outline-2"> +<h2 id="org26dcb46">The concept :</h2> +<div class="outline-text-2" id="text-org26dcb46"> <p> Basically the player is faced with a NxM field made up with the sign <b>“-”</b> and the player is denoted by the symbol <b>“+”</b>, there are also Bonuses <b>“B”</b> which add 1 to your score, Traps <b>“T”</b>, that remove one from your score, and Dead <b>“D”</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-org7a1e154" class="outline-2"> -<h2 id="org7a1e154">The code :</h2> -<div class="outline-text-2" id="text-org7a1e154"> +<div id="outline-container-org37eeebf" class="outline-2"> +<h2 id="org37eeebf">The code :</h2> +<div class="outline-text-2" id="text-org37eeebf"> <div class="org-src-container"> <pre class="src src-c"><span class="linenr"> 1: </span><span style="color: #f9e2af;">#include</span> <span style="color: #f38ba8;"><</span><span style="color: #a6e3a1;">stdio.h</span><span style="color: #f38ba8;">></span> <span class="linenr"> 2: </span><span style="color: #f9e2af;">#include</span> <span style="color: #f38ba8;"><</span><span style="color: #a6e3a1;">stdlib.h</span><span style="color: #f38ba8;">></span> @@ -167,23 +167,22 @@ Basically the player is faced with a NxM field made up with the sign <b>“- <p> -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) +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 7 or 8 lines (which will be beneficial later too) </p> <div class="org-src-container"> -<pre class="src src-c"><span class="linenr">4: </span><span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">input</span>,<span style="color: #cdd6f4;">map</span><span style="color: #f38ba8;">[</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">][</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">]</span> = <span style="color: #f38ba8;">{</span><span style="color: #a6e3a1;">'-'</span><span style="color: #f38ba8;">}</span>; -</pre> -</div> -<p> -Much better…but can be even more better, why stop at 5 when we can give the choice to the user !! -</p> -<div class="org-src-container"> -<pre class="src src-c"><span class="linenr">4: </span><span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">n</span>=<span style="color: #fab387;">5</span>,<span style="color: #cdd6f4;">m</span>=<span style="color: #fab387;">5</span>; -<span class="linenr">5: </span><span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">input</span>,<span style="color: #cdd6f4;">map</span><span style="color: #f38ba8;">[</span>n<span style="color: #f38ba8;">][</span>m<span style="color: #f38ba8;">]</span>=<span style="color: #f38ba8;">{</span><span style="color: #a6e3a1;">'-'</span><span style="color: #f38ba8;">}</span>; +<pre class="src src-c"><span class="linenr"> 4: </span><span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">n</span>=<span style="color: #fab387;">5</span>,<span style="color: #cdd6f4;">m</span>=<span style="color: #fab387;">5</span>; +<span class="linenr"> 5: </span><span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">input</span>,<span style="color: #cdd6f4;">map</span><span style="color: #f38ba8;">[</span><span style="color: #fab387;">50</span><span style="color: #f38ba8;">][</span><span style="color: #fab387;">50</span><span style="color: #f38ba8;">]</span>; +<span class="linenr"> 6: </span> <span style="color: #cba6f7;">for</span> <span style="color: #f38ba8;">(</span><span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">i</span> = <span style="color: #fab387;">0</span>; i < n; i++<span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> +<span class="linenr"> 7: </span> <span style="color: #cba6f7;">for</span> <span style="color: #fab387;">(</span><span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">j</span> = <span style="color: #fab387;">0</span>; j < m; j++<span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> +<span class="linenr"> 8: </span> map<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">][</span>j<span style="color: #f9e2af;">]</span> = <span style="color: #a6e3a1;">'-'</span>; +<span class="linenr"> 9: </span> <span style="color: #fab387;">}</span> +<span class="linenr">10: </span> <span style="color: #f38ba8;">}</span> +<span class="linenr">11: </span> </pre> </div> <p> -For now at least, n and m are hardcoded to 5, but this will change later. +For now at least, n and m are hardcoded to 5, but this will change later. And I picked 50x50 as a max size because why not </p> <p> @@ -283,12 +282,65 @@ After that we have some logic which should also be changed to account for the n <p> What this achieves is the “teleportation effect” whenever you are at the border of the screen! </p> + + +<p> +Now we fix things from line 80 to the end of the program, aka replacing ever occurrence of 5 with n or m +</p> +<div class="org-src-container"> +<pre class="src src-c"><span class="linenr"> 80: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>pos<span style="color: #fab387;">[</span><span style="color: #fab387;">0</span><span style="color: #fab387;">]</span> == bonus<span style="color: #fab387;">[</span><span style="color: #fab387;">0</span><span style="color: #fab387;">]</span> && pos<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span> == bonus<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> +<span class="linenr"> 81: </span> score++; +<span class="linenr"> 82: </span> <span style="color: #cba6f7;">do</span><span style="color: #fab387;">{</span> +<span class="linenr"> 83: </span> bonus<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>n<span style="color: #f9e2af;">)</span>; +<span class="linenr"> 84: </span> bonus<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>m<span style="color: #f9e2af;">)</span>; +<span class="linenr"> 85: </span> <span style="color: #fab387;">}</span><span style="color: #cba6f7;">while</span><span style="color: #fab387;">(</span><span style="color: #f9e2af;">(</span>bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span><span style="color: #fab387;">)</span>; +<span class="linenr"> 86: </span> <span style="color: #f38ba8;">}</span> +<span class="linenr"> 87: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>pos<span style="color: #fab387;">[</span><span style="color: #fab387;">0</span><span style="color: #fab387;">]</span> == trap<span style="color: #fab387;">[</span><span style="color: #fab387;">0</span><span style="color: #fab387;">]</span> && pos<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span> == trap<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> +<span class="linenr"> 88: </span> score--; +<span class="linenr"> 89: </span> <span style="color: #cba6f7;">do</span><span style="color: #fab387;">{</span> +<span class="linenr"> 90: </span> trap<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>n<span style="color: #f9e2af;">)</span>; +<span class="linenr"> 91: </span> trap<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>m<span style="color: #f9e2af;">)</span>; +<span class="linenr"> 92: </span> <span style="color: #fab387;">}</span><span style="color: #cba6f7;">while</span><span style="color: #fab387;">(</span><span style="color: #f9e2af;">(</span>trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span><span style="color: #fab387;">)</span>; +<span class="linenr"> 93: </span> <span style="color: #f38ba8;">}</span> +<span class="linenr"> 94: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>pos<span style="color: #fab387;">[</span><span style="color: #fab387;">0</span><span style="color: #fab387;">]</span> == death<span style="color: #fab387;">[</span><span style="color: #fab387;">0</span><span style="color: #fab387;">]</span> && pos<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span> == death<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> +<span class="linenr"> 95: </span> score = <span style="color: #fab387;">0</span>; +<span class="linenr"> 96: </span> <span style="color: #cba6f7;">do</span><span style="color: #fab387;">{</span> +<span class="linenr"> 97: </span> death<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>n<span style="color: #f9e2af;">)</span>; +<span class="linenr"> 98: </span> death<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>m<span style="color: #f9e2af;">)</span>; +<span class="linenr"> 99: </span> <span style="color: #fab387;">}</span><span style="color: #cba6f7;">while</span><span style="color: #fab387;">(</span><span style="color: #f9e2af;">(</span>death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span><span style="color: #fab387;">)</span>; +<span class="linenr">100: </span> <span style="color: #f38ba8;">}</span> +<span class="linenr">101: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>score % <span style="color: #fab387;">3</span> == <span style="color: #fab387;">0</span> && score != <span style="color: #fab387;">0</span> && stop == <span style="color: #fab387;">0</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> +<span class="linenr">102: </span> map<span style="color: #fab387;">[</span>death<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span><span style="color: #fab387;">][</span>death<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span><span style="color: #fab387;">]</span> = <span style="color: #a6e3a1;">'-'</span>; +<span class="linenr">103: </span> <span style="color: #cba6f7;">do</span><span style="color: #fab387;">{</span> +<span class="linenr">104: </span> death<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>n<span style="color: #f9e2af;">)</span>; +<span class="linenr">105: </span> death<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>m<span style="color: #f9e2af;">)</span>; +<span class="linenr">106: </span> <span style="color: #fab387;">}</span><span style="color: #cba6f7;">while</span><span style="color: #fab387;">(</span><span style="color: #f9e2af;">(</span>death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span><span style="color: #fab387;">)</span>; +<span class="linenr">107: </span> stop = <span style="color: #fab387;">1</span>; +<span class="linenr">108: </span> <span style="color: #f38ba8;">}</span> +<span class="linenr">109: </span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>score % <span style="color: #fab387;">3</span> != <span style="color: #fab387;">0</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> +<span class="linenr">110: </span> stop = <span style="color: #fab387;">0</span>; +<span class="linenr">111: </span> <span style="color: #f38ba8;">}</span> +<span class="linenr">112: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>moves % <span style="color: #fab387;">5</span> == <span style="color: #fab387;">0</span> && moves != <span style="color: #fab387;">0</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> +<span class="linenr">113: </span> <span style="color: #cba6f7;">do</span><span style="color: #fab387;">{</span> +<span class="linenr">114: </span> map<span style="color: #f9e2af;">[</span>trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">][</span>trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">]</span> = <span style="color: #a6e3a1;">'-'</span>; +<span class="linenr">115: </span> trap<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>n<span style="color: #f9e2af;">)</span>; +<span class="linenr">116: </span> trap<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span>= arc4random_uniform<span style="color: #f9e2af;">(</span>m<span style="color: #f9e2af;">)</span>; +<span class="linenr">117: </span> <span style="color: #fab387;">}</span><span style="color: #cba6f7;">while</span><span style="color: #fab387;">(</span><span style="color: #f9e2af;">(</span>trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span> || <span style="color: #f9e2af;">(</span>trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span> && trap<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span> == pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">1</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span><span style="color: #fab387;">)</span>; +<span class="linenr">118: </span> +<span class="linenr">119: </span> <span style="color: #f38ba8;">}</span> +</pre> +</div> + + +<p> +Aaaaand this should be it +</p> </div> </div> </div> <div id="postamble" class="status"> <p class="author">Author: Crystal</p> -<p class="date">Created: 2024-02-15 Thu 00:09</p> +<p class="date">Created: 2024-02-22 Thu 19:09</p> </div> </body> </html> diff --git a/src/org/blog/c/a.out b/src/org/blog/c/a.out new file mode 100755 index 0000000..073e240 --- /dev/null +++ b/src/org/blog/c/a.out Binary files differdiff --git a/src/org/blog/c/game.c b/src/org/blog/c/game.c new file mode 100644 index 0000000..02116e7 --- /dev/null +++ b/src/org/blog/c/game.c @@ -0,0 +1,126 @@ +#include <stdio.h> +#include <stdlib.h> +int main(int argc, char *argv[]) { + int n=5,m=5; + char input,map[50][50]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + map[i][j] = '-'; + } + } + int stop=0,i=0,moves=0,score=0,pos[2] = {n/2,m/2}; + int bonus[2]; + int trap[2] ; + int death[2]; + map[pos[0]][pos[1]] = '+'; + do{ + 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); + }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 < n; i++) { + for (int j = 0; j < m; 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] = 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"); + } + map[pos[0]][pos[1]] = '+'; + if (pos[0] == bonus[0] && pos[1] == bonus[1]) { + score++; + do{ + bonus[0]= arc4random_uniform(n); + bonus[1]= arc4random_uniform(m); + }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(n); + trap[1]= arc4random_uniform(m); + }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(n); + death[1]= arc4random_uniform(m); + }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(n); + death[1]= arc4random_uniform(m); + }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(n); + trap[1]= arc4random_uniform(m); + }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; +} diff --git a/src/org/blog/c/game.org b/src/org/blog/c/game.org index 3ed154e..1f618e4 100644 --- a/src/org/blog/c/game.org +++ b/src/org/blog/c/game.org @@ -150,17 +150,19 @@ int main(int argc, char *argv[]) { #+END_SRC - 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) - #+BEGIN_SRC c -n 4 -char input,map[5][5] = {'-'}; - #+END_SRC - Much better...but can be even more better, why stop at 5 when we can give the choice to the user !! + 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 7 or 8 lines (which will be beneficial later too) #+BEGIN_SRC c -n 4 int n=5,m=5; -char input,map[n][m]={'-'}; +char input,map[50][50]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + map[i][j] = '-'; + } + } + #+END_SRC - For now at least, n and m are hardcoded to 5, but this will change later. + For now at least, n and m are hardcoded to 5, but this will change later. And I picked 50x50 as a max size because why not 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 #+BEGIN_SRC c -n 11 @@ -239,3 +241,51 @@ After that we have some logic which should also be changed to account for the n #+END_SRC What this achieves is the "teleportation effect" whenever you are at the border of the screen! + + +Now we fix things from line 80 to the end of the program, aka replacing ever occurrence of 5 with n or m +#+BEGIN_SRC c -n 80 + if (pos[0] == bonus[0] && pos[1] == bonus[1]) { + score++; + do{ + bonus[0]= arc4random_uniform(n); + bonus[1]= arc4random_uniform(m); + }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(n); + trap[1]= arc4random_uniform(m); + }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(n); + death[1]= arc4random_uniform(m); + }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(n); + death[1]= arc4random_uniform(m); + }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(n); + trap[1]= arc4random_uniform(m); + }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])); + + } +#+END_SRC + + +Aaaaand this should be it |