Selecting most recent value from a variable
in
Programming Questions
•
1 year ago
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 problemsif (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 Rowif (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 Rowif (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 Rowif (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 0if (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 problemsif (grid [1] [1] == 1 && grid [i] [j] != 2) {grid [index] [index2] = 2;}}}}
void display () {
for (int i = 0; i < grid.length; i++) {for (int j = 0; j < grid.length; j++) {if (grid [i] [j] == 0) {fill (noplay);}else if (grid [i] [j] == 1) {fill (player1);}else if (grid [i] [j] == 2) {fill (comp);}
rect (i*100, j*100, width/3, width/3);}}}
1