public class Tree {
protected class Node {
Integer val;
Node[] leaves;
Node(int v, int grad) {
val = v;
leaves = new Node[grad];
}
}
int counter = 1;
int grad;
int height;
protected Node root;
public Tree(int grad, int hoehe) throws IllegalArgumentException {
if (grad < 0)
throw new IllegalArgumentException("Grad < 0");
if (hoehe < 0)
throw new IllegalArgumentException("Hoehe < 0");
if (grad == 0 && hoehe > 1)
throw new IllegalArgumentException("Grad = 0 && Hoehe > 1");
root = new Node(0, grad);
inOrderCreate(root, hoehe, grad);
this.height = hoehe;
this.grad = grad;
}
public void inOrderCreate(Node root, int height, int grad) {
if (root != null && height > 1) {
for (int j = 0; j < root.leaves.length; j++) {
if (root.leaves[j] == null)
root.leaves[j] = new Node(0, grad);
else {
for (int i = 0; i < root.leaves[j].leaves.length; i++) {
root.leaves[j].leaves[i] = new Node(0, grad);
}
}
inOrderCreate(root.leaves[j], height - 1, grad);
}
}
}
public int heightOfTree(Node node) {
if (node == null) {
return 0;
} else {
int max = 0;
for (Node n : node.leaves) {
if (heightOfTree(n) > max)
max = heightOfTree(n);
}
return 1 + max;
}
}
public int maxNode(Node node) {
int nodeCount = 0;
for (int i = 0; i < heightOfTree(node); i++) {
nodeCount += Math.pow(node.leaves.length, i);
}
return nodeCount;
}
public int maxLeaf(Node node) {
return (int) Math.pow(node.leaves.length, heightOfTree(node) - 1);
}
public int maxLeaves() {
return maxLeaf(root);
}
public int maxNodes() {
return maxNode(root);
}
public void printMaxNodesAndLeafs() {
System.out.println("Baum des Grades " + grad + " mit der Hoehe " + height);
System.out.println("---------------------------------");
if (root == null) {
System.out.println("maximale Anzahl von Knoten: 0");
System.out.println("maximale Anzahl von Blaettern: 0\n");
} else {
System.out.println("maximale Anzahl von Knoten: " + maxNodes());
System.out.println("maximale Anzahl von Blaettern: " + maxLeaves() + "\n");
}
}
public void filling(Node n) {
if (n != null) {
n.val = counter;
counter++;
for (int j = 0; j < n.leaves.length; j++) {
filling(n.leaves[j]);
}
}
}
This method will fill the binary tree with natural values.
public void fill() {
counter = 1;
if (root != null) {
root.val = counter;
counter++;
for (int j = 0; j < root.leaves.length; j++) {
filling(root.leaves[j]);
}
}
}
public void inOrderSearch(Node p, int val) {
if (p != null) {
if (heightOfTree(p) == val) {
for (int i = (int) ((Math.pow(grad, heightOfTree(p) - 1)) * grad) - 1; i > 0; i--) {
System.out.print(" ");
}
System.out.print(p.val);
}
for (int j = 0; j < p.leaves.length; j++) {
if (j != 0)
System.out.print(" ");
inOrderSearch(p.leaves[j], val);
}
}
}
This is my print method, which should print all values in order.
public void printTree() {
Node p = root;
if (p != null) {
for (int i = heightOfTree(p); i > 0; i--) {
inOrderSearch(root, i);
System.out.print("\n");
}
}
}
}
But as you can on the following images, the print is not symmetric.
This should be the output of this code:
But my output is this here, 10 and 12 should be symmetric:
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am told that A* implementation on following 8 puzzle Solver is wrong, can anybody please tell me where it's wrong and how to correct it?
Also: this throws Exception in thread "main" java.lang.OutOfMemoryError: Java heap space even though Build Process Heap size is set to 2048.
Here is Solver.java
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import java.util.PriorityQueue;
public class Solver {
private int moves = 0;
private SearchNode finalNode;
private Stack<Board> boards;
public Solver(Board initial) {
if (!initial.isSolvable()) throw new IllegalArgumentException("Unsolvable puzzle");
// this.initial = initial;
PriorityQueue<SearchNode> minPQ = new PriorityQueue<SearchNode>(initial.size() + 10);
Set<Board> previouses = new HashSet<Board>(50);
Board dequeuedBoard = initial;
Board previous = null;
SearchNode dequeuedNode = new SearchNode(initial, 0, null);
Iterable<Board> boards;
while (!dequeuedBoard.isGoal()) {
boards = dequeuedBoard.neighbors();
moves++;
for (Board board : boards) {
if (!board.equals(previous) && !previouses.contains(board)) {
minPQ.add(new SearchNode(board, moves, dequeuedNode));
}
}
previouses.add(previous);
previous = dequeuedBoard;
dequeuedNode = minPQ.poll();
dequeuedBoard = dequeuedNode.current;
}
finalNode = dequeuedNode;
}
// min number of moves to solve initial board
public int moves() {
if (boards != null) return boards.size()-1;
solution();
return boards.size() - 1;
}
public Iterable<Board> solution() {
if (boards != null) return boards;
boards = new Stack<Board>();
SearchNode pointer = finalNode;
while (pointer != null) {
boards.push(pointer.current);
pointer = pointer.previous;
}
return boards;
}
private class SearchNode implements Comparable<SearchNode> {
private final int priority;
private final SearchNode previous;
private final Board current;
public SearchNode(Board current, int moves, SearchNode previous) {
this.current = current;
this.previous = previous;
this.priority = moves + current.manhattan();
}
#Override
public int compareTo(SearchNode that) {
int cmp = this.priority - that.priority;
return Integer.compare(cmp, 0);
}
}
public static void main(String[] args) {
int[][] tiles = {{4, 1, 3},
{0, 2, 6},
{7, 5, 8}};
double start = System.currentTimeMillis();
Board board = new Board(tiles);
Solver solve = new Solver(board);
System.out.printf("# of moves = %d && # of actual moves %d & time passed %f\n, ", solve.moves(), solve.moves, (System.currentTimeMillis() - start) / 1000);
}
}
And Board.java, just in case:
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
public final class Board {
private final int[][] tilesCopy;
private final int N;
// cache
private int hashCode = -1;
private int zeroRow = -1;
private int zeroCol = -1;
private Collection<Board> neighbors;
/*
* Rep Invariant
* tilesCopy.length > 0
* Abstraction Function
* represent single board of 8 puzzle game
* Safety Exposure
* all fields are private and final (except cache variables). In the constructor,
* defensive copy of tiles[][] (array that is received from the client)
* is done.
*/
public Board(int[][] tiles) {
//this.N = tiles.length;
this.N = 3;
this.tilesCopy = new int[N][N];
// defensive copy
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (tiles[i][j] >= 0 && tiles[i][j] < N*N) tilesCopy[i][j] = tiles[i][j];
else {
System.out.printf("Illegal tile value at (%d, %d): "
+ "should be between 0 and N^2 - 1.", i, j);
System.exit(1);
}
}
}
checkRep();
}
public int tileAt(int row, int col) {
if (row < 0 || row > N - 1) throw new IndexOutOfBoundsException
("row should be between 0 and N - 1");
if (col < 0 || col > N - 1) throw new IndexOutOfBoundsException
("col should be between 0 and N - 1");
return tilesCopy[row][col];
}
public int size() {
return N;
}
public int hamming() {
int hamming = 0;
for (int row = 0; row < this.size(); row++) {
for (int col = 0; col < this.size(); col++) {
if (tileAt(row, col) != 0 && tileAt(row, col) != (row*N + col + 1)) hamming++;
}
}
return hamming;
}
// sum of Manhattan distances between tiles and goal
public int manhattan() {
int manhattan = 0;
int expectedRow = 0, expectedCol = 0;
for (int row = 0; row < this.size(); row++) {
for (int col = 0; col < this.size(); col++) {
if (tileAt(row, col) != 0 && tileAt(row, col) != (row*N + col + 1)) {
expectedRow = (tileAt(row, col) - 1) / N;
expectedCol = (tileAt(row, col) - 1) % N;
manhattan += Math.abs(expectedRow - row) + Math.abs(expectedCol - col);
}
}
}
return manhattan;
}
public boolean isGoal() {
if (tileAt(N-1, N-1) != 0) return false; // prune
for (int i = 0; i < this.size(); i++) {
for (int j = 0; j < this.size(); j++) {
if (tileAt(i, j) != 0 && tileAt(i, j) != (i*N + j + 1)) return false;
}
}
return true;
}
// change i && j' s name
public boolean isSolvable() {
int inversions = 0;
for (int i = 0; i < this.size() * this.size(); i++) {
int currentRow = i / this.size();
int currentCol = i % this.size();
if (tileAt(currentRow, currentCol) == 0) {
this.zeroRow = currentRow;
this.zeroCol = currentCol;
}
for (int j = i; j < this.size() * this.size(); j++) {
int row = j / this.size();
int col = j % this.size();
if (tileAt(row, col) != 0 && tileAt(row, col) < tileAt(currentRow, currentCol)) {
inversions++;
}
}
}
if (tilesCopy.length % 2 != 0 && inversions % 2 != 0) return false;
if (tilesCopy.length % 2 == 0 && (inversions + this.zeroRow) % 2 == 0) return false;
return true;
}
#Override
public boolean equals(Object y) {
if (!(y instanceof Board)) return false;
Board that = (Board) y;
// why bother checking whole array, if last elements aren't equals
return this.tileAt(N - 1, N - 1) == that.tileAt(N - 1, N - 1) && this.size() == that.size() && Arrays.deepEquals(this.tilesCopy, that.tilesCopy);
}
#Override
public int hashCode() {
if (this.hashCode != -1) return hashCode;
// more optimized version(Arrays.hashCode is too slow)?
this.hashCode = Arrays.deepHashCode(tilesCopy);
return this.hashCode;
}
public Collection<Board> neighbors() {
if (neighbors != null) return neighbors;
if (this.zeroRow == -1 && this.zeroCol == -1) findZeroTile();
neighbors = new HashSet<>();
if (zeroRow - 1 >= 0) generateNeighbor(zeroRow - 1, true);
if (zeroCol - 1 >= 0) generateNeighbor(zeroCol - 1, false);
if (zeroRow + 1 < this.size()) generateNeighbor(zeroRow + 1, true);
if (zeroCol + 1 < this.size()) generateNeighbor(zeroCol + 1, false);
return neighbors;
}
private void findZeroTile() {
outerloop:
for (int i = 0; i < this.size(); i++) {
for (int j = 0; j < this.size(); j++) {
if (tileAt(i, j) == 0) {
this.zeroRow = i; // index starting from 0
this.zeroCol = j;
break outerloop;
}
}
}
}
private void generateNeighbor(int toPosition, boolean isRow) {
int[][] copy = Arrays.copyOf(this.tilesCopy, tilesCopy.length);
if (isRow) swapEntries(copy, zeroRow, zeroCol, toPosition, zeroCol);
else swapEntries(copy, zeroRow, zeroCol, zeroRow, toPosition);
neighbors.add(new Board(copy));
}
private void swapEntries(int[][] array, int fromRow, int fromCol, int toRow, int toCol) {
int i = array[fromRow][fromCol];
array[fromRow][fromCol] = array[toRow][toCol];
array[toRow][toCol] = i;
}
public String toString() {
StringBuilder s = new StringBuilder(4 * N * N); // optimization?
// s.append(N + "\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
s.append(String.format("%2d ", tileAt(i, j)));
}
s.append("\n");
}
return s.toString();
}
private void checkRep() {
assert tilesCopy.length > 0;
}
}
The problem you have is at line
int[][] copy = Arrays.copyOf(this.tilesCopy, tilesCopy.length);
You assume here to have copied the matrix. But actually you've copied the array with references to arrays in the 2nd dimension.
In other words
copy == this.tolesCopy // false
copy[0] == this.tilesCopy[0] // true
copy[1] == this.tilesCopy[1] // true
copy[2] == this.tilesCopy[2] // true
what leads to have the same matrix changed multiple times, and even to an invalid/unsolvable state.
The easiest fix I see in your code would be to change the generateNeighbor method
private void generateNeighbor(int toPosition, boolean isRow) {
Board board = new Board(this.tilesCopy);
if (isRow) swapEntries(board.tilesCopy, zeroRow, zeroCol, toPosition, zeroCol);
else swapEntries(board.tilesCopy, zeroRow, zeroCol, zeroRow, toPosition);
neighbors.add(board);
}
Although the overal idea looks good. There are multiple other things that can be improved. I suggest you to have a look at this implementation
This project involves three classes. Star creates the stars, Starfield creates a matrix of stars, and StarfieldSimualation is the runner
When I omit row 0, it omits the bottom row, instead of the top one. But, it works for rows 1-4. Thanks in advance for the help.
Starfield Code
public class Starfield
{
private Star[][] Sky;
public Starfield(int rows, int cols)
{
if(rows < 0 || cols < 0)
{
rows = cols = 1;
}
Sky = new Star[rows][cols];
}
public String toString()
{
String S = "";
for (Star[] row: Sky)
{
for(Star X: row)
{
if(X != null)
{
S+= X.getImage() + "\t";
}
else
{
S += "- \t";
}
}
S += "\n";
}
return S;
}
public void addStar(String Image, String Constellation, int row, int col)
{
if(row < 0 || row >= Sky.length || col < 0 || col >= Sky[0].length)
{
return;
}
Sky[row][col] = new Star(Image, Constellation, row, col);
}
public Star[][] omitRow(int r)
{
int i, j, k;
k = 0;
Star[][] rowless = new Star[Sky.length - 1][Sky[0].length];
for(i = 0; i < Sky.length-1; i++)
{
for(j = 0; j < Sky[0].length; j++)
{
k++;
}
}
Star[] ans = new Star[k];
k = 0;
for(i = 0; i < Sky.length; i++)
{
for(j = 0; j < Sky[0].length; j++)
{
if(i != r)
{
ans[k] = Sky[i][j];
k++;
}
}
}
k = 0;
for(i = 0; i < rowless.length; i++)
{
for(j = 0; j < rowless[0].length; j++)
{
if(i != r)
{
rowless[i][j] = ans[k];
k++;
}
}
}
return rowless;
}
Star Code
public class Star
{
private Star[][] Sky;
private String Image, Constellation;
private int row, col;
public Star(String I, String C, int r, int c)
{
Image = I;
Constellation = C;
row = r;
col = c;
}
public String getImage()
{
return Image;
}
public String getConstellation()
{
return Constellation;
}
public int getRow()
{
return row;
}
public int getCol()
{
return col;
}
}
Starfield Simluation Code
public class StarFieldSimulation
{
public static void main(String[] args)
{
Starfield theSky = new Starfield(5,4);
theSky.addStar("*", "Orion", 2, 3);
theSky.addStar("*", "Orion", 2, 2);
theSky.addStar("*", "Orion", 2, 1);
theSky.addStar("*", "Orion", 2, 0);
theSky.addStar("*", "Orion", 1, 3);
System.out.println("IT'S THE SKY EVERYONE");
System.out.println(theSky.toString());
Star[][] minusrow = new Star[4][4];
minusrow = theSky.omitRow(4);
System.out.println("A row has been removed");
System.out.println();
for(Star[] row: minusrow)
{
for(Star X: row)
{
if(X != null)
{
System.out.print(X.getImage() + "\t");
}
else
{
System.out.print("-\t");
}
}
System.out.println();
}
System.out.println();
System.out.println("It's the sky again");
System.out.println(theSky.toString());
}
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at HelloWorld.main(HelloWorld.java:169)
These errors continue to pop up when I run my code against our sample input. We are given a document with polygons and have to use kruskal's algorithm and build a minimal spanning tree to find the shortest distance to each island without creating a cycle. If anyone can help or give advice on how to get rid of these errors that would be great! I dont understand how there can be a numberformatexception on a string ""....
import java.util.Scanner;
import java.util.ArrayList;
import java.io.File;
import java.io.IOException;
public class HelloWorld {
static class Point {
int x = 0;
int y = 0;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(x: " + x + " y: " + y + ")";
}
}
static class Polygon {
int numberOfVertices = 0;
ArrayList<Point> points = new ArrayList<Point>();
public Polygon(int numberOfVertices, ArrayList<Point> points) {
this.numberOfVertices = numberOfVertices;
this.points = points;
}
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < points.size(); i++) {
Point point = this.points.get(i);
stringBuilder.append(point);
}
return stringBuilder.toString();
}
}
static class PQItem implements Comparable<PQItem> {
int node1;
int node2;
double edge;
public PQItem(int node1, int node2, double edge) {
this.node1 = node1;
this.node2 = node2;
this.edge = edge;
}
public int compareTo(PQItem T) {
if (edge - T.edge < 0)
return 1;
else if (edge - T.edge > 0)
return -1;
else
return 0;
}
}
public static void BuildMinimalSpanningTree(int numberOfIslands, ArrayList<Polygon> polygons) {
PriorityQueue q = new PriorityQueue((numberOfIslands * numberOfIslands) / 2);
PQItem Temp;
int[] CheckPad = new int[numberOfIslands];
int FootPrint = 0;
int counter = 0;
double length = 0;
for (int i = 0; i < polygons.size(); i++)
for (int j = 0; j < polygons.size(); j++) {
Temp = new PQItem(i, j, ShortestDistance(polygons.get(i), polygons.get(j)));
}
for (int i = 0; i < polygons.size(); i++)
CheckPad[i] = -1 - i;
while (counter < polygons.size() - 1) {
Temp = (PQItem) q.Remove();
for (int i = 0; i < polygons.size(); i++)
for (int j = 0; j < polygons.size(); j++)
if (CheckPad[Temp.node1] != CheckPad[Temp.node2]) {
if (CheckPad[Temp.node1] < 0 && CheckPad[Temp.node2] < 0) {
CheckPad[Temp.node1] = FootPrint;
CheckPad[Temp.node2] = FootPrint;
FootPrint = FootPrint + 1;
}
else if (CheckPad[Temp.node1] < 0) {
CheckPad[Temp.node1] = CheckPad[Temp.node2];
}
else if (CheckPad[Temp.node2] < 0) {
CheckPad[Temp.node2] = CheckPad[Temp.node1];
}
else {
if (CheckPad[Temp.node1] < CheckPad[Temp.node2]) {
for (i = 0; i < polygons.size(); i++) {
if (CheckPad[i] == CheckPad[Temp.node2])
CheckPad[i] = CheckPad[Temp.node2];
else
for (j = 0; j < polygons.size(); j++)
if (CheckPad[j] == CheckPad[Temp.node2])
CheckPad[j] = CheckPad[Temp.node2];
}
}
System.out.println(Temp.edge);
length += Temp.edge;
counter++;
}
}
}
}
static double ShortestDistance(Polygon polygon1, Polygon polygon2) {
double shortestdistance = 0;
double Temporary = 0;
for (int i = 0; i < polygon1.numberOfVertices; i++)
for (int j = 0; j < polygon2.numberOfVertices; j++) {
Temporary = Math.pow(polygon1.points.get(i).x - polygon2.points.get(j).x, 2)
+ Math.pow(polygon1.points.get(i).y - polygon2.points.get(j).y, 2);
if (Temporary < shortestdistance)
shortestdistance = Temporary;
}
return Math.sqrt(shortestdistance);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Please enter the name of the file");
File file = new File(scanner.nextLine());
try {
Scanner fileScanner = new Scanner(file);
int numberOfIslands = Integer.parseInt(fileScanner.nextLine());
ArrayList<Polygon> polygons = new ArrayList<Polygon>();
while (fileScanner.hasNext()) {
String line = fileScanner.nextLine();
String[] numbers = line.split(" ");
ArrayList<Point> points = new ArrayList<Point>();
// PQItem NewItem = new PQItem(Node1, Node2, edge);
// info.Insert(NewItem);
for (int i = 1; i < numbers.length; i += 2) {
Point point = new Point(Integer.parseInt(numbers[i]), Integer.parseInt(numbers[(i + 1)]));
points.add(point);
}
// build tree
Polygon polygon = new Polygon(points.size(), points);
polygons.add(polygon);
// BuildMinSpanTree(numberOfIslands, polygons);
}
for (int i = 0; i < polygons.size(); i++) {
Polygon polygon = polygons.get(i);
System.out.println(polygon);
}
int minimalInterconnect = 0;
int totalLength = 0;
System.out.printf("The minimal interconnect consists of %d bridges with a total length of %d",
minimalInterconnect, totalLength);
} catch (IOException e) {
System.out.println(e);
}
}
HERE IS THE SAMPLE PROGRAM
3
4 0 0 0 1 1 1 1 0
4 2 0 2 1 3 1 3 0
3 4 0 5 0 5 1
public class PriorityQueue {
private Comparable[] HeapArray;
int Last, Limit;
PriorityQueue
public PriorityQueue(int Capacity) {
HeapArray = new Comparable[Capacity + 1];
Last = 0;
Limit = Capacity;
return;
}
// end constructor
public PriorityQueue() {
HeapArray = new Comparable[101];
Last = 0;
Limit = 100;
return;
}
// end constructor
public void Insert(Comparable PQI) {
if (Last == Limit) {
System.out.println("Priority Queue Overflow!");
System.exit(0);
}
// end if
HeapArray[++Last] = PQI;
this.UpHeap(Last);
return;
}
// end public method Insert
private void UpHeap(int k) {
Comparable V;
V = HeapArray[k];
while (k > 1 && HeapArray[k / 2].compareTo(V) < 0) {
HeapArray[k] = HeapArray[k / 2];
k = k / 2;
}
// end while
HeapArray[k] = V;
return;
}
// end private method UpHeap
public Comparable Remove() {
Comparable PQI;
if (Last == 0) {
System.out.println("Priority Queue Underflow!");
System.exit(0);
}
// end if
PQI = HeapArray[1];
HeapArray[1] = HeapArray[Last--];
this.DownHeap(1);
return PQI;
}
// end public method Remove
private void DownHeap(int k) {
Comparable V;
int j;
V = HeapArray[k];
while (k <= Last / 2) {
j = k + k;
if (j < Last && HeapArray[j].compareTo(HeapArray[j + 1]) < 0)
j++;
// end if
if (V.compareTo(HeapArray[j]) >= 0)
break;
// end if
HeapArray[k] = HeapArray[j];
k = j;
}
// end while
HeapArray[k] = V;
return;
}
// end private method DownHeap
public boolean IsEmpty() {
if (Last == 0)
return true;
else
return false;
// end if
}
// end public method IsEmpty
public boolean IsFull() {
if (Last == Limit)
return true;
else
return false;
// end if
}
// end public method IsFull
public int Length() {
return Last;
}
// end public method Length
}
// end class PriorityQueue
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
This is self explanatory. The input is expected as number however the input entered is String ""
The line that reads input and expects the value to be interger :
int numberOfIslands = Integer.parseInt(fileScanner.nextLine());
Provide correct input.
Also you could change the .nextLine() to nextInt()
Like many other person as I see, I have a problem with my implementation of the Minmax algorithm for the game Connect4. In the example below, the algorithm doesn't try to block the player two and I don't understand why. My evaluation function is really basic: a binary function. Maybe it's because of the depth, but I don't think so.
Code:
public class Moteur {
public static void addPawn(int[][] game, int column, int player) {
int i = 0;
while (i < 6 && game[i][column] == 0) {
i++;
}
game[(i==6)?5:i-1][column] = player;
}
public static void kickPawn(int[][] game, int column) {
int i = 0;
while (i < 6 && game[i][column] == 0) {
i++;
}
game[i][column] = 0;
}
public static void AI_play(int[][] game, int depth) {
int max = -10000;
int tmp = 0;
int maxj = -5;
int j;
for (j = 0; j < 7; j++) {
if(game[0][j] == 0) {
addPawn(game, j, 1);
tmp = Min(game, depth - 1);
if (tmp > max) {
max = tmp;
maxj = j;
}
kickPawn(game, j);
}
}
addPawn(game, maxj, 1);
}
public static int Max(int[][] game, int depth) {
if(depth == 0 || winner(game) != 0) {
return eval(game);
}
int max = -10000;
int j, tmp;
for(j = 0; j < 7; j++) {
if(game[0][j] == 0) {
addPawn(game, j, 2);
tmp = Min(game, depth - 1);
if(tmp > max) {
max = tmp;
}
kickPawn(game, j);
}
}
return max;
}
public static int Min(int[][] game, int depth) {
if(depth == 0 || winner(game) != 0) {
return eval(game);
}
int min = 10000;
int j, tmp;
for(j = 0; j < 7; j++) {
if(game[0][j] == 0) {
addPawn(game, j, 1);
tmp = Max(game, depth - 1);
if(tmp < min) {
min = tmp;
}
kickPawn(game, j);
}
}
return min;
}
public static int eval(int[][] game) {
int gameWinner, nb_pawns = 0;
for (int i = 0; i < game.length; i++) {
for (int j = 0; j < game[0].length; j++) {
if(game[i][j] != 0) {
nb_pawns++;
}
}
}
gameWinner = winner(game);
if(gameWinner == 1) {
return 1000 - nb_pawns;
} else if (gameWinner == 2) {
return -1000 + nb_pawns;
} else {
return 0;
}
}
public static int winner(int[][] game) {
Integ e = new Integ();
for (int j = 0; j < game[0].length; j++) {
if(game[5][j]!= 0 && testWinner(game, j, e)) {
return e.getI();
}
}
return 0;
}
public static boolean testWinner(int[][] game, int lastColumn, Integ e) {
int lastRow = 0;
while (lastRow < 6 && game[lastRow][lastColumn] == 0) {
lastRow++;
}
lastRow = lastRow;
int i = 0;
int j = 0;
int currentPlayer = game[lastRow][lastColumn];
e.setI(currentPlayer);
int sequence = 0;
i = lastRow;
boolean b = i < 3
&& game[i][lastColumn] == currentPlayer
&& game[i+1][lastColumn] == currentPlayer
&& game[i+2][lastColumn] == currentPlayer
&& game[i+3][lastColumn] == currentPlayer;
if(b) {
return true;
}
sequence = 0;
j = lastColumn;
do {
j--;
} while(0 < j && game[lastRow][j] == currentPlayer);
if(j < 0 || game[lastRow][j] != currentPlayer) {
j++;
}
while(j <= 6 && game[lastRow][j] == currentPlayer) {
j++;
sequence++;
}
if (sequence >= 4) {
return true;
}
sequence = 0;
i = lastRow;
j = lastColumn;
do {
i--;
j--;
} while(0 < i && 0 < j && game[i][j] == currentPlayer);
if(i < 0 || j < 0 || game[i][j] != currentPlayer) {
i++;
j++;
}
while(i <= 5 && j <= 6 && game[i][j] == currentPlayer) {
i++;
j++;
sequence++;
}
if (sequence >= 4) {
return true;
}
sequence = 0;
i = lastRow;
j = lastColumn;
do {
i++;
j--;
} while(i < 5 && 0 < j && game[i][j] == currentPlayer);
if (5 < i || j < 0 || game[i][j] != currentPlayer) {
i--;
j++;
}
while(0 <= i && j <= 6 && game[i][j] == currentPlayer) {
i--;
j++;
sequence++;
}
if (sequence >= 4) {
return true;
}
return false;
}
public static void main(String[] args) {
int[][] game = new int[6][7];
int depth = 5;
game[5][3] = 2;
game[4][3] = 2;
game[3][3] = 2;
AI_play(game, depth);
//game[4][0] = 2;
//AI_play(game, depth);
//game[5][2] = 2;
//AI_play(game, depth);
//game[5][3] = 2;
//AI_play(game, depth);
//game[1][0] = 2;
//AI_play(game, depth);
//game[5][4] = 2;
//AI_play(game, depth);
for (int i = 0; i < game.length; i++) {
for (int j = 0; j < game[0].length; j++) {
System.out.print(game[i][j]);
}
System.out.println("");
}
}
private static class Integ {
private int i;
public Integ() {
this.i = 0;
}
public void increment() {
this.i = this.i + 1;
}
public int getI() {
return this.i;
}
public void setI(int i) {
this.i = i;
}
}
}
Have tried debugging my MiniMax code but can't seem to find the problem here. The main method is evalGame(), which returns 1 for a win, -1 for a loss and 0 for a tie. Are there any obvious errors I have made in the MiniMax algorithm?
package tictactoe;
import static tictactoe.Player.*;
import java.util.Arrays;
public class MinimaxTTT {
private Player[] board = new Player[9];
private static Player player = X;
public MinimaxTTT() {
Arrays.fill(board, E);
player = X;
}
public MinimaxTTT(Player[] b, Player p) {
for(int i = 0; i < 9; i++) {
board[i] = b[i];
}
player = p;
}
public static boolean checkWin(Player[] b, Player p) {
if((b[0]==p&&b[1]==p&&b[2]==p)
|| (b[0]==p&&b[3]==p&&b[6]==p)
|| (b[0]==p&&b[4]==p&&b[8]==p)
|| (b[0]==p&&b[1]==p&&b[2]==p)
|| (b[1]==p&&b[4]==p&&b[7]==p)
|| (b[0]==p&&b[1]==p&&b[2]==p)
|| (b[2]==p&&b[4]==p&&b[6]==p)
|| (b[2]==p&&b[5]==p&&b[8]==p)
|| (b[0]==p&&b[3]==p&&b[6]==p)
|| (b[3]==p&&b[4]==p&&b[5]==p)
|| (b[0]==p&&b[4]==p&&b[8]==p)
|| (b[2]==p&&b[4]==p&&b[6]==p)
|| (b[3]==p&&b[4]==p&&b[5]==p)
|| (b[1]==p&&b[4]==p&&b[7]==p)
|| (b[2]==p&&b[5]==p&&b[8]==p)
|| (b[3]==p&&b[4]==p&&b[5]==p)
|| (b[0]==p&&b[3]==p&&b[6]==p)
|| (b[2]==p&&b[4]==p&&b[6]==p)
|| (b[6]==p&&b[7]==p&&b[8]==p)
|| (b[6]==p&&b[7]==p&&b[8]==p)
|| (b[1]==p&&b[4]==p&&b[7]==p)
|| (b[2]==p&&b[5]==p&&b[8]==p)
|| (b[6]==p&&b[7]==p&&b[8]==p)
|| (b[0]==p&&b[4]==p&&b[8]==p)) {
return true;
}
return false;
}
private static boolean checkTie(Player[] b) {
for(int i = 0; i < 9; i++) {
if(b[i] == E) {
return false;
}
}
return true;
}
private static boolean gameOver(Player[] b) {
return checkTie(b) || checkWin(b, X) || checkWin(b, O);
}
private static int maxOfArray(int[] a) {
int max = a[0];
for (int i : a)
if (max < i)
max = i;
return max;
}
private static int minOfArray(int[] a) {
int min = a[0];
for (int i : a)
if (min > i)
min = i;
return min;
}
private static int getEmptyNumber(Player[] b) {
int spaces = 0;
for(int i = 0; i < 9; i++) {
if(b[i] == E)
spaces++;
}
return spaces;
}
private static int evalGame(Player[] b, Player p, Player currentP) {
if(gameOver(b)) {
if(checkWin(b, p)) {
return 1;
} else if(checkWin(b, p == X ? O : X)) {
return -1;
} else {
return 0;
}
} else {
int[] arrayEval = new int[getEmptyNumber(b)];
for(int i = 0; i < getEmptyNumber(b); i++) {
arrayEval[i] = evalGame(possibleBoards(b, currentP)[i], p, currentP == X ? O : X);
}
if(currentP == p) {
return maxOfArray(arrayEval);
} else {
return minOfArray(arrayEval);
}
}
}
public static Player[][] possibleBoards(Player[] b, Player p) {
Player[][] toReturn = new Player[getEmptyNumber(b)][9];
int spaces = 0;
for(int i = 0; i < 9; i++) {
if(b[i] == E) {
for(int j = 0; j < 9; j++) {
toReturn[spaces][j] = b[j];
}
toReturn[spaces][i] = p;
spaces++;
}
}
return toReturn;
}
public static void main(String[] args) {
Player[] p = new Player[9];
Arrays.fill(p, E);
p[1] = X;
System.out.println(evalGame(p, O, O));
}
}
I have since looked over my code and realised I made a mistake while debugging. The working, edited code is below for anyone who is interested
package tictactoe;
import static tictactoe.Player.*;
import java.util.Arrays;
public class MinimaxTTT {
private Player[] board = new Player[9];
private Player player = X;
public MinimaxTTT() {
Arrays.fill(board, E);
player = X;
}
public MinimaxTTT(Player[] b, Player p) {
for(int i = 0; i < 9; i++) {
board[i] = b[i];
}
player = p;
}
public Player[] getBoard() {
return board;
}
public void setBoard(Player[] b) {
for(int i = 0; i < 9; i++) {
board[i] = b[i];
}
}
public void setSquare(int i, Player p) {
board[i] = p;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player p) {
player = p;
}
public void switchPlayers() {
player = player == X ? O : X;
}
public boolean checkWin(Player[] b, Player p) {
if(b[0] == p && b[1] == p && b[2] == p
|| b[3] == p && b[4] == p && b[5] == p
|| b[6] == p && b[7] == p && b[8] == p
|| b[0] == p && b[3] == p && b[6] == p
|| b[1] == p && b[4] == p && b[7] == p
|| b[2] == p && b[5] == p && b[8] == p
|| b[0] == p && b[4] == p && b[8] == p
|| b[2] == p && b[4] == p && b[6] == p) {
return true;
}
return false;
}
public boolean checkTie(Player[] b) {
for(int i = 0; i < 9; i++) {
if(b[i] == E) {
return false;
}
}
return true;
}
public boolean gameOver(Player[] b) {
return checkTie(b) || checkWin(b, X) || checkWin(b, O);
}
private int maxOfArray(int[] a) {
int max = a[0];
for (int i : a)
if (max < i)
max = i;
return max;
}
private int minOfArray(int[] a) {
int min = a[0];
for (int i : a)
if (min > i)
min = i;
return min;
}
private int getEmptyNumber(Player[] b) {
int spaces = 0;
for(int i = 0; i < 9; i++) {
if(b[i] == E)
spaces++;
}
return spaces;
}
private int findIndex(int[] is, int n) {
for(int i = 0; i < is.length; i++) {
if(is[i] == n)
return i;
}
return 0;
}
private int[] evalGame(Player[] b, Player p, Player currentP, int depth) {
int[] toReturn = new int[2];
if(gameOver(b)) {
if(checkWin(b, p)) {
toReturn[1] = 10 + depth;
return toReturn;
} else if(checkWin(b, p == X ? O : X)) {
toReturn[1] = depth - 10;
return toReturn;
} else {
toReturn[1] = 0;
return toReturn;
}
} else {
depth += 1;
int[] arrayEval = new int[getEmptyNumber(b)];
for(int i = 0; i < getEmptyNumber(b); i++) {
arrayEval[i] = evalGame(possibleBoards(b, currentP)[i], p, currentP == X ? O : X, depth)[1];
}
if(currentP == p) {
int position = findIndex(arrayEval, maxOfArray(arrayEval));
int base = 0;
for(int i = 0; i < 9; i++) {
if(b[i] == E) {
if(base == position) {
toReturn[0] = i;
break;
} else {
base++;
}
}
}
toReturn[1] = maxOfArray(arrayEval);
return toReturn;
} else {
toReturn[1] = minOfArray(arrayEval);
return toReturn;
}
}
}
public Player[][] possibleBoards(Player[] b, Player p) {
Player[][] toReturn = new Player[getEmptyNumber(b)][9];
int spaces = 0;
for(int i = 0; i < 9; i++) {
if(b[i] == E) {
for(int j = 0; j < 9; j++) {
toReturn[spaces][j] = b[j];
}
toReturn[spaces][i] = p;
spaces++;
}
}
return toReturn;
}
public Player[] move() {
if(!gameOver(board)) {
board[evalGame(board, player, player, 0)[0]] = player;
}
return board;
}
}