And then draw the X and O at the position of the center of the 9 fields (and not at position of the mouse directly as we do now)
(because at the moment we can draw the X and O anywhere)
Detect a Draw/Tie and a Win
You can count the moves and when there a 9 moves, the board is full and we have a Draw/Tie.
When you store which of the fields have been clicked by the players, you could check whether one player won the game. That means 3 X in one row, or 3 0. You can ask me more about this later.
char player=‘X’;
void setup(){
size(600,600);
background(255);//sets the colour of the background to white
strokeWeight(6);//sets the width of the stroke
noFill();
}
void draw(){
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
}
void mouseClicked(){
if(player==‘X’){
stroke(0,255,0);//sets the variable X to green
line(mouseX+50,mouseY-50,mouseX-50,mouseY+50);
line(mouseX+50,mouseY-50,mouseX+50,mouseY+50);
line(100,100+400/3,500,100+400/3);
line(100,100+400/3,500,100+400/3);
player='0';
}else{
player=‘X’;
ellipse(mouseX,mouseY,random(10),random(30));//draws a circle
You can always test your code and check if you are happy with how it runs.
Then change the line and run again.
Check if you are happy with how it runs, change other lines again.
I did this for you now, but it’s a good practice.
The grid
This belongs in draw() where you draw the lines / grid.
Actually both lines are the same. You need to change the values of the second line.
They must be parallel under each other (like this = ) to form the grid.
The circles:
ellipse(mouseX, mouseY, random(10), random(30));//draws a circle
I would suggest a minimal diameter that is always there and you add a random value to it.
ellipse(mouseX, mouseY, 40+random(10), 40+random(30));//draws a circle
Normally we use O (like in the word Open) and not 0 (zero) for the player.
Hi!
MouseX and MouseY are linear values. You need some “scalar” values like:
GridSize=100;
MX= int(MouseX/GridSize)*GridSize;
// (MX IS NOW A MULTIPLE OF 100)
Then use MX instead of MouseX, so you cant make an X on (51,51) and another one in (52,52).
Also I suggest you to make a matrix with X and O’s and use it internally to calculate winner positions.
The rule I use to use is:
For player X, For each cell, for each line
Line has 2 X= Value is +16
Line has 2 O= Value is +8
Line has 1 X= Value is +4
Line has 2 empty lines= Value is +1
In my understanding this is for the AI to decide which move to make. (when you play against the computer)
It assumes that the AI is X, so AI starts the game (because you wrote “For player X”).
I programmed this now, although I am not the OP. It was hard!
It’s fascinating, so thank you!
Remarks
I think you really have to add up the scores like you said, so it’s really the old value plus 8 etc. for every move (and not just = 8). Is that correct?
When you say for example: “Line has 2 X”: you assume that the remaining field in that line is empty (that’s not always the case). We have to check this additionally. Is that correct?
Also, often there are different moves for X that score the same. I think when X begins, he should go in the middle (field 5). When he has the choice he should go to a corner. Is that correct?
Asking your question, the center square should sum 8 at the beginning of the game (4 empty lines). At this early stage, the corners will add up to 6 and the middle of the sides sould sum.up to 4:
646
484
646
Then, X is at center.
Scores for Y will be:
202
0X0
202
So Y puts its mark in one of the corners.
I think my original algorithm gave also negative values for some positions, but I don’t remember clearly.
_________ edit end ____________
Of course, you can make another AI player for O, replacing all 'X’s with 'O’s in the algorithm I sent you.
I programmed AI tic-tac-toes in old-fashioned basic and in excel before. But I should confess I’m not good at playing it!