I have a piece of code to display an input string in shuffled order and also another code to display a simple grid.I am not sure how to integrate it though. I want the shuffled characters to sit in a random position in a grid and rest of the positions in the grid must be filled by random characters. How do I do it
//For drawing the grid
*import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DrawGrid
{
public DrawGrid()
{
int[][] grid = getGrid();
Panel panel = new JPanel(new GridLayout(8,8));
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
panel.add(new JLabel(String.valueOf(grid[i][j])));
}
}
JFrame frame = new JFrame();
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public int[][] getGrid() {
int[][] grid = new int[8][8];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = i;
}
}
return grid;
}
public static void main(String[] args) {
new DrawGrid();
}
}*
//to shuffle characters
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception
{
ArrayList<String> obj = new ArrayList<String>();
obj.add("G");
obj.add("E");
obj.add("O");
obj.add("M");
obj.add("E");
obj.add("T");
obj.add("R");
obj.add("Y");
Collections.shuffle(obj);
System.out.println(obj); //Prints the shuffled i/p string
}
}
Aucun commentaire:
Enregistrer un commentaire