我正在尝试将GUI网格的大小从25x25增加到40x40,我一直在尝试在代码中找到GUI网格大小,并且由于某种原因我无法这样做,如果有人我会真的很喜欢它可以帮助我理解需要更改哪些内容来改变GUI的网格大小。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author MouseTeam
*/
public class GUI extends JFrame {
private int mouseX = 0, mouseY = 0;
private JButton startButton; // start button!
private JButton[][] buttons; // have all the grid buttons
private JPanel gridPanel;// the panel holding the grid of buttons
private JPanel[][] mainGrid; // each panel holds a button from button variable matrix of buttons
private JLabel numberOfLeftFood, numberOfSteps; // labels in the gui
private ArrayList<Object[]> bestGenerationsAndGrid; // holds all best of each 10 generations
private JTextField terminalProbability, crossoverProbability, mutationProbability,
gridWidth, gridHeight, maxDepth, populationSize, numberOfGenerations;
private JLabel generationNoLabel;
/**
* initilizes the primere look of the gui and it's characteristics
*/
public GUI() {
int width = Parameters.gridWidth;
int height = Parameters.gridHeight;
gridPanel = new JPanel();
buttons = new JButton[width][height];
gridPanel.setLayout(new GridLayout(width, height));
mainGrid = new JPanel[width][height];
setSize(600, 600);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
//--------------------------------------------------------------------------//
JPanel right = new javax.swing.JPanel();
startButton = new javax.swing.JButton();
terminalProbability = new javax.swing.JTextField();
crossoverProbability = new javax.swing.JTextField();
mutationProbability = new javax.swing.JTextField();
gridWidth = new javax.swing.JTextField();
gridHeight = new javax.swing.JTextField();
maxDepth = new javax.swing.JTextField();
populationSize = new javax.swing.JTextField();
numberOfGenerations = new javax.swing.JTextField();
JLabel jLabel1 = new javax.swing.JLabel();
JLabel jLabel2 = new javax.swing.JLabel();
JLabel jLabel3 = new javax.swing.JLabel();
JLabel jLabel4 = new javax.swing.JLabel();
JLabel jLabel5 = new javax.swing.JLabel();
JLabel jLabel6 = new javax.swing.JLabel();
JLabel jLabel7 = new javax.swing.JLabel();
JLabel jLabel8 = new javax.swing.JLabel();
generationNoLabel = new javax.swing.JLabel();
numberOfLeftFood = new javax.swing.JLabel();
numberOfSteps = new javax.swing.JLabel();
JPanel center = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
terminalProbability.setText("0.4");
startButton.setBackground(new java.awt.Color(0, 0, 0));
startButton.setForeground(new java.awt.Color(255, 255, 0));
startButton.setText("Start");
startButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
startButtonAction(evt);
}
});
crossoverProbability.setText("0.8");
mutationProbability.setText("0.05");
gridWidth.setText("25");
gridHeight.setText("25");
maxDepth.setText("20");
populationSize.setText("100");
numberOfGenerations.setText("20");
jLabel1.setText("Terminal Probability");
jLabel2.setText("Mutation Probability");
jLabel3.setText("Crossover Probability");
jLabel4.setText("Grid Width");
jLabel5.setText("Grid Height");
jLabel6.setText("Max Depth");
jLabel7.setText("Population Size");
jLabel8.setText("Number Of Generations");
generationNoLabel.setText("Generation number");
generationNoLabel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
generationNoLabel.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
generationNoLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
numberOfLeftFood.setText("Number of left food");
numberOfLeftFood.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
numberOfLeftFood.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
numberOfLeftFood.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
numberOfSteps.setText("Number of steps");
numberOfSteps.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
numberOfSteps.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
numberOfSteps.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
javax.swing.GroupLayout rightLayout = new javax.swing.GroupLayout(right);
right.setLayout(rightLayout);
rightLayout.setHorizontalGroup(
rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(rightLayout.createSequentialGroup()
.addContainerGap()
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(generationNoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(startButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(rightLayout.createSequentialGroup()
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(rightLayout.createSequentialGroup()
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5)
.addComponent(jLabel6)
.addComponent(jLabel7)
.addComponent(jLabel1))
.addGap(5, 5, 5))
.addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(numberOfGenerations, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(maxDepth, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(gridHeight, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(gridWidth, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(mutationProbability, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(terminalProbability, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(crossoverProbability, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(populationSize, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(numberOfLeftFood, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(numberOfSteps, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
rightLayout.setVerticalGroup(
rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(rightLayout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(startButton)
.addGap(18, 18, 18)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(terminalProbability, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(mutationProbability, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(crossoverProbability, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(gridWidth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(gridHeight, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5))
.addGap(18, 18, 18)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(maxDepth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6))
.addGap(18, 18, 18)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(populationSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(rightLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(numberOfGenerations, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8))
.addGap(18, 18, 18)
.addComponent(generationNoLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(numberOfLeftFood)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(numberOfSteps)
.addContainerGap(65, Short.MAX_VALUE))
);
//---------------------------------------------------------------------------------------//
initGrid();
add(gridPanel, BorderLayout.CENTER);
add(right, BorderLayout.EAST);
}
/**
*take cheese coordinates and fill the grid with the cheese
*/
public void putCheese(ArrayList<Coordinate> cheese) {
for (int i = 0; i < cheese.size(); ++i) {
Coordinate coord = cheese.get(i);
updatDisplay(coord.i, coord.j, "2.png");
}
}
/**
* Make a button with a resizible images
*/
public static JButton makeButton(String path) {
try {
BufferedImage master;
if (path == "") {
path = "default.png";
}
master = ImageIO.read(new File(path));
JButton btn = new JButton() {
@Override
public Dimension getPreferredSize() {
return new Dimension(90, 50);
}
};
btn.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
JButton btn = (JButton) e.getComponent();
Dimension size = btn.getSize();
Insets insets = btn.getInsets();
size.width -= insets.left + insets.right;
size.height -= insets.top + insets.bottom;
if (size.width > size.height) {
size.width = -1;
} else {
size.height = -1;
}
Image scaled = master.getScaledInstance(size.width, size.height, java.awt.Image.SCALE_SMOOTH);
btn.setIcon(new ImageIcon(scaled));
}
});
return btn;
} catch (IOException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
/**
* start mouse move
*/
public void startMouse() {
MouseThread mTh = new MouseThread();
mTh.start();
}
/**
* handle Start button click, with initilize Parameters fields and displaying the grids variables and start it..
*
* @param evt
*/
public void startButtonAction(java.awt.event.ActionEvent evt) {
this.generationNoLabel.setText("Please wait..");
//initilize Parameters fields
Parameters.terminalProbability = Double.parseDouble(terminalProbability.getText());
Parameters.crossoverProbability = Double.parseDouble(crossoverProbability.getText());
Parameters.mutationProbability = Double.parseDouble(mutationProbability.getText());
if ((Parameters.terminalProbability < 0 || Parameters.terminalProbability > 1)
|| (Parameters.crossoverProbability < 0 || Parameters.crossoverProbability > 1)
|| (Parameters.mutationProbability < 0 || Parameters.mutationProbability > 1)) {
JOptionPane.showMessageDialog(null, "Please Enter probabilities between 0 and 1 inclusive");
return;
}
Parameters.gridWidth = Integer.parseInt(gridWidth.getText());
Parameters.gridHeight = Integer.parseInt(gridHeight.getText());
if (Parameters.gridWidth > 25 || Parameters.gridHeight > 25) {
JOptionPane.showMessageDialog(null, "Max recommended grid size is 25 * 25!");
return;
}
Parameters.maxDepth = Integer.parseInt(maxDepth.getText());
Parameters.populationSize = Integer.parseInt(populationSize.getText());
Parameters.numberOfGenerations = Integer.parseInt(numberOfGenerations.getText());
startButton.setEnabled(false);
// initilize main program objects like grid and tournament selection, population etc..
Grid g = new Grid(Parameters.gridHeight, Parameters.gridWidth, Parameters.cheeseFraction);
// System.out.println(g.getNumberOfCheeseInGrid());
TournamentSelection sel = new TournamentSelection(Parameters.crossoverProbability);
Population p = new Population(Parameters.populationSize, g, sel);
//now starting evolution
Evolution e = new Evolution(p, Parameters.numberOfGenerations);
e.evolve(this);
//start actual mouse simulation
startMouse();
// startButton.setEnabled(true);
}
/**
* update cell at x,y with picture that have path
*/
public void updatDisplay(int x, int y, String path) {
mainGrid[x][y].removeAll();
buttons[x][y] = makeButton(path);
buttons[x][y].setBackground(Color.green);
mainGrid[x][y].add(buttons[x][y]);
}
/**
* change visited cell to red
*/
public void updateVisitedButton(int x, int y) {
mainGrid[x][y].removeAll();
buttons[x][y].removeComponentListener(null);
buttons[x][y] = new JButton();
buttons[x][y].setBackground(Color.red);
mainGrid[x][y].add(buttons[x][y]);
}
/**
* reinitilize the grid with the given cheese positions
*
* @param cheese
*/
public void initGrid() {
gridPanel.removeAll();
int width = Parameters.gridWidth;
int height = Parameters.gridHeight;
buttons = new JButton[width][height];
gridPanel.setLayout(new GridLayout(width, height));
mainGrid = new JPanel[width][height];
//make all cells green again and put mouse at position 0 0
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
mainGrid[i][j] = new JPanel();
mainGrid[i][j].setLayout(new GridLayout(1, 1));
buttons[i][j] = new JButton();
buttons[i][j].setBackground(Color.green);
mainGrid[i][j].add(buttons[i][j]);
gridPanel.add(mainGrid[i][j]);
}
}
add(gridPanel, BorderLayout.CENTER);
updatDisplay(0, 0, "right.png");
validate();
repaint();
}
public void setBestGenerationsAndGrid(ArrayList<Object[]> bestGenerationsAndGrid) {
this.bestGenerationsAndGrid = bestGenerationsAndGrid;
}
/**
* the class that handles mouse moving on the grid
*/
class MouseThread extends Thread {
Coordinate coord;
boolean waited = false;
public MouseThread() {
}
@Override
public void run() {
//start actual mouse simulation
Object[] temp;
ArrayList<Coordinate> path, grid;
int[] fitness;
for (int j = 0; j < bestGenerationsAndGrid.size(); ++j) {
//have the best of the ith * 10 path and the correpsonding grid
temp = bestGenerationsAndGrid.get(j);
path = (ArrayList<Coordinate>) temp[0];
fitness = (int[]) temp[1];
grid = (ArrayList< Coordinate>) temp[2];
//make all grid green
initGrid();
//put cheese on the grid and the mouse
putCheese(grid);
generationNoLabel.setText("Best of the first " + ((j + 1) * 10) + " generations");
numberOfLeftFood.setText("Number of food left: " + fitness[0]);
numberOfSteps.setText("Number of Steps: " + fitness[1]);
//start simulation
for (int i = 0; i < path.size(); ++i) {
coord = path.get(i);
try {
if (!waited) {
Thread.sleep(3000);
waited = true;
} else {
Thread.sleep(100);
}
updateVisitedButton(mouseX, mouseY);
updatDisplay(coord.i, coord.j, "right.png");
validate();
repaint();
mouseX = coord.i;
mouseY = coord.j;
} catch (InterruptedException ex) {
JOptionPane.showMessageDialog(null, "error");
}
}
}
}
}
}
休息将在下面添加没有足够空间的网格类可以任何人精确定位?如何增加GUI网格大小? 谢谢
答案 0 :(得分:0)
网格类:
import java.util.ArrayList;
/**
* our program is summarize in a mouse looking for cheese on a grid, this class holds everything about the grid and the cheese on it
* @author MouseTeam
*/
public class Grid {
private char[][] grid;
private int width;
private int height;
private double cheeseFraction;
private final MersenneTwister rnd;
private int countCheese;
private ArrayList<Coordinate> points;
public Grid(int height, int width, double cheese){
rnd = Parameters.rnd;
this.width = width;
this.height = height;
this.cheeseFraction = cheese;
points = new ArrayList<Coordinate>();
grid = new char[height][width];
int buff = Parameters.wallBuff;
countCheese=0;
for(int i=buff; i<height-buff; i++)
for(int j=buff; j<width-buff; j++)
if (rnd.nextDouble()<Parameters.cheeseFraction){
grid[i][j] = '*';
points.add(new Coordinate(i,j));
countCheese++;
}
else
grid[i][j] = '-';
}
public Grid(Grid other){
rnd = other.rnd;
this.width = other.width;
this.height = other.height;
this.cheeseFraction = other.cheeseFraction;
grid = new char[height][width];
countCheese = other.countCheese;
for(int i=0; i<height; i++)
for(int j=0; j<width; j++)
grid[i][j] = other.grid[i][j];
}
public ArrayList<Coordinate> getCheesePoints(){
return this.points;
}
/**
* show where are the cheese on the grid
*/
public void printCheeseCoordinatesOnGrid(){
System.out.println("Cheese on grid: ");
for (Coordinate c: points){
System.out.println(c.i+ " "+c.j);
}
}
public boolean isCheese(int i, int j){
return grid[i][j] == '*';
}
public void removeCheese(int i, int j){
if(isCheese(i,j)){
grid[i][j] = '!';
countCheese--;
}
}
public int getNumberOfCheeseInGrid(){
return countCheese;
}
public void printGrid(){
for(int i=0; i<height; i++){
for(int j=0; j<width; j++)
if (grid[i][j]==0)
System.out.print("#");
else
System.out.print(grid[i][j]);
System.out.println();
}
}
boolean ifCheeseAhead(Info info) {
int i, j;
i=info.i;
j=info.j;
if (info.direction.equals("right"))
j = (j+1)%width;
if(info.direction.equals("left")){
if(j == 0) j=width-1;
j = j-1;
}
if (info.direction.equals("down"))
i = (i+1)%height;
if(info.direction.equals("up")){
if(i == 0) i=height-1;
i = i-1;
}
return isCheese(i,j);
}
boolean ifWallAhead(Info info) {
int i, j;
i=info.i;
j=info.j;
if (info.direction.equals("right")){
if(j+3 >=width)
return true;
else
return false;
}
if(info.direction.equals("left")){
if(j-3 <0)
return true;
else
return false;
}
if (info.direction.equals("down")){
if(i+3 >= height)
return true;
else
return false;
}
if(info.direction.equals("up")){
if(i-3 < 0)
return true;
else
return false;
}
return false;
}
boolean ifCheeseConcentrationAhead(Info info) {
int left=0, right=0, up=0, down=0;
int i,j;
for(i=0; i<height; i++){
for(j=info.j+1; j<width; j++)
if(isCheese(i,j))
right++;
for(j=0; j<info.j; j++)
if(isCheese(i,j))
left++;
}
for(j=0; j<width; j++){
for(i=0; i<info.i; i++)
if(isCheese(i,j))
up++;
for(i=info.i+1; i<height; i++)
if(isCheese(i,j))
down++;
}
if (info.direction.equals("right"))
return (right>=left && right>=up && right>=down);
if(info.direction.equals("left"))
return (left>=right && left>=up && left>=down);
if (info.direction.equals("down"))
return (down>=left && down>=right && down>=up);
if(info.direction.equals("up"))
return (up>=left && up>=right && up>=down);
System.err.println("Error direction 1!");
System.exit(1);
return false;
}
String ifSmellFrom(Info info) {
int stepsLeft= Integer.MAX_VALUE, stepsRight=Integer.MAX_VALUE, stepsUp=Integer.MAX_VALUE, stepsDown=Integer.MAX_VALUE;
double wind = windEffect(); //add the dependency upon the wind effect and direction
for(int right = 1; info.j+right<width; right++){
for(int up=0; up<=right && info.i-up>=0; up++)
if (isCheese(info.i-up,info.j+right)){
if (right+up < stepsRight)
stepsRight = right+up;
break;
}
for(int down=0; down<=right && info.i+down<height; down++)
if (isCheese(info.i+down,info.j+right)){
if (right+down < stepsRight)
stepsRight = right+down;
break;
}
}
for(int left = 1; info.j-left>=0; left++){
for(int up=0; up<=left && info.i-up>=0; up++)
if (isCheese(info.i-up,info.j-left)){
if (left+up < stepsLeft)
stepsLeft = left+up;
break;
}
for(int down=0; down<=left && info.i+down<height; down++)
if (isCheese(info.i+down,info.j-left)){
if (left+down < stepsLeft)
stepsLeft = left+down;
break;
}
}
for(int up = 1; info.i-up>=0; up++){
for(int left=0; left<=up && info.j-left>=0; left++)
if (isCheese(info.i-up,info.j-left)){
if (left+up < stepsUp)
stepsUp = left+up;
break;
}
for(int right=0; right<=up && info.j+right<width; right++)
if (isCheese(info.i-up,info.j+right)){
if (right+up < stepsUp)
stepsUp = right+up;
break;
}
}
for(int down = 1; info.i+down<height; down++){
for(int left=0; left<=down && info.j-left>=0; left++)
if (isCheese(info.i+down,info.j-left)){
if (left+down < stepsDown)
stepsDown = left+down;
break;
}
for(int right=0; right<=down && info.j+right<width; right++)
if (isCheese(info.i+down,info.j+right)){
if (right+down < stepsDown)
stepsDown = right+down;
break;
}
}
if (info.direction.equals("right")){
if(stepsRight <= stepsLeft && stepsRight <= stepsUp && stepsRight <= stepsDown) {
if(wind >= 0d && wind <= 0.8d)
return "forward";
else if(wind > 0.8d && wind <= 0.9d) {
return "left";
}else{
return "right";
}
}
if(stepsLeft <= stepsRight && stepsLeft <= stepsUp && stepsLeft <= stepsDown) {
if(wind >= 0d && wind <= 0.8d)
return "backward";
else if(wind > 0.8d && wind <= 0.9d) {
return "left";
}else{
return "right";
}
}
if(stepsUp <= stepsLeft && stepsUp <= stepsRight && stepsUp <= stepsDown){
if(wind >= 0d && wind <= 0.8d)
return "left";
else if(wind > 0.8d && wind <= 0.9d) {
return "forward";
}else{
return "backward";
}
}
if(stepsDown <= stepsLeft && stepsDown <= stepsRight && stepsDown <= stepsUp){
if(wind >= 0d && wind <= 0.8d)
return "right";
else if(wind > 0.8d && wind <= 0.9d) {
return "forward";
}else{
return "backward";
}
}
}
if(info.direction.equals("left")){
if(stepsRight <= stepsLeft && stepsRight <= stepsUp && stepsRight <= stepsDown) {
if(wind >= 0d && wind <= 0.8d)
return "backward";
else if(wind > 0.8d && wind <= 0.9d) {
return "left";
}else{
return "right";
}
}
if(stepsLeft <= stepsRight && stepsLeft <= stepsUp && stepsLeft <= stepsDown){
if(wind >= 0d && wind <= 0.8d)
return "forward";
else if(wind > 0.8d && wind <= 0.9d) {
return "left";
}else{
return "right";
}
}
if(stepsUp <= stepsLeft && stepsUp <= stepsRight && stepsUp <= stepsDown){
if(wind >= 0d && wind <= 0.8d)
return "right";
else if(wind > 0.8d && wind <= 0.9d) {
return "forward";
}else{
return "backward";
}
}
if(stepsDown <= stepsLeft && stepsDown <= stepsRight && stepsDown <= stepsUp){
if(wind >= 0d && wind <= 0.8d)
return "left";
else if(wind > 0.8d && wind <= 0.9d) {
return "forward";
}else{
return "backward";
}
}
}
if (info.direction.equals("down")){
if(stepsRight <= stepsLeft && stepsRight <= stepsUp && stepsRight <= stepsDown){
if(wind >= 0d && wind <= 0.8d)
return "left";
else if(wind > 0.8d && wind <= 0.9d) {
return "forward";
}else{
return "backward";
}
}
if(stepsLeft <= stepsRight && stepsLeft <= stepsUp && stepsLeft <= stepsDown){
if(wind >= 0d && wind <= 0.8d)
return "right";
else if(wind > 0.8d && wind <= 0.9d) {
return "forward";
}else{
return "backward";
}
}
if(stepsUp <= stepsLeft && stepsUp <= stepsRight && stepsUp <= stepsDown) {
if(wind >= 0d && wind <= 0.8d)
return "backward";
else if(wind > 0.8d && wind <= 0.9d) {
return "left";
}else{
return "right";
}
}
if(stepsDown <= stepsLeft && stepsDown <= stepsRight && stepsDown <= stepsUp){
if(wind >= 0d && wind <= 0.8d)
return "forward";
else if(wind > 0.8d && wind <= 0.9d) {
return "left";
}else{
return "right";
}
}
}
if(info.direction.equals("up")){
if(stepsRight <= stepsLeft && stepsRight <= stepsUp && stepsRight <= stepsDown){
if(wind >= 0d && wind <= 0.8d)
return "right";
else if(wind > 0.8d && wind <= 0.9d) {
return "forward";
}else{
return "backward";
}
}
if(stepsLeft <= stepsRight && stepsLeft <= stepsUp && stepsLeft <= stepsDown){
if(wind >= 0d && wind <= 0.8d)
return "left";
else if(wind > 0.8d && wind <= 0.9d) {
return "forward";
}else{
return "backward";
}
}
if(stepsUp <= stepsLeft && stepsUp <= stepsRight && stepsUp <= stepsDown){
if(wind >= 0d && wind <= 0.8d)
return "forward";
else if(wind > 0.8d && wind <= 0.9d) {
return "left";
}else{
return "right";
}
}
if(stepsDown <= stepsLeft && stepsDown <= stepsRight && stepsDown <= stepsUp){
if(wind >= 0d && wind <= 0.8d)
return "backward";
else if(wind > 0.8d && wind <= 0.9d) {
return "left";
}else{
return "right";
}
}
}
System.err.println("Error direction 2!");
System.exit(1);
return null;
}
boolean ifSmellAhead(Info info) {
return ifSmellFrom(info).equals("forward");
}
void ifCheeseAheadNmoveN(Info info, int N, ArrayList<Coordinate> path) {
if (info.direction.equals("right"))
if(info.j+N < width && isCheese(info.i,info.j+N)){
info.remainedSteps-=N;
for(int i=1; i<=N; i++)
path.add(new Coordinate(info.i, info.j+i));
info.j+=N;
}
if(info.direction.equals("left"))
if(info.j-N >= 0 && isCheese(info.i,info.j-N)){
info.remainedSteps-=N;
for(int i=1; i<=N; i++)
path.add(new Coordinate(info.i, info.j-i));
info.j-=N;
}
if (info.direction.equals("down"))
if(info.i+N < height && isCheese(info.i+N,info.j)){
for(int i=1; i<=N; i++)
path.add(new Coordinate(info.i+i, info.j));
info.i+=N;
info.remainedSteps-=N;
}
if(info.direction.equals("up"))
if(info.i-N >= 0 && isCheese(info.i-N,info.j)){
for(int i=1; i<=N; i++)
path.add(new Coordinate(info.i-i, info.j));
info.i-=N;
info.remainedSteps-=N;
}
removeCheese(info.i, info.j);
}
double windEffect() { //used to alter the smell function above on the random probability
boolean effect = rnd.nextBoolean();
if(effect)
return rnd.nextDouble();
else
return 0;
}
boolean ifFearAhead(Info arg0) { //based upon the definition that is mouse fears of something ahead of him and changes direction
//it is done on random probability:
double fearProb = rnd.nextDouble();
if (arg0.direction.equals("right")){
if(fearProb < 1d/4d)
return true;
else
return false;
}
if(arg0.direction.equals("left")){
if(fearProb < 2d/4d)
return true;
else
return false;
}
if (arg0.direction.equals("down")){
if(fearProb < 3d/4d)
return true;
else
return false;
}
if(arg0.direction.equals("up")){
if(fearProb < 4d/4d)
return true;
else
return false;
}
return false;
}
}
答案 1 :(得分:-1)
Parameters.gridWidth = Integer.parseInt(gridWidth.getText());
Parameters.gridHeight = Integer.parseInt(gridHeight.getText());
用途
gridWidth.setText("25");
gridHeight.setText("25");
从更进一步。
这对我来说似乎有些落后。我宁愿定义2个整数常量,并填充参数设置和文本框。