显示最高分

时间:2014-04-15 20:58:30

标签: c loops if-statement arduino

我有一个简单的程序,有一个循环来创建一个计时器,一个哔声启动游戏并结束游戏,有4个按钮,所以4个玩家。

我有一个按钮点击事件,每次点击按钮时都会记录一次点击我打印了所有玩家点击。

我需要一种方法来判断谁获得了最多的点击次数,这样我就可以点亮那里的按钮。

任何人都知道如何解决这个问题代码如下。

代码

    const int  buttonPin = 7;    // the pin that the pushbutton is attached to
const int  buttonPin2 = 6;    // the pin that the pushbutton is attached to
const int  buttonPin3 = 4;    // the pin that the pushbutton is attached to
const int  buttonPin4 = 3;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

int buttonPushCounter2 = 0;   // counter for the number of button presses
int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button

int buttonPushCounter3 = 0;   // counter for the number of button presses
int buttonState3 = 0;         // current state of the button
int lastButtonState3 = 0;     // previous state of the button

int buttonPushCounter4 = 0;   // counter for the number of button presses
int buttonState4 = 0;         // current state of the button
int lastButtonState4 = 0;     // previous state of the button

boolean player1 = false;
boolean player2 = false;
boolean player3 = false;
boolean player4 = false;


int speakerOut = 5;
  int a = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // read the pushbutton input pin:

   a ++;
  Serial.println(a);
    analogWrite(speakerOut, NULL);

  if(a > 50 && a < 300){
  analogWrite(speakerOut, 200);
  }

  if(a <= 49){
    analogWrite(speakerOut, NULL);
  }

  if(a >= 300 && a <= 2499){
      analogWrite(speakerOut, NULL);
  }

if(a > 300 && a < 2500){

  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) 
 {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button1 pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
   if (buttonState2 != lastButtonState2) 
 {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter2++;
      Serial.println("on");
      Serial.print("number of button2 pushes:  ");
      Serial.println(buttonPushCounter2);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  if (buttonState3 != lastButtonState3) 
 {
    // if the state has changed, increment the counter
    if (buttonState3 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter3++;
      Serial.println("on");
      Serial.print("number of button3 pushes:  ");
      Serial.println(buttonPushCounter3);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  if (buttonState4 != lastButtonState4) 
 {
    // if the state has changed, increment the counter
    if (buttonState4 == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter4++;
      Serial.println("on");
      Serial.print("number of button4 pushes:  ");
      Serial.println(buttonPushCounter4);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;
  lastButtonState2 = buttonState2;
  lastButtonState3 = buttonState3;
  lastButtonState4 = buttonState4;

}

if(a > 2500){
    analogWrite(speakerOut, 200);

}

if (a > 2700){
      analogWrite(speakerOut, NULL);

// below is a print out of click by each player
    Serial.print("player 1 clicks = ");
    Serial.println(buttonPushCounter);

    Serial.print("player 2 clicks = ");
    Serial.println(buttonPushCounter2);

    Serial.print("player 3 clicks = ");
    Serial.println(buttonPushCounter3);

    Serial.print("player 4 clicks = ");
    Serial.println(buttonPushCounter4);

   // if(buttonPushCounter > buttonPushCounter2 || buttonPushCounter > buttonPushCounter3 || buttonPushCounter > buttonPushCounter4){
   //   player1 = true;
   // }
    //if(buttonPushCounter2 > buttonPushCounter || ..... ){
    //  player2 = true;
    //}

   // Serial.print("The two players with the highest scores are player... ");   
}
}

所以我需要的是一种告诉谁获得最多点击次数的方法

2 个答案:

答案 0 :(得分:2)

使用qsort()

 #include<stdlib.h>

 typedef struct {
   int Player;
   int Score;
 } PS_T;

 int compar(const void *va, const void *vb) {
   PS_T *ia = (PS_T *) va;
   PS_T *ib = (PS_T *) vb;
   return (ia->Score >= ib->Score) - (ib->Score >= ia->Score);
 }

 // Return true if Silver beat Bronze (detect ties after 2nd place.)
 int Sort4(int *Gold, int *Silver, 
     int Score1, int Score2, int Score3, int Score4) {
   PS_T PS[4] = { { 1, Score1 }, { 2, Score2 }, { 3, Score3} , {4, Score4} };
   qsort(PS, 4, sizeof PS[0], compar);
   *Gold = PS[0].Player;
   *Silver = PS[1].Player;
   return PS[1].Score > PS[2].Score;
 }

 ...
 int First;
 int Second;
 Sort4(&First, &Second, buttonPushCounter, buttonPushCounter2, 
   buttonPushCounter3, buttonPushCounter4);

答案 1 :(得分:1)

你可以像这样定义一个宏:

#define MAX(a,b) ((a) > (b) ? (a) : (b)) 

(最多两个值)

(或其他任何计算最大值的东西) 然后用它来获得最好的分数

int bestScore = MAX(MAX(buttonPushCounter,buttonPushCounter2),MAX(buttonPushCounter3,buttonPushCounter4));

并最终使用该最佳分数来获得相应的玩家,使用简单的switch case或其他东西(由您决定如何做)这样:

player1 = (bestScore==buttonPushCounter); // player1=true if the scores match etc...
player2 = (bestScore==buttonPushCounter2);
player3 = (bestScore==buttonPushCounter3);
player4 = (bestScore==buttonPushCounter4);

这可能不是最优雅的方式,但是你可以看到是否有多个获胜者,比如两个拥有相同高分的玩家,以及&#34;显示&#34;所有这些。