Solution to Karel Coding – Hour of Code

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.


Here comes the possible solutions to Karel Coding: Hour of Code https://hoc.nclab.com/karel/

Level 1

Level 2

Level 3

Level 4

Level 5


Level 6


Level 7


Level 8


Level 9


Level 10


Level 11


Level 12

Level 13

Level 14

Level 15




MakeCode Microbit 遙控機器人 (Microbit Remote-Controlled Robot)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

本篇文章將講解如何製作一個簡易的遙控機器人,此篇文章所使用的機器人為【麥昆智能小車 (Maqueen)】。This post will build a simple remote car with Maqueen which is a micro:bit based robot platform for education.

一、功能如下(Game Design): 

訊號說明(The communication way):

使用英文字母f、b、l、r、s 五個字母,對應動作為:f 為前進、b 為後退、l 為左轉、r 為右轉、s 為停止。Using the first letter of each action: forward, backward, left, right, stop. That is, the letter f stands for forward. The letter b stands for backward, and so on. 

遙控器(The controller)

  • 按下 A + B 按鈕時,送出停止(stop)訊號。Sending a stop signal when button A and B are pressed.
  • Microbit 板子向上傾斜時,送出前進f(forward)訊號。Sending a forward signal when tilting the microbit front.
  • Microbit 板子向下傾斜時,送出後退b(backward)訊號。Sending a backward signal when tilting the microbit back.
  • Microbit 板子向左傾斜時,送出左轉l(left)訊號。Sending a left signal when tilting the microbit to the left.
  • Microbit 板子向右傾斜時,送出右轉r(right)訊號。Sending a right signal when tilting the microbit to the right.

車子(The car)
  • 收到停止(stop)訊號時,車子停止。On receiving a stop signal, the car will stop.
  • 收到前進f(forward)訊號時,車子前進。On receiving a forward signal, the car will move forward.
  • 收到後退f(backward)訊號時,車子後退。On receiving a backward signal, the car will move backward.
  • 收到左轉l(left)訊號時,車子左轉。On receiving a left signal, the car will turn left .
  • 收到右轉r(right)訊號時,車子右轉。On receiving a right signal, the car will turn right .

二、積木程式(Blocks Code)

程式啟動時(On Start):


遙控器(The controller)

  • 按下 A + B 按鈕時,送出停止(stop)訊號。Sending a stop signal when button A and B are pressed.
  • Microbit 板子向上傾斜時,送出前進f(forward)訊號。Sending a forward signal when tilting the microbit front.
  • Microbit 板子向下傾斜時,送出後退b(backward)訊號。Sending a backward signal when tilting the microbit back.
  • Microbit 板子向左傾斜時,送出左轉l(left)訊號。Sending a left signal when tilting the microbit to the left.
  • Microbit 板子向右傾斜時,送出右轉r(right)訊號。Sending a right signal when tilting the microbit to the right.

車子(The car)
  • 收到停止(stop)訊號時,車子停止。On receiving a stop signal, the car will stop.
  • 收到前進f(forward)訊號時,車子前進。On receiving a forward signal, the car will move forward.
  • 收到後退f(backward)訊號時,車子後退。On receiving a backward signal, the car will move backward.
  • 收到左轉l(left)訊號時,車子左轉。On receiving a left signal, the car will turn left .
  • 收到右轉r(right)訊號時,車子右轉。On receiving a right signal, the car will turn right .


三、結果(The Results)
影片(Demo Video):

範例網址(Example Code):https://makecode.microbit.org/_5Ah3eYLyRH49

bDesigner 與 Arduino 互動

若您覺得文章寫得不錯,請點選網誌上的廣告,來支持小編,謝謝。

bDesigner 簡短介紹
bDesigner 是由桃園市八德國小蔡佳倫老師自行研發之Scratch擴充積木軟體以及blockly程式。在他的 Youtube上也有很多的教學影片。除了可以使用 Scratch 3 以外,還可與多種硬體(如Arduino UNO、Nano、ESP32、Microbit等)互動,使用這一套就可以省去了一直安裝不同軟體的時間,非常適合接觸多種硬體與blockly程式教學的老師們。



