UP | HOME

The loneliness Game

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 :

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 :

  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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2023-11-01 Wed 20:17 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>ALSD1</title>
<meta name="author" content="Crystal" />
<meta name="generator" content="Org Mode" />
<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="https://crystal.tilde.institute/favicon.png">
</head>
<body>
<div id="org-div-home-and-up">
 <a accesskey="h" href="../../../uni_notes/"> UP </a>
 |
 <a accesskey="H" href="https://crystal.tilde.institute/"> HOME </a>
</div><div id="content" class="content">
<h1 class="title">ALSD1</h1>
<div id="table-of-contents" role="doc-toc">
<h2>Table of Contents</h2>
<div id="text-table-of-contents" role="doc-toc">
<ul>
<li><a href="#org99d5220">Contenu de la Matiére</a>
<ul>
<li><a href="#orgbd002d7">Chapitre 1: Elements de Base</a></li>
<li><a href="#orgeb962fb">Chapitre 2: Présentation du formalisme Algorithmique</a></li>
<li><a href="#org7a1fb51">Chapitre 3: Eléments de base du language C</a></li>
<li><a href="#org5f789aa">Chapitre 4: Modularité( Fonction et Procédure )</a></li>
<li><a href="#org330830d">Chapitre 5: Les structures des données statiques</a></li>
</ul>
</li>
<li><a href="#orgced3d78">Premier cours : Algorithmes <i>Oct 1</i> :</a>
<ul>
<li><a href="#orga9ff303">Définition d&rsquo;un algorithm :</a>
<ul>
<li><a href="#org38de9fd">Example d&rsquo;un Algo : Résolution d&rsquo;une équation du second ordre (ax²+bx+c=0)</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-org99d5220" class="outline-2">
<h2 id="org99d5220">Contenu de la Matiére</h2>
<div class="outline-text-2" id="text-org99d5220">
</div>
<div id="outline-container-orgbd002d7" class="outline-3">
<h3 id="orgbd002d7">Chapitre 1: Elements de Base</h3>
<div class="outline-text-3" id="text-orgbd002d7">
<ul class="org-ul">
<li>Algorithmique, procésseur, action.<br /></li>
<li>Programme et languages de programmation.<br /></li>
<li>Analyse des problémes.<br /></li>
</ul>
</div>
</div>
<div id="outline-container-orgeb962fb" class="outline-3">
<h3 id="orgeb962fb">Chapitre 2: Présentation du formalisme Algorithmique</h3>
</div>
<div id="outline-container-org7a1fb51" class="outline-3">
<h3 id="org7a1fb51">Chapitre 3: Eléments de base du language C</h3>
</div>
<div id="outline-container-org5f789aa" class="outline-3">
<h3 id="org5f789aa">Chapitre 4: Modularité( Fonction et Procédure )</h3>
</div>
<div id="outline-container-org330830d" class="outline-3">
<h3 id="org330830d">Chapitre 5: Les structures des données statiques</h3>
</div>
</div>
<div id="outline-container-orgced3d78" class="outline-2">
<h2 id="orgced3d78">Premier cours : Algorithmes <i>Oct 1</i> :</h2>
<div class="outline-text-2" id="text-orgced3d78">
</div>
<div id="outline-container-orga9ff303" class="outline-3">
<h3 id="orga9ff303">Définition d&rsquo;un algorithm :</h3>
<div class="outline-text-3" id="text-orga9ff303">
<p>
Un ensemble d&rsquo;opérations ecrites dans le language naturel.<br />
</p>
</div>
<div id="outline-container-org38de9fd" class="outline-4">
<h4 id="org38de9fd">Example d&rsquo;un Algo : Résolution d&rsquo;une équation du second ordre (ax²+bx+c=0)</h4>
<div class="outline-text-4" id="text-org38de9fd">
<ol class="org-ol">
<li>Si a=0 ET b=0 alors <b>l&rsquo;équation n&rsquo;est pas du 2nd ordre</b>.<br /></li>
<li>Si a=0 et b≠0 alors <b>x= -c/5</b> .<br /></li>
<li>Si a≠0 alors <b>calculer Δ= b²-4ac</b> :<br />
<ol class="org-ol">
<li>Si Δ=0 alors <b>x=-b/2a</b>.<br /></li>
<li>Si Δ&lt;0 alors <b>l&rsquo;équation n&rsquo;as pas de solution</b>.<br /></li>
<li>Si Δ&gt;0 alors <b>x=[-b±sqrt(Δ)]/2a</b><br /></li>
</ol></li>
</ol>
</div>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: Crystal</p>
<p class="date">Created: 2023-11-01 Wed 20:17</p>
</div>
</body>
</html>
) { 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 7 or 8 lines (which will be beneficial later too)

 4: int n=5,m=5;
 5: char input,map[50][50];
 6:     for (int i = 0; i < n; i++) {
 7:         for (int j = 0; j < m; j++) {
 8:             map[i][j] = '-';
 9:         }
10:     }
11: 

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

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.

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.

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.

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!

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.

After that we have some logic which should also be changed to account for the n and m changes yet again

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!

Now we fix things from line 80 to the end of the program, aka replacing ever occurrence of 5 with n or m

 80:     if (pos[0] == bonus[0] && pos[1] == bonus[1]) {
 81:         score++;
 82:         do{
 83:         bonus[0]= arc4random_uniform(n);
 84:         bonus[1]= arc4random_uniform(m);
 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(n);
 91:         trap[1]= arc4random_uniform(m);
 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(n);
 98:         death[1]= arc4random_uniform(m);
 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(n);
105:         death[1]= arc4random_uniform(m);
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(n);
116:         trap[1]= arc4random_uniform(m);
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:     }

Aaaaand this should be it

Author: Crystal

Created: 2024-02-22 Thu 19:09