# Easy question about if and function

**URL:** <https://discourse.processing.org/t/easy-question-about-if-and-function/20205>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [April 27, 2020, 11:41am UTC](https://discourse.processing.org/t/easy-question-about-if-and-function/20205 "2020-04-27T11:41:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thom](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thom](https://discourse.processing.org/u/thom)\
**Post date:** [April 27, 2020, 11:41am UTC](https://discourse.processing.org/t/easy-question-about-if-and-function/20205/1 "2020-04-27T11:41:02Z")

</div>

I’m trying to make a simple tic tac toe game in processing. I don’t understand why the functions don’t follow the if() statements.  
CODE:

```auto
int counter=0;
void setup(){
size(500,500);
background(255);
strokeWeight(15);
line(150,0,150,500);
line(350,0,350,500);
line(0,150,500,150);
line(0,350,500,350);
}
void draw(){
  if(counter%2==0)
  {
    ellipse();
  }
  else{
    triangle();
  }
}
void keyReleased(){
  counter=counter+1;
  println(counter);
}
void ellipse(){
   if (key=='a'){
      
        fill(0);
        ellipse(75,75,50,50);
    }
      if(key=='b'){
       
        fill(0);
        ellipse(250,75,50,50);
      }  
      if(key=='c'){
        
        fill(0);
        ellipse(425,75,50,50);
      }
            if(key=='d'){
       
        fill(0);
        ellipse(75,250,50,50);
      }
      if(key=='e'){
        
        fill(0);
        ellipse(250,250,50,50);
      }
       if(key=='f'){
        
        fill(0);
        ellipse(425,250,50,50);
      }
       if(key=='g'){
        
        fill(0);
        ellipse(75,425,50,50);
      }
      if(key=='h'){
       
        fill(0);
        ellipse(250,425,50,50);
      }
      if(key=='i'){
       
        fill(0);
        ellipse(425,425,50,50);
      }
}
void triangle(){
  if(key=='1'){
        
        fill(0);
        triangle(35,125,115,125,75,25);
      }
      
  if(key=='2'){
        
        fill(0);
        triangle(210,125,290,125,250,25);
      }
      if(key=='3'){
        
        fill(0);
        triangle(385,125,465,125,425,25);
      }
      if(key=='4'){
        
        fill(0);
        triangle(35,300,115,300,75,200);
      }
      if(key=='5'){
       
        fill(0);
        triangle(210,300,290,300,250,200);
      }
      if(key=='6'){
        
        fill(0);
        triangle(385,300,465,300,425,200);
      }
      if(key=='7'){
       
        fill(0);
        triangle(35,475,115,475,75,375);
      }
      if(key=='8'){

        fill(0);
        triangle(210,475,290,475,250,375);
      }
      if(key=='9'){
        
        fill(0);
        triangle(385,475,465,475,425,375);
      }
}

```

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [April 27, 2020, 4:05pm UTC](https://discourse.processing.org/t/easy-question-about-if-and-function/20205/2 "2020-04-27T16:05:11Z")

</div>

Are you using void setup? I didn’t see it, so first put those in then it would be easier.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 27, 2020, 4:09pm UTC](https://discourse.processing.org/t/easy-question-about-if-and-function/20205/3 "2020-04-27T16:09:59Z")

</div>

Hello,

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

**Author:** ![Adi](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@Adi](https://discourse.processing.org/u/Adi)\
**Post date:** [April 27, 2020, 4:36pm UTC](https://discourse.processing.org/t/easy-question-about-if-and-function/20205/4 "2020-04-27T16:36:55Z")

</div>

Yes, I found that helpful.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 27, 2020, 7:57pm UTC](https://discourse.processing.org/t/easy-question-about-if-and-function/20205/6 "2020-04-27T19:57:27Z")

</div>

> [@thom](#):
>
> I don’t understand why the functions don’t follow the if() statements.

I am not sure what you mean.

I copied your code and it runs.

Circles and triangles are shown

It works better when you reset key to 0

```auto
void keyReleased() {
  counter=counter+1;
  key=0; 
  println(counter);
}

```

but still a person can cheat when he presses 2 twice. Then he can make a move when it’s not his turn.

Chrisir

---

<div class="post-metadata">

**Author:** ![thom](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@thom](https://discourse.processing.org/u/thom)\
**Post date:** [April 27, 2020, 10:57pm UTC](https://discourse.processing.org/t/easy-question-about-if-and-function/20205/7 "2020-04-27T22:57:08Z")

</div>

thanks everyone for the answers, now i have solved it.