底下為筆者使用 bDesigner 與 Arduino UNO、Nano 的筆記紀錄。

與 Arduino Nano 互動
步驟一:燒錄
以MiniUSB 將 Nano 連接到電腦上,開啟 bDesigner,點選上方選單的燒錄 --> 開啟燒錄程式

選擇板子 Arduino NanoCOM編號(請讀者根據自己電腦上的編號做選擇),按下燒錄

此時會出現燒錄的畫面。

完成後,請關閉燒錄程式視窗。

步驟二:開啟 Scratch 3
軟體點選 Scratch 3,積木類型選 USB連多個 Arduino

步驟三:添加 Scratch 3擴展
點選 Scratch 3 左下方的擴展圖示,選擇bDesigner(Arduino)擴展



步驟四:設計 Arduino 內建 LED閃爍積木程式
設計如下的積木程式:


此時,按下綠旗,就可以看到 Nano 板子上標示 L 的 LED 燈閃爍三次(下圖紅色矩形的LED燈)

與 Arduino UNO 互動
此部分除了在燒錄時,需選擇板子 Arduino UNO外,其餘步驟和 與 Arduino Nano 互動 一樣。

那要如何讓 Arduino 自行運作(不需要電腦的離機模式)呢?

MakeCode Microbit 遊戲設計:貓追老鼠遊戲(Microbit Game: Cat Chasing Mouse Game)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

本篇文章將講解如何製作一個簡易追逐遊戲:【貓追老鼠】。
In this post, we're going to create a a simple chasing game: "cat chasing mouse", where you tilt the microbit to control a cat which is chasing a blinking mouse on the LED screen.

一、遊戲功能如下(Game Design): 

  • 遊戲時間一分鐘。One minute game.
  • 遊戲開始時,貓在畫面正中央。The cat is at the screen center when the game on start.
  • 老鼠不停閃爍,老鼠跑到隨機位置。The mouse is blinking and at random position on the screen.
  • 板子往上傾斜時,貓往上移動。The cat move up when tilting the microbit front.
  • 板子往下傾斜時,貓往下移動。The cat move down when tilting the microbit back.
  • 板子往右傾斜時,貓往右移動。The cat move to right when tilting the microbit to the right.
  • 板子往左傾斜時,貓往左移動。The cat move to left when tilting the microbit to the left.
  • 貓捉到老鼠時,得一分,老鼠跑到隨機位置。 When the cat catches the mouse, the mouse runs to random position. The player scores 1 point.

二、積木程式(Blocks Code)

遊戲時間一分鐘。One minute game.
遊戲開始時,貓在畫面正中央。The cat is at the screen center when the game on start.
老鼠不停閃爍,老鼠跑到隨機位置。The mouse is blinking and at random position on the screen.

板子往上傾斜時,貓往上移動。The cat move up when tilting the microbit front.
板子往下傾斜時,貓往下移動。The cat move down when tilting the microbit back.

板子往右傾斜時,貓往右移動。The cat move to right when tilting the microbit to the right.
板子往左傾斜時,貓往左移動。The cat move to left when tilting the microbit to the left.


貓捉到老鼠時,得一分,老鼠跑到隨機位置。 When the cat catches the mouse, the mouse runs to random position. The player scores 1 point.


三、結果(The Results)
影片(Demo Video):



請自由發揮額外功能,例如雙人對戰等。
More Ideas examples: Two players game...etc. 

範例網址(Example Code):https://makecode.microbit.org/_3tkeiCJ79W7b

C語言練習題:陣列(C language exercise: Array)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

練習一:整數陣列加總
使用一個大小為10的整數陣列來讀取使用者所輸入的10個整數,並算出此十個整數的總和。

Exercise 1: Sum of an array of integer
Using an integer array of size 10 to store input from user. Now print the sum of these 10 integers.

練習一參考解法:
Exercise 1 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numbers[10], sum = 0;

    for(int i = 0; i < 10; i++)
    {
        printf("Enter an integer for numbers[%d]:", i);
        scanf("%d", &numbers[i]);
        sum = sum + numbers[i];
    }

    printf("\nSum:%d", sum);

    return 0;
}

練習二:整數陣列中最大的整數
使用亂數函數 rand() 產生 10 整數,並找出最大值。

Exercise 2: Find the largest integer
Using rand() function to generate 10 integers and find the biggest integer.

練習二參考解法:
Exercise 2 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numbers[10], big = 0;

    printf("The numbers:");
    for(int i = 0; i < 10; i++)
    {
        numbers[i] = rand();
        printf("%d,", numbers[i]);
        if(big < numbers[i])
            big = numbers[i];
    }

    printf("\nThe biggest integer:%d.\n", big);
    return 0;
}

練習三:Greeting message
Get the user's name from keyboard and print it with greeting message.

Exercise 3: 打招呼
請使用者輸入自己的姓名,程式輸出【姓名】,【打招呼訊息】。

練習三參考解法:
Exercise 3 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char name[50];
    printf("Input your name:");
    scanf("%s", name);  // name is a string that don't need'&' operator

    printf("%s, nice to meet you.\n", name);
    return 0;
}

練習四:奇數或偶數
使用亂數函數 rand() 產生 10 整數,分別對奇數的數加總以及偶數的數加總。

Exercise 4: Even or Odd
Using rand() function to generate 10 integers. Now, print the sum of even numbers and the sum of odd numbers. 

練習四參考解法:
Exercise 4 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int numbers[10], evenSum = 0, oddSum = 0;

    printf("The numbers:");
    for(int i = 0; i < 10; i++)
    {
        numbers[i] = rand() % 10;
        printf("%d,", numbers[i]);

        if(numbers[i] % 2)
        {
            oddSum += numbers[i];
        }
        else
        {
            evenSum += numbers[i];
        }
    }

    printf("\nThe sum of even numbers:%d\n", evenSum);
    printf("The sum of odd numbers:%d\n", oddSum);
    return 0;
}

練習五:二維陣列
使用亂數函數 rand()來建立大小為 3 x 3 的二維陣列。並輸出此矩陣的元素以及矩陣所有元素的平均值。

Exercise 5: 2D Array
Using rand() function to write a program for a 2D array of size 3x3. Then display the matrix and calculate the average of the matrix's elements.


練習五參考解法:
Exercise 5 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int matrix[3][3];
    float avg = 0.0f;


    printf("The matrix:\n");
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            matrix[i][j] = rand() % 100;
            avg += matrix[i][j];
            printf("matrix[%d][%d] = %2d, ", i, j, matrix[i][j]);
        }
        printf("\n");
    }

    avg = avg / 9.0;
    printf("Average:%f\n", avg);

    return 0;
}

練習六:矩陣相加
使用亂數函數 rand()來建立兩個大小為 3 x 3 的二維陣列。並輸出此兩矩陣相加後的結果。

Exercise 6:  Matrix Addition
Using rand() function to write a program for two 2D array of size 3x3. And output the addition of the two matrix.

練習六參考解法:
Exercise 6 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>

void prtMatrix(int m[3][3])
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            printf("[%d][%d] = %2d, ", i, j, m[i][j]);
        }
        printf("\n");
    }

}

int main()
{
    int a[3][3];
    int b[3][3];
    int c[3][3];

    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            a[i][j] = rand() % 100;
            b[i][j] = rand() % 100;
            c[i][j] = a[i][j] + b[i][j];
        }
    }

    printf("a:\n");
    prtMatrix(a);

    printf("\n");

    printf("b:\n");
    prtMatrix(b);

    printf("\n");

    printf("The addition of matrix a and matrix b:\n");
    prtMatrix(c);

    return 0;
}


