- added tris class
- added miniMax class
- fixed puzzle8 initialization
This commit is contained in:
2023-12-12 14:26:54 +01:00
parent a093dc93e7
commit c4d4bc76b8
3 changed files with 69 additions and 11 deletions

View File

@@ -1,10 +1,10 @@
package net.berack.upo.ai.problem1;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Random;
/**
* Classe utilizzata per la rappresentazione del problema delle 8 tessere.
@@ -27,18 +27,23 @@ public class Puzzle8 implements Iterable<Integer> {
RIGHT
}
private int[] puzzle = new int[LENGTH * LENGTH];
private int[] puzzle;
private int blank = 0;
/**
* Genera una nuova istanza del problema con le tessere posizionate in modo casuale.
*/
public Puzzle8() {
var values = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8);
Collections.shuffle(values);
var rand = new Random();
this.puzzle = new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8};
for(var i = this.puzzle.length - 1; i > 0; i--) {
var j = rand.nextInt(i + 1);
var temp = this.puzzle[i];
this.puzzle[i] = this.puzzle[j];
this.puzzle[j] = temp;
for(var i = 0; i < this.puzzle.length; i++) {
this.puzzle[i] = values.get(i).intValue();
if(this.puzzle[i] == 0) this.blank = i;
}
}
@@ -53,7 +58,7 @@ public class Puzzle8 implements Iterable<Integer> {
* @throws UnsupportedOperationException nel caso la mossa non sia disponibile.
*/
public Puzzle8(Puzzle8 original, Move move) {
Arrays.copyOf(original.puzzle, this.puzzle.length);
this.puzzle = Arrays.copyOf(original.puzzle, this.puzzle.length);
this.blank = original.blank;
this.move(move);
@@ -75,7 +80,7 @@ public class Puzzle8 implements Iterable<Integer> {
throw new IllegalArgumentException("The size of the array must be " + LENGTH*LENGTH);
var check = new int[LENGTH * LENGTH];
Arrays.copyOf(values, values.length);
this.puzzle = Arrays.copyOf(values, values.length);
for(var i = 0; i < this.puzzle.length; i++) {
var curr = this.puzzle[i];
@@ -116,7 +121,8 @@ public class Puzzle8 implements Iterable<Integer> {
* @return il valore della tessera nelle coordinate selezionate
*/
public int get(int x, int y) {
return puzzle[x * LENGTH + y];
if(x >= LENGTH) throw new IndexOutOfBoundsException();
return puzzle[y * LENGTH + x];
}
/**

View File

@@ -0,0 +1,14 @@
package net.berack.upo.ai.problem2;
public class MiniMax<State, Action> {
public Action nextMax(int depth) {
return null;
}
public Action nextMin(int depth) {
return null;
}
}

View File

@@ -1,6 +1,7 @@
package net.berack.upo.ai.problem2;
import java.util.Arrays;
import java.util.Objects;
public class Tris {
public static final int LENGTH = 3;
@@ -14,16 +15,53 @@ public class Tris {
VALUE_O
}
private State[] tris = new State[LENGTH * LENGTH];
private State[] tris;
/**
* Crea una nuova istanza del gioco con tutti gli spazi vuoti
*/
public Tris() {
this.tris = new State[LENGTH * LENGTH];
Arrays.fill(tris, State.EMPTY);
}
/**
* Crea una nuova istanza del gioco a partire da quella passata in input
* @param current l'istanza correte
* @param state lo stato che si vuole mettere ad una cella
* @param x la coordinata x in cui mettere lo stato
* @param y la coordinata y in cui mettere lo stato
*/
public Tris(Tris current, State state, int x, int y) {
Arrays.copyOf(current.tris, current.tris.length);
this.set(state, x, y);
}
/**
* Permette di prendere il valore della cella specificata dalle coordinate
* @param x la coordinata x da cui ricevere lo stato
* @param y la coordinata y da cui ricevere lo stato
* @return il valore della cella
*/
public State get(int x, int y) {
if(x >= LENGTH) throw new IndexOutOfBoundsException();
return this.tris[y * LENGTH + x];
}
/**
* Permette di mettere lo stato scelto nella cella specificata dalle coordinate.
* Nel caso in cui lo stato scelto dalle coordinate non risulti EMPTY
* il metodo lancerà una eccezione.
* @param state lo stato da impostare
* @param x la coordinata x in cui mettere lo stato
* @param y la coordinata y in cui mettere lo stato
*/
public void set(State state, int x, int y) {
if(x >= LENGTH) throw new IndexOutOfBoundsException();
var index = y * LENGTH + x;
if(this.tris[index] != State.EMPTY)
throw new IllegalArgumentException();
this.tris[index] = Objects.requireNonNull(state);
}
}