Created
April 21, 2020 15:03
-
-
Save raunaqsingh2020/a1d0eb14064df9e132dd4724336190e9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.awt.Color; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.WindowConstants; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.Point; | |
import java.awt.event.MouseListener; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.KeyAdapter; | |
import java.awt.event.KeyEvent; | |
import java.util.*; | |
import java.io.*; | |
import java.awt.Font; | |
public class GUI { | |
private JFrame frame; | |
public GUI() { | |
frame = new JFrame("Bubbles Program"); | |
frame.setSize(500, 550); | |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); | |
frame.setPreferredSize(frame.getSize()); | |
frame.add(new InnerProgram(frame.getSize())); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public static void main(String... argv) { | |
new GUI(); | |
} | |
public static class InnerProgram extends JPanel implements Runnable, MouseListener { | |
ArrayList<String> names = new ArrayList<String>(); | |
private Thread animator; | |
Dimension d; | |
String str = ""; | |
int[][] board = {{1,2,3},{4,5,6},{7,8,0}}; | |
public InnerProgram (Dimension dimension) { | |
setSize(dimension); | |
setPreferredSize(dimension); | |
addMouseListener(this); | |
addKeyListener(new TAdapter()); | |
setFocusable(true); | |
d = getSize(); | |
//for animating the screen - you won't need to edit | |
if (animator == null) { | |
animator = new Thread(this); | |
animator.start(); | |
} | |
setDoubleBuffered(true); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
Graphics2D g2 = (Graphics2D)g; | |
g2.setColor(Color.black); | |
g2.fillRect(0, 0,(int)d.getWidth() , (int)d.getHeight()); | |
Color co = new Color(255,255,255); | |
Color c1 = new Color(255,255,255); | |
if(isSolvable(board)){ | |
str = "Solvable"; | |
}else{ | |
str = "Not Solvable"; | |
} | |
ArrayList<Integer> boardlist = new ArrayList<Integer>(); | |
for(int r = 0; r < 3; r++){ | |
for(int c = 0; c < 3; c++){ | |
boardlist.add(board[r][c]); | |
} | |
} | |
boolean won = true; | |
for(int i = 0; i < boardlist.size(); i++){ | |
for(int j = i; j < boardlist.size(); j++){ | |
if(boardlist.get(j)<boardlist.get(i) && boardlist.get(j)!=0){ | |
won = false; | |
} | |
} | |
} | |
if(won) | |
str = "Solved!"; | |
if(str.equals("Solvable")){ | |
c1 = new Color(100,255,100); | |
}else if (str.equals("Not Solvable")){ | |
c1 = new Color(255,100,100); | |
}else{ | |
c1 = new Color(255,100,255); | |
} | |
g2.setColor(c1); | |
g2.setFont(new Font("Arial", Font.PLAIN, 30)); | |
g2.drawString(str,100,65); | |
int fontSize = 50; | |
g2.setColor(co); | |
g2.setFont(new Font("Arial", Font.PLAIN, fontSize)); | |
g2.drawRect(100,100,300,300); | |
for(int r = 0; r < 3; r++){ | |
for(int c = 0; c < 3; c++){ | |
int x = c*100 + 100; | |
int y = r*100 + 100; | |
g2.setColor(c1); | |
g2.drawRect(x,y,100,100); | |
g2.setColor(co); | |
g2.drawString(""+board[r][c],x+37,y+68); | |
} | |
} | |
g2.drawRect(180,430,140,50); | |
g2.setFont(new Font("Arial", Font.PLAIN,20)); | |
g2.drawString("new board",200,463); | |
} | |
public int random (int a, int b){ | |
int max=a; | |
int min=b; | |
int random=(int)(Math.random() * (max - min) + min); | |
return random; | |
} | |
public void mousePressed(MouseEvent e) { | |
int x = e.getX(); | |
int y = e.getY(); | |
int zerorow=-5; | |
int zerocol=-5; | |
for(int r = 0; r < 3; r++){ | |
for(int c = 0; c < 3; c++){ | |
if(board[r][c]==0){ | |
zerorow=r; | |
zerocol=c; | |
} | |
} | |
} | |
int clickrow=-5; | |
int clickcol=-5; | |
if(x > 100 && x < 400 && y > 100 && y < 400){ | |
if(x>100 && x < 200) | |
clickcol=0; | |
if(x>200 && x < 300) | |
clickcol=1; | |
if(x>300 && x < 400) | |
clickcol=2; | |
if(y>100 && y < 200) | |
clickrow=0; | |
if(y>200 && y < 300) | |
clickrow=1; | |
if(y>300 && y < 400) | |
clickrow=2; | |
} | |
if(clickrow!=-5 && clickcol!=-5){ | |
if(clickrow==zerorow&&(clickcol==zerocol+1||clickcol==zerocol-1)){ | |
int temp = board[clickrow][clickcol]; | |
board[clickrow][clickcol]=0; | |
board[zerorow][zerocol]=temp; | |
}else if(clickcol==zerocol&&(clickrow==zerorow+1||clickrow==zerorow-1)){ | |
int temp = board[clickrow][clickcol]; | |
board[clickrow][clickcol]=0; | |
board[zerorow][zerocol]=temp; | |
} | |
} | |
ArrayList<Integer> num = new ArrayList<Integer>(); | |
for(int i = 0; i < 9;i++) | |
num.add(i); | |
if(x>180 && x < 320 && y > 430 && y < 480){ | |
for(int r = 0; r < 3; r++){ | |
for(int c = 0; c < 3; c++){ | |
int index = (int)(Math.random()*num.size()); | |
board[r][c]=num.get(index); | |
num.remove(index); | |
} | |
} | |
} | |
} | |
public boolean isSolvable(int[][] board) { | |
int inversions = 0; | |
for(int r = 0; r < 3; r++){ | |
for(int c = 0; c < 3; c++){ | |
int temp = board[r][c]; | |
for(int r2 = r; r2 < 3; r2++){ | |
for(int c2 = 0; c2 < 3; c2++){ | |
if(r2 > r || c2 >= c){ | |
if(board[r2][c2] < temp && board[r2][c2]!=0){ | |
inversions++; | |
} | |
} | |
} | |
} | |
} | |
} | |
return(inversions%2 ==0); | |
} | |
public void mouseReleased(MouseEvent e) { | |
} | |
public void mouseEntered(MouseEvent e) { | |
} | |
public void mouseExited(MouseEvent e) { | |
} | |
public void mouseClicked(MouseEvent e) { | |
} | |
private class TAdapter extends KeyAdapter { | |
public void keyReleased(KeyEvent e) { | |
int keyr = e.getKeyCode(); | |
} | |
public void keyPressed(KeyEvent e) { | |
int key = e.getKeyCode(); | |
// String c = KeyEvent.getKeyText(e.getKeyCode()); | |
// c = Character.toString((char) key); | |
} | |
}//end of adapter | |
public void run() { | |
long beforeTime, timeDiff, sleep; | |
beforeTime = System.currentTimeMillis(); | |
int animationDelay = 37; | |
long time = System.currentTimeMillis(); | |
while (true) {// infinite loop | |
// spriteManager.update(); | |
repaint(); | |
try { | |
time += animationDelay; | |
Thread.sleep(Math.max(0, time - System.currentTimeMillis())); | |
} catch (InterruptedException e) { | |
System.out.println(e); | |
} // end catch | |
} // end while loop | |
}// end of run | |
}//end of class | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment