2012-03-12 4 views
0

私は、回転式電話機とarduinoキットを使って、自動化された電話メニューに基づいたゲームを作成するために、私のクラスの1つの経験デザインプロジェクトに取り組んでいます。ロータリーダイヤルからのシリアル入力がarduinoを介して実行されているので、今はメニューを書き込む処理を使用しています。プロンプト入力メニューを作るためにネストされたifまたはcaseを書く方法は?

私は行動の概要を持っていて、if then文を書くようになっていますが、今は大文字と小文字の区別がつきました。

私は全く新しいですが、クラスで多くのことを学んでいます。

私の質問は、どのように連続したセットのネストされたif/thenステートメントまたはユースケースを使用して、一連のプロンプトと入力を切り替えるのですか?ここで

は、これまでの私のスケッチです:

import processing.serial.*; 

Serial port; // Create object from Serial class 
float val; // Data received from the serial port 

boolean task1prompted; 
boolean task1; 
boolean task2; 
boolean dialed; 

PFont font; 

void setup() { 
    size(800, 400); 
    background(0, 0, 0); 
    smooth(); 
    // IMPORTANT NOTE: 
    // The first serial port retrieved by Serial.list() 
    // should be your Arduino. If not, uncomment the next 
    // line by deleting the // before it. Run the sketch 
    // again to see a list of serial ports. Then, change 
    // the 0 in between [ and ] to the number of the port 
    // that your Arduino is connected to. 
    //println(Serial.list()); 
    String arduinoPort = Serial.list()[0]; 
    port = new Serial(this, arduinoPort, 9600); 

    task1 = false; 
    task2 = false; 
    task1prompted = false; 

    font = createFont("Arial", 32); 
    textFont(font, 32); 
    textAlign(CENTER); 
} 

void draw() { 
    if (port.available() > 0) { // If data is available, 
     val = port.read();  // read it and store it in val 
     if (val >= 48 && val <= 57) { 
      val = map(val, 48, 57, 0, 9); // Convert the value 
     } 
     println(val); 
    } 

    if (val == 97) { 
     println("dialing"); 
    } 

    if (val == 98){ 
     println("dialed"); 
     dialed = true; 
    } 

    /// switch will activate the task1 variable. 
    // Play sound file for the prompt. 
    if (task1prompted == false){ 
     delay(1000); 
     println("for spanish, press one. for french, press 2..."); 
     task1prompted = true; 
    } 

    task1 = true; 

    if (task1 == true && dialed == true) { 
     ///play sound file 

     if (val == 5) { 
      println("Thank you for playing... Blah blah next prompt."); 
      dialed = true; 
      task1=false; 
      task2=true; 
     } else 
      if (val != 5) { 
      println("We're sorry, all of our international operators are busy"); 
      task1 = true; 
      task2 = false; 
      dialed = false; 
     } 

    } 
    else 
     if (task2 == true){ 
      delay(1000); 
      println("task2 start"); 
     } 
} 

私のインストラクターは、はるかにこれを取得するために私を助けて、私は次のタスク/プロンプトに起こって維持する方法についての答えを精練されています。ケースとスイッチを使う方が簡単でしょうか?私はif文を入れ子にしていますか?

次のようにまあ、私はちょうどスケッチとケースでこれを試してみたコマンド:

/// Switch will activate the task1 variable. 
    // Play sound file for the prompt. 
    if (task1prompted == false){ 
     delay(1000); 
     println("for spanish, press one. for french, press 2..."); 
     task1prompted = true; 
    } 

    task1 = true; 

    if (task1 == true && dialed == true) { 
     ///Play sound file 

     int lang = (int)(val+0); 

     switch(lang) { 
      case 1: 
      case 2: 
      case 3: 
      case 4: 
       println("sorry no international operators"); // If 1-4 go back to choices 
       task1 = true; 
       task2 = false; 
       dialed = false; 
       break; 
      case 5: 
       println("thank you, move to next prompt"); // If 5 go to next prompt 
       task1=false; 
       task2=true; 
       dialed = true; 
       break; 
      case 6: 
      case 7: 
      case 8: 
      case 9: 
      case 0: 
       println("not a valid option, you lose"); // If not 1-5 go back to beginning 
       task1=false; 
       task2=false; 
       dialed = true; 
       break; 
     } 

     if (task2prompted == false){ 
      delay(1000); 
      println("please listen while we test the line"); 
      task2prompted = true; 
     } 

     task2 = true; 

     if (task2 == true && dialed == true) { 
     } ///Play sound file 

     int tone = (int)(val+0); 

     switch(tone) { 
      case 1: 
      case 2: 
      case 3: 
      case 5: 
      case 6: 
      case 7: 
      case 8: 
      case 9: 
      case 0: 
      println("not a valid connection, you lose"); // If not 4 go back to beginning 
      task2 = false; 
      task3 = false; 
      dialed = false; 
      break; 
      case 4: 
      println("thank you, move to next prompt"); // If 4 move to next prompt 
      task2=false; 
      task3=true; 
      dialed = true; 
      break; 
     } 
    } 
} 

私は、これはレベルではなく、すべてが同時に起こるがあることを確認してくださいする方法についてはまだ混乱しています。

答えて

0

finite state machinesを参照してください。これは、イベント駆動型ユーザーインターフェイスを処理するためのかなり一般的なアプローチです。

0

私はあなたの質問が何であるか全くわからないが、おそらく、ここで何かがちょうどあなたがcase文で

探しているものを明確にされていない場合、あなたはすべてのためのケースを作る必要はありません、それにお答えします単一の出力が発生する可能性があります。それが最後であるため、デフォルトの場合は休憩を必要としない

switch(tone) { 
     case 4: 
     println("thank you, move to next prompt"); // If 4 move to next prompt 
     task2=false; 
     task3=true; 
     dialed = true; 
     break; 
     default: 
     println("not a valid connection, you lose"); // If not 4 go back to beginning 
     task2 = false; 
     task3 = false; 
     dialed = false; 
    } 

:あなたはそれを行うことを避けることができる方法は、default文に

例を使用することです。しかし、本質的にそれ以外のものが当たっていなければ、すべての場合をキャッチすることです。

また

if (val == 97) { 
    println("dialing"); 
} 

if (val == 98){ 
    println("dialed"); 
    dialed = true; 
} 

上記のコードの一部で、両方を通じて1は

if (val == 97) { 
    println("dialing"); 
} 

else if (val == 98){ 
    println("dialed"); 
    dialed = true; 
} 
正しいかどうかを確認する必要はありませために「あれば、他を」使用することをお勧めします
関連する問題