# My program isn't doing what I want it to do (x and o game / Tic Tac Toe)

**URL:** <https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516>\
**Category:** Coding Questions\
**Created:** [November 18, 2020, 7:42pm UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516 "2020-11-18T19:42:31Z")\
**Posts on this page:** 9\
**Page:** 4

<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:** [December 30, 2020, 5:52pm UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516/62 "2020-12-30T17:52:31Z")

</div>

Please post your entire code

---

<div class="post-metadata">

**Author:** ![DijaAngel](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@DijaAngel](https://discourse.processing.org/u/DijaAngel)\
**Post date:** [December 30, 2020, 5:55pm UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516/63 "2020-12-30T17:55:25Z")

</div>

the one above is my entire code but i changed my mouseClicked function to showWhoseTurnAnd\_AI\_Manager()

---

<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:** [December 30, 2020, 5:58pm UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516/64 "2020-12-30T17:58:29Z")

</div>

Ar start of mouseClicked say

if (player==‘X’)  
return;

---

<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:** [December 30, 2020, 5:59pm UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516/65 "2020-12-30T17:59:45Z")

</div>

> [@DijaAngel](#):
>
> i changed my mouseClicked function to showWhoseTurnAnd\_AI\_Manager()

I am afraid you need both functions

One is to handle the Human input

One to call moveAI

---

<div class="post-metadata">

**Author:** ![DijaAngel](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@DijaAngel](https://discourse.processing.org/u/DijaAngel)\
**Post date:** [December 30, 2020, 6:22pm UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516/66 "2020-12-30T18:22:02Z")

</div>

```auto
PImage background; // image for background 
char player='0'; // this tells us whose turn it is during the game
char playerAI='X';
int score=0; // the score

//Program screens
final int mainMenu = 0;//defines mainMenu
final int game = 1;//defines game
int state = mainMenu;//sets mainmenu as first screen player sees
int[]x=
  {
  110, 300, 490, 
  110, 300, 490, 
  110, 300, 490
};
int[]y=
  {
  150, 150, 150, 
  300, 300, 300, 
  450, 450, 450
};
char[] clicked =  
  {
  ' ', ' ', ' ', 
  ' ', ' ', ' ', 
  ' ', ' ', ' '
};

void setup() {
  size(600, 600);//sets screen size
  background=loadImage("yo.jpg");//sets picture for background
  strokeWeight(6);//sets the width of the stroke
}

void draw() {
  switch(state) {//this functions switches the state of the program

  case mainMenu:
    runMenu();//sets the screen for the main menu
    break;

  case game:
    runGame();//sets the screen for the game
    break;
  }
}

void runMenu() {
  if (background==null)//sets the condition for which the background changes 
    background(255); //sets background to white
  else 
  background(background);//sets background to image

  fill(0); // black text 
  textSize(32);//sets text size
  text("X and O game", 100, 100);//displays X and O game
  textSize(21);//sets text size
  text("Press 'a' to begin the game", 150, 180);//displays press a to begin the game
  if (key=='a') {//states what happens if a key is pressed
    background(110);//sets background colour
    state=game;//takes player to game
  }
}

void runGame() {
  textSize(32);//sets text size
  fill(0, 102, 153);//determines text colour
  text("X and 0 game", 210, 40);//displays x and o game

  // draw grid 
  stroke(0);//sets the colour of the lines to black
  line(200, 100, 200, 500);//creates the first vertical line
  line(400, 100, 400, 500);//creates the second vertical line
  line(100, 100+400/3, 500, 100+400/3);//draws the first horizontal line
  line(100, 250+400/3, 500, 250+400/3);//draws the second horizontal lin

  if (player=='X') { //states what happens when its player x's turn 
    deleteFooter();
    fill(0, 102, 153);//sets text colour
    textSize(32);//sets text size
    text("It’s X’s turn", 100, 550);//displays its x turn
  } else {//states what happens if its not player x's turn
    deleteFooter(); 
    textSize(32);//sets text size
    fill(0, 102, 153);//sets text colour
    text("It’s 0’s turn", 100, 550);//displays its o's turn
  }
  showWhoseTurnAnd_AI_Manager();
}

void mouseClicked() {
  if (state != game)//this is to make sure we are in the right state
    return; // leave
    if(player=='X')
    return;

  if (player=='X') { //sets the condition for what happens when its player x's turn
    for (int i=0; i<x.length; i++) {
      if (dist(mouseX, mouseY, x[i], y[i])<50) {
        stroke(0, 255, 0); //sets the color for the letter X to green
        line(x[i]+50, y[i]-50, x[i]-50, y[i]+50);//draws the first line of the letter X
        line(x[i]-50, y[i]-50, x[i] +50, y[i] +50);//draws the second line of the letter X
        moveAI();
      }
    }

    player='0';//this statement states that its 0's turn now
  } else {
    for (int i=0; i<x.length; i++) {
      if (dist(mouseX, mouseY, x[i], y[i])<50) {
        stroke(255, 0, 0);//changes the colour of the circle to red
        noFill();//this gives the ellipse just the outline and makes it hollow
        ellipse(x[i], y[i], 100, 100);//draws a circle
        
      }
    }

    player='X';//this statement states that its X's turn now
  }
}

void deleteFooter() {
  // make a rect to delete the old text "It’s 0’s turn"/"It’s X’s turn"
  fill(110);//fills rectangle
  noStroke(); //no stroke
  rect(0, 520, width, 200);//draws a rectangle
}
// AI player 0
// A stupid AI plays a random move on an empty field/cell 

void moveAI() {
  // make a list of empty cells on the board 
  IntList possibleMoves=new IntList(); 

  for (int i = 0; i<clicked.length; i++) {  
    if (clicked[i] == ' ') { 
      possibleMoves.append(i);
    }//if
  }//for

  // Shuffle the list 
  possibleMoves.shuffle(); 

  // Success 
  // Make the move. Since we shuffled the list, we can always take possible move #0 from the list 
  clicked[possibleMoves.get(0)]=playerAI; // make / record move
  player='0'; // change turn
}//func 
//
void showWhoseTurnAnd_AI_Manager() {
  if (player=='X') {
    // display message 
    println("AI thinks");
    fill(0, 255, 0);
    text("AI thinks", 33, height-33);
    moveAI(); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! AI places X here !
  } else {
    // display message 
    fill(0); 
    text("Your move, Player O.", 33, height-33);
  }
}

```

I tried but ai doesn’t play anything

---

<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:** [December 30, 2020, 7:37pm UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516/67 "2020-12-30T19:37:32Z")

</div>

a few errors:

```auto
player='0'; 

```

must be `X`

Also, remember to store Human’s move into clicked as well:

` clicked[i]='0';`

Otherwise the AI could make a move on a cell that is already occupied by ‘0’.

**Remark 1**

But the AI is only to demonstrate how to combine Human player with AI player.

It is only a random “AI”. It figures out which cells of the board are empty and chooses one of the cells randomly (using shuffle) to make the move.

For a smart AI you need a strategy.

For example the AI must see when it can make a move to win.

This means,

- when it finds a line with two X’s, it must make a move in the same line on the empty field to **WIN**.
- when it finds a line with two 0’s, it must make a move in the same line on the empty field to **hinder** the Human to win.
- When he finds an empty cell where two lines cross and both lines have one X and two empty cells, then must take this empty cell to make **a fork**.

A line here is either horizontal, vertical or diagonal line of three cells where a player can win.

These rules can be achieved by count scores for each cell and then make the AI move on the highest scoring cell. That’s what [JoseMY](https://discourse.processing.org/u/JoseMY) discussed above.

**Remark 2**

You need a detector for Win and for Draw/Tie.

I hope you get a full understanding of the Sketch.

My apologies that this is so hard.

Have a happy new year!

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)\
**Post date:** [March 24, 2021, 2:25pm UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516/68 "2021-03-24T14:25:14Z")

</div>

Hi, Chrisr!

I’m glad to see you finally made it!  
I hope you found useful some of my hints, though it seems you finally went your own way.

---

<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 24, 2021, 10:44am UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516/69 "2021-04-24T10:44:54Z")

</div>

I made a 3D Tic tac toe now with an AI based on your rules

I also made a random player and the AI played the random player overnight with 156000 games. It didn’t loose one game.  
(now \>260.000 games but I don’t think that automatically means, the AI plays perfect)

 ![Unbenanqnt](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9e9ac198130fec2fa56796fcb99a87923f7238a0.jpeg)

---

<div class="post-metadata">

**Author:** ![JoseMY](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josemy/32/2016_2.png) [@JoseMY](https://discourse.processing.org/u/JoseMY)\
**Post date:** [April 24, 2021, 3:19pm UTC](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516/70 "2021-04-24T15:19:44Z")

</div>

Great!! I like how you tested the optimal values!

[Previous page](https://discourse.processing.org/t/my-program-isnt-doing-what-i-want-it-to-do-x-and-o-game-tic-tac-toe/25516.md?page=3)
