Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
coopertim13 committed Mar 18, 2020
0 parents commit 1fb3951
Show file tree
Hide file tree
Showing 8 changed files with 323 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.settings/
bin
.classpath
.project
Binary file added data/Cabin-Regular.ttf
Binary file not shown.
Binary file added data/full_pack_2025.ttf
Binary file not shown.
8 changes: 8 additions & 0 deletions data/stage1.rvb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
A0->C20=water
D0->F3=water
H2->H16=road
H2->S2=road
H2=puppy red
H16=lion blue
S2=bunny red
H3=bunny blue
30 changes: 30 additions & 0 deletions src/Cell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.awt.*;

public class Cell extends Rectangle {
// fields
final static int size = 45;
Color color = Color.WHITE;
boolean computerProvided;

//constructors
public Cell(int x, int y){
super(x,y,size,size);
}

//methods
void paint(Graphics g, Point mousePos){
g.setColor(color);
g.fillRect(x,y,size,size);
g.setColor(Color.BLACK);
g.drawRect(x,y,size,size);
//g.drawString(num+"", x+25, y+35);
}

public boolean contains(Point p){
if (p != null){
return super.contains(p);
} else {
return false;
}
}
}
36 changes: 36 additions & 0 deletions src/CellIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.*;

public class CellIterator implements Iterator<Cell> {
Cell[][] data;
int outer;
int inner;
boolean runOut;

public CellIterator(Cell[][] data){
this.data = data;
outer = 0;
inner = 0;
runOut = false;
}

@Override
public boolean hasNext() {
return !runOut;
}

@Override
public Cell next() {
Cell ret = data[outer][inner];
inner++;
if (inner >= data[outer].length){
inner = 0;
outer++;
if (outer >= data.length){
runOut = true;
}
}

return ret;
}

}
169 changes: 169 additions & 0 deletions src/Grid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.function.Consumer;

