diff options
Diffstat (limited to 'blog/c')
-rw-r--r-- | blog/c/game.html | 331 |
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>“-”</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-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 <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]] = '+'; + map[pos[0]][pos[1]] = ’<del>’; 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'; + }while((bonus[0] <code>= trap[0] && bonus[1] =</code> trap[1]) || (bonus[0] <code>= death[0] && bonus[1] =</code> death[1]) || (trap[0] <code>= death[0] && trap[1] =</code> death[1]) || (bonus[0] <code>= pos[0] && bonus[1] =</code> pos[1]) || (trap[0] <code>= pos[0] && trap[1] =</code> pos[1]) || (death[0] <code>= pos[0] && death[1] =</code> 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++) { + printf(“Map:\n”); + for (int i = 0; i < 5; i+</del>) { for (int j = 0; j < 5; j++) { - printf("%c ", map[i][j]); + printf(“%c ”, map[i][j]); } - printf("\n"); + 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); + 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') { + 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]–; } - } 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]–; } - } else if (input == 's') { + } else if (input == ’s’) { +</p> - printf("Moving down\n"); - map[pos[0]][pos[1]] = '-'; - if (pos[0] == 4) { +<p> + printf("Moving down\n”); + map[pos[0]][pos[1]] = ’-’; + 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> ’d’) { + printf(“Moving right\n”); + map[pos[0]][pos[1]] = ’-’; + 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] && 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])); + } else if (input =</code> ’c’) { + printf(“Quitting\n”); +} else { + printf(“Invalid input\n”); +} +map[pos[0]][pos[1]] = ’<del>’; +if (pos[0] <code>= bonus[0] && pos[1] =</code> bonus[1]) { + score+</del>; + do{ + bonus[0]= arc4random_uniform(5); + bonus[1]= arc4random_uniform(5); + }while((bonus[0] <code>= trap[0] && bonus[1] =</code> trap[1]) || (bonus[0] <code>= death[0] && bonus[1] =</code> death[1]) || (bonus[0] <code>= pos[0] && bonus[1] =</code> pos[1])); +} +if (pos[0] <code>= trap[0] && pos[1] =</code> trap[1]) { + score–; + do{ + trap[0]= arc4random_uniform(5); + trap[1]= arc4random_uniform(5); + }while((trap[0] <code>= bonus[0] && trap[1] =</code> bonus[1]) || (trap[0] <code>= death[0] && trap[1] =</code> death[1]) || (trap[0] <code>= pos[0] && trap[1] =</code> pos[1])); } - if (score % 3 == 0 && score != 0 && stop == 0) { +if (pos[0] <code>= death[0] && pos[1] =</code> death[1]) { + score = 0; + do{ + death[0]= arc4random_uniform(5); + death[1]= arc4random_uniform(5); + }while((death[0] <code>= bonus[0] && death[1] =</code> bonus[1]) || (death[0] <code>= trap[0] && death[1] =</code> trap[1]) || (death[0] <code>= pos[0] && death[1] =</code> pos[1])); +} +if (score % 3 <code>= 0 && score !</code> 0 && stop <code>= 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])); + death[0]</code> arc4random_uniform(5); + death[1]= arc4random_uniform(5); + }while((death[0] <code>= bonus[0] && death[1] =</code> bonus[1]) || (death[0] <code>= trap[0] && death[1] =</code> trap[1]) || (death[0] <code>= pos[0] && death[1] =</code> pos[1])); + stop = 1; +} +else if (score % 3 != 0) { +stop = 0; +} +if (moves % 5 <code>= 0 && moves !</code> 0) { + do{ + map[trap[0]][trap[1]] = ’-’; + trap[0]= arc4random_uniform(5); + trap[1]= arc4random_uniform(5); + }while((trap[0] <code>= bonus[0] && trap[1] =</code> bonus[1]) || (trap[0] <code>= death[0] && trap[1] =</code> death[1]) || (trap[0] <code>= pos[0] && 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]] = ’B’; + map[trap[0]][trap[1]] = ’T’; + map[death[0]][death[1]] = ’D’; moves++; - }while(input != 'c'); + }while(input != ’c’); return 0; } - -</pre> +</p> +</blockquote> <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) </p> -<pre class="example" id="orgc476b5a"> -char input,map[5][5] = {'-'}; -</pre> +<blockquote> +<p> +char input,map[5][5] = {’-’}; +</p> +</blockquote> <p> Much better…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]={’-’}; +</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>’+’</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>’c’</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 < n; i++) { - for (int j = 0; j < m; j++) { - printf("%c ", map[i][j]); - } - printf("\n"); - } - -</pre> +<blockquote> +<p> +for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + printf(“%c ”, map[i][j]); + } + printf(“\n”); + } +</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]–; + } +} 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]–; + } +} else if (input == ’s’) { +</p> - printf("Moving down\n"); - map[pos[0]][pos[1]] = '-'; - if (pos[0] == n-1) { +<p> + printf("Moving down\n”); + map[pos[0]][pos[1]] = ’-’; + 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> ’d’) { + printf(“Moving right\n”); + map[pos[0]][pos[1]] = ’-’; + 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> ’c’) { + printf(“Quitting\n”); +} else { + printf(“Invalid input\n”); +} +</p> +</blockquote> <p> What this achieves is the “teleportation effect” whenever you are at the border of the screen! @@ -281,7 +332,7 @@ What this achieves is the “teleportation effect” 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> |