I am currently in the process of making a tic tac toe machine. I have variables called 'index' and 'index2' that tell me what squares have the value 0 (not selected) and I want to apply them so the computer selects a square that is not selected during its turn. However because 'index' and 'index2' are always updating the computer selects all the squares that are not selected!
void computerlogic (){
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
//indicates the squares with a value of 0
if (mousePressed == true) {
if ( grid [i] [j] == 0) {
index = i;
index2 = j;
}
}
//Tells the computer to choose only squares that are not selected
//This is where I am having problems
if (grid [1] [1] == 1 && grid [i] [j] != 2) {
grid [index] [index2] = 2;
}
}
}
}
I want the computer to only choose once per turn however I have no idea how to do this. Also, for bonus points, can someone tell me how to shorten the "mouse press" code? But thats not as important as it works.
Thanks!
All the code:
int cols = 3;
int rows = 3;
int index = -1;
int index2 = -1;
int [] [] grid = new int [cols] [rows];
color noplay = color (255);
color player1 = color(255, 0, 0);
color comp = color(0, 255, 0);
void setup () {
size (300, 300);
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
grid [i] [j] = 0;
}
}
}
void draw () {
mousepress (mouseX, mouseY);
computerlogic();
display ();
}
void mousepress (int mX, int mY) {
//First Row
if (mousePressed == true) {
if (mX < width/3 && mY < width/3) {
grid [0] [0] = 1;
}
}
if (mousePressed == true) {
if (mX > width/3 && mY < width/3 && mX < 2*(width/3)) {
grid [1] [0] = 1;
}
}
if (mousePressed == true) {
if (mY < width/3 && mX > 2*(width/3)) {
grid [2] [0] = 1;
}
}
//Second Row
if (mousePressed == true) {
if (mX < width/3 && mY < 2*(width/3) && mY > width/3) {
grid [0] [1] = 1;
}
}
if (mousePressed == true) {
if (mX > width/3 && mY < 2*(width/3) && mY > width/3 && mX < 2*(width/3)) {
grid [1] [1] = 1;
}
}
if (mousePressed == true) {
if (mX > 2*(width/3) && mY > width/3 && mY < 2*(width/3)) {
grid [2] [1] = 1;
}
}
//Third Row
if (mousePressed == true) {
if (mX <width/3 && mY > 2*(width/3)) {
grid [0] [2] = 1;
}
}
if (mousePressed == true) {
if (mX > width/3 && mY > 2*(width/3) && mX < 2*(width/3)) {
grid [1] [2] = 1;
}
}
if (mousePressed == true) {
if (mX > 2*(width/3) && mY > 2*(width/3 )) {
grid [2] [2] = 1;
}
}
}
void computerlogic () {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid.length; j++) {
//indicates the squares with a value of 0
if (mousePressed == true) {
if ( grid [i] [j] == 0) {
index = i;
index2 = j;
}
}
//Tells the computer to choose only squares that are not selected
Im trying to make a ball bounce when it hits the bottom of the screen. I want it to bounce as if it were a real ball, with every bounce covering less height than the one before. I've already programmed the gravity, its just the bounce which im finding difficult.
Here is the code.
int x = 200;
int y = 50;
int speed = 0;
int grav = 1 ;
//////////////////////
void setup() {
size (400,800);
smooth();
noStroke();
}
/////////////////////
void draw() {
background (0);
ellipse (x,y,30,30);
//This is where it makes gravity plus the ellpse move
y = y + speed;
speed = speed + grav;
//This is the if statement im stuck on. Regarding the bounce.
//If i leave it with speed = - speed then it bounces and comes
hi, im 14 and have recently got involved in processing.
I started with processing by messing around and creating stupid interactive games where you draw circles and they get bigger or other stupid things like that, but a few days ago i started on a big project. To make a tic tac toe machine, payer vs computer. After reading some of "Learning Processing" by Daniel Shiffman i knew the basics of Case and Swich so i thought it was going to be pretty much easy. However i realized that my method of writing down every possible combination of a tic tac toe game was way too long and that their must be a better way to do it.
So ive come to ask help on my tic tac toe game. Ive already done alot of the work and would appreciate any help even if it means starting all over again.
I will copy and paste some of the logic of the first square to give you an idea of how it works.