class Grid implements Iterable<Cell> {
static int cellAmount = 12;
static int cellSize = 45;
static int gridPadding = 10;
static Cell[][] cells = new Cell[cellAmount][cellAmount];
static int countComplete = 0;

// constructor
public Grid(){
for(int x = 0; x < cells.length; x++){
for(int y = 0; y < cells[x].length; y++){
cells[x][y] = new Cell(position(x), position(y));
}
}
}

private int position(int a) {
return gridPadding + cellSize * a;
}

// methods
public void paint(Graphics g, Point mousePos){
g.setFont(new Font("Cabin-Regular", Font.PLAIN, 20));
doToEachCell( (Cell c) -> c.paint(g, mousePos) );
g.setColor(Color.GREEN);
g.fillRect(0,(gridPadding+(cellSize*cellAmount)+2),560,100);
g.setColor(Color.BLACK);
g.drawString("Solve", 270, (gridPadding+(cellSize*cellAmount)+55));
}

public void mouseClicked(int x, int y) {
for(int i = 0; i < cells.length; i++){
for(int j = 0; j < cells[i].length; j++){
if (cells[i][j].contains(x, y)) {
if(cells[i][j].color == Color.WHITE) {
cells[i][j].color = Color.RED;
countComplete++;
}
else if(cells[i][j].color == Color.RED) {
cells[i][j].color = Color.BLUE;
countComplete++;
}
else {
cells[i][j].color = Color.WHITE;
countComplete--;
}
}
}
}
if(y>=(gridPadding+(cellSize*cellAmount))) {
solve();
}
}

public static void solve() {
countComplete += threes();
//while(countComplete != cellAmount * cellAmount) {
// countComplete+=threes();
//}
}

public static int threes() {
int changes = 0;
ArrayList<Cell> cellAround = new ArrayList<>();
for(int i = 0; i < cells.length; i++) {
for(int j = 0; j < cells[i].length; j++) {
cellAround.clear();
cellAround = cellTwoAround(cells[i][j]);
for(int k = 0; k < cellAround.size(); k++) {
if(cellAround.get(k).color.equals(cells[i][j].color) && !(cells[i][j].color.equals(Color.WHITE))) {
if(cells[i][j].color.equals(Color.RED)) {
cellInbetween(cells[i][j], cellAround.get(k)).color = Color.BLUE;
}
else {
cellInbetween(cells[i][j], cellAround.get(k)).color = Color.RED;
}
changes++;
}
}
}
}
return changes;
}

public static ArrayList<Cell> cellTwoAround(Cell c) {
int i = getIJ(c)[0];
int j = getIJ(c)[1];
ArrayList<Cell> cellsAround = new ArrayList<>();
if(i - 2 >= 0) {
cellsAround.add(cells[i-2][j]);
}
if(i + 2 <= cellAmount - 1) {
cellsAround.add(cells[i+2][j]);
}
if(j - 2 >= 0) {
cellsAround.add(cells[i][j-2]);
}
if(j + 2 <= cellAmount-1) {
cellsAround.add(cells[i][j+2]);
}
return cellsAround;
}

public static Cell cellInbetween(Cell a, Cell b) {
for(int i = 0; i < cells.length; i++) {
for(int j = 0; j < cells.length; j++) {
if(cells[i][j].equals(a)) {
if(i - 2 >= 0) {
if(cells[i-2][j].equals(b)) {
return cells[i-1][j];
}
}
if(i + 2 <= cellAmount - 1) {
if(cells[i+2][j].equals(b)) {
return cells[i+1][j];
}
}
if(j - 2 >= 0) {
if(cells[i][j-2].equals(b)) {
return cells[i][j-1];
}
}
if(j + 2 <= cellAmount - 1) {
if(cells[i][j+2].equals(b)) {
return cells[i][j+1];
}
}
}
}
}
return new Cell(-20,-20);
}

public static int[] getIJ(Cell c) {
for(int i = 0; i < cells.length; i++){
for(int j = 0; j < cells[i].length; j++){
if (cells[i][j].equals(c)) {
int[] a = {i,j};
return a;
}
}
}
return new int[0];
}

/**
* Takes a cell consumer (i.e. a function that has a single `Cell` argument and
* returns `void`) and applies that consumer to each cell in the grid.
* @param func The `Cell` to `void` function to apply at each spot.
*/
public void doToEachCell(Consumer<Cell> func){
for(Cell c : this){
func.accept(c);
}
}

@Override
public CellIterator iterator(){
return new CellIterator(cells);
}

}
76 changes: 76 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.time.Duration;
import java.time.Instant;

class Main extends JFrame {

class App extends JPanel implements MouseListener {

Grid grid;

public App() {
int width = 560;
int height = 650;
setPreferredSize(new Dimension(width, height));
this.addMouseListener(this);
grid = new Grid();
}

@Override
public void paint(Graphics g) {
grid.paint(g, getMousePosition());
}

@Override
public void mouseClicked(MouseEvent e) {
grid.mouseClicked(e.getX(), e.getY());
}

@Override
public void mousePressed(MouseEvent e) {}

@Override
public void mouseReleased(MouseEvent e) {}

@Override
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {}

}

public static void main(String[] args) throws Exception {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new java.io.File("data/Cabin-Regular.ttf")));
Main window = new Main();
window.run();
}

private Main() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
App canvas = new App();
this.setContentPane(canvas);
this.pack();
this.setVisible(true);
}

public void run() {
while (true) {
Instant startTime = Instant.now();
this.repaint();
Instant endTime = Instant.now();
long howLong = Duration.between(startTime, endTime).toMillis();
try{
Thread.sleep(20l - howLong);
} catch (InterruptedException e){
System.out.println("thread was interrupted, but who cares?");
} catch (IllegalArgumentException e){
System.out.println("application can't keep up with framerate");
}
}
}
}

0 comments on commit 1fb3951

Please sign in to comment.