Created
June 30, 2016 22:33
-
-
Save LucasAlfare/36f3978ff4fc340788e1c3a0ce3d6e31 to your computer and use it in GitHub Desktop.
Sources of my UD square-1 Algorith Converter.
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.main; | |
import javax.swing.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.util.ArrayList; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* Created by lucas on 30/06/16. | |
*/ | |
public class Core { | |
private static final String REGEX = "\\((-?\\d+),(-?\\d+)\\)"; | |
private String currentScramble, convertedScrambles = ""; | |
private boolean exceptionFired; | |
private Gui gui; | |
public Core() { | |
gui = new Gui(); | |
gui.getConvertBt().addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
convertedScrambles = ""; | |
int counter = 1; | |
for (String inputScramble : getInputScrambles()) { | |
convertedScrambles += counter + "°) " + convertSequence(getScrambleMoves(inputScramble)) + ";\n"; | |
counter++; | |
} | |
gui.getOutput().setText(!exceptionFired | |
? | |
convertedScrambles | |
: | |
"Não foi possível compreender o formato do(s) scramble(s) digitado(s)!\n" | |
+ "Por favor, verifique o conteúdo de Input."); | |
} | |
}); | |
} | |
public String convertSequence(ArrayList<int[]> nums) { | |
String r = startsWithSlash(currentScramble) ? "/" : ""; | |
for (int[] num : nums) { | |
r += "(" + (num[1] * -1) + "," + (num[0] * -1) + ")/"; | |
} | |
if (!endsWithSlash(currentScramble)) { | |
String aux = ""; | |
for (int i = 0; i < r.length() - 1; i++) { | |
aux += r.charAt(i); | |
} | |
r = aux; | |
} | |
return r; | |
} | |
private ArrayList<int[]> getScrambleMoves(String scramble) { | |
currentScramble = scramble; | |
ArrayList<int[]> aux = new ArrayList<>(); | |
Pattern p = Pattern.compile(REGEX); | |
for (String s : scramble.split("/")) { | |
if (!s.isEmpty()) { | |
try { | |
Matcher m = p.matcher(s); | |
m.find(); | |
int top = Integer.parseInt(m.group(1)), botton = Integer.parseInt(m.group(2)); | |
aux.add(new int[]{top, botton}); | |
exceptionFired = false; | |
} catch (IllegalStateException e) { | |
exceptionFired = true; | |
} | |
} | |
} | |
return aux; | |
} | |
private boolean startsWithSlash(String scramble) { | |
return scramble.charAt(0) == '/'; | |
} | |
private boolean endsWithSlash(String scramble) { | |
return scramble.charAt(scramble.length() - 1) == '/'; | |
} | |
private boolean isAnMovePattern(String s) { | |
return s.matches(REGEX); | |
} | |
private String[] getInputScrambles() { | |
if (gui.getInput() != null) { | |
if (!inputIsEmpty(gui.getInput())) { | |
return gui.getInput().getText().split(";"); | |
} | |
} | |
return new String[]{" "}; | |
} | |
private boolean inputIsEmpty(JTextArea input) { | |
return input.getText().isEmpty(); | |
} | |
public Gui getGui(){ | |
return this.gui; | |
} | |
} |
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.main; | |
import javax.swing.*; | |
/** | |
* Created by lucas on 30/06/16. | |
*/ | |
public class Gui extends JFrame{ | |
private JButton convertBt; | |
private JTextArea input; | |
private JTextArea output; | |
public Gui(){ | |
this.initComponents(); | |
this.setTitle("UD square-1 Algorithm Converter by Lucas Sousa"); | |
this.setLocationRelativeTo(null); | |
} | |
private void initComponents() { | |
JPanel jPanel1 = new JPanel(); | |
JLabel jLabel1 = new JLabel(); | |
JScrollPane jScrollPane1 = new JScrollPane(); | |
input = new javax.swing.JTextArea(); | |
JPanel jPanel2 = new JPanel(); | |
JScrollPane jScrollPane2 = new JScrollPane(); | |
output = new javax.swing.JTextArea(); | |
convertBt = new javax.swing.JButton(); | |
JMenuBar jMenuBar1 = new JMenuBar(); | |
JMenu jMenu1 = new JMenu(); | |
JMenuItem jMenuItem1 = new JMenuItem(); | |
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); | |
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Input algorithms")); | |
jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 15)); // NOI18N | |
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT); | |
jLabel1.setText("Coloque os algoritmos a serem convertidos aqui (separados com semicolon \";\"):"); | |
input.setColumns(20); | |
input.setRows(5); | |
jScrollPane1.setViewportView(input); | |
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); | |
jPanel1.setLayout(jPanel1Layout); | |
jPanel1Layout.setHorizontalGroup( | |
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE) | |
.addComponent(jScrollPane1) | |
); | |
jPanel1Layout.setVerticalGroup( | |
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(jPanel1Layout.createSequentialGroup() | |
.addComponent(jLabel1) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addGap(0, 0, Short.MAX_VALUE)) | |
); | |
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Output algorithms")); | |
output.setColumns(20); | |
output.setRows(5); | |
jScrollPane2.setViewportView(output); | |
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); | |
jPanel2.setLayout(jPanel2Layout); | |
jPanel2Layout.setHorizontalGroup( | |
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jScrollPane2) | |
); | |
jPanel2Layout.setVerticalGroup( | |
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE) | |
); | |
convertBt.setText("Convert!"); | |
jMenu1.setText("File"); | |
jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, java.awt.event.InputEvent.ALT_MASK)); | |
jMenuItem1.setText("Sair"); | |
jMenuItem1.addActionListener(new java.awt.event.ActionListener() { | |
public void actionPerformed(java.awt.event.ActionEvent evt) { | |
System.exit(0); | |
} | |
}); | |
jMenu1.add(jMenuItem1); | |
jMenuBar1.add(jMenu1); | |
setJMenuBar(jMenuBar1); | |
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); | |
getContentPane().setLayout(layout); | |
layout.setHorizontalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap() | |
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(convertBt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) | |
.addGap(0, 0, 0)) | |
); | |
layout.setVerticalGroup( | |
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) | |
.addGroup(layout.createSequentialGroup() | |
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) | |
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(convertBt) | |
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) | |
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) | |
.addGap(23, 23, 23)) | |
); | |
pack(); | |
} | |
public JButton getConvertBt() { | |
return convertBt; | |
} | |
public JTextArea getInput() { | |
return input; | |
} | |
public JTextArea getOutput() { | |
return output; | |
} | |
} |
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.main; | |
/** | |
* Created by lucas on 30/06/16. | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
new Core().getGui().setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment