From 49635ef23e3e599cb4b72bd74f924c8c47d4f5eb Mon Sep 17 00:00:00 2001 From: Jay Boogaert Date: Thu, 3 Oct 2024 13:58:50 -0400 Subject: [PATCH] fixed issue --- src/com/zetcode/Board.java | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/com/zetcode/Board.java b/src/com/zetcode/Board.java index e81c4e9..d637929 100644 --- a/src/com/zetcode/Board.java +++ b/src/com/zetcode/Board.java @@ -189,12 +189,37 @@ private void checkCollision() { } private void locateApple() { - - int r = (int) (Math.random() * RAND_POS); - apple_x = ((r * DOT_SIZE)); - - r = (int) (Math.random() * RAND_POS); - apple_y = ((r * DOT_SIZE)); + boolean goodSquare = false; + while (true){ + int r = (int) (Math.random() * RAND_POS); + apple_x = ((r * DOT_SIZE)); + for (int z = 0; z < dots; z++) { + if (x[z] == apple_x){ + goodSquare = false; + break; + } + goodSquare = true; + } + if (goodSquare){ + break; + } + } + goodSquare = true; + while (true){ + goodSquare = false; + int r = (int) (Math.random() * RAND_POS); + apple_y = ((r * DOT_SIZE)); + for (int z = 0; z < dots; z++) { + if (y[z] == apple_y){ + goodSquare = false; + break; + } + goodSquare = true; + } + if (goodSquare){ + break; + } + } } @Override