Homework 13 - james-bern/CS136 GitHub Wiki
- You can use Cow.java (✨ grab a fresh copy!--it has a bugfix you may need) or any of the past homework, but you do not have to.
- You must get permission beforehand if you want to use a language other than Java.
- You must get permission beforehand if you want to use any external code other than past homeworks and Cow.java.
- You must get permission beforehand if you want to work with a partner.
- Note: Your project will have to be twice as good because there are two of you. 🙂👍
You may do your final project on whatever you like, provided you can answer the following questions.
- What is the title of my project?
- What data structures will I use? Note: Arrays count.
- What is the game/app that I am proposing? Who or what lives in it? What does it do? How does it feel?
- Will the viewer/player interact with my project? How so?
- Does Jim think my project is doable? What is my fallback plan if my project ends up being harder/more time-consuming than I expect? What extensions can I do if my project ends up being easier/less time-consuming than I expect?
- What is the very first thing I will implement? (Drawing "the data" is usually a good first step.)
- Woo!-doku
- 2D array of ints to represent the board. (
int[][] board = new int[9][9];
) - A color sudoku board, that does a happy dance when you solve it.
- Click to select cells. Type numbers on the keyboard to fill in numbers.
- Yes! And you can write a sudoku solver or automatic board generation if you have extra time!
- Store a board I found on the internet as a 2D array (
-1
's for empty cells) and draw it to the Terminal usingSystem.out.println
.
This sets up Cow to have the lower left corner of the window be
👀
class HW13 extends App {
// NxN grid
// each grid cell is 1 unit long (see main())
static int N = 16;
void setup() {
}
void loop() {
}
public static void main(String[] arguments) {
App app = new HW13();
app.setWindowBackgroundColor(Vector3.white);
// // the units of your game
// make world an N x N square
app.setWindowSizeInWorldUnits(N, N);
// put world origin in lower left corner of the screen
app.setWindowCenterInWorldUnits(N / 2.0, N / 2.0);
// // NOT the units of your game
// how much of your screen the window takes up
app.setWindowHeightInPixels(512);
// where the window spawns on your screen
app.setWindowTopLeftCornerInPixels(16, 16);
app.run();
}
}
Here we ask if the mouse position is inside a rectangle.
👀
class HW13 extends App {
static int N = 16;
void setup() {
}
void loop() {
Vector2 lowerLeftCorner = new Vector2(0.5, 0.5);
Vector2 upperRightCorner = new Vector2(1.7, 2.8);
drawCornerRectangle(lowerLeftCorner, upperRightCorner, Vector3.blue);
boolean isMouseInsideOfTheRectangle =
(lowerLeftCorner.x < mousePosition.x) && (mousePosition.x < upperRightCorner.x)
&& (lowerLeftCorner.y < mousePosition.y) && (mousePosition.y < upperRightCorner.y);
Vector3 circleColor;
if (isMouseInsideOfTheRectangle) {
circleColor = Vector3.red;
} else {
circleColor = Vector3.green;
}
drawCircle(mousePosition, 0.1, circleColor);
}
public static void main(String[] arguments) {
App app = new HW13();
app.setWindowBackgroundColor(Vector3.white);
app.setWindowSizeInWorldUnits(N, N);
app.setWindowCenterInWorldUnits(N / 2.0, N / 2.0);
app.setWindowHeightInPixels(512);
app.setWindowTopLeftCornerInPixels(16, 16);
app.run();
}
}
Here we draw a grid and highlight the cell that the mouse is inside of.
Because the grid is an integer grid (the corners of the cells are int
'ss.
The upper right corner is the lower left corner plus
👀
class HW13 extends App {
static int N = 16;
void setup() {
}
void loop() {
// highlight square
int x = (int) mousePosition.x;
int y = (int) mousePosition.y;
drawCornerRectangle(new Vector2(x, y), new Vector2(x + 1, y + 1), Vector3.yellow);
// draw grid
for (int i = 0; i <= N; ++i) {
drawLine(new Vector2(0.0, i), new Vector2(N, i), Vector3.black);
drawLine(new Vector2(i, 0.0), new Vector2(i, N), Vector3.black);
}
}
public static void main(String[] arguments) {
App app = new HW13();
app.setWindowBackgroundColor(Vector3.white);
app.setWindowSizeInWorldUnits(N, N);
app.setWindowCenterInWorldUnits(N / 2.0, N / 2.0);
app.setWindowHeightInPixels(512);
app.setWindowTopLeftCornerInPixels(16, 16);
app.run();
}
}
Here we simulate the physics of a cannonball.
If the position of the ball is
Note: If we consider the state to be
👀
class HW13 extends App {
static int N = 16;
Vector2 s; // position
Vector2 v; // velocity
Vector2 a; // acceleration
double h; // timestep ("delta t")
void setup() {
s = new Vector2(0.0, 0.0);
v = new Vector2(2.0, 4.0);
a = new Vector2(0.0, -1.0);
h = 0.1;
}
void loop() {
// explicit euler update
v = v.plus(a.times(h)); // v += h * a;
s = s.plus(v.times(h)); // s += h * v;
drawCircle(s, 0.2, Vector3.black);
}
public static void main(String[] arguments) {
App app = new HW13();
app.setWindowBackgroundColor(Vector3.white);
app.setWindowSizeInWorldUnits(N, N);
app.setWindowCenterInWorldUnits(N / 2.0, N / 2.0);
app.setWindowHeightInPixels(512);
app.setWindowTopLeftCornerInPixels(16, 16);
app.run();
}
}
Here we store the color of each cell in the setup()
, we map the row and column of each cell to a double between Vector3
static method rainbowSwirl
to turn that double into a color.
👀
class HW13 extends App {
static int N = 16;
Vector3 gradient[][];
void setup() {
gradient = new Vector3[N][N];
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
double t = (double) (N * j + i) / (N * N);
gradient[i][j] = Vector3.rainbowSwirl(t);
}
}
}
void loop() {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
// NOTE: We have to cast the numerator to a double, otherwise an int divided by an int is an int!
double t = (double) (N * i + j) / (N * N);
drawCornerRectangle(new Vector2(i, j), new Vector2(i + 1, j + 1), gradient[i][j]);
}
}
}
public static void main(String[] arguments) {
App app = new HW13();
app.setWindowBackgroundColor(Vector3.white);
app.setWindowSizeInWorldUnits(N, N);
app.setWindowCenterInWorldUnits(N / 2.0, N / 2.0);
app.setWindowHeightInPixels(512);
app.setWindowTopLeftCornerInPixels(16, 16);
app.run();
}
}