2014年2月14日金曜日

開発環境

Head First JavaScript ―頭とからだで覚えるJavaScriptの基本( Michael Morrison (著), 豊福 剛 (翻訳)、オライリージャパン)の6章(関数)、自分で考えてみよう(p.281)をDartで考えてみる。

その他参考書籍

自分で考えてみよう(p.281)

コード

sample.dart

import 'dart:html';

void main() {
  find_seats.onClick.listen((MouseEvent) => findSeats());
  int i;
  int m = seats.length * seats[0].length;
  for (i = 0; i < m; i += 1) {
    int n = i;
    ImageElement img = querySelector('#seat$i');
    img.onClick.listen((MouseEvent event) => showSeatStatus(n));
  }
  initSeats();
}

InputElement find_seats = querySelector('#findseats');

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;
  }
}

void showSeatStatus(int seat_num) {
  window.alert('この席は${getSeatStatus(seat_num)}です。');
}

String getSeatStatus(int seat_num){
  if (selected_seat != -1 &&
      (seat_num == selected_seat || seat_num == selected_seat + 1 ||
      seat_num == selected_seat + 2)) {
    return 'あなたの席';
  }
  int l = seats[0].length;
  if (seats[seat_num ~/ l][seat_num % l]){
    return '空席';
  }
  return '満席';
}




0 コメント:

コメントを投稿