Salutare , am si eu nevoie de putin ajutor la un proiect in Java pentru facultate.
Trebuie sa creez o interfata cu o tabla de sah (si sa am un buton de pe care sa aleg marimea tablei care sa fie intre 5x5-20x20) ,calul trebuie sa inceapa pe patratelul pe care am dat click si sa inceapa sa parcurga toata tabla fara sa mearga pe acelasi patratel de 2 ori .Arătaţi mişcările calului pe fiecare drum. Setaţi viteza de deplasare la un nivel
rezonabil sau permiteţi prin intermediul uni Slider setarea aceasta.(Asta am reusit sa o fac singur)
- Afişaţi timpul necesar pentru calcul şi apoi toate mişcările făcute. (timpu nu am reusit sa il fac dar miscariile mi le afiseaza ,pe fiecare patratel pe care l-a parcurs apare numaru la mutare)
- Salvaţi mişcările şi poziţie iniţială a calului.
Eu am probleme la partea cu marimea tablei doarece am luat un algoritm care momentam fuctioneaza doar daca ii dau marimea de 5x5 6x6 7x7 8x8 dar o dau mai mare se blocheaza .
Nu reusesc sa schimb pozitia de start doar pe 0,0 merge deocamdata , iar inca o problema o mai am la faptul ca inloc sa inceapa de la 1 numaratoarea miscariilor incepe de la 2 asa ca ultima mutare pe ultimul patratel nu se face mai ca ajunge la 64 de mutarii ..
In mare parte pe mine m-a ajutat pana acuma un prieten care se are mai bine cu programarea ca mine ,pe mine nu ma prea pasioneza si as fii foarte recunoscator daca cineva m-ar putea ajuta cu cateva sfaturi ,nu cred ca mai am foarte multe de schimbat la cod .
https://imgur.com/bas7HsF
Aceasta e prima casa Main.java
package application;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;
import application.Postion;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
public class Main extends Application {
TextField s_X;
TextField s_Y;
TextField s_S;
ImageView cal;
int display_index = 0;
double last_delay = 0;
int n ;
public Scene buildBoard()
{
Image SQ_W = new Image(new File("C:\\Users\\New one\\Desktop\\square_W.png").toURI().toString());
Image SQ_B = new Image(new File("C:\\Users\\New one\\Desktop\\square_B.png").toURI().toString());
n = Integer.valueOf(s_S.getText().toString());
VBox rows = new VBox(2);
boolean white = true;
// CREARE TABLA DE SAH //
for(int i=0;i<n;i++)
{
HBox columns = new HBox(2);
rows.getChildren().add(columns);
for(int j=0;j<n;j++)
{
if(white)
columns.getChildren().add(new ImageView(SQ_W));
else
columns.getChildren().add(new ImageView(SQ_B));
white = !white;
}
if(n%2==0)
white = !white;
}
HBox items= new HBox(10);
Label text_slider= new Label();
text_slider.setText("Speed: ");
Slider slider = new Slider(200, 1000, 500);
items.getChildren().add(text_slider);
items.getChildren().add(slider);
rows.getChildren().add(items);
Pane pane= new Pane();
if(solve.solveKT(Integer.valueOf(s_S.getText().toString()),
Integer.valueOf(s_X.getText().toString()),
Integer.valueOf(s_Y.getText().toString())))
{
display_index = 0;
cal = new ImageView(new Image(new File("C:\\Users\\New one\\Desktop\\horse.png").toURI().toString()));
pane.getChildren().add(cal);
startTimer(slider,pane);
}
else
{
new Alert(AlertType.ERROR).showAndWait();
}
Pane root = new Pane();
root.getChildren().addAll(rows,pane);
Scene scene = new Scene(root,
64 * n + 2 * (n-1)+ 5,
64 * n + 2 * (n-1)+ 25);
return scene;
}
private void startTimer(Slider slider, Pane parent) {
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
public void run() {
if(solve.solution[display_index] == null)
{
timer.cancel();
return;
}
Postion t = solve.solution[display_index++];
Postion global = getPosition(t);
Platform.runLater(new Runnable(){
@Override
public void run()
{
Label text_index= new Label();
text_index.setText(String.valueOf(display_index));
text_index.setLayoutX(global.x + 32);
text_index.setLayoutY(global.y + 32);
parent.getChildren().add(text_index);
cal.setLayoutX(global.x);
cal.setLayoutY(global.y);
}});
if(slider.getValue() != last_delay)
{
timer.cancel(); // cancel time
last_delay = slider.getValue();
startTimer(slider,parent);
}
// start the time again with a new period time
}
}, 0, (int)slider.getValue());
}
public Scene startPage()
{
VBox rows = new VBox(5);
HBox items1 = new HBox(10);
Label text_marime = new Label();
text_marime.setText("Marime tabla: ");
TextField input_marime = new TextField();
items1.getChildren().add(text_marime);
items1.getChildren().add(input_marime);
HBox items2 = new HBox(44);
Label text_x_start = new Label();
text_x_start.setText("Start X: ");
TextField input_x_start = new TextField();
items2.getChildren().add(text_x_start);
items2.getChildren().add(input_x_start);
HBox items3 = new HBox(44);
Label text_y_start = new Label();
text_y_start.setText("Start Y: ");
TextField input_y_start = new TextField();
items3.getChildren().add(text_y_start);
items3.getChildren().add(input_y_start);
HBox items4 = new HBox(10);
Button btn_ok = new Button();
btn_ok.setText("OK");
btn_ok.setOnAction(event -> btn_OK());
items4.getChildren().add(btn_ok);
rows.getChildren().add(items1);
rows.getChildren().add(items2);
rows.getChildren().add(items3);
rows.getChildren().add(items4);
s_S = input_marime;
s_X = input_x_start;
s_Y = input_y_start;
Scene scene = new Scene(rows,300,125);
return scene;
}
public Postion getPosition(Postion l)
{
Postion ret = new Postion();
ret.x = 64 * l.x + 2 * (l.x-1);
ret.y = 64 * l.y + 2 * (l.y-1);
return ret;
}
public void btn_OK()
{
Scene scene = buildBoard();
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
@Override
public void start(Stage primaryStage) {
try {
//BorderPane root = new BorderPane();
Scene scene = startPage();
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Acesta este algoritmu de parcurgere a tablei , solve.java.
package application;
import application.Postion;
public class solve
{
static int N = 8;
//FUNCTIE CARE VERIFICA DACA PARAMETRII SUNT CUPRINSI IN MARIMEA TABLEI //
static boolean isSafe(int x, int y, int sol[][]) {
return (x >= 0 && x < N && y >= 0 &&
y < N && sol[x][y] == -1);
}
static Postion solution[];
/* A utility function to print solution
matrix sol[N][N] */
static void printSolution(int sol[][]) {
}
/* This function solves the Knight Tour problem
using Backtracking. This function mainly
uses solveKTUtil() to solve the problem. It
returns false if no complete tour is possible,
otherwise return true and prints the tour.
Please note that there may be more than one
solutions, this function prints one of the
feasible solutions. */
static boolean solveKT(int size, int s_X,int s_Y) {
int sol[][] = new int[size][size];
N=size;
solution = new Postion[size*size+1];
/* Initialization of solution matrix */
for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
sol[x][y] = -1;
// xMove si yMove mutariile posibile ale calului //
int xMove[] = {2, 1, -1, -2, -2, -1, 1, 2};
int yMove[] = {1, 2, 2, 1, -1, -2, -2, -1};
// Since the Knight is initially at the first block
sol[s_X][s_Y] = 0;
solution[0] = new Postion(s_X,s_Y);
/* Start from 0,0 and explore all tours using
solveKTUtil() */
if (!solveKTUtil(s_X, s_Y, 1, sol, xMove, yMove)) {
System.out.println("Solution does not exist");
return false;
} else
printSolution(sol);
return true;
}
//Algoritmul lui lee pentru parcurgerea intregii table //
static boolean solveKTUtil(int x, int y, int movei,
int sol[][], int xMove[],
int yMove[]) {
int k, next_x, next_y;
if (movei == N * N)
return true;
solution[movei] = new Postion(x,y);
/* Try all next moves from the current coordinate
x, y */
for (k = 0; k < 8; k++) {
next_x = x + xMove[k];
next_y = y + yMove[k];
if (isSafe(next_x, next_y, sol)) {
sol[next_x][next_y] = movei;
if (solveKTUtil(next_x, next_y, movei + 1,
sol, xMove, yMove)) {
if( solution[movei] == null) solution[movei] = new Postion(x,y);
return true;
}
else
sol[next_x][next_y] = -1;
}
}
return false;
}
}