Я создаю игру для лаборатории изображений, в которой игрок выбирает другой цвет из сетки 3 на 3 клеток. Когда я вызываю createGrid, он всегда сканирует условие как ложное при установке gameGrid - я могу сказать, потому что весь экран превращается в diffColor, который изменяется переменной сложности. В конце концов, я хочу, чтобы каждый раз в произвольном месте находилась другая ячейка.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.ImageObserver;
import java.util.ArrayList;
import java.util.Collections;
public class EagleEye extends FlexiblePictureExplorer implements ImageObserver {
private static Picture background = new Picture(900,900);
private final Picture blank = new Picture(300,300);
private Picture samePic;
private Picture diffPic;
public static final boolean DIFFERENT = false;
public static final boolean SAME = true;
//settings
private int gameDimensions = 900;
private int gridDimensions = 3;
private int cellDimensions = gameDimensions / gridDimensions;
private int difficulty = 100;
private final Picture[][] gameGrid = new Picture[gridDimensions][gridDimensions];
private final boolean[][] statusGrid = new boolean[gridDimensions][gridDimensions];
public EagleEye(DigitalPicture Picture) {
super(background);
setTitle("Eagle Eye");
colorSetter();
createGrid();
updateImage();
}
private void createGrid() {
//add same cells
ArrayList<Picture> gameBoard = new ArrayList<>();
for(int i = 0; i < (gridDimensions*gridDimensions) - 1 ;i++) {
gameBoard.add(samePic);
}
//add in the different cell
int diffPicRLocation = (int)(Math.random() * gridDimensions);
gameBoard.add(diffPicRLocation, diffPic);
for (int x = 0; x < gridDimensions; x++) {
for (int y = 0; y < gridDimensions; y++) {
gameGrid[x][y] = gameBoard.remove(0);
if (gameGrid[x][y] == samePic) {
gameGrid[x][y] = samePic;
statusGrid[x][y] = SAME;
}
else {
gameGrid[x][y] = diffPic;
statusGrid[x][y] = DIFFERENT;
}
}
}
}
private void colorSetter() {
samePic = colorBox(blank);
diffPic = changeColor(samePic);
}
private Picture colorBox(Picture blankp) {
int r = (int)(Math.random() * 255 + 1);
int g = (int)(Math.random() * 255 + 1);
int b = (int)(Math.random() * 255 + 1);
Pixel[][] pixels = specGetPixels2D(blankp);
for (Pixel[] rowArray : pixels)
{
for (Pixel pixelObj : rowArray)
{
pixelObj.setRed(r);
pixelObj.setGreen(g);
pixelObj.setBlue(b);
}
}
return blankp;
}
private Picture changeColor(Picture blankp) {
Pixel[][] pixels = specGetPixels2D(blankp);
for (Pixel[] rowArray : pixels)
{
for (Pixel pixelObj : rowArray)
{
pixelObj.setRed(difficulty);
pixelObj.setGreen(difficulty);
pixelObj.setBlue(difficulty);
}
}
return blankp;
}
private Pixel[][] specGetPixels2D(Picture pict) {
int width = pict.getWidth();
int height = pict.getHeight();
Pixel[][] pixelArray = new Pixel[height][width];
for (int row = 0; row < height; row++)
for (int col = 0; col < width; col++)
pixelArray[row][col] = new Pixel(pict,col,row);
return pixelArray;
}
private void updateImage() {
Picture disp = new Picture(gameDimensions, gameDimensions);
Graphics2D graphics = disp.createGraphics();
for (int i = 0; i < gridDimensions; i++) {
for (int j = 0; j < gridDimensions; j++) {
Picture pict;
if (statusGrid[i][j] == DIFFERENT) {
pict = diffPic;
} else {
pict = samePic;
}
// This is used instead of the copy() method for performance
// reasons
graphics.drawImage(pict.getBufferedImage(), cellDimensions * i, cellDimensions * j, this);
}
}
setImage(disp);
setTitle("EagleEye");
}
public void mouseClickedAction(DigitalPicture pict, Pixel pix) {
}
private void endGame() {
}
public static void main(String[] args) {
Picture x = new Picture();
EagleEye game = new EagleEye(x);
}
}