Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoneroberto committed May 31, 2019
1 parent 8abdcbc commit 73bf2b4
Show file tree
Hide file tree
Showing 17 changed files with 920 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
strategy/StrategyIF.class
strategy/StrategyNE.class
strategy/StrategyNW.class
strategy/StrategySE.class
strategy/StrategySW.class
unstable/Board.class
unstable/Configuration.class
unstable/Move.class
unstable/Player.class
unstable/Unstable.class
unstable/Utility.class
Unstable.jar
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# unstable
# Unstable

## Introduction
AI playing the Unstable (Chain Reaction) game.

https://brilliant.org/wiki/chain-reaction-game/

The project is implemented using the following AI techniques: Iterative Deepening, Negamax, Alpha-Beta Pruning.

## Installation
Clone and change directory:
```
git clone https://github.com/tassoneroberto/unstable.git
cd unstable
```
(OPTION 1) Compile and run:
```
javac -cp . unstable/Player.java
java unstable/Player
```
(OPTION 2) Make JAR file and run:
```
jar cfe Unstable.jar unstable/Player .
java -jar Unstable.jar
```
41 changes: 41 additions & 0 deletions strategy/StrategyEN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package strategy;

import java.util.Arrays;

import unstable.Board;
import unstable.Configuration;
import unstable.Utility;

public class StrategyEN implements StrategyIF {

@Override
public Configuration[] generateAllNextMoves(int player, Board board) {
Configuration[] moves = new Configuration[board.ownedCells[player] + board.ownedCells[0]];
int index = 0, i, j;
if (player == 1) {
for (j = board.boardValues[0].length - 1; j >= 0; j--) {
for (i = 0; i < board.boardValues.length; i++) {
if (board.boardValues[i][j] >= 0) {
moves[index] = new Configuration(Utility.MOVES[1][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
} else {
for (j = board.boardValues[0].length - 1; j >= 0; j--) {
for (i = 0; i < board.boardValues.length; i++) {
if (board.boardValues[i][j] <= 0) {
moves[index] = new Configuration(Utility.MOVES[2][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
}
Arrays.sort(moves);
return moves;
}
}
41 changes: 41 additions & 0 deletions strategy/StrategyES.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package strategy;

import java.util.Arrays;

import unstable.Board;
import unstable.Configuration;
import unstable.Utility;

public class StrategyES implements StrategyIF {

@Override
public Configuration[] generateAllNextMoves(int player, Board board) {
Configuration[] moves = new Configuration[board.ownedCells[player] + board.ownedCells[0]];
int index = 0, i, j;
if (player == 1) {
for (j = board.boardValues[0].length - 1; j >= 0; j--) {
for (i = board.boardValues.length - 1; i >= 0; i--) {
if (board.boardValues[i][j] >= 0) {
moves[index] = new Configuration(Utility.MOVES[1][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
} else {
for (j = board.boardValues[0].length - 1; j >= 0; j--) {
for (i = board.boardValues.length - 1; i >= 0; i--) {
if (board.boardValues[i][j] <= 0) {
moves[index] = new Configuration(Utility.MOVES[2][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
}
Arrays.sort(moves);
return moves;
}
}
8 changes: 8 additions & 0 deletions strategy/StrategyIF.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package strategy;

import unstable.Board;
import unstable.Configuration;

public interface StrategyIF {
public Configuration[] generateAllNextMoves(int player, Board board);
}
41 changes: 41 additions & 0 deletions strategy/StrategyNE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package strategy;

import java.util.Arrays;

import unstable.Board;
import unstable.Configuration;
import unstable.Utility;

public class StrategyNE implements StrategyIF {

@Override
public Configuration[] generateAllNextMoves(int player, Board board) {
Configuration[] moves = new Configuration[board.ownedCells[player] + board.ownedCells[0]];
int index = 0, i, j;
if (player == 1) {
for (i = 0; i < board.boardValues.length; i++) {
for (j = board.boardValues[0].length - 1; j >= 0; j--) {
if (board.boardValues[i][j] >= 0) {
moves[index] = new Configuration(Utility.MOVES[1][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
} else {
for (i = 0; i < board.boardValues.length; i++) {
for (j = board.boardValues[0].length - 1; j >= 0; j--) {
if (board.boardValues[i][j] <= 0) {
moves[index] = new Configuration(Utility.MOVES[2][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
}
Arrays.sort(moves);
return moves;
}
}
41 changes: 41 additions & 0 deletions strategy/StrategyNW.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package strategy;

import java.util.Arrays;

import unstable.Board;
import unstable.Configuration;
import unstable.Utility;

public class StrategyNW implements StrategyIF {

@Override
public Configuration[] generateAllNextMoves(int player, Board board) {
Configuration[] moves = new Configuration[board.ownedCells[player] + board.ownedCells[0]];
int index = 0, i, j;
if (player == 1) {
for (i = 0; i < board.boardValues.length; i++) {
for (j = 0; j < board.boardValues[0].length; j++) {
if (board.boardValues[i][j] >= 0) {
moves[index] = new Configuration(Utility.MOVES[1][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
} else {
for (i = 0; i < board.boardValues.length; i++) {
for (j = 0; j < board.boardValues[0].length; j++) {
if (board.boardValues[i][j] <= 0) {
moves[index] = new Configuration(Utility.MOVES[2][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
}
Arrays.sort(moves);
return moves;
}
}
41 changes: 41 additions & 0 deletions strategy/StrategySE.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package strategy;

import java.util.Arrays;

import unstable.Board;
import unstable.Configuration;
import unstable.Utility;

public class StrategySE implements StrategyIF {

@Override
public Configuration[] generateAllNextMoves(int player, Board board) {
Configuration[] moves = new Configuration[board.ownedCells[player] + board.ownedCells[0]];
int index = 0, i, j;
if (player == 1) {
for (i = board.boardValues.length - 1; i >= 0; i--) {
for (j = board.boardValues[0].length - 1; j >= 0; j--) {
if (board.boardValues[i][j] >= 0) {
moves[index] = new Configuration(Utility.MOVES[1][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
} else {
for (i = board.boardValues.length - 1; i >= 0; i--) {
for (j = board.boardValues[0].length - 1; j >= 0; j--) {
if (board.boardValues[i][j] <= 0) {
moves[index] = new Configuration(Utility.MOVES[2][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
}
Arrays.sort(moves);
return moves;
}
}
41 changes: 41 additions & 0 deletions strategy/StrategySW.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package strategy;

import java.util.Arrays;

import unstable.Board;
import unstable.Configuration;
import unstable.Utility;

public class StrategySW implements StrategyIF {

@Override
public Configuration[] generateAllNextMoves(int player, Board board) {
Configuration[] moves = new Configuration[board.ownedCells[player] + board.ownedCells[0]];
int index = 0, i, j;
if (player == 1) {
for (i = board.boardValues.length - 1; i >= 0; i--) {
for (j = 0; j < board.boardValues[0].length; j++) {
if (board.boardValues[i][j] >= 0) {
moves[index] = new Configuration(Utility.MOVES[1][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
} else {
for (i = board.boardValues.length - 1; i >= 0; i--) {
for (j = 0; j < board.boardValues[0].length; j++) {
if (board.boardValues[i][j] <= 0) {
moves[index] = new Configuration(Utility.MOVES[2][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
}
Arrays.sort(moves);
return moves;
}
}
41 changes: 41 additions & 0 deletions strategy/StrategyWN.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package strategy;

import java.util.Arrays;

import unstable.Board;
import unstable.Configuration;
import unstable.Utility;

public class StrategyWN implements StrategyIF {

@Override
public Configuration[] generateAllNextMoves(int player, Board board) {
Configuration[] moves = new Configuration[board.ownedCells[player] + board.ownedCells[0]];
int index = 0, i, j;
if (player == 1) {
for (j = 0; j < board.boardValues[0].length; j++) {
for (i = 0; i < board.boardValues.length; i++) {
if (board.boardValues[i][j] >= 0) {
moves[index] = new Configuration(Utility.MOVES[1][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
} else {
for (j = 0; j < board.boardValues[0].length; j++) {
for (i = 0; i < board.boardValues.length; i++) {
if (board.boardValues[i][j] <= 0) {
moves[index] = new Configuration(Utility.MOVES[2][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
}
Arrays.sort(moves);
return moves;
}
}
41 changes: 41 additions & 0 deletions strategy/StrategyWS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package strategy;

import java.util.Arrays;

import unstable.Board;
import unstable.Configuration;
import unstable.Utility;

public class StrategyWS implements StrategyIF {

@Override
public Configuration[] generateAllNextMoves(int player, Board board) {
Configuration[] moves = new Configuration[board.ownedCells[player] + board.ownedCells[0]];
int index = 0, i, j;
if (player == 1) {
for (j = 0; j < board.boardValues[0].length; j++) {
for (i = board.boardValues.length - 1; i >= 0; i--) {
if (board.boardValues[i][j] >= 0) {
moves[index] = new Configuration(Utility.MOVES[1][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
} else {
for (j = 0; j < board.boardValues[0].length; j++) {
for (i = board.boardValues.length - 1; i >= 0; i--) {
if (board.boardValues[i][j] <= 0) {
moves[index] = new Configuration(Utility.MOVES[2][i][j], board);
if (moves[index].heuristic == Utility.MAX_VALUE)
return moves;
index++;
}
}
}
}
Arrays.sort(moves);
return moves;
}
}
Loading

0 comments on commit 73bf2b4

Please sign in to comment.