Programming 101: различия между версиями

Материал из FIJTeam Project Wiki
Перейти к навигации Перейти к поиску
>Skull132
Нет описания правки
>Skull132
Строка 37: Строка 37:
==== Lecture 1: Homework: Making a Calculator ====
==== Lecture 1: Homework: Making a Calculator ====


I tried writing this indepth today but I found out that I was too tired. Manana, as the saying goes.
Objective: We make a calculator!
 
The calculator, as a minimum, should be able to:
* Take two inputs from the user.
* Execute '''one''' of the following binary mathematical operations, based on user input:
** Multiplication (a * b);
** Division (a / b);
** Addition (a + b);
** Deduction (a - b).
* Output the result to world (via world << somestuff).
 
I've taken the liberty of dividing the task up into sections. This should (hopefully) help you better digest it! I've also hidden some spoilers/advice for things that I perceive difficult. If you get stuck, feel free to expand and see if they help out! If not, feel free to pester me via Discord.
 
===== Section 1: User Input =====
 
Something we didn't learn how to do was to get input from the user. But we did learn about functions! As I said during the lecture, DM comes with a lot of ready-made functions. And the one we use to get input from the user is called, you'll never guess it, input()!
 
Here's a usage example:
<pre>
/proc/main()
    var/a = input("Please input a number for A") as num
</pre>
 
