I am making a chess engine in C++ and with this algorithm, I get expected play with max depth set to 1. Beyond that however, it ignores pieces that are in danger and seems to even willingly put itself in danger.
Here is my code:
int negamax(int depth, int alpha, int beta)
{
int max = -INFINITY;
MoveList legalMoves;
MoveGeneration::generateCaptureMoves(&legalMoves);
MoveGeneration::generateQuietMoves(&legalMoves);
// No legal moves
if(legalMoves.count == 0)
{
if(Position::isCheck())
{
// Checkmate
if(Position::activeColor == WHITE)
return VAL_VICTORY;
else
return -VAL_VICTORY;
}
else
{
// Stalemate
return 0;
}
}
// Go through legal moves
for(int i = 0; i < legalMoves.count; i++)
{
// Get move score
Position::doMove(legalMoves[i]);
int score;
if(depth == 0)
score = quiescence(MAX_QUIESCENCE_DEPTH, alpha, beta);
else
score = -negamax(depth - 1, alpha, beta);
Position::undoMove();
// Pruning
if(Position::activeColor == WHITE && score > beta) break;
if(Position::activeColor == BLACK && score < alpha) break;
if(Position::activeColor == WHITE && score > alpha) alpha = score;
if(Position::activeColor == BLACK && score < beta) beta = score;
// Best so far?
if(score > max)
{
max = score;
if(depth == MAX_DEPTH)
bestMove = legalMoves[i];
}
}
return max;
}
Try:
score = -negamax(depth - 1, -beta, -alpha);
Related
I have a method isPerfect(x) (with 4<=x<=10000) below, how to write test cases based on Equivalence Class, Boundary Value, and Basis Path Testing:
public boolean checkPerfectNumber(int x) {
if(x >= 4 && x <= 10000) {
int sum = 0;
for(int i = 1; i < x; i++) {
if(x % i == 0) {
sum += i;
}
}
if(sum == x) return true;
}
return false;
}
THIS IS ALL IN THE STEP EVENT.
I am currently trying to create my first state machine for some enemy AI. Being very new to gml and gamemaker studio 2, my code is very basic as I do not know how to implement built in functions.
In 1 of my states, the rush state, the enemy is supposed to chase the player. I do this by creating some conditions, if the player is to the left of the enemy, the enemy will run left. If the player is to the right, the enemy will run right. This in theory is what I have coded but when I get in game, sometimes it will work, but then spasm and go the other way.
If my character is in the circle, it doesn't run towards the player but instead away. This does not change if I reverse the conditions.
case behaviour.rush:
{
//radius in square
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
//direction to face player
if (behaviourState == behaviour.rush && playerObject.x >. warriorx) hsp = -4;
else if (behaviourState == behaviour.rush &&
playerObject.x <= warriorx) hsp = 4;
x = x + hsp;
}
if (!point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.idle;
}
}
My full code:
image_speed = 1;
vsp = vsp+grv;
Print(behaviourState);
if (hsp > 0) image_xscale = 3; else if (hsp < 0) image_xscale = -3;
//animation
if (behaviourState == behaviour.idle) sprite_index = tikiAxeWarriorIdle;
if (hsp == 2 || hsp == -2)sprite_index = tikiAxeWarriorWalk;
if (hsp == 4 || hsp == -4) sprite_index = tikiAxeWarriorRush;
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
Print("in circle");
}
Print(hsp);
switch(behaviourState)
{
case behaviour.idle:
{
//stand still
if (alarm[1] <= 0) alarm[1] = room_speed * 4;
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.rush;
}
}
case behaviour.wander:
{
if (alarm[0] <= 0) alarm[0] = room_speed * 3;
//move
if (!place_meeting(x + hsp,y,wallObject))
{
x = x + hsp;
}
if (place_meeting(x + hsp,y,wallObject))//checking frame before.
{
hsp = -hsp;
}
//check for behaviour.rush.
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.rush;
}
}
case behaviour.rush:
{
//radius in square
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
//direction to face player
if (behaviourState == behaviour.rush && playerObject.x > warriorx) hsp = -4;
else if (behaviourState == behaviour.rush && playerObject.x <= warriorx) hsp = 4;
x = x + hsp;
}
if (!point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.idle;
}
}
case behaviour.attack:
{
//attack.
//if player is hit, he dies.
}
}
The expected result is for the enemy to "rush" towards my player position and when over stepped, to face the other way. If my player leaves the circle, it should go back to the idle state.
hsp = -hsp sounds very sensitive to the spasm. and since it's affect the hsp as whole, it'll also affect the place_meeting(x + hsp,y,wallObject) part.
I don't know the solution right away, but I think you should take a look at that section.
I also personally prefer to split your current hsp in two variables: the direction and speed, where the direction is a value of 1 or -1 and the speed is like hsp, but returns a positive value. And then combine them using speed * direction.
That way, you can calculate the speed without taking direction into account, and vice versa. Which may also solve the conflict you're currently having.
With switch statements, I believe you need to add "break;" in between each Case.
The YoYo documentation here has some good information on switch statements. Now this might not enable your code to run exactly as you're hoping, but it will get you one bug less towards your goal.
The pertinent part of the link states:
"...execution continues after the first case statement with the correct value, until a break statement is encountered....the break is not required, and if there is no break statement the execution simply continues with the code for the next case statement".
So essentially, without a break statement, the code will start at the case block matching the current value, and then run through the code of the all cases below it until it hits a "break;".
Here's what your code would look like with the "break;" added in to the correct spots.
image_speed = 1;
vsp = vsp+grv;
Print(behaviourState);
if (hsp > 0) image_xscale = 3; else if (hsp < 0) image_xscale = -3;
//animation
if (behaviourState == behaviour.idle) sprite_index = tikiAxeWarriorIdle;
if (hsp == 2 || hsp == -2)sprite_index = tikiAxeWarriorWalk;
if (hsp == 4 || hsp == -4) sprite_index = tikiAxeWarriorRush;
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
Print("in circle");
}
Print(hsp);
switch(behaviourState)
{
case behaviour.idle:
{
//stand still
if (alarm[1] <= 0) alarm[1] = room_speed * 4;
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.rush;
}
}
break;
case behaviour.wander:
{
if (alarm[0] <= 0) alarm[0] = room_speed * 3;
//move
if (!place_meeting(x + hsp,y,wallObject))
{
x = x + hsp;
}
if (place_meeting(x + hsp,y,wallObject))//checking frame before.
{
hsp = -hsp;
}
//check for behaviour.rush.
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.rush;
}
}
break;
case behaviour.rush:
{
//radius in square
if (point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
//direction to face player
if (behaviourState == behaviour.rush && playerObject.x > warriorx) hsp = -4;
else if (behaviourState == behaviour.rush && playerObject.x <= warriorx) hsp = 4;
x = x + hsp;
}
if (!point_in_circle(playerObject.x,playerObject.y,x,y,200))
{
behaviourState = behaviour.idle;
}
}
break;
case behaviour.attack:
{
//attack.
//if player is hit, he dies.
}
break;
}
Hope this helps!
Also, a quick tip!
One thing I noticed with your code is you repeat a couple expressions a few times. For readability sake, I recommend assigning these to a variable if possible. For example:
var in_circle = point_in_circle(playerObject.x,playerObject.y,x,y,200);
if in_circle {
}
if !in_circle {
}
I got a question to answer with the best complexity we can think about.
We got one sorted array (int) and X value. All we need to do is to find how many places in the array equals the X value.
This is my solution to this situation, as i don't know much about complexity.
All i know is that better methods are without for loops :X
class Question
{
public static int mount (int [] a, int x)
{
int first=0, last=a.length-1, count=0, pointer=0;
boolean found=false, finish=false;
if (x < a[0] || x > a[a.length-1])
return 0;
while (! found) **//Searching any place in the array that equals to x value;**
{
if ( a[(first+last)/2] > x)
last = (first+last)/2;
else if ( a[(first+last)/2] < x)
first = (first+last)/2;
else
{
pointer = (first+last)/2;
count = 1;
found = true; break;
}
if (Math.abs(last-first) == 1)
{
if (a[first] == x)
{
pointer = first;
count = 1;
found = true;
}
else if (a[last] == x)
{
pointer = last;
count = 1;
found = true;
}
else
return 0;
}
if (first == last)
{
if (a[first] == x)
{
pointer = first;
count = 1;
found = true;
}
else
return 0;
}
}
int backPointer=pointer, forwardPointer=pointer;
boolean stop1=false, stop2= false;
while (!finish) **//Counting the number of places the X value is near our pointer.**
{
if (backPointer-1 >= 0)
if (a[backPointer-1] == x)
{
count++;
backPointer--;
}
else
stop1 = true;
if (forwardPointer+1 <= a.length-1)
if (a[forwardPointer+1] == x)
{
count++;
forwardPointer++;
}
else
stop2 = true;
if (stop1 && stop2)
finish=true;
}
return count;
}
public static void main (String [] args)
{
int [] a = {-25,0,5,11,11,99};
System.out.println(mount(a, 11));
}
}
The print command count it right and prints "2".
I just want to know if anyone can think about better complexity for this method.
Moreover, how can i know what is the time/space-complexity of the method?
All i know about time/space-complexity is that for loop is O(n). I don't know how to calculate my method complexity.
Thank a lot!
Editing:
This is the second while loop after changing:
while (!stop1 || !stop2) //Counting the number of places the X value is near our pointer.
{
if (!stop1)
{
if ( a[last] == x )
{
stop1 = true;
count += (last-pointer);
}
else if ( a[(last+forwardPointer)/2] == x )
{
if (last-forwardPointer == 1)
{
stop1 = true;
count += (forwardPointer-pointer);
}
else
forwardPointer = (last + forwardPointer) / 2;
}
else
last = ((last + forwardPointer) / 2) - 1;
}
if (!stop2)
{
if (a[first] == x)
{
stop2 = true;
count += (pointer - first);
}
else if ( a[(first+backPointer)/2] == x )
{
if (backPointer - first == 1)
{
stop2 = true;
count += (pointer-backPointer);
}
else
backPointer = (first + backPointer) / 2;
}
else
first = ((first + backPointer) / 2) + 1;
}
}
What do you think about the changing? I think it would change the time complexity to O(long(n)).
First let's examine your code:
The code could be heavily refactored and cleaned (which would also result in more efficient implementation, yet without improving time or space complexity), but the algorithm itself is pretty good.
What it does is use standard binary search to find an item with the required value, then scans to the back and to the front to find all other occurrences of the value.
In terms of time complexity, the algorithm is O(N). The worst case is when the entire array is the same value and you end up iterating all of it in the 2nd phase (the binary search will only take 1 iteration). Space complexity is O(1). The memory usage (space) is unaffected by growth in input size.
You could improve the worst case time complexity if you keep using binary search on the 2 sub-arrays (back & front) and increase the "match range" logarithmically this way. The time complexity will become O(log(N)). Space complexity will remain O(1) for the same reason as before.
However, the average complexity for a real-world scenario (where the array contains various values) would be very close and might even lean towards your own version.
I am trying to create a board game where all the pieces are able to move the same as a rook in chess. (i.e. Horizontally or vertically as many spaces as they wish)
My board is a simple 2d integer array, with values of 0,1,2 depending on whether the space is empty, has a red piece or a black piece.
My code for the movement so far is shown below, it creates a true or false value if the move is allowed or not:
int[][] board;
public boolean validMove(int fromRow, int fromCol, int toRow, int toCol) {
if (pieceAt(toRow, toCol) != EMPTY) {
return false;
} else if (fromRow - toRow == 0 && fromCol - toCol != 0) {
return true;
} else if (fromCol - toCol == 0 && fromRow - toRow != 0) {
// Trying to add piece collision code
for (int i = fromRow; i < toRow; i++) {
if (pieceAt(toCol, i) != EMPTY)
return false;
}
return true;
} else {
return false;
}
}
My problem is trying to create collision detection, if another piece is in the way it should not be able to move past it, however with my code currently it can. Can anyone help me do this?
Try code below. It's quite naive (and also not tested), but I think it should work just as is. And also I think it illustrates the idea pretty well (see comments). It's in C, but I'm sure you can transform it easily to Java (or whatever language you use).
bool validMove(int fromRow, int fromCol, int toRow, int toCol)
{
int i;
// Attempt to move to the same cell
if (fromRow == toRow && fromCol == toCol)
return false;
// Collision detection
if (fromRow == toRow) {
// Horizontal move
if (fromCol < toCol) {
// Move right
for (i = fromCol + 1; i <= toCol; ++i)
if (pieceAt(fromRow, i) != EMPTY)
return false;
} else {
// Move left
for (i = fromCol - 1; i >= toCol; --i)
if (pieceAt(fromRow, i) != EMPTY)
return false;
}
} else if (fromCol == toCol) {
// Vertical move
if (fromRow < toRow) {
// Move down
for (i = fromRow + 1; i <= toRow; ++i)
if (pieceAt(i, fromCol) != EMPTY)
return false;
} else {
// Move up
for (i = fromRow - 1; i >= toRow; --i)
if (pieceAt(i, fromCol) != EMPTY)
return false;
}
} else {
// Not a valid rook move (neither horizontal nor vertical)
return false;
}
return true;
}
EDIT
You can also optimize your code by reducing conditional statement count, using the method proposed by Toon Krijthe. The main idea is to use "delta" values (dx/dy) for incrementing or decrementing cell indexes. In that case the destination cell should be checked explicitly.
Code:
bool validMove(int fromRow, int fromCol, int toRow, int toCol)
{
int i;
// Attempt to move to the same cell
if (fromRow == toRow && fromCol == toCol)
return false;
// Collision detection
if (fromRow == toRow) { // Horizontal move
int dx = (fromCol < toCol) ? 1 : -1;
for (i = fromCol + dx; i != toCol; i += dx)
if (pieceAt(fromRow, i) != EMPTY)
return false;
} else if (fromCol == toCol) { // Vertical move
int dy = (fromRow < toRow) ? 1 : -1;
for (i = fromRow + dy; i != toRow; i += dy)
if (pieceAt(i, fromCol) != EMPTY)
return false;
} else { // Not a valid rook move
return false;
}
// Return true if destination cell is free
return pieceAt(toRow, toCell) == EMPTY;
}
you could have a 2D char array with each cell representing the position on the board. there you can have a char for the three states of the position (white/red/empty).
The other way i can think of is before each move to check from the startPosition till the endPosition positions their states. but performance wise i think the some kind of array will be much better
You can simply "walk" each field in order to check if it is empty.
For example:
int[][] board;
public boolean validMove(int fromRow, int fromCol, int toRow, int toCol)
{
if (pieceAt(toRow, toCol) != EMPTY) return false;
else if (fromRow == toRow)
{
// horizontal move
if (fromCol == toCol) return false; // same position
int dx, x;
if (fromCol < toCol)
dx = 1;
else
dx = -1;
for (x = fromCol + dx; x != toCol; x += dx)
{
if (pieceAt(toRow, x) != EMPTY) return false; // occupied
}
}
else if (fromCol == toCol)
{
// vertical move
int dy, y;
if (fromRow < toRow)
dy = 1;
else
dy = -1;
for (y = fromRow + dy; y != toRow; y += dy)
{
if (pieceAt(y, toCol) != EMPTY) return false; // occupied
return true; // free path
}
}
else return false; // no horizontal or vertical move
}
This is a cross-post from someone who answered my original question here.
I'm not sure how to go about executing the 3 functions I'm after (as well as introducing even more than 3 in the future).
I am simply trying to Fade/Blink the selected Colour of an RGB LED (and perhaps introduce more functions in the future) where its RGB data is coming back from iOS and sent to an RFDuino BLE module.
Sends a "fade" string to the module picked up by RFduinoBLE_onReceive (char *data, int len) on the Arduino end.
- (IBAction)fadeButtonPressed:(id)sender {
[rfduino send:[#"fade" dataUsingEncoding:NSUTF8StringEncoding]];
}
- (IBAction)blinkButtonPressed:(id)sender {
[rfduino send:[#"blink" dataUsingEncoding:NSUTF8StringEncoding]];
}
Selected Color:
- (void)setColor
{
NSLog(#"colors: RGB %f %f %f", red, green, blue);
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
[colorSwatch setHighlighted:YES];
[colorSwatch setTintColor:color];
uint8_t tx[3] = { red * 255, green * 255, blue * 255 };
NSData *data = [NSData dataWithBytes:(void*)&tx length:3];
[rfduino send:data];
}
This is originally how I set the RGB colour:
void RFduinoBLE_onReceive (char *data, int len) {
if (len >= 3) {
// Get the RGB values.
uint8_t red = data[0];
uint8_t green = data[1];
uint8_t blue = data[2];
// Set PWM for each LED.
analogWrite(rgb2_pin, red);
analogWrite(rgb3_pin, green);
analogWrite(rgb4_pin, blue);
}
}
This was the provided answer that now compiles on Arduino, but I have no idea how to actually execute my functions and where?
#include <RFduinoBLE.h>
int state;
char command;
String hexstring;
// RGB pins.
int redPin = 2;
int grnPin = 3;
int bluPin = 4;
void setup () {
state = 1;
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
// This is the data we want to appear in the advertisement
// (the deviceName length plus the advertisement length must be <= 18 bytes.
RFduinoBLE.deviceName = "iOS";
RFduinoBLE.advertisementInterval = MILLISECONDS(300);
RFduinoBLE.txPowerLevel = -20;
RFduinoBLE.advertisementData = "rgb";
// Start the BLE stack.
RFduinoBLE.begin();
}
void loop () {
//RFduino_ULPDelay(INFINITE);
}
void processCommand (int command, String hex) {
// hex ?
// command ?
}
void RFduinoBLE_onReceive (char *data, int len) {
for (int i = 0; i < len; i++) {
stateMachine(data[i]);
}
}
void stateMachine (char data) {
switch (state) {
case 1:
if (data == 1) {
state = 2;
}
break;
case 2:
if (data == 'b' || data == 'f' || data == 'c') {
command = data;
hexstring = "";
state = 3;
} else if (data != 1) { // Stay in state 2 if we received another 0x01.
state = 1;
}
break;
case 3:
if ((data >= 'a' && data <= 'z') || (data >= '0' && data <= '9')) {
hexstring = hexstring + data;
if (hexstring.length() == 6) {
state = 4;
}
} else if (data == 1) {
state = 2;
} else {
state = 1;
}
break;
case 4:
if (data == 3) {
processCommand(command, hexstring);
state = 1;
} else if (data == 1) {
state = 2;
} else {
state = 1;
}
break;
}
}
EDIT: Final code
#include <RFduinoBLE.h>
// State properties.
int state = 1;
char command;
String hexstring;
// RGB pins.
int redPin = 2;
int grnPin = 3;
int bluPin = 4;
// Setup function to set RGB pins to OUTPUT pins.
void setup () {
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
// This is the data we want to appear in the advertisement
// (the deviceName length plus the advertisement length must be <= 18 bytes.
RFduinoBLE.deviceName = "iOS";
RFduinoBLE.advertisementInterval = MILLISECONDS(300);
RFduinoBLE.txPowerLevel = -20;
RFduinoBLE.advertisementData = "rgb";
// Start the BLE stack.
RFduinoBLE.begin();
}
void loop () {
switch (command) {
case 1:
// Blink.
break;
case 2:
// Fade.
break;
}
//RFduino_ULPDelay(INFINITE);
}
// Converts HEX as a String to actual HEX values.
// This is needed to properly convert the ASCII value to the hex
// value of each character.
byte getVal (char c) {
if (c >= '0' && c <= '9') return (byte)(c - '0');
else return (byte)(c - 'a' + 10);
}
// Process each function/command.
void processCommand (int command, String hex) {
switch (command) {
case 'b':
command = 1; // Set blink mode.
break;
case 'f':
command = 2; // Set fade mode.
break;
case 'c':
// We put together 2 characters as is
// done with HEX notation and set the color.
byte red = getVal(hex.charAt(1)) + (getVal(hex.charAt(0)) << 4);
byte green = getVal(hex.charAt(3)) + (getVal(hex.charAt(2)) << 4);
byte blue = getVal(hex.charAt(5)) + (getVal(hex.charAt(4)) << 4);
// Set the color.
setColor (red, green, blue);
break;
}
}
// Sets the color of each RGB pin.
void setColor (byte red, byte green, byte blue) {
analogWrite(redPin, red);
analogWrite(grnPin, green);
analogWrite(bluPin, blue);
}
// This function returns data from the radio.
void RFduinoBLE_onReceive (char *data, int len) {
for (int i = 0; i < len; i++) {
stateMachine(data[i]);
}
}
// Main state machine function, which processes
// data depending on the bytes received.
void stateMachine (char data) {
switch (state) {
case 1:
if (data == 1) {
state = 2;
}
break;
case 2:
if (data == 'b' || data == 'f' || data == 'c') {
command = data;
hexstring = "";
state = 3;
} else if (data != 1) { // Stay in state 2 if we received another 0x01.
state = 1;
}
break;
case 3:
if ((data >= 'a' && data <= 'z') || (data >= '0' && data <= '9')) {
hexstring = hexstring + data;
if (hexstring.length() == 6) {
state = 4;
}
} else if (data == 1) {
state = 2;
} else {
state = 1;
}
break;
case 4:
if (data == 3) {
processCommand(command, hexstring);
state = 1;
} else if (data == 1) {
state = 2;
} else {
state = 1;
}
break;
}
}
There is some code here that you can use to convert hex characters to a byte.
So, you can add this to your existing code -
byte getVal(char c)
{
if (c >= '0' && c <= '9')
return (byte)(c - '0');
else
return (byte)(c-'a'+10)
}
void processCommand (int command, String hex)
{
switch (command) {
case 'b':
command = 1; // set blink mode
break;
case 'f':
command=2; // set fade mode
break;
case 'c':
byte red=getVal(hex.charAt(1)) + (getVal(hex.charAt(0)) << 4);
byte green=getVal(hex.charAt(3)) + (getVal(hex.charAt(2)) << 4);
byte blue=getVal(hex.charAt(5)) + (getVal(hex.charAt(4)) << 4);
setColor(red,green,blue);
}
}
void setColor(byte red,byte green,byte blue)
{
// Set PWM for each LED.
analogWrite(rgb2_pin, red);
analogWrite(rgb3_pin, green);
analogWrite(rgb4_pin, blue);
}
On the iOS side you can use something like this -
-(void) sendCommand:(char)command arg1:(Byte)arg1 arg2:(Byte)arg2 arg3:(Byte) arg3 {
NSString *commandString=[NSString stringWithFormat:#"\001%c%02x%02x%02x\003",command,arg1,arg2,arg3];
NSData *commandData=[commandString dataUsingEncoding:NSASCIIStringEncoding];
[rfduino send:data];
}
- (IBAction)fadeButtonPressed:(id)sender {
[self sendCommand:'f' arg1:0 arg2:0 arg3:0];
}
- (IBAction)blinkButtonPressed:(id)sender {
[self sendCommand:'b' arg1:0 arg2:0 arg3:0];
}
- (void)setColor
{
NSLog(#"colors: RGB %f %f %f", red, green, blue);
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
[colorSwatch setHighlighted:YES];
[colorSwatch setTintColor:color];
[self sendCommand:c arg1:red*255 arg2:green*255 arg3:blue*255];
}