diff options
Diffstat (limited to 'blog')
-rw-r--r-- | blog/asm/1.html | 538 | ||||
-rw-r--r-- | blog/c/cherry.html | 171 | ||||
-rw-r--r-- | blog/c/game.html | 346 | ||||
-rw-r--r-- | blog/lisp/episode1.html | 29 | ||||
-rw-r--r-- | blog/misc/merlin.html | 191 |
5 files changed, 0 insertions, 1275 deletions
diff --git a/blog/asm/1.html b/blog/asm/1.html deleted file mode 100644 index 4ed3574..0000000 --- a/blog/asm/1.html +++ /dev/null @@ -1,538 +0,0 @@ -<?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> -<!-- 2024-04-10 Wed 21:05 --> -<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> -<meta name="viewport" content="width=device-width, initial-scale=1" /> -<title>x86 Assembly from my understanding</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="../../../favicon.png"> -</head> -<body> -<div id="org-div-home-and-up"> - <a accesskey="h" href=""> UP </a> - | - <a accesskey="H" href="https://crystal.tilde.institute/"> HOME </a> -</div><div id="content" class="content"> -<h1 class="title">x86 Assembly from my understanding</h1> -<p> -Soooo this article (or maybe even a series of articles, who knows ?) will be about x86 assembly, or rather, what I understood from it and my road from the bottom-up hopefully reaching a good level of understanding -</p> -<div id="outline-container-orgd66d87f" class="outline-2"> -<h2 id="orgd66d87f">Memory :</h2> -<div class="outline-text-2" id="text-orgd66d87f"> -<p> -Memory is a sequence of octets (Aka 8bits) that each have a unique integer assigned to them called <b>The Effective Address (EA)</b>, in this particular CPU Architecture (the i8086), the octet is designated by a couple (A segment number, and the offset in the segment) -</p> - - -<ul class="org-ul"> -<li>The Segment is a set of 64 consecutive Koctets (1 Koctet = 1024 octets).</li> -<li>And the offset is to specify the particular octet in that segment.</li> -</ul> - -<p> -The offset and segment are encoded in 16bits, so they take a value between 0 and 65535 -</p> -</div> -<div id="outline-container-orgb9ec69c" class="outline-4"> -<h4 id="orgb9ec69c">Important :</h4> -<div class="outline-text-4" id="text-orgb9ec69c"> -<p> -The relation between the Effective Address and the Segment & Offset is as follow : -</p> - -<p> -<b><b>Effective address = 16 x segment + offset</b></b> keep in mind that this equation is encoded in decimal, which will change soon as we use Hexadecimal for convention reasons. -</p> -</div> -<ul class="org-ul"> -<li><a id="org407193f"></a>Example :<br /> -<div class="outline-text-5" id="text-org407193f"> -<p> -Let the Physical address (Or Effective Address, these two terms are interchangeable) <b>12345h</b> (the h refers to Hexadecimal, which can also be written like this <b>0x12345</b>), the register <b>DS = 1230h</b> and the register <b>SI = 0045h</b>, the CPU calculates the physical address by multiplying the content of the segment register <b>DS</b> by 10h (or 16) and adding the content of the register <b>SI</b>. so we get : <b>1230h x 10h + 45h = 12345h</b> -</p> - - -<p> -Now if you are a clever one ( I know you are, since you are reading this <3 ) you may say that the physical address <b>12345h</b> can be written in more than one way….and you are right, more precisely : <b>2<sup>12</sup> = 4096</b> different ways !!! -</p> -</div> -</li> -</ul> -</div> -<div id="outline-container-orgb81ab14" class="outline-3"> -<h3 id="orgb81ab14">Registers</h3> -<div class="outline-text-3" id="text-orgb81ab14"> -<p> -The 8086 CPU has 14 registers of 16bits of size. From the POV of the user, the 8086 has 3 groups of 4 registers of 16bits. One state register of 9bits and a counting program of 16bits inaccessible to the user (whatever this means). -</p> -</div> -<div id="outline-container-orgcd127f7" class="outline-4"> -<h4 id="orgcd127f7">General Registers</h4> -<div class="outline-text-4" id="text-orgcd127f7"> -<p> -General registers contribute to arithmetic’s and logic and addressing too. -</p> - - -<p> -Each half-register is accessible as a register of 8bits, therefor making the 8086 backwards compatible with the 8080 (which had 8bit registers) -</p> - - -<p> -Now here are the Registers we can find in this section: -</p> - - -<p> -<b>AX</b>: This is the accumulator. It is of 16 bits and is divided into two 8-bit registers AH and AL to also perform 8-bit instructions. It is generally used for arithmetical and logical instructions but in 8086 microprocessor it is not mandatory to have an accumulator as the destination operand. Example: -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">ADD</span> <span style="color: #cba6f7;">AX</span>, AX <span style="color: #6c7086;">;</span><span style="color: #6c7086;">(AX = AX + AX)</span> -</pre> -</div> - -<p> -<b>BX</b>: This is the base register. It is of 16 bits and is divided into two 8-bit registers BH and BL to also perform 8-bit instructions. It is used to store the value of the offset. Example: -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">BL</span>, [<span style="color: #fab387;">500</span>] <span style="color: #6c7086;">;</span><span style="color: #6c7086;">(BL = 500H)</span> -</pre> -</div> - -<p> -<b>CX</b>: This is the counter register. It is of 16 bits and is divided into two 8-bit registers CH and CL to also perform 8-bit instructions. It is used in looping and rotation. Example: -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">CX</span>, <span style="color: #fab387;">0005</span> -<span style="color: #89b4fa;">LOOP</span> -</pre> -</div> - -<p> -<b>DX</b>: This is the data register. It is of 16 bits and is divided into two 8-bit registers DH and DL to also perform 8-bit instructions. It is used in the multiplication and input/output port addressing. Example: -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">MUL</span> <span style="color: #cba6f7;">BX</span> (DX, AX = AX * BX) -</pre> -</div> -</div> -</div> -</div> -<div id="outline-container-orgde83b9e" class="outline-3"> -<h3 id="orgde83b9e">Addressing and registers…again</h3> -<div class="outline-text-3" id="text-orgde83b9e"> -</div> -<div id="outline-container-org598f23b" class="outline-4"> -<h4 id="org598f23b">I realized what I wrote here before was almost gibberish, sooo here we go again I guess ?</h4> -<div class="outline-text-4" id="text-org598f23b"> -<p> -Well lets take a step back to the notion of effective addresses VS relative ones. -</p> -</div> -</div> -<div id="outline-container-orga54d5c9" class="outline-4"> -<h4 id="orga54d5c9">Effective = 10h x Segment + Offset . Part1</h4> -<div class="outline-text-4" id="text-orga54d5c9"> -<p> -When trying to access a specific memory space, we use this annotation <b>[Segment:Offset]</b>, so for example, and assuming <b>DS = 0100h</b>. We want to write the value <b>0x0005</b> to the memory space defined by the physical address <b>1234h</b>, what do we do ? -</p> -</div> -<ul class="org-ul"> -<li><a id="org330429e"></a>Answer :<br /> -<div class="outline-text-5" id="text-org330429e"> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">MOV</span> [DS:0234h], 0x0005 -</pre> -</div> - -<p> -Why ? Let’s break it down : -</p> - - - -<div id="orge9d2dab" class="figure"> -<p><img src="../../src/gifs/lain-dance.gif" alt="lain-dance.gif" /> -</p> -</div> - - -<p> -We Already know that <b>Effective = 10h x Segment + Offset</b>, So here we have : <b>1234h = 10h x DS + Offset</b>, we already know that <b>DS = 0100h</b>, we end up with this simple equation <b>1234h = 1000h + Offset</b>, therefor the Offset is <b>0234h</b> -</p> - - -<p> -Simple, right ?, now for another example -</p> -</div> -</li> -</ul> -</div> -<div id="outline-container-org21257b6" class="outline-4"> -<h4 id="org21257b6">Another example :</h4> -<div class="outline-text-4" id="text-org21257b6"> -<p> -What if we now have this instruction ? -</p> -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">MOV</span> [0234h], 0x0005 -</pre> -</div> -<p> -What does it do ? You might or might not be surprised that it does the exact same thing as the other snipped of code, why though ? Because apparently and for some odd reason I don’t know, the compiler Implicitly assumes that the segment used is the <b>DS</b> one. So if you don’t specify a register( we will get to this later ), or a segment. Then the offset is considered an offset with a DS segment. -</p> -</div> -</div> -<div id="outline-container-org7c948b1" class="outline-4"> -<h4 id="org7c948b1">Segment + Register <3</h4> -<div class="outline-text-4" id="text-org7c948b1"> -<p> -Consider <b>DS = 0100h</b> and <b>BX = BP = 0234h</b> and this code snippet: -</p> -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">MOV</span> [BX], 0x0005 <span style="color: #6c7086;">; </span><span style="color: #a6e3a1; font-weight: bold;">NOTE</span><span style="color: #6c7086;"> : ITS NOT THE SAME AS MOV BX, 0x0005. Refer to earlier paragraphs</span> -</pre> -</div> - - -<p> -Well you guessed it right, it also does the same thing, but now consider this : -</p> -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">MOV</span> [BP], 0x0005 -</pre> -</div> - -<p> -If you answered that its the same one, you are wrong. And this is because the segment used changes according to the offset as I said before in an implicit way. Here is the explicit equivalent of the two commands above: -</p> -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">MOV</span> [DS:BX], 0x0005 - <span style="color: #cba6f7;">MOV</span> [SS:BP], 0x0005 -</pre> -</div> - -<p> -The General rule of thumb is as follows : -</p> -<ul class="org-ul"> -<li>If the offset is : DI SI or BX, the Segment used is DS.</li> -<li>If its BP or SP, then the segment is SS.</li> -</ul> -</div> -<ul class="org-ul"> -<li><a id="orgec605fb"></a>Note<br /> -<div class="outline-text-5" id="text-orgec605fb"> -<p> -The values of the registers CS DS and SS are automatically initialized by the OS when launching the program. So these segments are implicit. AKA : If we want to access a specific data in memory, we just need to specify its offset. Also you can’t write directly into the DS or CS segment registers, so something like -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">DS</span>, 0x0005 <span style="color: #6c7086;">; </span><span style="color: #6c7086;">Is INVALID</span> -<span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">DS</span>, AX <span style="color: #6c7086;">; </span><span style="color: #6c7086;">This one is VALID</span> -</pre> -</div> -</div> -</li> -</ul> -</div> -</div> -</div> -<div id="outline-container-org30acf72" class="outline-2"> -<h2 id="org30acf72">The ACTUAL thing :</h2> -<div class="outline-text-2" id="text-org30acf72"> -<p> -Enough technical rambling, and now we shall go to the fun part, the ACTUAL CODE. But first, some names you should be familiar with : -</p> - -<ul class="org-ul"> -<li><b>Mnemonics</b> : Or <b>Instructions</b>, are the…well…Instructions executed by the CPU like <b>MOV</b> , <b>ADD</b>, <b>MUL</b>…etc, they are case <b>insensitive</b> but i like them better in UPPERCASE.</li> -<li><b>Operands</b> : These are the options passed to the instructions, like <b>MOV dst, src</b>, and they can be anything from a memory location, to a variable to an immediate address.</li> -</ul> -</div> -<div id="outline-container-org03a7d0f" class="outline-3"> -<h3 id="org03a7d0f">Structure of an assembly program :</h3> -<div class="outline-text-3" id="text-org03a7d0f"> -<p> -While there is no “standard” structure, i prefer to go with this one : -</p> - -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">org</span> 100h -<span style="color: #cba6f7;">.data</span> - <span style="color: #6c7086;">; </span><span style="color: #6c7086;">variables and constants</span> - -<span style="color: #cba6f7;">.code</span> - <span style="color: #6c7086;">; </span><span style="color: #6c7086;">instructions</span> -</pre> -</div> -</div> -</div> -<div id="outline-container-orgbea80df" class="outline-3"> -<h3 id="orgbea80df">MOV dst, src</h3> -<div class="outline-text-3" id="text-orgbea80df"> -<p> -The MOV instruction copies the Second operand (src) to the First operand (dst)… The source can be a memory location, an immediate value, a general-purpose register (AX BX CX DX). As for the Destination, it can be a general-purpose register or a memory location. -</p> - - -<p> -these types of operands are supported: -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">REG</span>, memory -<span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">memory</span>, REG -<span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">REG</span>, REG -<span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">memory</span>, immediate -<span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">REG</span>, immediate -</pre> -</div> -<p> -<b>REG</b>: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP. -</p> - -<p> -<b>memory</b>: [BX], [BX+SI+7], variable -</p> - -<p> -<b>immediate</b>: 5, -24, 3Fh, 10001101b -</p> - - -<p> -for segment registers only these types of MOV are supported: -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">SREG</span>, memory -<span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">memory</span>, SREG -<span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">REG</span>, SREG -<span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">SREG</span>, REG -<span style="color: #89b4fa;">SREG</span>: <span style="color: #cba6f7;">DS</span>, ES, SS, and only as second operand: CS. -</pre> -</div> -<p> -<b>REG</b>: AX, BX, CX, DX, AH, AL, BL, BH, CH, CL, DH, DL, DI, SI, BP, SP. -</p> - -<p> -<b>memory</b>: [BX], [BX+SI+7], variable -</p> -</div> -<div id="outline-container-orge229cf5" class="outline-4"> -<h4 id="orge229cf5">Note : The MOV instruction <b>cannot</b> be used to set the value of the CS and IP registers</h4> -</div> -</div> -<div id="outline-container-org05f299b" class="outline-3"> -<h3 id="org05f299b">Variables :</h3> -<div class="outline-text-3" id="text-org05f299b"> -<p> -Let’s say you want to use a specific value multiple times in your code, do you prefer to call it using something like <b>var1</b> or <b>E4F9:0011</b> ? If your answer is the second option, you can gladly skip this section, or even better, seek therapy. -</p> - -<p> -Anyways, we have two types of variables, <b>bytes</b> and <b>words(which are two bytes)</b>, and to define a variable, we use the following syntax -</p> - -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">name</span> <span style="color: #cba6f7;">DB</span> value <span style="color: #6c7086;">; </span><span style="color: #6c7086;">To Define a Byte</span> -<span style="color: #89b4fa;">name</span> <span style="color: #cba6f7;">DW</span> value <span style="color: #6c7086;">; </span><span style="color: #6c7086;">To Define a Word</span> -</pre> -</div> - -<p> -<b>name</b> - can be any letter or digit combination, though it should start with a letter. It’s possible to declare unnamed variables by not specifying the name (this variable will have an address but no name). -<b>value</b> - can be any numeric value in any supported numbering system (hexadecimal, binary, or decimal), or “?” symbol for variables that are not initialized. -</p> -</div> -<div id="outline-container-orga473d7b" class="outline-4"> -<h4 id="orga473d7b">Example code :</h4> -<div class="outline-text-4" id="text-orga473d7b"> -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">org</span> 100h - <span style="color: #cba6f7;">.data</span> - <span style="color: #cba6f7;">x</span> db <span style="color: #fab387;">33</span> - <span style="color: #cba6f7;">y</span> dw 1350h - - <span style="color: #cba6f7;">.code</span> - <span style="color: #cba6f7;">MOV</span> AL, x - <span style="color: #cba6f7;">MOV</span> BX, y -</pre> -</div> -</div> -</div> -<div id="outline-container-org8ceedbb" class="outline-4"> -<h4 id="org8ceedbb">Arrays :</h4> -<div class="outline-text-4" id="text-org8ceedbb"> -<p> -We can also define Arrays instead of single values using comma separated vaues. like this for example -</p> -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">a</span> db 48h, 65h, 6Ch, 6Fh, 00H - <span style="color: #cba6f7;">b</span> db 'Hello', <span style="color: #fab387;">0</span> -</pre> -</div> - -<p> -Surprise Surprise, the arrays a and b are identical, the reason behind it is that characters are first converted to their ASCII values then stored in memory!!! Wonderful right ? And guess what, accessing values in assembly IS THE SAME AS IN C !!! -</p> -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">MOV</span> AL, a[<span style="color: #fab387;">0</span>] <span style="color: #6c7086;">; </span><span style="color: #6c7086;">Copies 48h to AL</span> - <span style="color: #cba6f7;">MOV</span> BL, b[<span style="color: #fab387;">0</span>] <span style="color: #6c7086;">; </span><span style="color: #6c7086;">Also Copies 48h to BL</span> -</pre> -</div> -<p> -You can also use any of the memory index registers BX, SI, DI, BP, for example: -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">SI</span>, <span style="color: #fab387;">3</span> -<span style="color: #89b4fa;">MOV</span> <span style="color: #cba6f7;">AL</span>, a[SI] -</pre> -</div> - -<p> -If you need to declare a large array you can use DUP operator. -The syntax for <b>DUP</b>: -</p> - -<p> -number DUP ( value(s) ) -<b>number</b> - number of duplicate to make (any constant value). -<b>value</b> - expression that DUP will duplicate. -</p> - -<p> -for example: -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">c</span> <span style="color: #cba6f7;">DB</span> <span style="color: #fab387;">5</span> DUP(<span style="color: #fab387;">9</span>) -<span style="color: #6c7086;">;</span><span style="color: #6c7086;">is an alternative way of declaring:</span> -<span style="color: #89b4fa;">c</span> <span style="color: #cba6f7;">DB</span> <span style="color: #fab387;">9</span>, <span style="color: #fab387;">9</span>, <span style="color: #fab387;">9</span>, <span style="color: #fab387;">9</span>, <span style="color: #fab387;">9</span> -</pre> -</div> -<p> -one more example: -</p> -<div class="org-src-container"> -<pre class="src src-asm"><span style="color: #89b4fa;">d</span> <span style="color: #cba6f7;">DB</span> <span style="color: #fab387;">5</span> DUP(<span style="color: #fab387;">1</span>, <span style="color: #fab387;">2</span>) -<span style="color: #6c7086;">;</span><span style="color: #6c7086;">is an alternative way of declaring:</span> -<span style="color: #89b4fa;">d</span> <span style="color: #cba6f7;">DB</span> <span style="color: #fab387;">1</span>, <span style="color: #fab387;">2</span>, <span style="color: #fab387;">1</span>, <span style="color: #fab387;">2</span>, <span style="color: #fab387;">1</span>, <span style="color: #fab387;">2</span>, <span style="color: #fab387;">1</span>, <span style="color: #fab387;">2</span>, <span style="color: #fab387;">1</span>, <span style="color: #fab387;">2</span> -</pre> -</div> -<p> -Of course, you can use DW instead of DB if it’s required to keep values larger then 255, or smaller then -128. DW cannot be used to declare strings. -</p> -</div> -</div> -<div id="outline-container-org8fefb4b" class="outline-4"> -<h4 id="org8fefb4b">LEA</h4> -<div class="outline-text-4" id="text-org8fefb4b"> -<p> -LEA stands for (Load Effective Address) is an instruction used to get the offset of a specific variable. We will see later how its used, but first. here is something we will need : -</p> - -<p> -In order to tell the compiler about data type, -these prefixes should be used: -</p> - -<p> -<b>BYTE PTR</b> - for byte. -<b>WORD PTR</b> - for word (two bytes). -</p> - -<p> -For example: -<b>BYTE PTR [BX]</b> ; byte access. - or -<b>WORD PTR [BX]</b> ; word access. -assembler supports shorter prefixes as well: -</p> - -<ul class="org-ul"> -<li>b. - for BYTE PTR</li> -<li>w. - for WORD PTR</li> -</ul> - -<p> -in certain cases the assembler can calculate the data type automatically. -</p> -</div> -<ul class="org-ul"> -<li><a id="orgd644e48"></a>Example :<br /> -<div class="outline-text-5" id="text-orgd644e48"> -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">org</span> 100h - <span style="color: #cba6f7;">.data</span> - <span style="color: #cba6f7;">VAR1</span> db 50h - <span style="color: #cba6f7;">VAR2</span> dw 1234h - <span style="color: #cba6f7;">.code</span> - <span style="color: #cba6f7;">MOV</span> AL, VAR1 <span style="color: #6c7086;">; </span><span style="color: #6c7086;">We check the value of VAR1 by putting it in AL</span> - <span style="color: #cba6f7;">MOV</span> AX, VAR2 <span style="color: #6c7086;">; </span><span style="color: #6c7086;">Same here</span> - <span style="color: #cba6f7;">LEA</span> BX, VAR1 <span style="color: #6c7086;">; </span><span style="color: #6c7086;">BX receives the Address of VAR1</span> - <span style="color: #cba6f7;">MOV</span> b.[BX], 44h - <span style="color: #cba6f7;">MOV</span> AL, VAR1 <span style="color: #6c7086;">; </span><span style="color: #6c7086;">We effectively changed the content of the VAR1 variable</span> - <span style="color: #cba6f7;">LEA</span> BX, VAR2 - <span style="color: #cba6f7;">MOV</span> w.[BX], 5678h - <span style="color: #cba6f7;">MOV</span> AX, VAR2 -</pre> -</div> -</div> -</li> -</ul> -</div> -<div id="outline-container-org99559c2" class="outline-4"> -<h4 id="org99559c2">Constants :</h4> -<div class="outline-text-4" id="text-org99559c2"> -<p> -Constants in Assembly only exist until the code is assembled, meaning that if you disassemble your code later, you wont see your constant definitions. -</p> - -<p> -Defining constants is pretty straight forward : -</p> -<div class="org-src-container"> -<pre class="src src-asm"> <span style="color: #cba6f7;">name</span> EQU value -</pre> -</div> - -<p> -Of course constants cant be changed, and aren’t stored in memory. So they are like little macros that live in your code. -</p> -</div> -</div> -</div> -<div id="outline-container-org9179f72" class="outline-3"> -<h3 id="org9179f72">⚐ :</h3> -<div class="outline-text-3" id="text-org9179f72"> -<p> -Now comes the notion of <b>Flags</b>, which are bits in the <b>Status register</b>, which are used for logical and arithmetical instructions and can take a value of 1 or 0 . Here are the 8 flags that exist for the 8086 CPU : -</p> -<ul class="org-ul"> -<li><b>Carry Flag(CF):</b> Set to 1 when there is an <b>unsigned overflow</b>, for example when you add 255 + 1( not in range [0,255] ). by default its set to 0.</li> -<li><b>Overflow Flag(CF):</b> Set to 1 when there is a <b>signed overflow</b>, for example when you add 100 + 50( not in range [-128, 128[ ). by default its set to 0.</li> -<li><b>Zero Flag(ZF):</b> Set to 1 when the result is 0. by default its set to 0.</li> -<li><b>Auxiliary Flag(AF):</b> Set to 1 when there is an <b>unsigned overflow</b> for low nibble (4bits), or in human words : when there is a carry inside the number. for example when you add 29H + 4CH , 9 + C => 15. So we carry the 1 to 2 + 4 and AF is set to 1.</li> -<li><b>Parity Flag(PF):</b> Set to 1 when the result has an even number of one bits. and 0 if it has an odd number of one bits. Even if a result is a word, only the Low 8bits are analyzed.</li> -<li><b>Sign Flag(SF):</b> Self explanatory, set to 1 if the result is negative and 0 if its positive.</li> -<li><b>Interrupt Enable Flag(IF):</b> When its set to 1, the CPU reacts to interrupts from external devices.</li> -<li><b>Direction Flag(DF):</b> When this flag is set to 0, the processing is done forward, if its set to 1, its done backward.</li> -</ul> -</div> -</div> -</div> -</div> -<div id="postamble" class="status"> -<p class="author">Author: Crystal</p> -<p class="date">Created: 2024-04-10 Wed 21:05</p> -</div> -</body> -</html> diff --git a/blog/c/cherry.html b/blog/c/cherry.html deleted file mode 100644 index e2d9221..0000000 --- a/blog/c/cherry.html +++ /dev/null @@ -1,171 +0,0 @@ -<?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> -<!-- 2024-03-17 Sun 21:46 --> -<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> -<meta name="viewport" content="width=device-width, initial-scale=1" /> -<title>Reviving Caesar with a Cherry-flavored Crystal</title> -<meta name="author" content="Crystal & Sloth" /> -<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="../../../favicon.png"> -</head> -<body> -<div id="org-div-home-and-up"> - <a accesskey="h" href=""> UP </a> - | - <a accesskey="H" href="https://crystal.tilde.institute/"> HOME </a> -</div><div id="content" class="content"> -<h1 class="title">Reviving Caesar with a Cherry-flavored Crystal</h1> -<div id="outline-container-org0bff230" class="outline-2"> -<h2 id="org0bff230">What ?…</h2> -<div class="outline-text-2" id="text-org0bff230"> -<p> -That is probably your reaction reading this title, and no, this isn’t a randomly generated sentence, but rather a simple encryption algorithm I recently made (Actually the first encryption algorithm i make at all!!). Meet <b>Cherry-Crystal Encryption</b>. -</p> -</div> -</div> -<div id="outline-container-org4770304" class="outline-2"> -<h2 id="org4770304">Okay so, what is this all about ?</h2> -<div class="outline-text-2" id="text-org4770304"> -<p> -This encryption Algorithm that we will call <b>CCE</b> for short, takes inspiration from the Caesar cipher which needn’t an introduction (you can find great explanations online). But what about mine you might ask ? -</p> - - -<ul class="org-ul"> -<li>It’s actually pretty simple. We start with a <b>Cherry</b> or a <b>Visible phrase</b>, or a <b>Decoy</b>, that we will share to people who we don’t want to know the secret phrase..</li> -<li>Then we ask the user to enter their <b>Crystal</b>, <b>invisible phrase</b> or <b>secret</b>.</li> -<li>The program then outputs an array of Integers called the <b>Mask</b>, or the <b>Shift</b>. That is, the shift required to go from cherry<sub>i</sub> to crystal<sub>i</sub>.</li> -<li>Finally, we use both the <b>Cherry</b> and <b>Mask</b> to get the <b>Crystal</b>, a single missing number or letter from both of them can and will output rubbish content.</li> -</ul> -</div> -</div> -<div id="outline-container-org4496ca5" class="outline-2"> -<h2 id="org4496ca5">The Code :</h2> -<div class="outline-text-2" id="text-org4496ca5"> -<div class="org-src-container"> -<pre class="src src-c"><span style="color: #f9e2af;">#include</span> <span style="color: #f38ba8;"><</span><span style="color: #a6e3a1;">stdio.h</span><span style="color: #f38ba8;">></span> -<span style="color: #f9e2af;">#include</span> <span style="color: #f38ba8;"><</span><span style="color: #a6e3a1;">stdlib.h</span><span style="color: #f38ba8;">></span> -<span style="color: #f9e2af;">#include</span> <span style="color: #f38ba8;"><</span><span style="color: #a6e3a1;">string.h</span><span style="color: #f38ba8;">></span> - -<span style="color: #f9e2af;">void</span> <span style="color: #89b4fa;">sloth</span><span style="color: #f38ba8;">(</span><span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">cherry</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">crystal</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">mask</span><span style="color: #fab387;">[]</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> - <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">i</span>; - <span style="color: #cba6f7;">for</span> <span style="color: #fab387;">(</span>i = <span style="color: #fab387;">0</span>; i < strlen<span style="color: #f9e2af;">(</span>cherry<span style="color: #f9e2af;">)</span> - <span style="color: #fab387;">1</span>; i++<span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> - mask<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span> = cherry<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span> - crystal<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span>; - <span style="color: #fab387;">}</span> - <span style="color: #cba6f7;">for</span> <span style="color: #fab387;">(</span>i = strlen<span style="color: #f9e2af;">(</span>cherry<span style="color: #f9e2af;">)</span> - <span style="color: #fab387;">1</span>; i < strlen<span style="color: #f9e2af;">(</span>crystal<span style="color: #f9e2af;">)</span> - <span style="color: #fab387;">1</span>; i++<span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> - mask<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span> = crystal<span style="color: #f9e2af;">[</span>i<span style="color: #f9e2af;">]</span>; - <span style="color: #fab387;">}</span> -<span style="color: #f38ba8;">}</span> -<span style="color: #f9e2af;">void</span> <span style="color: #89b4fa;">moon</span><span style="color: #f38ba8;">(</span><span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">cherry</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">crystal</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">mask</span><span style="color: #fab387;">[]</span>, <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">length</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> - <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">i</span>, <span style="color: #cdd6f4;">end</span> = <span style="color: #fab387;">1</span>; - <span style="color: #cba6f7;">for</span> <span style="color: #fab387;">(</span>i = <span style="color: #fab387;">0</span>; i < length; i++<span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> - <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</span>i == strlen<span style="color: #a6e3a1;">(</span>cherry<span style="color: #a6e3a1;">)</span> - <span style="color: #fab387;">1</span> || end == <span style="color: #fab387;">0</span><span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> - crystal<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">]</span> = mask<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">]</span>; - end = <span style="color: #fab387;">0</span>; - <span style="color: #f9e2af;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #f9e2af;">{</span> - crystal<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">]</span> = cherry<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">]</span> - mask<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">]</span>; - <span style="color: #f9e2af;">}</span> - <span style="color: #fab387;">}</span> -<span style="color: #f38ba8;">}</span> -<span style="color: #f9e2af;">int</span> <span style="color: #89b4fa;">main</span><span style="color: #f38ba8;">(</span><span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">argc</span>, <span style="color: #f9e2af;">char</span> *<span style="color: #cdd6f4;">argv</span><span style="color: #fab387;">[]</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> - <span style="color: #cba6f7;">const</span> <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">size</span> = <span style="color: #fab387;">1028</span>; - <span style="color: #f9e2af;">char</span> <span style="color: #cdd6f4;">cherry</span><span style="color: #fab387;">[</span>size<span style="color: #fab387;">]</span>, <span style="color: #cdd6f4;">cherry2</span><span style="color: #fab387;">[</span>size<span style="color: #fab387;">]</span>, <span style="color: #cdd6f4;">crystal</span><span style="color: #fab387;">[</span>size<span style="color: #fab387;">]</span>, <span style="color: #cdd6f4;">crystal2</span><span style="color: #fab387;">[</span>size<span style="color: #fab387;">]</span>; - <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">mask</span><span style="color: #fab387;">[</span>size<span style="color: #fab387;">]</span>, <span style="color: #cdd6f4;">mask2</span><span style="color: #fab387;">[</span>size<span style="color: #fab387;">]</span>, <span style="color: #cdd6f4;">i</span>; - <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">length</span> = <span style="color: #fab387;">0</span>; - puts<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Enter the Cherry: "</span><span style="color: #fab387;">)</span>; - fgets<span style="color: #fab387;">(</span>cherry, size, stdin<span style="color: #fab387;">)</span>; - puts<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Enter the Crystal: "</span><span style="color: #fab387;">)</span>; - fgets<span style="color: #fab387;">(</span>crystal, size, stdin<span style="color: #fab387;">)</span>; - sloth<span style="color: #fab387;">(</span>cherry, crystal, mask<span style="color: #fab387;">)</span>; - <span style="color: #cba6f7;">for</span> <span style="color: #fab387;">(</span>i = <span style="color: #fab387;">0</span>; i < strlen<span style="color: #f9e2af;">(</span>crystal<span style="color: #f9e2af;">)</span> - <span style="color: #fab387;">1</span>; i++<span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> - printf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">"%d "</span>, mask<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span>; - length++; - <span style="color: #fab387;">}</span> - printf<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"\nYour mask is : %d characters long"</span>, length<span style="color: #fab387;">)</span>; - puts<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"\n===Decryption: ===\n"</span><span style="color: #fab387;">)</span>; - puts<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Enter the Cherry: "</span><span style="color: #fab387;">)</span>; - fgets<span style="color: #fab387;">(</span>cherry2, size, stdin<span style="color: #fab387;">)</span>; - puts<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Enter the size of the Mask: "</span><span style="color: #fab387;">)</span>; - scanf<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"%d"</span>, &length<span style="color: #fab387;">)</span>; - puts<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Enter the mask: "</span><span style="color: #fab387;">)</span>; - <span style="color: #cba6f7;">for</span> <span style="color: #fab387;">(</span>i = <span style="color: #fab387;">0</span>; i < length; i++<span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> - scanf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">"%d"</span>, &mask2<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span>; - <span style="color: #fab387;">}</span> - puts<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"The Crystal is: "</span><span style="color: #fab387;">)</span>; - moon<span style="color: #fab387;">(</span>cherry2, crystal2, mask2, length<span style="color: #fab387;">)</span>; - puts<span style="color: #fab387;">(</span>crystal2<span style="color: #fab387;">)</span>; - <span style="color: #cba6f7;">return</span> <span style="color: #fab387;">0</span>; -<span style="color: #f38ba8;">}</span> - -</pre> -</div> - -<p> -The program has been tested both on Alpine OS with Musl libc (thanks <a href="https://kaa.neocities.org/">Kin</a>) and on OpenBSD 7.5-current. In the close future I will make a git repo as i’m planning to upgrade it and just make it better overall, who knows, maybe i will make a library out of it!! -</p> -</div> -</div> -<div id="outline-container-org6c3305a" class="outline-2"> -<h2 id="org6c3305a">How does it work ?</h2> -<div class="outline-text-2" id="text-org6c3305a"> -</div> -<div id="outline-container-orgf462a7c" class="outline-3"> -<h3 id="orgf462a7c">Slothing (Encrypting) 🦥:</h3> -<div class="outline-text-3" id="text-orgf462a7c"> -<p> -<del>What is it with these names I pick ?</del> Anyways, the <b>sloth(char *cherry, char *crystal, int *mask)</b> void function takes as parameters three variables: -</p> - -<ul class="org-ul"> -<li>A pointer to a <b>char array</b> or simply said <b>a string</b>, It’s the <b>Cherry</b>.</li> -<li>Another pointer to the <b>Crystal</b>.</li> -<li>And Finally, a pointer to an array of integers <b>The Mask</b> which will be output-ed by the function.</li> -</ul> - - -<p> -The general idea of it is like this : (we will use a quick example) -</p> - -<ul class="org-ul"> -<li><b>Cherry</b>: H e l l o \0.</li> -<li><b>Crystal</b>: W o r l d \0.</li> -<li>Cherry[0] here is <b>H</b>, or in ASCII <b>72</b>. And Crystal[0] is <b>W</b> or <b>87</b>.</li> -<li>Mask[0] in this case is : Cherry[0] - Crystal[0]. which is <b>-15</b>. We then repeat the same steps for each letter on the <b>Crystal</b>.</li> -</ul> - -<p> -Why the emphasis on <b>Crystal</b> ? Because we might end up with a case of a Crystal larger than a Cherry. we set the offset to the ASCII value of <b>Crystal[i]</b>, okay which to be fair is not the safest option out there, but I’m planning on fixing it sooner or later. In the case of a large Cherry but a small Crystal…it works but now looking at the code, i have no idea why it works the intended way…. -</p> -</div> -</div> -<div id="outline-container-org7cb73ea" class="outline-3"> -<h3 id="org7cb73ea">Mooning (Decrypting) 🌕:</h3> -<div class="outline-text-3" id="text-org7cb73ea"> -<p> -The function <b>moon(char *cherry, char *crystal, int *mask, int length)</b> works the same way as the sloth function, but in reverse and a small change. -</p> - -<ul class="org-ul"> -<li><b>The for loop goes through all the elements of the Mask and reconstructing the Crystal using the reverse equation of the encryption</b>. But when it arrives at the end of the <b>Cherry</b> (here we enter the case of a Cherry smaller than a Crystal). Then we will just assume that <b>Mask[i]</b> is the ASCII code of <b>Crystal[i]</b>, and we continue this assumption until the end of the loop.</li> -</ul> - - -<p> -And voila that’s it. Of course there might be some things I will change, but the overall concept is here! -</p> -</div> -</div> -</div> -</div> -<div id="postamble" class="status"> -<p class="author">Author: Crystal & Sloth</p> -<p class="date">Created: 2024-03-17 Sun 21:46</p> -</div> -</body> -</html> diff --git a/blog/c/game.html b/blog/c/game.html deleted file mode 100644 index 90c1965..0000000 --- a/blog/c/game.html +++ /dev/null @@ -1,346 +0,0 @@ -<?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> -<!-- 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> -<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="../../../favicon.png"> -</head> -<body> -<div id="org-div-home-and-up"> - <a accesskey="h" href=""> UP </a> - | - <a accesskey="H" href="https://crystal.tilde.institute/"> HOME </a> -</div><div id="content" class="content"> -<h1 class="title">The loneliness Game</h1> -<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-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-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> -<span class="linenr"> 3: </span><span style="color: #f9e2af;">int</span> <span style="color: #89b4fa;">main</span><span style="color: #f38ba8;">(</span><span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">argc</span>, <span style="color: #f9e2af;">char</span> *<span style="color: #cdd6f4;">argv</span><span style="color: #fab387;">[]</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> -<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: #fab387;">[</span><span style="color: #fab387;">5</span><span style="color: #fab387;">][</span><span style="color: #fab387;">5</span><span style="color: #fab387;">]</span> = <span style="color: #fab387;">{</span> -<span class="linenr"> 5: </span> <span style="color: #f9e2af;">{</span><span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span><span style="color: #f9e2af;">}</span>, -<span class="linenr"> 6: </span> <span style="color: #f9e2af;">{</span><span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span><span style="color: #f9e2af;">}</span>, -<span class="linenr"> 7: </span> <span style="color: #f9e2af;">{</span><span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span><span style="color: #f9e2af;">}</span>, -<span class="linenr"> 8: </span> <span style="color: #f9e2af;">{</span><span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span><span style="color: #f9e2af;">}</span>, -<span class="linenr"> 9: </span> <span style="color: #f9e2af;">{</span><span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span>, <span style="color: #a6e3a1;">'-'</span><span style="color: #f9e2af;">}</span> -<span class="linenr"> 10: </span> <span style="color: #fab387;">}</span>; -<span class="linenr"> 11: </span> <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">stop</span>=<span style="color: #fab387;">0</span>,<span style="color: #cdd6f4;">i</span>=<span style="color: #fab387;">0</span>,<span style="color: #cdd6f4;">moves</span>=<span style="color: #fab387;">0</span>,<span style="color: #cdd6f4;">score</span>=<span style="color: #fab387;">0</span>,<span style="color: #cdd6f4;">pos</span><span style="color: #fab387;">[</span><span style="color: #fab387;">2</span><span style="color: #fab387;">]</span> = <span style="color: #fab387;">{</span><span style="color: #fab387;">2</span>, <span style="color: #fab387;">2</span><span style="color: #fab387;">}</span>; -<span class="linenr"> 12: </span> <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">bonus</span><span style="color: #fab387;">[</span><span style="color: #fab387;">2</span><span style="color: #fab387;">]</span>; -<span class="linenr"> 13: </span> <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">trap</span><span style="color: #fab387;">[</span><span style="color: #fab387;">2</span><span style="color: #fab387;">]</span> ; -<span class="linenr"> 14: </span> <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">death</span><span style="color: #fab387;">[</span><span style="color: #fab387;">2</span><span style="color: #fab387;">]</span>; -<span class="linenr"> 15: </span> map<span style="color: #fab387;">[</span>pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span><span style="color: #fab387;">][</span>pos<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"> 16: </span> <span style="color: #cba6f7;">do</span><span style="color: #fab387;">{</span> -<span class="linenr"> 17: </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><span style="color: #fab387;">5</span><span style="color: #f9e2af;">)</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><span style="color: #fab387;">5</span><span style="color: #f9e2af;">)</span>; -<span class="linenr"> 18: </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><span style="color: #fab387;">5</span><span style="color: #f9e2af;">)</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><span style="color: #fab387;">5</span><span style="color: #f9e2af;">)</span>; -<span class="linenr"> 19: </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><span style="color: #fab387;">5</span><span style="color: #f9e2af;">)</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><span style="color: #fab387;">5</span><span style="color: #f9e2af;">)</span>; -<span class="linenr"> 20: </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>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>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: #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: #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"> 21: </span> map<span style="color: #fab387;">[</span>bonus<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span><span style="color: #fab387;">][</span>bonus<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;">'B'</span>; -<span class="linenr"> 22: </span> map<span style="color: #fab387;">[</span>trap<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span><span style="color: #fab387;">][</span>trap<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;">'T'</span>; -<span class="linenr"> 23: </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;">'D'</span>; -<span class="linenr"> 24: </span> <span style="color: #cba6f7;">do</span><span style="color: #fab387;">{</span> -<span class="linenr"> 25: </span> printf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">"Map:\n"</span><span style="color: #f9e2af;">)</span>; -<span class="linenr"> 26: </span> <span style="color: #cba6f7;">for</span> <span style="color: #f9e2af;">(</span><span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">i</span> = <span style="color: #fab387;">0</span>; i < <span style="color: #fab387;">5</span>; i++<span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> -<span class="linenr"> 27: </span> <span style="color: #cba6f7;">for</span> <span style="color: #a6e3a1;">(</span><span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">j</span> = <span style="color: #fab387;">0</span>; j < <span style="color: #fab387;">5</span>; j++<span style="color: #a6e3a1;">)</span> <span style="color: #a6e3a1;">{</span> -<span class="linenr"> 28: </span> printf<span style="color: #f38ba8;">(</span><span style="color: #a6e3a1;">"%c "</span>, map<span style="color: #fab387;">[</span>i<span style="color: #fab387;">][</span>j<span style="color: #fab387;">]</span><span style="color: #f38ba8;">)</span>; -<span class="linenr"> 29: </span> <span style="color: #a6e3a1;">}</span> -<span class="linenr"> 30: </span> printf<span style="color: #a6e3a1;">(</span><span style="color: #a6e3a1;">"\n"</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr"> 31: </span> <span style="color: #f9e2af;">}</span> -<span class="linenr"> 32: </span> printf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">"Score: %d\n"</span>, score<span style="color: #f9e2af;">)</span>; -<span class="linenr"> 33: </span> printf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">"Moves: %d\n"</span>, moves<span style="color: #f9e2af;">)</span>; -<span class="linenr"> 34: </span> printf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">"Enter a direction (w,a,s,d) or c to quit: "</span><span style="color: #f9e2af;">)</span>; -<span class="linenr"> 35: </span> scanf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">" %c"</span>, &input<span style="color: #f9e2af;">)</span>; -<span class="linenr"> 36: </span><span style="color: #6c7086;">// </span><span style="color: #6c7086;">pos[0] updown pos[1] lr</span> -<span class="linenr"> 37: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</span>input == <span style="color: #a6e3a1;">'w'</span><span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> -<span class="linenr"> 38: </span> printf<span style="color: #a6e3a1;">(</span><span style="color: #a6e3a1;">"Moving up\n"</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr"> 39: </span> map<span style="color: #a6e3a1;">[</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">][</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">]</span> = <span style="color: #a6e3a1;">'-'</span>; -<span class="linenr"> 40: </span> <span style="color: #cba6f7;">if</span> <span style="color: #a6e3a1;">(</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span> == <span style="color: #fab387;">0</span><span style="color: #a6e3a1;">)</span> <span style="color: #a6e3a1;">{</span> -<span class="linenr"> 41: </span> pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span> = <span style="color: #fab387;">4</span>; -<span class="linenr"> 42: </span> <span style="color: #a6e3a1;">}</span> -<span class="linenr"> 43: </span> <span style="color: #cba6f7;">else</span> <span style="color: #a6e3a1;">{</span> -<span class="linenr"> 44: </span> pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span>--; -<span class="linenr"> 45: </span> <span style="color: #a6e3a1;">}</span> -<span class="linenr"> 46: </span> <span style="color: #f9e2af;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</span>input == <span style="color: #a6e3a1;">'a'</span><span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> -<span class="linenr"> 47: </span> printf<span style="color: #a6e3a1;">(</span><span style="color: #a6e3a1;">"Moving left\n"</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr"> 48: </span> map<span style="color: #a6e3a1;">[</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">][</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">]</span> = <span style="color: #a6e3a1;">'-'</span>; -<span class="linenr"> 49: </span> <span style="color: #cba6f7;">if</span> <span style="color: #a6e3a1;">(</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span> == <span style="color: #fab387;">0</span><span style="color: #a6e3a1;">)</span> <span style="color: #a6e3a1;">{</span> -<span class="linenr"> 50: </span> pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span> = <span style="color: #fab387;">4</span>; -<span class="linenr"> 51: </span> <span style="color: #a6e3a1;">}</span> -<span class="linenr"> 52: </span> <span style="color: #cba6f7;">else</span> <span style="color: #a6e3a1;">{</span> -<span class="linenr"> 53: </span> pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span>--; -<span class="linenr"> 54: </span> <span style="color: #a6e3a1;">}</span> -<span class="linenr"> 55: </span> <span style="color: #f9e2af;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</span>input == <span style="color: #a6e3a1;">'s'</span><span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> -<span class="linenr"> 56: </span> -<span class="linenr"> 57: </span> printf<span style="color: #a6e3a1;">(</span><span style="color: #a6e3a1;">"Moving down\n"</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr"> 58: </span> map<span style="color: #a6e3a1;">[</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">][</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">]</span> = <span style="color: #a6e3a1;">'-'</span>; -<span class="linenr"> 59: </span> <span style="color: #cba6f7;">if</span> <span style="color: #a6e3a1;">(</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span> == <span style="color: #fab387;">4</span><span style="color: #a6e3a1;">)</span> <span style="color: #a6e3a1;">{</span> -<span class="linenr"> 60: </span> pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span> = <span style="color: #fab387;">0</span>; -<span class="linenr"> 61: </span> <span style="color: #a6e3a1;">}</span> -<span class="linenr"> 62: </span> <span style="color: #cba6f7;">else</span> <span style="color: #a6e3a1;">{</span> -<span class="linenr"> 63: </span> pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span>++; -<span class="linenr"> 64: </span> <span style="color: #a6e3a1;">}</span> -<span class="linenr"> 65: </span> <span style="color: #f9e2af;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</span>input == <span style="color: #a6e3a1;">'d'</span><span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> -<span class="linenr"> 66: </span> printf<span style="color: #a6e3a1;">(</span><span style="color: #a6e3a1;">"Moving right\n"</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr"> 67: </span> map<span style="color: #a6e3a1;">[</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">][</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">]</span> = <span style="color: #a6e3a1;">'-'</span>; -<span class="linenr"> 68: </span> <span style="color: #cba6f7;">if</span> <span style="color: #a6e3a1;">(</span>pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span> == <span style="color: #fab387;">4</span><span style="color: #a6e3a1;">)</span> <span style="color: #a6e3a1;">{</span> -<span class="linenr"> 69: </span> pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span> = <span style="color: #fab387;">0</span>; -<span class="linenr"> 70: </span> <span style="color: #a6e3a1;">}</span> -<span class="linenr"> 71: </span> <span style="color: #cba6f7;">else</span> <span style="color: #a6e3a1;">{</span> -<span class="linenr"> 72: </span> pos<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span>++; -<span class="linenr"> 73: </span> <span style="color: #a6e3a1;">}</span> -<span class="linenr"> 74: </span> <span style="color: #f9e2af;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</span>input == <span style="color: #a6e3a1;">'c'</span><span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> -<span class="linenr"> 75: </span> printf<span style="color: #a6e3a1;">(</span><span style="color: #a6e3a1;">"Quitting\n"</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr"> 76: </span> <span style="color: #f9e2af;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #f9e2af;">{</span> -<span class="linenr"> 77: </span> printf<span style="color: #a6e3a1;">(</span><span style="color: #a6e3a1;">"Invalid input\n"</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr"> 78: </span> <span style="color: #f9e2af;">}</span> -<span class="linenr"> 79: </span> map<span style="color: #f9e2af;">[</span>pos<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">][</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: #a6e3a1;">'+'</span>; -<span class="linenr"> 80: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</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;">0</span><span style="color: #a6e3a1;">]</span> && pos<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> -<span class="linenr"> 81: </span> score++; -<span class="linenr"> 82: </span> <span style="color: #cba6f7;">do</span><span style="color: #a6e3a1;">{</span> -<span class="linenr"> 83: </span> bonus<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr"> 84: </span> bonus<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr"> 85: </span> <span style="color: #a6e3a1;">}</span><span style="color: #cba6f7;">while</span><span style="color: #a6e3a1;">(</span><span style="color: #f38ba8;">(</span>bonus<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> && bonus<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>bonus<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> && bonus<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>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;">0</span><span style="color: #fab387;">]</span> && bonus<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span> == pos<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span><span style="color: #f38ba8;">)</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr"> 86: </span> <span style="color: #f9e2af;">}</span> -<span class="linenr"> 87: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</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;">0</span><span style="color: #a6e3a1;">]</span> && pos<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> -<span class="linenr"> 88: </span> score--; -<span class="linenr"> 89: </span> <span style="color: #cba6f7;">do</span><span style="color: #a6e3a1;">{</span> -<span class="linenr"> 90: </span> trap<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr"> 91: </span> trap<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr"> 92: </span> <span style="color: #a6e3a1;">}</span><span style="color: #cba6f7;">while</span><span style="color: #a6e3a1;">(</span><span style="color: #f38ba8;">(</span>trap<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> && trap<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>trap<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> && trap<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>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;">0</span><span style="color: #fab387;">]</span> && trap<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span> == pos<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span><span style="color: #f38ba8;">)</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr"> 93: </span> <span style="color: #f9e2af;">}</span> -<span class="linenr"> 94: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</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;">0</span><span style="color: #a6e3a1;">]</span> && pos<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> -<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: #a6e3a1;">{</span> -<span class="linenr"> 97: </span> death<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr"> 98: </span> death<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr"> 99: </span> <span style="color: #a6e3a1;">}</span><span style="color: #cba6f7;">while</span><span style="color: #a6e3a1;">(</span><span style="color: #f38ba8;">(</span>death<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> && death<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>death<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> && death<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>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;">0</span><span style="color: #fab387;">]</span> && death<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span> == pos<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span><span style="color: #f38ba8;">)</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr">100: </span> <span style="color: #f9e2af;">}</span> -<span class="linenr">101: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</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: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> -<span class="linenr">102: </span> map<span style="color: #a6e3a1;">[</span>death<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">][</span>death<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span><span style="color: #a6e3a1;">]</span> = <span style="color: #a6e3a1;">'-'</span>; -<span class="linenr">103: </span> <span style="color: #cba6f7;">do</span><span style="color: #a6e3a1;">{</span> -<span class="linenr">104: </span> death<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr">105: </span> death<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr">106: </span> <span style="color: #a6e3a1;">}</span><span style="color: #cba6f7;">while</span><span style="color: #a6e3a1;">(</span><span style="color: #f38ba8;">(</span>death<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> && death<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>death<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> && death<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>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;">0</span><span style="color: #fab387;">]</span> && death<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span> == pos<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span><span style="color: #f38ba8;">)</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr">107: </span> stop = <span style="color: #fab387;">1</span>; -<span class="linenr">108: </span> <span style="color: #f9e2af;">}</span> -<span class="linenr">109: </span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</span>score % <span style="color: #fab387;">3</span> != <span style="color: #fab387;">0</span><span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> -<span class="linenr">110: </span> stop = <span style="color: #fab387;">0</span>; -<span class="linenr">111: </span> <span style="color: #f9e2af;">}</span> -<span class="linenr">112: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f9e2af;">(</span>moves % <span style="color: #fab387;">5</span> == <span style="color: #fab387;">0</span> && moves != <span style="color: #fab387;">0</span><span style="color: #f9e2af;">)</span> <span style="color: #f9e2af;">{</span> -<span class="linenr">113: </span> <span style="color: #cba6f7;">do</span><span style="color: #a6e3a1;">{</span> -<span class="linenr">114: </span> map<span style="color: #f38ba8;">[</span>trap<span style="color: #fab387;">[</span><span style="color: #fab387;">0</span><span style="color: #fab387;">]</span><span style="color: #f38ba8;">][</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: #a6e3a1;">'-'</span>; -<span class="linenr">115: </span> trap<span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr">116: </span> trap<span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span>= arc4random_uniform<span style="color: #f38ba8;">(</span><span style="color: #fab387;">5</span><span style="color: #f38ba8;">)</span>; -<span class="linenr">117: </span> <span style="color: #a6e3a1;">}</span><span style="color: #cba6f7;">while</span><span style="color: #a6e3a1;">(</span><span style="color: #f38ba8;">(</span>trap<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> && trap<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>trap<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> && trap<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>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;">0</span><span style="color: #fab387;">]</span> && trap<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span> == pos<span style="color: #fab387;">[</span><span style="color: #fab387;">1</span><span style="color: #fab387;">]</span><span style="color: #f38ba8;">)</span><span style="color: #a6e3a1;">)</span>; -<span class="linenr">118: </span> -<span class="linenr">119: </span> <span style="color: #f9e2af;">}</span> -<span class="linenr">120: </span> map<span style="color: #f9e2af;">[</span>bonus<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">][</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: #a6e3a1;">'B'</span>; -<span class="linenr">121: </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;">'T'</span>; -<span class="linenr">122: </span> map<span style="color: #f9e2af;">[</span>death<span style="color: #a6e3a1;">[</span><span style="color: #fab387;">0</span><span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">][</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: #a6e3a1;">'D'</span>; -<span class="linenr">123: </span> moves++; -<span class="linenr">124: </span> <span style="color: #fab387;">}</span><span style="color: #cba6f7;">while</span><span style="color: #fab387;">(</span>input != <span style="color: #a6e3a1;">'c'</span><span style="color: #fab387;">)</span>; -<span class="linenr">125: </span> <span style="color: #cba6f7;">return</span> <span style="color: #fab387;">0</span>; -<span class="linenr">126: </span><span style="color: #f38ba8;">}</span> -<span class="linenr">127: </span> -</pre> -</div> - - -<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 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;">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. And I picked 50x50 as a max size because why not -</p> - -<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> -<div class="org-src-container"> -<pre class="src src-c"><span class="linenr">11: </span> <span style="color: #f9e2af;">int</span> <span style="color: #cdd6f4;">stop</span>=<span style="color: #fab387;">0</span>,<span style="color: #cdd6f4;">i</span>=<span style="color: #fab387;">0</span>,<span style="color: #cdd6f4;">moves</span>=<span style="color: #fab387;">0</span>,<span style="color: #cdd6f4;">score</span>=<span style="color: #fab387;">0</span>,<span style="color: #cdd6f4;">pos</span><span style="color: #f38ba8;">[</span><span style="color: #fab387;">2</span><span style="color: #f38ba8;">]</span> = <span style="color: #f38ba8;">{</span>n/<span style="color: #fab387;">2</span>,m/<span style="color: #fab387;">2</span><span style="color: #f38ba8;">}</span>; -</pre> -</div> - -<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. -</p> - - -<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> -<div class="org-src-container"> -<pre class="src src-c"><span class="linenr">17: </span> <span style="color: #f9e2af;">bonus</span><span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span> = arc4random_uniform<span style="color: #f38ba8;">(</span>n<span style="color: #f38ba8;">)</span>; <span style="color: #f9e2af;">bonus</span><span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span> = arc4random_uniform<span style="color: #f38ba8;">(</span>m<span style="color: #f38ba8;">)</span>; -<span class="linenr">18: </span> <span style="color: #f9e2af;">trap</span><span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span> = arc4random_uniform<span style="color: #f38ba8;">(</span>n<span style="color: #f38ba8;">)</span>; <span style="color: #f9e2af;">trap</span><span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span> = arc4random_uniform<span style="color: #f38ba8;">(</span>m<span style="color: #f38ba8;">)</span>; -<span class="linenr">19: </span> <span style="color: #f9e2af;">death</span><span style="color: #f38ba8;">[</span><span style="color: #fab387;">0</span><span style="color: #f38ba8;">]</span> = arc4random_uniform<span style="color: #f38ba8;">(</span>n<span style="color: #f38ba8;">)</span>; <span style="color: #f9e2af;">death</span><span style="color: #f38ba8;">[</span><span style="color: #fab387;">1</span><span style="color: #f38ba8;">]</span> = arc4random_uniform<span style="color: #f38ba8;">(</span>m<span style="color: #f38ba8;">)</span>; -</pre> -</div> - -<p> -Looking good so far!!, We then have line 21-23 which also shows the pickups as their respective symbols in the map. -</p> - - -<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> -<div class="org-src-container"> -<pre class="src src-c"><span class="linenr">26: </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">27: </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">28: </span> printf<span style="color: #f9e2af;">(</span><span style="color: #a6e3a1;">"%c "</span>, map<span style="color: #a6e3a1;">[</span>i<span style="color: #a6e3a1;">][</span>j<span style="color: #a6e3a1;">]</span><span style="color: #f9e2af;">)</span>; -<span class="linenr">29: </span> <span style="color: #fab387;">}</span> -<span class="linenr">30: </span> printf<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"\n"</span><span style="color: #fab387;">)</span>; -<span class="linenr">31: </span> <span style="color: #f38ba8;">}</span> -<span class="linenr">32: </span> -</pre> -</div> -<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> - -<p> -After that we have some logic which should also be changed to account for the n and m changes yet again -</p> -<div class="org-src-container"> -<pre class="src src-c"><span class="linenr">37: </span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>input == <span style="color: #a6e3a1;">'w'</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> -<span class="linenr">38: </span> printf<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Moving up\n"</span><span style="color: #fab387;">)</span>; -<span class="linenr">39: </span> map<span style="color: #fab387;">[</span>pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span><span style="color: #fab387;">][</span>pos<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">40: </span> <span style="color: #cba6f7;">if</span> <span style="color: #fab387;">(</span>pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span> == <span style="color: #fab387;">0</span><span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> -<span class="linenr">41: </span> pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span> = n-<span style="color: #fab387;">1</span>; -<span class="linenr">42: </span> <span style="color: #fab387;">}</span> -<span class="linenr">43: </span> <span style="color: #cba6f7;">else</span> <span style="color: #fab387;">{</span> -<span class="linenr">44: </span> pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span>--; -<span class="linenr">45: </span> <span style="color: #fab387;">}</span> -<span class="linenr">46: </span> <span style="color: #f38ba8;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>input == <span style="color: #a6e3a1;">'a'</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> -<span class="linenr">47: </span> printf<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Moving left\n"</span><span style="color: #fab387;">)</span>; -<span class="linenr">48: </span> map<span style="color: #fab387;">[</span>pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span><span style="color: #fab387;">][</span>pos<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">49: </span> <span style="color: #cba6f7;">if</span> <span style="color: #fab387;">(</span>pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span> == <span style="color: #fab387;">0</span><span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> -<span class="linenr">50: </span> pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span> = m-<span style="color: #fab387;">1</span>; -<span class="linenr">51: </span> <span style="color: #fab387;">}</span> -<span class="linenr">52: </span> <span style="color: #cba6f7;">else</span> <span style="color: #fab387;">{</span> -<span class="linenr">53: </span> pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span>--; -<span class="linenr">54: </span> <span style="color: #fab387;">}</span> -<span class="linenr">55: </span> <span style="color: #f38ba8;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>input == <span style="color: #a6e3a1;">'s'</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> -<span class="linenr">56: </span> -<span class="linenr">57: </span> printf<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Moving down\n"</span><span style="color: #fab387;">)</span>; -<span class="linenr">58: </span> map<span style="color: #fab387;">[</span>pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span><span style="color: #fab387;">][</span>pos<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">59: </span> <span style="color: #cba6f7;">if</span> <span style="color: #fab387;">(</span>pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span> == n-<span style="color: #fab387;">1</span><span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> -<span class="linenr">60: </span> pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span> = <span style="color: #fab387;">0</span>; -<span class="linenr">61: </span> <span style="color: #fab387;">}</span> -<span class="linenr">62: </span> <span style="color: #cba6f7;">else</span> <span style="color: #fab387;">{</span> -<span class="linenr">63: </span> pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span>++; -<span class="linenr">64: </span> <span style="color: #fab387;">}</span> -<span class="linenr">65: </span> <span style="color: #f38ba8;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>input == <span style="color: #a6e3a1;">'d'</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> -<span class="linenr">66: </span> printf<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Moving right\n"</span><span style="color: #fab387;">)</span>; -<span class="linenr">67: </span> map<span style="color: #fab387;">[</span>pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">0</span><span style="color: #f9e2af;">]</span><span style="color: #fab387;">][</span>pos<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">68: </span> <span style="color: #cba6f7;">if</span> <span style="color: #fab387;">(</span>pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span> == m-<span style="color: #fab387;">1</span><span style="color: #fab387;">)</span> <span style="color: #fab387;">{</span> -<span class="linenr">69: </span> pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span> = <span style="color: #fab387;">0</span>; -<span class="linenr">70: </span> <span style="color: #fab387;">}</span> -<span class="linenr">71: </span> <span style="color: #cba6f7;">else</span> <span style="color: #fab387;">{</span> -<span class="linenr">72: </span> pos<span style="color: #f9e2af;">[</span><span style="color: #fab387;">1</span><span style="color: #f9e2af;">]</span>++; -<span class="linenr">73: </span> <span style="color: #fab387;">}</span> -<span class="linenr">74: </span> <span style="color: #f38ba8;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #cba6f7;">if</span> <span style="color: #f38ba8;">(</span>input == <span style="color: #a6e3a1;">'c'</span><span style="color: #f38ba8;">)</span> <span style="color: #f38ba8;">{</span> -<span class="linenr">75: </span> printf<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Quitting\n"</span><span style="color: #fab387;">)</span>; -<span class="linenr">76: </span> <span style="color: #f38ba8;">}</span> <span style="color: #cba6f7;">else</span> <span style="color: #f38ba8;">{</span> -<span class="linenr">77: </span> printf<span style="color: #fab387;">(</span><span style="color: #a6e3a1;">"Invalid input\n"</span><span style="color: #fab387;">)</span>; -<span class="linenr">78: </span> <span style="color: #f38ba8;">}</span> -</pre> -</div> - -<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-22 Thu 19:09</p> -</div> -</body> -</html> diff --git a/blog/lisp/episode1.html b/blog/lisp/episode1.html deleted file mode 100644 index 72c5b97..0000000 --- a/blog/lisp/episode1.html +++ /dev/null @@ -1,29 +0,0 @@ -<?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-12-17 Sun 19:04 --> -<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> -<meta name="viewport" content="width=device-width, initial-scale=1" /> -<title>Learn Lisp With me. Episode 1</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="../index.html"> UP </a> - | - <a accesskey="H" href="https://crystal.tilde.institute/"> HOME </a> -</div><div id="content" class="content"> -<h1 class="title">Learn Lisp With me. Episode 1</h1> -</div> -<div id="postamble" class="status"> -<p class="author">Author: Crystal</p> -<p class="date">Created: 2023-12-17 Sun 19:04</p> -</div> -</body> -</html> diff --git a/blog/misc/merlin.html b/blog/misc/merlin.html deleted file mode 100644 index ec7f975..0000000 --- a/blog/misc/merlin.html +++ /dev/null @@ -1,191 +0,0 @@ -<?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> -<!-- 2024-04-12 Fri 19:51 --> -<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> -<meta name="viewport" content="width=device-width, initial-scale=1" /> -<title>ON MERLIN'S BEARD</title> -<meta name="author" content="Crystal for my sloth" /> -<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="../../../favicon.png"> -</head> -<body> -<div id="org-div-home-and-up"> - <a accesskey="h" href=""> UP </a> - | - <a accesskey="H" href="https://crystal.tilde.institute/"> HOME </a> -</div><div id="content" class="content"> -<h1 class="title">ON MERLIN’S BEARD</h1> -<p> -Let’s say, hypothetically, you have a <b>Xiaomi Redmi Note 9</b> (codenamed <b>Merlin</b>), and we would assume that it’s broken in a way that it shows the <b>Xiaomi logo</b> only then reboots/shuts-down, your first solution would be “Oh let’s take it to a repairshop, they would fix it” but why do so when you could fix it at home, alone, with the help of this little guide. -</p> -<div id="outline-container-org88c83c3" class="outline-2"> -<h2 id="org88c83c3">Axioms :</h2> -<div class="outline-text-2" id="text-org88c83c3"> -<p> -We will assume the following things : -</p> -<ul class="org-ul"> -<li>You have a Redmi Note 9…or any Redmi, but some steps WILL vary ever so slightly.</li> -<li>The phone at least gives a sign of life, the Xiaomi logo is well enough.</li> -<li>You have a PC, preferably running the second best OS known to mankind, <b>Linux !!!</b>. If you are not running Linux, a good start is <b>Ubuntu</b> (we will assume the usage of a <b>Debian</b>-based distro here, if you use anything other than them, you are probably smart enough to figure this shit yourself)</li> -<li>You have a good USB-C cable.</li> -<li>You have a working brain.</li> -</ul> -</div> -</div> -<div id="outline-container-org0e634c8" class="outline-2"> -<h2 id="org0e634c8">Step Uno : Downloading the required files and the medkits :</h2> -<div class="outline-text-2" id="text-org0e634c8"> -</div> -<div id="outline-container-orgdbc8e67" class="outline-3"> -<h3 id="orgdbc8e67">android-tools :</h3> -<div class="outline-text-3" id="text-orgdbc8e67"> -<div class="org-src-container"> -<pre class="src src-sh"><span style="color: #f9e2af;">sudo</span> apt update -<span style="color: #f9e2af;">sudo</span> apt install android-tools-adb android-tools-fastboot -</pre> -</div> -</div> -</div> -<div id="outline-container-orga65ea6a" class="outline-3"> -<h3 id="orga65ea6a">mtkclient and it’s dependencies :</h3> -<div class="outline-text-3" id="text-orga65ea6a"> -</div> -<div id="outline-container-orgcbafb81" class="outline-4"> -<h4 id="orgcbafb81">Dependencies :</h4> -<div class="outline-text-4" id="text-orgcbafb81"> -<div class="org-src-container"> -<pre class="src src-sh"><span style="color: #f9e2af;">sudo</span> apt install python3 <span style="color: #f9e2af;">git</span> libusb-1.0-0 python3-pip -</pre> -</div> -</div> -</div> -<div id="outline-container-org4a33127" class="outline-4"> -<h4 id="org4a33127">Grab the files :</h4> -<div class="outline-text-4" id="text-org4a33127"> -<div class="org-src-container"> -<pre class="src src-shell"><span style="color: #f9e2af;">git</span> clone https://github.com/bkerler/mtkclient -<span style="color: #f9e2af;">cd</span> mtkclient -pip3 install . -</pre> -</div> -</div> -</div> -<div id="outline-container-orge0a0f85" class="outline-4"> -<h4 id="orge0a0f85">Install rules :</h4> -<div class="outline-text-4" id="text-orge0a0f85"> -<div class="org-src-container"> -<pre class="src src-shell"><span style="color: #f9e2af;">sudo</span> usermod -a -G plugdev $<span style="color: #cdd6f4;">USER</span> -<span style="color: #f9e2af;">sudo</span> usermod -a -G dialout $<span style="color: #cdd6f4;">USER</span> -<span style="color: #f9e2af;">sudo</span> <span style="color: #f9e2af;">cp</span> mtkclient/Setup/Linux/*.rules /etc/udev/rules.d -<span style="color: #f9e2af;">sudo</span> udevadm control -R -</pre> -</div> -<p> -and do <b>NOT</b> forget to reboot, otherwise you will face errors. -</p> -</div> -</div> -</div> -<div id="outline-container-org283a105" class="outline-3"> -<h3 id="org283a105">Fastboot image :</h3> -<div class="outline-text-3" id="text-org283a105"> -<p> -Personally, I prefer to use a custom ROM for these XIMI phones, but since this is a fool-proof guide, we will use the latest <b>Global</b> fastboot image, note that the difference between <b>Global</b> , <b>EEA (European Economic Area)</b> , <b>Turkish</b> , <b>Russian</b> and <b>Taiwan</b> ROMS are little though the Global version is most of the time behind the other ones in terms of updates , but the biggest one comes with the <b>Chinese</b> one, it’s more polished, runs faster, but is filled with Chinese-only apps, and does not have Google services (which is a huge W for me). But at the end of the day, it’s up to you! -</p> - -<p> -We will be using this one <a href="https://xiaomifirmwareupdater.com/miui/merlin/stable/V13.0.2.0.SJOMIXM/">https://xiaomifirmwareupdater.com/miui/merlin/stable/V13.0.2.0.SJOMIXM/</a> which is the Global version of miui. Make sure to scroll all the way <b>Down</b> and download the one with <b>Type: Fastboot</b>, it will be larger in size, but it’s the one we need. Use any mirror that you want, and wait patiently for it to download. Once it finishes, extract it to a folder, it’s a big archive so it will take time. -</p> -</div> -</div> -<div id="outline-container-org4addd56" class="outline-3"> -<h3 id="org4addd56">Meanwhile, mtkclient :</h3> -<div class="outline-text-3" id="text-org4addd56"> -<p> -While waiting for your download to finish, go to your mtkclient folder and run this command: -</p> -<div class="org-src-container"> -<pre class="src src-shell">python mtk_gui -</pre> -</div> -<p> -this should bring up the GUI client, if it doesn’t, replace <b>python</b> with <b>python3</b>. Once it’s running, make sure your phone is powered-off, and plug the USB-C cable into your PC <b>While holding both the Up and Down volume buttons</b> until you see a change in the GUI app. If you notice a red error message on the console which will be running in the background, relaunch using <b>sudo</b>. -</p> - -<p> -Now explore the app but do <b>NOT</b> touch anything with the word <b>Write</b> or <b>Erase</b>, also the ones with <b>Read</b> are useless for you, what is important is looking for the <b>Unlock bootloader</b> button, click on it, and wait until it’s okay, then remove your phone from the cable. aaaand we shoould be done for this part. -</p> -</div> -</div> -<div id="outline-container-org25169bd" class="outline-3"> -<h3 id="org25169bd">The Fun Part :</h3> -<div class="outline-text-3" id="text-org25169bd"> -<p> -Once the fastboot image is downloaded and extracted, go to the folder and look for this file <b>flash_all_except_storage.sh</b> this will be the one you would need. -</p> - -<p> -<b>DO NOT EVER EVER EVER, UNDER ANY CASE, TOUCH A FILE THAT HAS THE WORD LOCK IN IT.</b> -</p> - -<p> -Once you located this file, open a terminal there, and give it exec permissions. Or in other words : -</p> -<div class="org-src-container"> -<pre class="src src-shell"><span style="color: #f9e2af;">chmod</span> +x ./flash_all_except_storage.sh -</pre> -</div> -<p> -and now take your phone <b>again</b> and now Power it on while holding the <b>Vol DOWN</b> button, keep that position until you see a <b>fastboot</b> screen, or a Russian robot rewiring the organs of it’s victim. -</p> - -<p> -Once you see it, plug the phone to the pc, and run -</p> -<div class="org-src-container"> -<pre class="src src-shell"><span style="color: #f9e2af;">sudo</span> fastboot devices -</pre> -</div> -<p> -sudo might not be needed for you, but i dont know why, it never worked for me without sudo (probably the rules step) -</p> - -<p> -Once you see your device in there, it means it recognized it, go again to the folder with <b>flash_all_except_storage</b> and run it -</p> -<div class="org-src-container"> -<pre class="src src-shell"><span style="color: #f9e2af;">sudo</span> ./flash_all_except_storage.sh -</pre> -</div> -</div> -</div> -<div id="outline-container-org95f8d76" class="outline-3"> -<h3 id="org95f8d76">Waiting :</h3> -<div class="outline-text-3" id="text-org95f8d76"> -<p> -This step takes a lot of time, so be sure to let your pc in a place where you don’t risk accidentally moving your phone and disconnecting it. DO NOT CLOSE THE TERMINAL, once it’s finished, it should reboot automatically and voila !! good as knew, and you might even keep your local files if you provide your password -</p> -</div> -</div> -</div> -<div id="outline-container-org1a283a8" class="outline-2"> -<h2 id="org1a283a8">Edge cases :</h2> -<div class="outline-text-2" id="text-org1a283a8"> -<ul class="org-ul"> -<li>If the initial setup (after the reboot) doesn’t work, try the flash_all.sh file instead.</li> -</ul> -</div> -</div> -</div> -<div id="postamble" class="status"> -<p class="author">Author: Crystal for my sloth</p> -<p class="date">Created: 2024-04-12 Fri 19:51</p> -</div> -</body> -</html> |