The "as num" component will ensure that the input functions returns a number, otherwise you might get a piece of text, and we all remember what happened when we tried to do "42 + "a b c"". (Bad things.) The DM reference article describing the usage of input can be found [http://www.byond.com/docs/ref/#/proc/input here].
 
Beyond numbers A and B, there's one more thing the user must select: what operation to do. This will still require the input() proc, but we need to decide how the user will communicate their wishes to us. The easiest way is to use a number, eg: 0 means add, 1 means deduct, etcetera. But you can also use text strings! Or any other trick you can find in the reference manual or come up.
 
The final thing for this section is validation! Make sure to validate your inputs! If you allow for 0 - 3 to be equations, then the user inputting 69 should surely print an error! During an error, you should also communicate the error to the user! And obviously simply exit the main function using return.
 
'''Bonus:'''
* Make validation of input arguments a separate proc, that takes 3 parameters: a, b, and the action.
 
 
<div class="toccolours mw-collapsible mw-collapsed" style="width:99%">
'''Spoilers:''' Example validation
<div class="mw-collapsible-content">
<pre>
/proc/main()
    var/a = ...
    var/b = ...
    var/action = ... //!< Valid [0, 3], indicates which operation to perform.
 
    if (action < 0 || action > 3)
        world << "Invalid operation specified."
        return
 
    if (action == 2) // 2 => division
        if (b <= 0)
            world << "Cannot divide by 0"
            return
</pre>
</div></div>


== Setup ==
== Setup ==

Версия от 16:52, 14 апреля 2020

Lecture Topic List

Lecture 1: In the Very Beginning

Topic list:

  • General project outline and getting things rolling.
    • Wtf the project files are and what are they meant for.
    • Let's get it all running for a moment and print a pretty "HELLO WORLD".
  • The main function, what it is, what it does.
  • VARIABLES
    • What they are. (The core of programming.)
    • How to declare, name.
    • What values can they be given?
      • Let's ignore objects for now, primitives only.
      • Numbers. Strings. null.
    • Operations on variables. Maths! Literally. Maths.
  • FUNCTIONS
    • Code is executed SEQUENTIALLY.
    • Functions are the same as they are in maths. Including parameters!
    • Let's do some debugging magic with introducing a new function.
    • Return statement and values.
    • Implement some basic maths functions with parameters and return statements.
    • Don't worry about stateful code for the time being. Next lecture, when we get to objects, we'll also get to worry about state.
    • DM reference manual plug. There are many functions that already exist made ready for you!
  • CONTROL STATEMENTS
    • if-else-if-else.
      • What classifies as truthy? TRUE/FALSE, null, "", 0.
    • if-eles-if-else vs if if if.
    • Control statements with return. So early returns.
      • Make some kind of data validation function that later does maths. Eg. only add if data is in range of [A, B]. Return error otherwise and print shit appropriately.
    • Looops.
      • Simple for-loops. While-loops.
  • LISTS
    • Will we make it this far? We'll find out!

Lecture 1: Homework: Making a Calculator

Objective: We make a calculator!

The calculator, as a minimum, should be able to:

  • Take two inputs from the user.
  • Execute one of the following binary mathematical operations, based on user input:
    • Multiplication (a * b);
    • Division (a / b);
    • Addition (a + b);
    • Deduction (a - b).
  • Output the result to world (via world << somestuff).

I've taken the liberty of dividing the task up into sections. This should (hopefully) help you better digest it! I've also hidden some spoilers/advice for things that I perceive difficult. If you get stuck, feel free to expand and see if they help out! If not, feel free to pester me via Discord.

Section 1: User Input

Something we didn't learn how to do was to get input from the user. But we did learn about functions! As I said during the lecture, DM comes with a lot of ready-made functions. And the one we use to get input from the user is called, you'll never guess it, input()!

Here's a usage example:

/proc/main()
    var/a = input("Please input a number for A") as num

The "as num" component will ensure that the input functions returns a number, otherwise you might get a piece of text, and we all remember what happened when we tried to do "42 + "a b c"". (Bad things.) The DM reference article describing the usage of input can be found here.

Beyond numbers A and B, there's one more thing the user must select: what operation to do. This will still require the input() proc, but we need to decide how the user will communicate their wishes to us. The easiest way is to use a number, eg: 0 means add, 1 means deduct, etcetera. But you can also use text strings! Or any other trick you can find in the reference manual or come up.

The final thing for this section is validation! Make sure to validate your inputs! If you allow for 0 - 3 to be equations, then the user inputting 69 should surely print an error! During an error, you should also communicate the error to the user! And obviously simply exit the main function using return.

Bonus:

  • Make validation of input arguments a separate proc, that takes 3 parameters: a, b, and the action.


Spoilers: Example validation

/proc/main()
    var/a = ...
    var/b = ...
    var/action = ... //!< Valid [0, 3], indicates which operation to perform.

    if (action < 0 || action > 3)
        world << "Invalid operation specified."
        return

    if (action == 2) // 2 => division
        if (b <= 0)
            world << "Cannot divide by 0"
            return

Setup

Setting up Visual Studio Code

Use Matt's Guide to Contribution and Coding to set up Visual Studio Code and related plugins. Specifically, follow the first half of the second post. All the way up until it starts talking about the admin config.

Downloading the Base Project

The base code for this project is hosted on Github, accessible via this repository. A link to directly download the repository as a .zip file is here. To set up the project, unzip the folder onto your desktop or somewhere else. It can now be opened with Visual Studio Code: simply right click one of the internal folders (labelled "setX") and select "Open With Code".

Project Structure & Compiling

In Visual Studio Code, all projects should contain at least 3 files:

  • world.dm - Do not touch this file for now, it does some background magic to make the setup work.
  • course.dm - This is the file you should be modifying to make stuff work! It'll contain a function called /proc/main(), which is what you should modify to make stuff happen.
  • course.dme - This is the DM Environment file for the project. It's basically what ties the entire project together and makes it able to compile.

After you've made any changes to the code, you have to press Ctrl-Shift-B to compile the code. Compilation is required to produce the .dmb file which we will be hosting with DreamDaemon.

Hosting

After compilation, we need to host the project to join and see what our code does!

This is done using a program called DreamDaemon. It's installed in your program files, right besides DreamMaker.exe and BYOND.exe. Find it, open it. And then follow the pictorial guide to success:

Helpful Resources

Here's a list of helpful resources for programming DM specifically: