Related
After studying many websites with MiniMax algorithm I found one which is the easiest to understand - well, almost as I have a problem to figure out a part of the source code. It is very well known MiniMax in Java taken from here.
Unfortunately I dont understand how the evaluation function is called. It has 6 function parameters but I don't know where these numbers are coming from (I mean zero, one and two) like below:
score += evaluateLine(0, 0, 0, 1, 0, 2); // row 0
The function "evaluateLine" parameters are:
private int evaluateLine(int row1, int col1, int row2, int col2, int row3, int col3)
But still I don't know what these numbers mean. Anyone can help me with it as I completely stuck.
/** The heuristic evaluation function for the current board
#Return +100, +10, +1 for EACH 3-, 2-, 1-in-a-line for computer.
-100, -10, -1 for EACH 3-, 2-, 1-in-a-line for opponent.
0 otherwise */
private int evaluate() {
int score = 0;
// Evaluate score for each of the 8 lines (3 rows, 3 columns, 2 diagonals)
score += evaluateLine(0, 0, 0, 1, 0, 2); // row 0
score += evaluateLine(1, 0, 1, 1, 1, 2); // row 1
score += evaluateLine(2, 0, 2, 1, 2, 2); // row 2
score += evaluateLine(0, 0, 1, 0, 2, 0); // col 0
score += evaluateLine(0, 1, 1, 1, 2, 1); // col 1
score += evaluateLine(0, 2, 1, 2, 2, 2); // col 2
score += evaluateLine(0, 0, 1, 1, 2, 2); // diagonal
score += evaluateLine(0, 2, 1, 1, 2, 0); // alternate diagonal
return score;
}
/** The heuristic evaluation function for the given line of 3 cells
#Return +100, +10, +1 for 3-, 2-, 1-in-a-line for computer.
-100, -10, -1 for 3-, 2-, 1-in-a-line for opponent.
0 otherwise */
private int evaluateLine(int row1, int col1, int row2, int col2, int row3, int col3) {
int score = 0;
Basically I'm trying to improve on the Ghosts in a Pacman game I'm making. In the orginal pacman when a Ghost is eaten when Pacman has picked up the Power the Ghosts eyes would navigate back to the home area and then spawn the ghost back in. I would like to do this to. It would also help me with implementing a Ghost AI to make them move smarter then just random.
So basically those eyes would have to navigate through this:
And the board is being drawn from the below 2D array:
//0's = Walls or location not allowed to go
//1's = Dot Spot
//2's = Clear Path nothing on it but safe to move
//3's = Power Dot
//-1's = only ghosts can go through
//5= Top entry spot
//6= bottom entry point
//7 = Cherry
//(Spots = row - 1 same with columns = - 1. First # is row. Second is col
public int board[][] =
{{2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}, //1
{2, 0, 3, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 3, 0, 2}, //2
{2, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 2}, //3
{2,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,2}, //4
{2,0,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0,2}, //5
{2,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,2}, //6
{2,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,2}, //7
{2,2,2,2,0,1,0,1,1,1,1,1,1,1,0,1,0,2,2,2,2}, //8
{0,0,0,0,0,1,0,1,0,0,-1,0,0,1,0,1,0,0,0,0,0}, //9
{5,2,2,2,2,1,1,1,0,2,2,2,0, 1 ,1,1,2,2,2,2,6}, //10 - cherry
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0}, //11
{2,2,2,2,0,1,0,1,1,1,2,1,1,1,0,1,0,2,2,2,2}, //12
{2,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,2}, //13
{2,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,2}, //14 - pacman on this row
{2,0,1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1,0,2}, //15
{2,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,2}, //16
{2,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,0,2}, //17
{2,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,2}, //18
{2,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,2}, //19
{2,0,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,0,2}, //20
{2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2}}; //21
So my question is how should I go about making the eyes go to the centre home spot in the fastest way without going through walls?
Just use a precomputed inkblot. The "home" squares are labelled zero. Then, all unassigned neighbour squares of a square labelled n are assigned label n +1. Now all your "dead" ghosts have to do is move to a neighbouring square with a lower label. Eventually they will get home having taken the shortest path. Easy!
You can look into Lee's algorithm for shortest path inside a matrix with obstacles.
I want to create a line graph but the range of numbers are different sometimes its like :
1,2,5,8,10
and sometimes its like :
-5 , -1 , 4 , 5, 15 , 8 ,20 ,...
by studying excel and openoffice I understand that they can indicate a range something like :
minNumber - k to maxNumber + k
and they divide the Y axis into equal area.
is there any specific formula for doing these things ?
When I looked at this problem I found that commercial products do a nice job of extending the range just enough to achieve pleasingly nice round numbers for their tick marks. If this is something you are interested in, take a look at this C# code that I wrote to try out different algorithms:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TickPicker
{
class TickPicker
{
double tickLow;
double tickHigh;
double tickUnit;
double tickCount;
//static double[] targetUnits = { 1, 2, 5, 10 };
static double[] targetUnits = { 1, 2, 2.5, 5, 10 };
static int[] roundFactors = { 1, 5, 4, 2, 1 };
//static double[] targetUnits = { 1, 1.5, 2.5, 5, 10 };
//static double[] targetUnits = { 1, 1.25, 2, 2.5, 5, 10 };
//static double[] targetUnits = { 1, 1.25, 1.5, 2, 2.5, 5, 10 };
//static double[] targetUnits = { 1, 1.25, 2, 5, 10 };
//static double[] targetUnits = { 1, 1.25, 1.5, 2, 5, 10 };
static double[] targetLogs = arrayLog(targetUnits);
TickPicker(double low, double high, int tickCountGoal)
{
double range = high - low;
double divGoal = range / (tickCountGoal - 2);
double logGoal = Math.Log10(divGoal);
double powerFactor = Math.Floor(logGoal);
logGoal = logGoal - powerFactor;
int closestIndex = findClosest(targetLogs, logGoal);
tickUnit = targetUnits[closestIndex] * (Math.Pow(10, powerFactor));
// Ensure the actual range encompasses the intended range
// The roundFactor discourages the .5 on the low and high range
int roundFactor = roundFactors[closestIndex];
tickLow = Math.Floor(low / tickUnit / roundFactor) * tickUnit * roundFactor;
tickHigh = Math.Ceiling(high / tickUnit / roundFactor) * tickUnit * roundFactor;
tickCount = (tickHigh - tickLow) / tickUnit;
}
static double[] arrayLog(double[] inputs)
{
double[] retVal = new double[inputs.Length];
int x = 0;
foreach (double input in inputs)
{
retVal[x] = Math.Log10(inputs[x]);
x++;
}
return retVal;
}
static int findClosest(double[] candidates, double input)
{
int low = 0;
for(int i = 1; i < candidates.Length && input > candidates[i]; i++)
{
low = i;
}
int high = low + 1;
return candidates[high] - input < input - candidates[low] ? high : low;
}
static void testPicker(double low, double high, int tickCountGoal)
{
TickPicker picker = new TickPicker(low, high, tickCountGoal);
System.Console.WriteLine("[{0}:{1}]/{2} gives [{3}:{4}] with {5} ticks of {6} units each.", low, high, tickCountGoal, picker.tickLow, picker.tickHigh, picker.tickCount, picker.tickUnit);
}
static void Main(string[] args)
{
testPicker(4.7, 39.2, 13);
testPicker(4.7, 39.2, 16);
testPicker(4.7, 39.2, 19);
testPicker(4.7, 39.2, 21);
testPicker(4.7, 39.2, 24);
testPicker(1967, 2011, 20);
testPicker(1967, 2011, 10);
testPicker(2.71, 3.14, 5);
testPicker(.0568, 13, 20);
}
}
}
I think you mean minNumber and not meanNumber.
The formula is close to what you say. Your range is maxNumber - minNumber. If you want some space on either side add 2k, where k is the space for either side. You know your graph is Y pixels wide. Y/range is how many pixels there are per unit for your graph.
You basically apply this adjustment while drawing the graph. Apply a shift based on your min, and then a strech based on your pixel/unit.
So Drawing point X means you are actually drawing at ((X - min) / pixels per unit).
I'm not 100% convinced that this is a programming question (although there are a decent handful of graphing libraries one could use), but the general idea would be this.
You have these points coming in, let's say -5. That's the 0th value we've received, so for an x value of 0, we place -5 on the y-axis. We repeat for -1, 4, etc.
So, you'd get a list looking something like this (figuratively):
X | Y
0 | -5
1 | -1
2 | 4
3 | 5
4 | 15
The result is a scatterplot (not a line), but there are tools in Excel to get that for you.
[EDIT] To get it as an actual function, you could use the following form:
y-y_1=m(x-x_1)
Where m = (y_2-y_1)/(x_2-x_1), y_2 is your highest y-value, y_1 is your lowest, x_2 is your highest x-value, and x_1 is your lowest.
I'm using a 2D Array in Java and it's the first time doing so. I'm wondering what is wrong with the array as I'm getting many errors...? (probably a really stupid error)
Code
int board[21][21] = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,1,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1},
{1,0,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,1},
{1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1},
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0},
{1,1,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1},
{0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0},
{1,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,1},
{1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,1,0,1},
{1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1},
{1,0,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,0,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1},
{1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
Error List
Here is a link instead of posting it here since it's a lot of errors
You can't specify the size of arrays in the declaration. If you remove your size integers, it will compile. However, you should always place your brackets on your variable's type, not on the variable itself when you declare it as such:
int[][] board;
rather than:
int board[][];
If you want to create a blank array with a specific size, then you would do so with the new operator:
int[][] board = new int[21][21];
However, you never specify a size if you define your array though array initialization i.e.
int[][] board = {{...}, ...};
Remove 21. You should say
int board[][] = ....
You have to define dimensions of array when creating them. For example you can say new int[5] and create 5 elements long int array. But when you assign this array to variable you say: int[] arr = new int[5]. Here int[] is a type (int array). It is similar to int * in C. You do not have to say how big will be the array when you are defining its type. You just have to say what is its element type and how many dimensions it will have.
Firstly, you get a compile error for telling the size of your 2D array in the declaration
int board[21][21]
usually you want to do it this way:
int board[][] = {{1,2}{1,3}}
This is a 2D array with 2 columns and 2 rows
1 2
1 3
So if you want to a 2D array with 21 rows and 21 columns you should do this:
int board[][] = {{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{],{},{},{},{},{}}
and fill whatever you need in between the inner brackets.
Change first line to board[][] ={{ all your numbers}} and all will be ok.
Your declaration is wrong.
int board[][] = new int[][] {{...
remove dimensions from declaration:
int board[][]
I'm trying to get a 3D array initialized for a game I'm working on, after multiple syntax changes I couldn't figure out how to get it to work! What I started with was:
public class AnimationView extends SurfaceView implements SurfaceHolder.Callback {//Create bitmaps.
Bitmap bitmapGoal = BitmapFactory.decodeResource(this.getResources(), R.drawable.goal);
Bitmap bitmapOrig = BitmapFactory.decodeResource(this.getResources(), R.drawable.ball);
Bitmap bitmap = Bitmap.createScaledBitmap(bitmapOrig, 150, 150, true);
//initialize the canvas.
private Canvas c;
private int score[] = {0, 0, 0, 0};
public int numBalls = 1;
//we support up to 4 balls. thus each array is 4 bit.
private int ballX[] = {0, 200, 400, 600};
private double ballY[] = {0, 0, 0, 0};
private double dirV[] = {0, 0, 0, 0};
private int dirH[] = {30, 30, 30, 30};
private static final int SCALE = 10;
private double elasticity = .6;
private int rotationNow[] = {5, 5, 5, 5};
private int rotationDraw[] = {0, 0, 0, 0};
class AnimationThread extends Thread {
//Are we running currently?
private boolean mRun;
//layer 1 is how many balls, 4 layers deep.
//layer 2 is which ball we're talking about, either 1, 2, 3, or 4 layers deep, depending on layer 1.
//layer 3 is the bounds of the ball, dependent on how many there are total.
//layer 3 is formatted x-min, x-max, y-min, y-max
int[][][] bounds = new int[][][] {
{ {0, c.getWidth() - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} },
//end first layer
{ {0, c.getWidth() / 2 - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {c.getWidth() / 2, c.getWidth() - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {0, 0, 0, 0}, {0, 0, 0, 0} },
//end second layer
{ {0, c.getWidth() / 3 - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {c.getWidth() / 3, c.getWidth() * 2 / 3 - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {c.getWidth() * 2 / 3, c.getWidth() - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {0, 0, 0, 0} },
//end third layer
{ {0, c.getWidth() / 2, 0, c.getHeight() / 2}, {c.getWidth() / 2, c.getWidth(), 0, c.getHeight() / 2}, {0, c.getWidth() / 2, c.getHeight() / 2, c.getHeight()}, {c.getWidth() / 2, c.getWidth(), c.getHeight() / 2, c.getHeight()} }
//end fourth, and final layer!
};
Sorry about the weird formatting error. I know this doesn't help anything. There is a new line between ymax and int[][][].
You don't exactly need to look through it and understand, but this compiled and then errored out during execution. So then I tried to make a simple 3D array, I started with:
int[][][] bounds = new int[1][1][1];
bounds[0][0][0] = 0;
Eclipse had it's red squiggly under the semi-colon on the first line. Saying
'Syntax error on token ";", { expected after this token'
This is where it gets frustrating. Because that exact same code copy/pasted into a regular Java program works fine, but I can NOT get it to work inside an Android project. I then simplified some stuff, to this:
int[] bounds = new int[1];
bounds[0] = 0;
Exact same error, exact same place! Why Eclipse?? I also tried it with "int bounds[][][]" as opposed to "int[][][] bounds" but no difference, still same error.
I've rebooted my computer, cleaned my project multiple times, restarted Eclipse. I'm out of ideas. Do you have any??
Well, seems like the problem is not before, but after the code you pasted.
this assignment - bounds[0][0][0] = 0; is probably not in any method and this is illegal. When Eclipse sees an expression that need to be inside a method, it expects the line above to be the method declaration, so it expects '{' as a beginning of a method block, and not ';'
Ok, I feel ridiculous. After being very, very confused at why the variable couldn't initialize and why the very simple code then wouldn't compile. It turns out, though the canvas and bitmaps were available, it was infact them returning null values into the array.
So I got it working now.
Also, for my first question here, I was extremely impressed with the speediness of the solutions. Thanks a ton!
int[][][] bounds = new int[1][1][1];
bounds[0][0][0] = 0;
I copied these two lines and seem to be compiling fine.
I think you might have forgot to comment the earlier declaration of bounds . (or) you might be missing braces or something like that