Created
April 23, 2020 01:51
-
-
Save feehe21/df55be82be9b9a63fae4a3e2ad3f6c98 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.*; | |
public class Board | |
{ | |
Tile[][]table; | |
int emptyX; | |
int emptyY; | |
String total; | |
boolean solved; | |
public Board() | |
{ | |
table = new Tile[3][3]; | |
total = ""; | |
solved = false; | |
} | |
public void setBoard(){//initializes board | |
//randomizes digits | |
int[] a = new int[9]; | |
int rand; | |
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6,7,8,9)); | |
for(int i = 0; i < numbers.size(); i++){ | |
rand = (int)(Math.random() * 9); | |
if(a[rand] == 0){ | |
a[rand] = numbers.get(i); | |
}else{ | |
i--; | |
} | |
} | |
//places in board using array 1-9 | |
int spot = 0; | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 3; j++){ | |
table[i][j] = new Tile(a[spot]-1); | |
if(a[spot]-1 == 0){ | |
emptyX = j; | |
emptyY = i; | |
} | |
a[spot]-=1; | |
spot++; | |
} | |
} | |
if(!solvableBoard(a)){ | |
System.out.println("Unsolvable. Trying again."); | |
setBoard(); | |
} | |
total = ""; | |
for(int i = 0; i < a.length; i++){ | |
total += a[i]; | |
} | |
} | |
public boolean solvableBoard(int[]board){//determines if the board is solvable | |
int totalInverted = 0; | |
for (int i = 0; i < board.length; i++) { | |
if (board[i] == 0) | |
continue; | |
int testInverted = 0; | |
for(int j = i+ 1; j < board.length; j++){ | |
if(board[i] > board[j] && board[j] != 0){ | |
testInverted++; | |
} | |
} | |
//System.out.format("%d: %d inversions\n", board[i], testInverted); | |
totalInverted += testInverted; | |
} | |
if(totalInverted % 2 == 0){ | |
return true; | |
} | |
return false; | |
} | |
public void move(int x, int y){//moves tile if applicable | |
if(solved){ | |
return; | |
} | |
if((Math.abs(x-emptyX) == 1 && y == emptyY) || (Math.abs(y-emptyY) == 1 && x == emptyX)){ | |
System.out.println("Move " + table[y][x].digit); | |
Tile hold = table[y][x]; | |
table[y][x] = table[emptyY][emptyX]; | |
table[emptyY][emptyX] = hold; | |
emptyX = x; | |
emptyY = y; | |
}else{ | |
System.out.println("No move"); | |
} | |
if(isSolved()){ | |
solved = true; | |
} | |
} | |
public boolean isSolved(){ //0 first | |
int spot = 0; | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 3; j++){ | |
if(spot != table[i][j].digit){ | |
return false; | |
} | |
spot++; | |
} | |
} | |
return true; | |
} | |
} |
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.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 static Board table; | |
public GUI() { | |
table = new Board(); | |
table.setBoard(); | |
frame = new JFrame("Tiles Program"); | |
frame.setSize(800, 800); | |
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 = ""; | |
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); | |
g2.setColor(co); | |
int fontSize = 100; | |
g2.setFont(new Font("TimesRoman", Font.PLAIN, fontSize)); | |
//g2.drawString(table.total + "",20,70); | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 3; j++){ | |
g2.drawRect(j*200 + 100,i*200+100,200,200); | |
//System.err.println(i +" " + j); | |
if(table.table[j][i].digit != 0) | |
g2.drawString("" + table.table[j][i].digit , (i+1) *200,(j+1) *200); | |
} | |
} | |
} | |
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(); | |
str = x + " " + y; | |
if(x < 100 || y < 100 || x > 700 || y > 700){ | |
table.setBoard(); | |
}else{ | |
System.out.println("Into Move: " + (x-100)/200 + " " + (y-100)/200); | |
table.move((x-100)/200, (y-100)/200); | |
} | |
} | |
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 | |
} |
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
public class Tile | |
{ | |
int digit; | |
public Tile(int d) | |
{ | |
digit = d; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment