Random actions in JAVA -
Random actions in JAVA -
i'm writing first own chess game in java. code works 2 players, manually playing keyboard, , expand player against computer game.
my problem is, i've never dealt random actions. method moving piece on board, needs recieve 4 inputs: 2 ints piece location, , 2 ints destination on board.
choosing wrong inputs ok since movepiece method checks if ints not out of board's bounds, reach piece of current player's color, there's piece in coordination , not empty, etc.
but how these 4 ints randomly? , how create them closest possible "real" inputs (so dont spend alot of time disqualifying bad inputs) ?
thanks alot :d
java provides @ to the lowest degree 2 options generate random values:
http://docs.oracle.com/javase/6/docs/api/java/util/random.html http://docs.oracle.com/javase/6/docs/api/java/lang/math.html#random()
e.g. random int values bounded 4:
int x = new random().nextint(4); int y = (int) (math.random() * 4); system.out.println(x + " " + y); >> 2 3 java random chess
Comments
Post a Comment