練習七:矩陣相減
使用亂數函數 rand()來建立兩個大小為 3 x 3 的二維陣列。並輸出此兩矩陣相減後的結果。

Exercise 7: Matrix Subtraction
Using rand() function to write a program for two 2D array of size 3x3. And output the subtraction of the two matrix.

練習七參考解法:
Exercise 7 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <stdlib.h>

void prtMatrix(int m[3][3])
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            printf("[%d][%d] = %2d, ", i, j, m[i][j]);
        }
        printf("\n");
    }

}

int main()
{
    int a[3][3];
    int b[3][3];
    int c[3][3];

    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            a[i][j] = rand() % 100;
            b[i][j] = rand() % 100;
            c[i][j] = a[i][j] - b[i][j];
        }
    }

    printf("a:\n");
    prtMatrix(a);

    printf("\n");

    printf("b:\n");
    prtMatrix(b);

    printf("\n");

    printf("The subtraction of matrix a and matrix b:\n");
    prtMatrix(c);

    return 0;
}

練習八:方陣相乘
使用亂數函數 rand()來建立兩個大小為 3 x 3 的方陣 A 與 B。並輸出此兩方陣A乘B後的結果。

Exercise 8: Square Matrices Multiplication
Using rand() function to write a program for two 2D array of size 3x3. And output the subtraction of the two square matrices.

練習八參考解法:
Exercise 8 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void prtMatrix(int m[3][3])
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            printf("[%d][%d] = %3d, ", i, j, m[i][j]);
        }
        printf("\n");
    }

}

int main()
{
    int A[3][3];
    int B[3][3];
    int C[3][3];

    srand((unsigned) time(0));

    // Matrices initialization
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            A[i][j] = rand() % 10;
            B[i][j] = rand() % 10;
            C[i][j] = 0;
        }
    }

    // C = A x B
    int sum = 0;
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            sum = 0;
            for(int k = 0; k < 3; k++)
            {
                sum = sum + A[i][k] * B[k][j];
            }
            C[i][j] = sum;
        }
    }

    printf("A:\n");
    prtMatrix(A);

    printf("\n");

    printf("B:\n");
    prtMatrix(B);

    printf("\n");

    printf("The A X B:\n");
    prtMatrix(C);

    return 0;
}


練習九:取代陣列內的元素
使用亂數函數 rand() 產生 10 個整數,尋找特定的數值,並用 -1 來取代。

Exercise 9: Replace an element in an array
Using rand() function to generate 10 integers. Then find a specific integer and replace it with -1. 


練習九參考解法:
Exercise 9 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int numbers[10], target = -1;

    srand(time(0));

    printf("Enter an integer between [0 - 9]:");
    scanf("%d", &target);

    printf("The numbers:\n");
    for(int i = 0; i < 10; i++)
    {
        numbers[i] = rand() % 10;
        printf("%2d,", numbers[i]);

        // Find the target and replace it with -1
        if(numbers[i] == target)
            numbers[i] = -1;
    }

    printf("\nAfter replacing, the numbers:\n");
    for(int i = 0; i < 10; i++)
    {
        printf("%2d,", numbers[i]);

    }

    printf("\n");
    return 0;
}

練習十:費氏數列
輸入一個正整數 N,使用陣列的方式來產生前 N 個的費氏數列。

Exercise 10: Fibonacci Sequence
Input a positive integer N. Then show the fibonacci series up to n terms.

練習十參考解法:
Exercise 10 solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int fibo[1000];
    int N;
    int t;

    printf("Enter a positive integer [1-999]:");
    scanf("%d", &N);

    fibo[0] = 0;
    fibo[1] = 1;

    for(int i = 2; i < N; i++)
    {
        fibo[i] = fibo[i-1] + fibo[i-2];
    }


    for(int i = 0; i < N; i++)
        printf("%2d,", fibo[i]);

    printf("\n");
    return 0;
}