Created
January 10, 2018 19:56
-
-
Save little-einstien/caedf88246e0984d33b6a33608200b83 to your computer and use it in GitHub Desktop.
Maze in java 2d
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
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="EntryPointsManager"> | |
<entry_points version="2.0" /> | |
</component> | |
<component name="ProjectKey"> | |
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" /> | |
</component> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/Maze.iml" filepath="$PROJECT_DIR$/Maze.iml" /> | |
</modules> | |
</component> | |
</project> |
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
<template> | |
<input-field default="com.company">IJ_BASE_PACKAGE</input-field> | |
</template> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="Palette2"> | |
<group name="Swing"> | |
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> | |
</item> | |
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> | |
</item> | |
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> | |
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> | |
<initial-values> | |
<property name="text" value="Button" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="RadioButton" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="CheckBox" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> | |
<initial-values> | |
<property name="text" value="Label" /> | |
</initial-values> | |
</item> | |
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> | |
<preferred-size width="150" height="-1" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> | |
<preferred-size width="150" height="50" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> | |
<preferred-size width="200" height="200" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> | |
<preferred-size width="200" height="200" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> | |
</item> | |
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> | |
</item> | |
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> | |
</item> | |
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> | |
<preferred-size width="-1" height="20" /> | |
</default-constraints> | |
</item> | |
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> | |
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> | |
</item> | |
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> | |
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> | |
</item> | |
</group> | |
</component> | |
</project> |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
</module> | |
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
package com.niit.app; | |
import com.niit.constant.SquareType; | |
import com.niit.exception.InvalidMazeStructureException; | |
import com.niit.maze.Maze; | |
import javax.swing.*; | |
import java.awt.*; | |
import static com.niit.constant.Position.*; | |
import static com.niit.constant.SquareType.*; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
try { | |
JFrame window = new JFrame(); | |
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
window.setBounds(300, 300, 300, 300); | |
Maze m = new Maze(5,5,R,N,N,N,N,G,N,B,N,B,B,N,B,N,N,N,N,B,N,B,N,S,N,N,N,N); | |
window.getContentPane().add(m); | |
window.setVisible(true); | |
SquareType[] st = m.getSquareTypeList(); | |
//Thread.sleep(1000); | |
m.move(); | |
m.move(); | |
m.resetMaze(); | |
// System.out.print(m.openAhed()); | |
/* for(int i = 0 ;i<m.getSquareTypeList().length ; i++){ | |
st[i] = SquareType.B; | |
m.setSquareTypeList(st); | |
window.repaint(); | |
Thread.sleep(1500); | |
}*/ | |
//Graphics.drawRect(50,50,100,100); | |
} catch (InvalidMazeStructureException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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
package com.niit.bean; | |
import com.niit.exception.Events; | |
public class DebuggPattern { | |
private Events[] eventsHistory ; | |
private String str; | |
public DebuggPattern(Events[] eventsHistory, String str) { | |
this.eventsHistory = eventsHistory; | |
this.str = str; | |
} | |
public Events[] getEventsHistory() { | |
return eventsHistory; | |
} | |
public void setEventsHistory(Events[] eventsHistory) { | |
this.eventsHistory = eventsHistory; | |
} | |
public String getStr() { | |
return str; | |
} | |
public void setStr(String str) { | |
this.str = str; | |
} | |
} |
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
package com.niit.constant; | |
public enum Position implements Cloneable{ | |
U(1,"UP"), | |
D(2,"DOWN"), | |
L(3,"LEFT"), | |
R(4,"RIGHT"); | |
private final int id; | |
private final String value; | |
Position(int id, String value) { | |
this.id = id; | |
this.value = value; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getValue() { | |
return value; | |
} | |
} |
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
package com.niit.constant; | |
public enum SquareType implements Cloneable { | |
N(1,"N"), | |
S(2,"S"), | |
G(3,"G"), | |
B(4,"B"), | |
X(5,"X"); | |
private final int id; | |
private final String value; | |
SquareType(int id, String value) { | |
this.id = id; | |
this.value = value; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getValue() { | |
return value; | |
} | |
} |
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
package com.niit.exception; | |
public enum Events { | |
M(1,"M"), | |
R(2,"R"), | |
W(3,"W"), | |
E(4,"E"), | |
X(5,"X"), | |
U(6,"U"), | |
O(7,"O"), | |
G(8,"G"); | |
private final int id; | |
private final String value; | |
Events(int id, String value) { | |
this.id = id; | |
this.value = value; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getValue() { | |
return value; | |
} | |
} |
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
package com.niit.exception; | |
public class InvalidMazeStructureException extends Exception { | |
String msg; | |
public InvalidMazeStructureException(String msg){ | |
super(msg); | |
} | |
} |
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
package com.niit.graphics; | |
import java.awt.*; | |
import javax.swing.JComponent; | |
import javax.swing.JFrame; | |
class MyCanvas extends JComponent { | |
public void paint(Graphics g) { | |
g.setColor(Color.GREEN); | |
g.fillRect (0, 20, 20, 20); | |
g.setColor(Color.RED); | |
g.fillRect (0, 40, 20, 20); | |
g.setColor(Color.BLACK); | |
g.fillRect (0, 60, 20, 20); | |
g.setColor(Color.GREEN); | |
g.fillRect (20, 20, 20, 20); | |
g.setColor(Color.RED); | |
g.fillRect (20, 40, 20, 20); | |
g.setColor(Color.BLACK); | |
g.fillRect (20, 60, 20, 20); | |
/*g.setColor(Color.RED)/; | |
g.fillRect (11, 20, 20, 20); | |
g.fillRect (12, 20, 20, 20); | |
g.fillRect (13, 20, 20, 20)*/; | |
} | |
} | |
public class DrawRect { | |
public static void main(String[] a) { | |
JFrame window = new JFrame(); | |
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
window.setBounds(30, 30, 300, 300); | |
window.getContentPane().add(new MyCanvas()); | |
window.setVisible(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
package com.niit.maze; | |
import com.niit.bean.DebuggPattern; | |
import com.niit.constant.Position; | |
import com.niit.constant.SquareType; | |
import com.niit.exception.Events; | |
import com.niit.exception.InvalidMazeStructureException; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Maze extends JComponent { | |
private int row; | |
private int col; | |
private final int SIDE = 20; | |
private Position botStartSquareType,botStartSquareType_bkp; | |
private SquareType[] squareTypeList,squareTypeList_bkp; | |
int srcIndex = 0; | |
boolean error = false; | |
List<Events> historyList = new ArrayList<>(); | |
public Position getBotStartSquareType() { | |
return botStartSquareType; | |
} | |
public void setBotStartSquareType(Position botStartSquareType) { | |
this.botStartSquareType = botStartSquareType; | |
} | |
public SquareType[] getSquareTypeList() { | |
return squareTypeList; | |
} | |
public void setSquareTypeList(SquareType[] squareTypeList) { | |
this.squareTypeList = squareTypeList; | |
} | |
public Maze(int row, int col,Position botStartPosition,SquareType... squareTypeList) throws InvalidMazeStructureException { | |
if(row*col != squareTypeList.length){ | |
throw new InvalidMazeStructureException("Invalid Values for Squares \n Please enter "+(row*col)+" values"); | |
} | |
setCol(col); | |
setRow(row); | |
setBotStartSquareType(botStartPosition); | |
setSquareTypeList(squareTypeList); | |
squareTypeList_bkp = squareTypeList.clone(); | |
botStartSquareType_bkp = getBotStartSquareType(); | |
} | |
public int getRow() { | |
return row; | |
} | |
public void setRow(int row) { | |
this.row = row; | |
} | |
public int getCol() { | |
return col; | |
} | |
public void setCol(int col) { | |
this.col = col; | |
} | |
public void paint(Graphics g) { | |
Graphics2D g2d = (Graphics2D)g; | |
g2d.setStroke(new BasicStroke(5)); | |
for(int i = 0; i < row; i++){ | |
for (int j = 0; j < col; j++){ | |
g2d.setColor(resolveSquareType(getSquareTypeList()[i*row+j])); | |
g2d.fillRect (j*(SIDE+1), i*(SIDE+1), SIDE, SIDE); | |
if(getSquareTypeList()[i*row+j] == SquareType.S){ | |
srcIndex = i*row+j; | |
} | |
} | |
} | |
if(error){ | |
g2d.setColor(Color.BLACK); | |
g2d.drawString("Oops! The bot cant go there",0,(row+1)*SIDE); | |
} | |
} | |
public void move(){ | |
try { | |
Thread.sleep(1500); | |
if (botStartSquareType == Position.R && srcIndex + 1 < squareTypeList.length && squareTypeList[srcIndex + 1] == SquareType.N) { | |
markSquare(); | |
squareTypeList[srcIndex = srcIndex + 1] = SquareType.S; | |
} else if (botStartSquareType == Position.L && srcIndex - 1 > 0 && squareTypeList[srcIndex - 1] == SquareType.N) { | |
markSquare(); | |
squareTypeList[srcIndex = srcIndex - 1] = SquareType.S; | |
} else if (botStartSquareType == Position.U && srcIndex - row < 0 && squareTypeList[srcIndex - row] == SquareType.N) { | |
markSquare(); | |
squareTypeList[srcIndex = srcIndex - row] = SquareType.S; | |
} else if (botStartSquareType == Position.D && srcIndex + row < squareTypeList.length && squareTypeList[srcIndex + row] == SquareType.N) { | |
markSquare(); | |
squareTypeList[srcIndex = srcIndex + row] = SquareType.S; | |
} else { | |
error = true; | |
} | |
repaint(); | |
historyList.add(Events.M); | |
}catch (InterruptedException ie) { | |
ie.printStackTrace(); | |
} | |
} | |
public boolean openAhed() { | |
historyList.add(Events.O); | |
return botStartSquareType == Position.R && srcIndex + 1 < squareTypeList.length && squareTypeList[srcIndex + 1] != SquareType.B | |
&& botStartSquareType == Position.L && srcIndex - 1 > 0 && squareTypeList[srcIndex - 1] == SquareType.N | |
&& botStartSquareType == Position.D && srcIndex + row < squareTypeList.length &&squareTypeList[srcIndex + row] == SquareType.N | |
&& botStartSquareType == Position.U && srcIndex - row > 0 && squareTypeList[srcIndex - row] == SquareType.N; | |
} | |
public void rotateRight() { | |
switch (botStartSquareType){ | |
case L: | |
botStartSquareType = Position.U; | |
break ; | |
case U: | |
botStartSquareType = Position.R; | |
break ; | |
case R: | |
botStartSquareType = Position.D; | |
break ; | |
case D: | |
botStartSquareType = Position.L; | |
break ; | |
} | |
historyList.add(Events.R); | |
} | |
public boolean goal(){ | |
historyList.add(Events.G); | |
return srcIndex > squareTypeList.length ? squareTypeList[srcIndex] == SquareType.G : false; | |
} | |
public void markSquare(){ | |
historyList.add(Events.X); | |
squareTypeList[srcIndex] = SquareType.X; | |
} | |
public void unmarkSquare(){ | |
historyList.add(Events.U); | |
squareTypeList[srcIndex] = SquareType.N; | |
} | |
private Color resolveSquareType(SquareType squareType){ | |
switch(squareType) { | |
case N: | |
return Color.GREEN; | |
case S: | |
return Color.YELLOW; | |
case G: | |
return Color.BLUE; | |
case X: | |
return Color.RED; | |
default: | |
return Color.BLACK; | |
} | |
} | |
public void resetMaze(){ | |
squareTypeList = squareTypeList_bkp; | |
srcIndex = 0; | |
botStartSquareType = botStartSquareType_bkp; | |
historyList.clear(); | |
repaint(); | |
} | |
List<DebuggPattern> debuggPatterns = new ArrayList<>(); | |
Events[] eventsHistory; | |
public void addDubugPattern(String str, Events... eventsHistory){ | |
debuggPatterns.add(new DebuggPattern(eventsHistory,str)); | |
} | |
public void resetDebuggPatterns(){ | |
debuggPatterns.clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment