From c4d4bc76b8407a8fed253da5a696df586ab8477f Mon Sep 17 00:00:00 2001 From: Berack96 Date: Tue, 12 Dec 2023 14:26:54 +0100 Subject: [PATCH] Tris - added tris class - added miniMax class - fixed puzzle8 initialization --- .../net/berack/upo/ai/problem1/Puzzle8.java | 24 +++++++---- .../net/berack/upo/ai/problem2/MiniMax.java | 14 +++++++ .../java/net/berack/upo/ai/problem2/Tris.java | 42 ++++++++++++++++++- 3 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 src/main/java/net/berack/upo/ai/problem2/MiniMax.java diff --git a/src/main/java/net/berack/upo/ai/problem1/Puzzle8.java b/src/main/java/net/berack/upo/ai/problem1/Puzzle8.java index 97be53c..73f0a15 100644 --- a/src/main/java/net/berack/upo/ai/problem1/Puzzle8.java +++ b/src/main/java/net/berack/upo/ai/problem1/Puzzle8.java @@ -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 { 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 { * @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 { 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 { * @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]; } /** diff --git a/src/main/java/net/berack/upo/ai/problem2/MiniMax.java b/src/main/java/net/berack/upo/ai/problem2/MiniMax.java new file mode 100644 index 0000000..b780b3c --- /dev/null +++ b/src/main/java/net/berack/upo/ai/problem2/MiniMax.java @@ -0,0 +1,14 @@ +package net.berack.upo.ai.problem2; + +public class MiniMax { + + + + public Action nextMax(int depth) { + return null; + } + + public Action nextMin(int depth) { + return null; + } +} diff --git a/src/main/java/net/berack/upo/ai/problem2/Tris.java b/src/main/java/net/berack/upo/ai/problem2/Tris.java index 03816bf..d9c940c 100644 --- a/src/main/java/net/berack/upo/ai/problem2/Tris.java +++ b/src/main/java/net/berack/upo/ai/problem2/Tris.java @@ -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); } }