開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
Head First JavaScript ―頭とからだで覚えるJavaScriptの基本( Michael Morrison (著), 豊福 剛 (翻訳)、オライリージャパン)の6章(関数)、自分で考えてみよう(p.257)をDartで考えてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
自分で考えてみよう(p.257)
コード
sample.dart
import 'dart:html';
void main(){
initSeats();
run.onClick.listen((MouseEvent event) => findSeats());
}
ButtonElement run = querySelector('#run_dart');
String avail = 'http://farm8.staticflickr.com/7433/12430328593_556792b1d4_o.png';
String unavail = 'http://farm4.staticflickr.com/3763/12430660514_4537e17d44_o.png';
String select = 'http://farm4.staticflickr.com/3713/12430175965_6ac91a482f_o.png';
List<List<bool>> seats = [[false, true, false, true, true, true, false, true, false],
[false, true, false, false, true, false, true, true, true],
[true, true, true, true, true, true, false, true, false],
[true, true, true, false, true, false, false, true, false]];
void setSeat(int n, String src, String alt) {
ImageElement seat = querySelector('#seat$n');
seat.src = src;
seat.alt = alt;
}
void initSeats(){
int i;
int max_i = seats.length;
for (i = 0; i < max_i; i += 1) {
int j;
int max_j = seats[i].length;
for (j = 0; j < max_j; j += 1){
int n = i * max_j + j;
if (seats[i][j]) {
setSeat(n, avail, 'Available');
} else {
setSeat(n, unavail, 'Unavailable');
}
}
}
}
int selected_seat = -1;
void findSeats() {
bool finished = false;
if (selected_seat >= 0) {
selected_seat = -1;
initSeats();
}
int i = 0;
int max_i = seats.length;
while (i < max_i && !finished) {
int j;
int max_j = seats[i].length;
for (j = 0; j < max_j - 2; j += 1) {
if (seats[i][j] && seats[i][j + 1] && seats[i][j + 2]) {
selected_seat = i * max_j + j;
setSeat(selected_seat, select, 'Your');
setSeat(selected_seat + 1, select, 'Your');
setSeat(selected_seat + 2, select, 'Your');
bool accept = window.confirm('${i + 1}行の座席${j + 1}列から${j + 3}列が' +
'空いています。予約しますか?');
if (accept) {
finished = true;
break;
} else {
setSeat(selected_seat, avail, 'Available');
setSeat(selected_seat + 1, avail, 'Available');
setSeat(selected_seat + 2, avail, 'Available');
selected_seat = -1;
}
}
}
i += 1;
}
}
0 コメント:
コメントを投稿