Created
October 26, 2018 22:23
-
-
Save feehe21/09e2d7ad6504547d95b6d1e269bd41de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.WindowConstants; | |
import java.awt.Dimension; | |
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Point; | |
import java.awt.event.MouseListener; | |
import java.awt.event.MouseEvent; | |
public class DrawGrid { | |
private JFrame frame; | |
public DrawGrid() { | |
frame = new JFrame("DrawGrid"); | |
frame.setSize(600, 400); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.setPreferredSize(frame.getSize()); | |
frame.add(new MultiDraw(frame.getSize())); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public static void main(String... argv) { | |
new DrawGrid(); | |
} | |
public static class MultiDraw extends JPanel implements MouseListener { | |
int startX = 0; | |
int startY = 0; | |
int side = 40; | |
int player = 0; | |
String playerColor; | |
int xMatch = 0; | |
int yMatch = 0; | |
boolean notEnd = true; | |
Color[][] grid = new Color[8][8]; | |
public MultiDraw(Dimension dimension) { | |
setSize(dimension); | |
setPreferredSize(dimension); | |
addMouseListener(this); | |
//1. initialize array here | |
int x = 0; | |
for (int row = 0; row < grid.length; row++) { | |
for (int col = 0; col < grid[0].length; col++) { | |
if(row % 2 == 0){ | |
if(col % 2 == 0){ | |
grid[row][col] = Color.white; | |
}else{ | |
grid[row][col] = Color.black; | |
} | |
}else{ | |
if(col % 2 == 0){ | |
grid[row][col] = Color.black; | |
}else{ | |
grid[row][col] = Color.white; | |
} | |
} | |
} | |
} | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
Graphics2D g2 = (Graphics2D)g; | |
Dimension d = getSize(); | |
startX = 0; | |
startY = 0; | |
if(notEnd){ | |
for (int row = 0; row < grid.length; row++) { | |
for (int col = 0; col < grid[0].length; col++) { | |
g2.setColor(grid[row][col]); | |
g2.fillRect(startX, startY, side, side); | |
startX += side; | |
} | |
startY += side; | |
startX = 0; | |
} | |
} | |
//2) draw grid here | |
//g2.setColor(); | |
//g2.fillRect(); | |
} | |
public void mousePressed(MouseEvent e) { | |
int x = e.getX() / side; | |
//int y = e.getY() / side; | |
//System.out.println(x + " " + y); | |
int y = grid.length - 1; | |
for(int i = grid.length - 1; i >= 0; i--){ | |
if(!grid[i][x].equals(Color.red) && !grid[i][x].equals(Color.blue)){ | |
if(player%2 == 0){ | |
grid[i][x] = Color.red; | |
playerColor = "Red"; | |
}else{ | |
grid[i][x] = Color.blue; | |
playerColor = "Blue"; | |
} | |
if(win(x, i) == true && four(x, i) == true){ | |
System.out.println(playerColor + " wins!"); | |
notEnd = false; | |
//System.exit(0); | |
} | |
player ++; | |
i = -1; | |
}else if(i == 0){ | |
System.out.println("That column is full. Try somewhere else"); | |
} | |
} | |
/*if(grid[grid.length - 1][x].equals(Color.red)){ | |
}*/ | |
//grid[y][x] = Color.red; | |
repaint(); | |
} | |
public boolean win(int x, int i){ | |
System.out.println("Enters win function"); | |
xMatch = 0; | |
yMatch = 0; | |
if(x>0){ | |
//System.out.println("1"); | |
if(grid[i][x].equals(grid[i][x-1])){ | |
xMatch = -1; | |
yMatch = 0; | |
//System.out.println("2"); | |
} | |
} | |
if(x < grid[0].length - 1){ | |
//System.out.println("3"); | |
if(grid[i][x].equals(grid[i][x+1])){ | |
xMatch = 1; | |
yMatch = 0; | |
//System.out.println("4"); | |
} | |
} | |
if(i < grid.length - 1){ | |
//System.out.println("5"); | |
if(grid[i][x].equals(grid[i+1][x])){ | |
yMatch = 1; | |
xMatch = 0; | |
//System.out.println("6"); | |
} | |
} | |
if(i > 0){ | |
//System.out.println("7"); | |
if(grid[i][x].equals(grid[i-1][x])){ | |
yMatch = -1; | |
xMatch = 0; | |
//System.out.println("8"); | |
} | |
} | |
if(i < grid.length - 1 && x < grid[0].length - 1){ | |
//System.out.println("9"); | |
if(grid[i][x].equals(grid[i+1][x+1])){ | |
//System.out.println("10"); | |
xMatch = 1; | |
yMatch = 1; | |
} | |
} | |
if(i>0 && x < grid[0].length - 1){ | |
//System.out.println("11"); | |
if(grid[i][x].equals(grid[i-1][x+1])){ | |
//System.out.println("12"); | |
xMatch = 1; | |
yMatch = -1; | |
} | |
} | |
if(i < grid.length - 1 && x > 0){ | |
//System.out.println("13"); | |
if(grid[i][x].equals(grid[i+1][x-1])){ | |
//System.out.println("14"); | |
xMatch = -1; | |
yMatch = 1; | |
} | |
} | |
if(x>0 && i > 0){ | |
//System.out.println("15"); | |
if(grid[i][x].equals(grid[i-1][x-1])){ | |
//System.out.println("16"); | |
xMatch = -1; | |
yMatch = -1; | |
} | |
} | |
System.out.println(xMatch + ", " + yMatch); | |
boolean win; | |
if(xMatch == 0 && yMatch == 0){ | |
win = false; | |
}else{ | |
win = true; | |
} | |
System.out.println(win); | |
return win; | |
} | |
public boolean four(int x, int y){ | |
/*ArrayList<Integer> inRow = new ArrayList<Integer>(); | |
inRow.add(x); | |
inRow.add(y); | |
inRow.add(x + xMatch); | |
inRow.add(y + yMatch);*/ | |
int inRow = 2; | |
int fromTop = y; | |
int fromBottom = grid.length - y; | |
int fromLeft = x; | |
int fromRight = grid[0].length - x; | |
if(yMatch == 0){ | |
//next two if statements address the opposite side than the found match | |
if(notTouching(2, 1, x, y) && xMatch == -1 && grid[y][x + 1].equals(grid[y][x])){ | |
inRow++; | |
if(notTouching(2, 2, x, y) && grid[y][x + 2].equals(grid[y][x])){ | |
inRow++; | |
}else if(notTouching(0, 2, x, y) && grid[y][x - 2].equals(grid[y][x])){ | |
inRow++; | |
} | |
}else if(notTouching(0, 1, x, y) && xMatch == 1 && grid[y][x - 1].equals(grid[y][x])){ | |
inRow ++; | |
if(notTouching(2, 2, x, y) && grid[y][x + 2].equals(grid[y][x])){ | |
inRow++; | |
}else if(notTouching(0, 2, x, y) && grid[y][x - 2].equals(grid[y][x])){ | |
inRow++; | |
} | |
}else if(notTouching(0, 3, x, y) && xMatch == -1 && grid[y][x - 2].equals(grid[y][x]) | |
&& grid[y][x - 3].equals(grid[y][x])){//if the spot clicked was at the end of the line of 4 | |
inRow += 2; | |
}else if(notTouching(2, 3, x, y) && xMatch == 1 && grid[y][x + 2].equals(grid[y][x]) | |
&& grid[y][x + 3].equals(grid[y][x])){//if the spot clicked was at the end of the line of 4 | |
inRow += 2; | |
} | |
}else if(xMatch == 0){ | |
//next two if statements address the opposite side than the found match | |
if(notTouching(3, 1, x, y) && yMatch == -1 && grid[y + 1][x].equals(grid[y][x])){ | |
inRow++; | |
if(notTouching(1, 2, x, y) && grid[y - 2][x].equals(grid[y][x])){ | |
inRow++; | |
}else if(notTouching(3, 2, x, y) && grid[y + 2][x].equals(grid[y][x])){ | |
inRow++; | |
} | |
}else if(notTouching(1, 1, x, y) && yMatch == 1 && grid[y - 1][x].equals(grid[y][x])){ | |
inRow ++; | |
if(notTouching(3, 2, x, y) && grid[y + 2][x].equals(grid[y][x])){ | |
inRow++; | |
}else if(notTouching(1, 2, x, y) && grid[y - 2][x].equals(grid[y][x])){ | |
inRow++; | |
} | |
}else if(notTouching(1, 3, x, y) && yMatch == -1 && grid[y - 2][x].equals(grid[y][x]) | |
&& grid[y - 3][x].equals(grid[y][x])){//if the spot clicked was at the end of the line of 4 | |
inRow += 2; | |
}else if(notTouching(3, 3, x, y) && yMatch == 1 && grid[y + 2][x].equals(grid[y][x]) | |
&& grid[y + 3][x].equals(grid[y][x])){//if the spot clicked was at the end of the line of 4 | |
inRow += 2; | |
} | |
}else{ | |
if((-1 * xMatch) + x >= 0 && (-1 * xMatch) + x <= grid[0].length - 1 | |
&& (-1 * yMatch) + y >= 0 && (-1 * yMatch) + y <= grid.length - 1 | |
&& grid[y + (-1 * yMatch)][x + (-1 * xMatch)].equals(grid[y][x])){ | |
inRow++; | |
if((-2 * xMatch) + x >= 0 && (-2 * xMatch) + x <= grid[0].length - 1 | |
&& (-2 * yMatch) + y >= 0 && (-2 * yMatch) + y <= grid.length - 1 | |
&& grid[y + (-2 * yMatch)][x + (-2 * xMatch)].equals(grid[y][x])){ | |
inRow++; | |
} | |
} | |
if((2 * xMatch) + x >= 0 && (2 * xMatch) + x <= grid[0].length - 1 | |
&& (2 * yMatch) + y >= 0 && (2 * yMatch) + y <= grid.length - 1 | |
&& grid[y + (2 * yMatch)][x + (2 * xMatch)].equals(grid[y][x])){ | |
inRow++; | |
if((3 * xMatch) + x >= 0 && (3 * xMatch) + x <= grid[0].length - 1 | |
&& (3 * yMatch) + y >= 0 && (3 * yMatch) + y <= grid.length - 1 | |
&& grid[y + (3 * yMatch)][x + (3 * xMatch)].equals(grid[y][x])){ | |
inRow++; | |
} | |
} | |
} | |
if(inRow >= 4){ | |
return true; | |
} | |
return false; | |
} | |
public boolean notTouching(int edge, int edgeToClickDistance, int x, int y){ | |
if(edge == 0){//edge = far left side | |
if(x > (-1 + edgeToClickDistance)){ | |
return true; | |
} | |
}else if(edge == 1){//edge = top | |
if(y > (-1 + edgeToClickDistance)){ | |
return true; | |
} | |
}else if(edge == 2){//edge = far right | |
if(x < (grid[0].length - 1 - edgeToClickDistance)){ | |
return true; | |
} | |
}else if(edge == 3){//edge = bottom | |
if(y < (grid.length - 1 - edgeToClickDistance)){ | |
return true; | |
} | |
} | |
return false; | |
} | |
//meathod not used | |
public boolean rowOfFour(int x, int y){ | |
int inRow = 2; | |
if(grid[y][x].equals(grid[y + (yMatch * -1)][x + (xMatch * -1)])){ | |
inRow ++; | |
if(grid[y][x].equals(grid[y + (yMatch * -2)][x + (xMatch * -2)])){ | |
inRow++; | |
if(grid[y][x].equals(grid[y + (yMatch * -3)][x + (xMatch * -3)])){ | |
inRow ++; | |
} | |
} | |
} | |
//if(y < grid.length - 2 && x < grid[0].length - 2 && y > 0 && x > 0 | |
if(x > 1 && xMatch == -1){ | |
if(y > 1 && yMatch == -1){ | |
} | |
if(y < grid.length - 3 && yMatch == 1){ | |
} | |
if(y == 0){ | |
} | |
} | |
if(x < grid[0].length - 3 && xMatch == 1){ | |
if(y > 1 && yMatch == -1){ | |
} | |
if(y < grid.length - 3 && yMatch == 1){ | |
} | |
if(y == 0){ | |
} | |
} | |
if(x == 0){ | |
} | |
if(grid[y][x].equals(grid[y + (yMatch * 2)][x + (xMatch * 2)])){ | |
inRow ++; | |
if(y < grid.length - 2 && x < grid[0].length - 2 && | |
grid[y][x].equals(grid[y + (yMatch * 3)][x + (xMatch * 3)])){ | |
inRow ++; | |
} | |
} | |
if(inRow == 4){ | |
return true; | |
} | |
return false; | |
} | |
public void mouseReleased(MouseEvent e) { | |
} | |
public void mouseEntered(MouseEvent e) { | |
} | |
public void mouseExited(MouseEvent e) { | |
} | |
public void mouseClicked(MouseEvent e) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment