   Math.random()  Arrrrrg!!!!!
  Im having another problem with my Math.random()method(java btw)i have created a randomization function for a tic tac toe game and the whole thing seems ok except when it comes to runtime here is the source code:import java.util.*;public class TicTacToe{public static void main(String[] args){board brd = new board();AI AIntel = new AI();brd.drawBoard();Scanner in = new Scanner(System.in);int placement;int win = 3;do{System.out.println(" 0 | 1 | 2 ");System.out.println("-----------");System.out.println(" 3 | 4 | 5 ");System.out.println("-----------");System.out.println(" 6 | 7 | 8 ");System.out.println("Enter Position to play:");placement = in.nextInt();if (placement >=0 || placement <=8){brd.setSquare(placement, 1);AIntel.calcPlacement(brd);brd.drawBoard();win = brd.testWin();}else{    System.out.println("Invalid Input");}    }while(win == 3);}}class token{public token(int pos, int t){type = t;position = pos;}public int getPos(){return position;}public int getType(){return type;}public void setType(int typ){type = typ;}public void setPos(int ps){position = ps;}private int position;private int type;}class board{public board(){token[] sqrs = new token [9];for(int e = 0;e <=8; e++){sqrs[e] = new token(e+1, 3);}squares = sqrs;}public token getSquare(int pos){return squares[pos];}public void setSquare(int pos, int typ){squares[pos].setType(typ);}public void drawBoard(){for(int i = 0; i <=8; i++){if(squares.getType() == 1){System.out.print(" X ");}else if(squares.getType() == 2){System.out.print(" O ");}else if(squares.getType() == 3){System.out.print("   ");}if ((i+1)%3 == 0 && i != 8){System.out.println();System.out.println("-----------");}else if(squares.getPos() != 9){System.out.print("|");}else{System.out.println();}}}public int testWin(){if(squares[0].getType() == 1 && squares[1].getType() == 1 && squares[2].getType() == 1 ||   squares[3].getType() == 1 && squares[4].getType() == 1 && squares[5].getType() == 1 ||   squares[6].getType() == 1 && squares[7].getType() == 1 && squares[8].getType() == 1 ||   squares[0].getType() == 1 && squares[3].getType() == 1 && squares[6].getType() == 1 ||   squares[1].getType() == 1 && squares[4].getType() == 1 && squares[7].getType() == 1 ||   squares[2].getType() == 1 && squares[5].getType() == 1 && squares[8].getType() == 1 ||   squares[0].getType() == 1 && squares[4].getType() == 1 && squares[8].getType() == 1 ||   squares[2].getType() == 1 && squares[4].getType() == 1 && squares[6].getType() == 1){return 1;}if(squares[0].getType() == 2 && squares[1].getType() == 2 && squares[2].getType() == 2 ||   squares[3].getType() == 2 && squares[4].getType() == 2 && squares[5].getType() == 2 ||   squares[6].getType() == 2 && squares[7].getType() == 2 && squares[8].getType() == 2 ||   squares[0].getType() == 2 && squares[3].getType() == 2 && squares[6].getType() == 2 ||   squares[1].getType() == 2 && squares[4].getType() == 2 && squares[7].getType() == 2 ||   squares[2].getType() == 2 && squares[5].getType() == 2 && squares[8].getType() == 2 ||   squares[0].getType() == 2 && squares[4].getType() == 2 && squares[8].getType() == 2 ||   squares[2].getType() == 2 && squares[4].getType() == 2 && squares[6].getType() == 2){return 2;}else{return 3;}}private token[] squares;}class AI{public AI(){}public void randomPlacement(board brd){int pos;do{    pos = (int)(Math.random()*9);}while(brd.getSquare(pos).getType() != 3 );brd.setSquare(pos, 2);}    public void calcPlacement(board brd)    {int turn = 0;for(int i = 0; i <= 8 && turn == 0; i++){if(i == 3 || i == 6){if(brd.getSquare(i).getType() == 2 && brd.getSquare(i+1).getType() == 2){brd.setSquare(i+2, 2);turn = 1;}else if(brd.getSquare(i).getType() == 2 && brd.getSquare(i+2).getType() == 2) {brd.setSquare(i+1, 2);turn = 1;    }else if(brd.getSquare(i+1).getType() == 2 && brd.getSquare(i+2).getType() == 2){brd.setSquare(i, 2);turn = 1;}else if(brd.getSquare(i).getType() == 1 && brd.getSquare(i+1).getType() == 1){brd.setSquare(i+2, 2);turn = 1;}else if(brd.getSquare(i).getType() == 1 && brd.getSquare(i+2).getType() == 1) {brd.setSquare(i+1, 2);turn = 1;}else if(brd.getSquare(i+1).getType() == 1 && brd.getSquare(i+2).getType() == 1){brd.setSquare(i, 2);turn = 1;}}else if(i == 1 || i == 2){if(brd.getSquare(i).getType() == 2 && brd.getSquare(i+3).getType() == 2){brd.setSquare(i+6, 2);turn = 1;}else if(brd.getSquare(i).getType() == 2 && brd.getSquare(i+6).getType() == 2) {brd.setSquare(i+3, 2);turn = 1;    }else if(brd.getSquare(i+3).getType() == 2 && brd.getSquare(i+6).getType() == 2){brd.setSquare(i, 2);turn = 1;}else if(brd.getSquare(i).getType() == 1 && brd.getSquare(i+3).getType() == 1){brd.setSquare(i+6, 2);turn = 1;}else if(brd.getSquare(i).getType() == 1 && brd.getSquare(i+6).getType() == 1) {brd.setSquare(i+3, 2);turn = 1;}else if(brd.getSquare(i+3).getType() == 1 && brd.getSquare(i+6).getType() == 1){brd.setSquare(i, 2);turn = 1;}}if(i == 0){if(brd.getSquare(0).getType() == 2 && brd.getSquare(4).getType() == 2){brd.setSquare(8, 2);turn = 1;}else if(brd.getSquare(0).getType() == 2 && brd.getSquare(8).getType() == 2) {brd.setSquare(4, 2);turn = 1;    }else if(brd.getSquare(4).getType() == 2 && brd.getSquare(8).getType() == 2){brd.setSquare(0, 2);turn = 1;}else if(brd.getSquare(0).getType() == 1 && brd.getSquare(4).getType() == 1){brd.setSquare(8, 2);turn = 1;}else if(brd.getSquare(0).getType() == 1 && brd.getSquare(8).getType() == 1) {brd.setSquare(4, 2);turn = 1;}else if(brd.getSquare(4).getType() == 1 && brd.getSquare(8).getType() == 1){brd.setSquare(0, 2);turn = 1;}else if(brd.getSquare(0).getType() == 2 && brd.getSquare(i+3).getType() == 2){brd.setSquare(i+6, 2);turn = 1;}else if(brd.getSquare(i).getType() == 2 && brd.getSquare(i+6).getType() == 2) {brd.setSquare(i+3, 2);turn = 1;    }else if(brd.getSquare(i+3).getType() == 2 && brd.getSquare(i+6).getType() == 2){brd.setSquare(i, 2);turn = 1;}else if(brd.getSquare(i).getType() == 1 && brd.getSquare(i+3).getType() == 1){brd.setSquare(i+6, 2);turn = 1;}else if(brd.getSquare(i).getType() == 1 && brd.getSquare(i+6).getType() == 1) {brd.setSquare(i+3, 2);turn = 1;}else if(brd.getSquare(i+3).getType() == 1 && brd.getSquare(i+6).getType() == 1){brd.setSquare(i, 2);turn = 1;}else if(brd.getSquare(2).getType() == 2 && brd.getSquare(4).getType() == 2){brd.setSquare(6, 2);turn = 1;}else if(brd.getSquare(0).getType() == 2 && brd.getSquare(8).getType() == 2) {brd.setSquare(4, 2);turn = 1;}else if(brd.getSquare(5).getType() == 2 && brd.getSquare(8).getType() == 2){brd.setSquare(0, 2);turn = 1;}else if(brd.getSquare(2).getType() == 1 && brd.getSquare(4).getType() == 1){brd.setSquare(6, 2);turn = 1;}else if(brd.getSquare(0).getType() == 1 && brd.getSquare(8).getType() == 1) {brd.setSquare(5, 2);turn = 1;}else if(brd.getSquare(5).getType() == 1 && brd.getSquare(8).getType() == 1){brd.setSquare(0, 2);turn = 1;}}else if(i == 2){if(brd.getSquare(2).getType() == 2 && brd.getSquare(4).getType() == 2){brd.setSquare(6, 2);turn = 1;}else if(brd.getSquare(0).getType() == 2 && brd.getSquare(8).getType() == 2) {brd.setSquare(4, 2);turn = 1;}else if(brd.getSquare(5).getType() == 2 && brd.getSquare(8).getType() == 2){brd.setSquare(0, 2);turn = 1;}else if(brd.getSquare(2).getType() == 1 && brd.getSquare(4).getType() == 1){brd.setSquare(6, 2);turn = 1;}else if(brd.getSquare(0).getType() == 1 && brd.getSquare(8).getType() == 1) {brd.setSquare(5, 2);turn = 1;}else if(brd.getSquare(5).getType() == 1 && brd.getSquare(8).getType() == 1){brd.setSquare(0, 2);turn = 1;}}else{randomPlacement(brd);}}}}please help me understand my mistakes it's the only way to learn and make progressThanks to all who respond,Francis...   
  Nobody understands this?Francis...   ;  What, exactly, is the problem with it? I.e., what behavior do you expect, and what are you getting?   ;Quote:Original post by 14 year oldNobody understands this?Francis...Actually I think nobody would bother studying hundreds lines of code, especially since you're not even mentioning what the problem is.   ;Quote:Original post by Driv3MeFarWhat, exactly, is the problem with it? I.e., what behavior do you expect, and what are you getting?I'm very sorry for not mentioning th problem, I was in a hurry it was the end of my lunch break and I wrote it fast and never rewrote it.Well as you see in my calcPlacement method if there are no wins/ loses opportunities(or anything to play onto(in hard mode))(basically on the first turn. this is the expected output: 0 | 1 | 2----------- 3 | 4 | 5----------- 6 | 7 | 8Enter Selection:1(computer responds here)   | X | -----------   |   | O -----------   |   | but what happens:0 | 1 | 2----------- 3 | 4 | 5----------- 6 | 7 | 8Enter Selection:1(computer responds here) O | X | O----------- O | O | O -----------   | O | Oor something of the sort(randomly fills a whole bunch of squares)   ;  A little more info on this problem:I have made tests and this bug only happens when the randomPlacement() method is used in an if() statement,This is the one bug stopping this game from being finished sop pleae help.Thank you,Francis...   