'Using if statements if Stddraw in java so that you can interact with your code like its a game
int n = 8; // number of rows int m = 8; // number of columns
// Please do yourself a favour and read the StdDraw documentation
// https://introcs.cs.princeton.edu/java/stdlib/javadoc/StdDraw.html
StdDraw.setXscale(-2, m+2);
StdDraw.setYscale(-2, n+2);
StdDraw.setPenColor(StdDraw.BLACK);
// Draw Grid
for (int i = 0; i < m; i++) {
StdDraw.line(i, 0, i, n);
}
for (int i = 0; i < n; i++) {
StdDraw.line(0, i, m, i);
}
StdDraw.line(m,0,m,n);
StdDraw.line(0,n,m,n);
Color colour0 = StdDraw.GRAY;
StdDraw.setPenColor(colour0);
// Draw blocks and the gray blocks that the player will interact with.
for (int i = 0; i < n; i++) {
StdDraw.filledSquare(0.5, i + 0.5, 0.4);
}
// Mouse Example
boolean gameIsRunning = true;
// boolean mouseHack = false; // Ensures that multiple mouse clicks do not occur simultaneously
boolean mouseHack = false;
while (gameIsRunning) {
if (StdDraw.isMousePressed()) {
if (mouseHack) {
continue;
}
int x;
int y;
x = (int) StdDraw.mouseX();
y = (int) StdDraw.mouseY();
Color colour = StdDraw.BLACK;
StdDraw.setPenColor(colour);
StdDraw.square(x + 0.5, y + 0.5, 0.4);
mouseHack = true;
if (StdDraw.hasNextKeyTyped()) { //check for keyboard input
char userInput = 0;
userInput = StdDraw.nextKeyTyped();
userInput = '0';
Color color0 = StdDraw.GREEN;
StdDraw.setPenColor(color0);
}
if (StdDraw.hasNextKeyTyped()) { //check for keyboard input
char userInput1 = 1;
userInput1 = StdDraw.nextKeyTyped();
userInput1 = '1';
Color color1 = StdDraw.YELLOW;
StdDraw.setPenColor(color1);
}
if (StdDraw.hasNextKeyTyped()) { //check for keyboard input
char userInput2 = 2;
userInput2 = StdDraw.nextKeyTyped();
userInput2 = '2';
Color color2 = StdDraw.BLUE;
StdDraw.setPenColor(color2);
}
if (StdDraw.hasNextKeyTyped()) { //check for keyboard input
char userInput3 = 3;
userInput3 = StdDraw.nextKeyTyped();
userInput3 = '3';
Color color3 = StdDraw.RED;
StdDraw.setPenColor(color3);
}
} else {
mouseHack = false;
}
}
}
I want the code to say that if the block is gray (the block is in a "cell") then once the player chooses a color (for example: green or red) then put the color in the block but then also put a gray block in the next "cell". the initial gray blocks are in the first column ("column0"). I have the mouseInput user working already but it works in all cells, it should only work in the gray blocked cells. Please help!!!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
