Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomize conditionals #201

Merged
merged 5 commits into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions stages/conditional01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module.exports = {
name: 'conditional01',
version: 3,
parts: {
wireI: null,
wireL: null,
wireT: null,
'times-2': null,
'times-3': null,
'plus-1': null,
'plus-2': null,
'minus-2': null,
equal: null,
add: null,
sub: null,
div: null,
mul: null,
'c-contact': null,
conditional: null,
transistor: null,
},
inputX: 4,
outputX: 4,
input: [5, -10, null, null],
output: [15, -20, null, null],
ioGenerator: (random) => {
const input1 = Math.floor(random() * 200);
const output1 = input1 % 2 === 0 ? 2 * input1 : 3 * input1;

const input2 = Math.floor(random() * 200);
const output2 = input2 % 2 === 0 ? 2 * input2 : 3 * input2;

return {
input: [5, -10, input1, input2],
output: [15, -20, output1, output2],
};
},
width: 9,
height: 7,
clockLimit: 100,
statement: '偶数なら2倍,奇数なら3倍してみよう!',
title: '条件分岐 -基本- 1',
modal: 'conditionals',
};
35 changes: 0 additions & 35 deletions stages/conditional01.yml

This file was deleted.

43 changes: 43 additions & 0 deletions stages/conditional02.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = {
name: 'conditional02',
version: 3,
parts: {
wireI: null,
wireL: null,
wireT: null,
'times-2': null,
'times-3': null,
'plus-1': null,
'plus-2': null,
'minus-2': null,
equal: null,
add: null,
sub: null,
div: null,
mul: null,
'c-contact': null,
conditional: null,
transistor: null,
},
inputX: 5,
outputX: 5,
input: [5, 6, null, null],
output: [10, 18, null, null],
ioGenerator: (random) => {
const candidates = Array.from({length: 200}, (item, index) => index + 1)
.filter((val) => val !== 5 && val !== 6);

const input1 = candidates[Math.floor(random() * candidates.length)];
const input2 = candidates[Math.floor(random() * candidates.length)];

return {
input: [5, 6, input1, input2],
output: [10, 18, input1, input2],
};
},
width: 11,
height: 11,
clockLimit: 100,
statement: '5は2倍、6は3倍、それ以外は1倍をしてみよう!',
title: '条件分岐 -基本- 2',
};
34 changes: 0 additions & 34 deletions stages/conditional02.yml

This file was deleted.

69 changes: 69 additions & 0 deletions stages/conditional03.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
module.exports = {
name: 'conditional03',
version: 3,
parts: {
wireI: null,
wireL: null,
wireT: null,
'times-2': null,
'times-3': null,
'plus-1': null,
'plus-2': null,
'minus-2': null,
equal: null,
add: null,
sub: null,
div: null,
mul: null,
'c-contact': null,
conditional: null,
transistor: null,
diode: null,
},
inputX: 4,
outputX: 4,
input: [5, 3, null, null, null],
output: [0, 5, null, null, null],
ioGenerator: (random) => {
const candidates = [0, 1, 2, 4, 6];
const inputs = [];

// Shuffle array and take heading 3
const index1 = Math.floor(random() * 5);
inputs.push(candidates[index1]);
candidates[index1] = candidates[0];

const index2 = Math.floor(random() * 4) + 1;
inputs.push(candidates[index2]);
candidates[index2] = candidates[1];

const index3 = Math.floor(random() * 3) + 2;
inputs.push(candidates[index3]);

inputs.sort((a, b) => a - b);

const outputs = inputs.map((input) => ((input + 2) % 7));

return {
input: [
5,
3,
inputs[0],
inputs[1],
inputs[2],
],
output: [
0,
5,
outputs[0],
outputs[1],
outputs[2],
],
};
},
width: 9,
height: 9,
clockLimit: 50,
statement: 'mod7で2を足してみよう(入力は[0,6]であることが保証されている)',
title: '条件分岐 -基本- 3',
};
35 changes: 0 additions & 35 deletions stages/conditional03.yml

This file was deleted.

6 changes: 3 additions & 3 deletions stages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ module.exports = [
require('./sixth-power.yml'),
require('./remainder.yml'),
require('./plus-32.yml'),
require('./conditional01.yml'),
require('./conditional02.yml'),
require('./conditional03.yml'),
require('./conditional01.js'),
require('./conditional02.js'),
require('./conditional03.js'),
require('./factorial.js'),
require('./parity.js'),
require('./fibonacci.js'),
Expand Down
56 changes: 56 additions & 0 deletions test/unit/stages.ls
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ require! {
'core-js/es5'
'core-js/es6'
'../../stages/calc03'
'../../stages/conditional01'
'../../stages/conditional02'
'../../stages/conditional03'
'../../stages/factorial'
'../../stages/parity'
'../../stages/fibonacci'
Expand Down Expand Up @@ -51,6 +54,16 @@ sum-of-digits = (n) -> n.to-string!split '' .map (parse-int _, 10) .reduce (+)
fibonacci-calc = (n, value = 1, prev = 0) ->
if n is 0 then prev else fibonacci-calc n - 1, value + prev, value

conditional1-calc = (x) ->
if x % 2 is 0 then 2 * x else 3 * x

conditional2-calc = (x) ->
if x is 5
10
else if x is 6
18
else x

describe 'Stage Data' ->
before-each ->
@random = seedrandom ''
Expand All @@ -67,6 +80,49 @@ describe 'Stage Data' ->
expect io.input.0 .to.equal 1
expect io.input.1 .to.equal 3

describe 'conditional01 stage' ->
It 'generates 2x if even, 3x otherwise' ->
io = conditional01.io-generator @random

expect io .to.satisfy io-spec

expect zip io.input, io.output .to.all.satisfy ([input, output]) ->
output is conditional1-calc input
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputが200未満になる旨のテストもあったほうがよさそう


expect io.input.0 .to.equal 5
expect io.input.1 .to.equal -10
expect io.input.2 .to.be.below 200
expect io.input.3 .to.be.below 200

describe 'conditional02 stage' ->
It 'generates 2*5 if 5, 3*6 else if 6, 1 otherwise' ->
io = conditional02.io-generator @random

expect io .to.satisfy io-spec

expect zip io.input, io.output .to.all.satisfy ([input, output]) ->
output is conditional2-calc input

expect io.input.0 .to.equal 5
expect io.input.1 .to.equal 6
expect io.input.2 .to.be.below 200
expect io.input.3 .to.be.below 200

describe 'conditional03 stage' ->
It 'generates two addition in modulo 7' ->
io = conditional03.io-generator @random

expect io .to.satisfy io-spec

expect zip io.input, io.output .to.all.satisfy ([input, output]) ->
output is (input + 2) % 7

expect io.input.0 .to.equal 5
expect io.input.1 .to.equal 3
expect io.input.2 .to.be.below 200
expect io.input.3 .to.be.below 200
expect io.input.4 .to.be.below 200

describe 'factoriol stage' ->
It 'generates factorals' ->
io = factorial.io-generator @random
Expand Down