How to make only one of the variables have the value of 0? - objective-c

I am trying to make it so that of the 4 variables, (squareType1, squareType2, squareType3, and squareType4) only one of them has a value of 0 but which variable it is should be random. The coding is off I know that but I just don't know how to fix it.
squareType1 = arc4random() %2;
squareType2 = arc4random() %2;
squareType3 = arc4random() %2;
squareType4 = arc4random() %2;
if (squareType2 == 0 || squareType3 == 0 || squareType4 == 0) {
squareType1 = 1;
}
if (squareType1 == 0 || squareType3 == 0 || squareType4 == 0) {
squareType2 = 1;
}
if (squareType2 == 0 || squareType1 == 0 || squareType4 == 0) {
squareType3 = 1;
}
if (squareType2 == 0 || squareType3 == 0 || squareType1 == 0) {
squareType4 = 1;
}

A simpler way to do this would be to set all 4 values to 1 initially and then randomly reset one of the variables to 0, e.g.
squareType1 = squareType2 = squareType3 = squareType4 = 1;
switch (arc4random() % 4)
{
case 0: squareType1 = 0; break;
case 1: squareType2 = 0; break;
case 2: squareType3 = 0; break;
case 3: squareType4 = 0; break;
}
Note that you will probably find life easier, in this instance and in general, if you refactor the four separate variables into a single array, e.g. int squareType[4];.

Related

Fizz-Buzz solution is not working properly in kotlin

In the Fizz-Buzz problem, we replace a number multiple of 3 with the word fizz and a number divisible by 5 with the word buzz. If a number is divisible by both three and five, we replace it with the word"FizzBuzz." in a counting incremental loop.
But my code is not working properly. Please take a look and let me know what I am doing wrong.
for (i in 1..100){
if ( i%5 == 0 || i%3 == 0) {
println("FizzBuzz")}
else if(i%5 == 0) {
println("Buzz")}
else if(i%3 == 0){
println("Fizz")}
else {
println(i)
}
}
You are using || instead of &&.
Replace:
if (i%5 == 0 || i%3 == 0) {
println("FizzBuzz")
}
With:
if (i%5 == 0 && i%3 == 0) {
println("FizzBuzz")
}
Or with:
if (i%15 == 0) {
println("FizzBuzz")
}
The more elegant solution is:
fun fizzBuzz(currentNumber: Int) = when {
currentNumber % 15 == 0 -> "FizzBuzz"
currentNumber % 3 == 0 -> "Fizz"
currentNumber % 5 == 0 -> "Buzz"
else -> "$currentNumber"
}
for (currentNumber in 1..100) {
print(fizzBuzz(currentNumber))
}

Using promela and Spin to solve algorithmic puzzle about frogs

I study verification at the university using promela. And as an example, i need to solve algorithmic puzzle about frogs. I tried to solve the problem but something doesn't work right
In how many moves will the frogs exchange places? Frogs jump in turn on an empty cage when it is nearby or through one frog of the opposite color.
enter image description here
bool all_frog_done;
active proctype frog_jump()
{
byte i = 0;
byte position[7];
position[0] = 1;
position[1] = 1;
position[2] = 1;
position[3] = 0;
position[4] = 2;
position[5] = 2;
position[6] = 2;
all_frog_done = false;
printf("MSC: position[0] %d position[1] %d position[2] %d position[3] %d position[4] %d position[5] %d position[6] %d \n", position[0], position[1], position[2], position[3], position[4], position[5], position[6]);
do
:: (position[0] == 2) && (position[1] == 2) && (position[2] == 2) && (position[3] == 0) && (position[4] == 1) && (position[5] == 1) && (position[6] == 1) ->
all_frog_done = true; break;
:: else ->
if
:: (position[i] == 1) && (position[i+1] == 0) && (i != 6) ->
position[i] = 0;
position[i+1] = 1;
:: (position[i] == 2) && (position[i-1] == 0) && (i != 0) ->
position[i] = 0;
position[i-1] = 2;
:: (position[i] == 1) && (position[i+1] == 2) && (position[i+2] == 0) && (i <= 4)->
position[i] = 0;
position[i+2] = 1;
:: (position[i] == 2) && (position[i-1] == 1) && (position[i-2] == 0) && (i >= 2)->
position[i] = 0;
position[i-2] = 2;
fi;
printf("MSC: position[0] %d position[1] %d position[2] %d position[3] %d position[4] %d position[5] %d position[6] %d \n", position[0], position[1], position[2], position[3], position[4], position[5], position[6]);
if
:: (i < 7) -> i++;
:: (i > 0) -> i--;
fi;
od;
}

movementspeed not updating in a state machine

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 {
}

Getting indexOutOfBoundsException and I don't know why

I have been having this problem for a few hours. I don't know what it is, but I am having a hard time thinking clearly at the moment. This method displays a set of images. The first part of the method is just setting the gridbag constraints, whereas the next part in the if statement is creating jlabels and adding them to an arraylist of jlabels. The exception is being thrown when I try and add mouselisteners to the jlabels after they have been added to the arraylist (this is on line 112, and i have commented this on the code).
public void displayComplexStimulus(BufferedImage[] complexStimulus){
for(int i = 0; i < numberOfElements; i++){
if (i == 0 || i == 1 || i == 2){
c.gridx = i;
c.gridy = 0;
}
else if(i == 3 || i == 4 || i == 5){
c.gridx = i - 3;
c.gridy = 1;
}
else {
c.gridx = i - 6;
c.gridy = 2;
}
if(counter == 1){
if (phase1Trial.getPositionOfCorrectImage()!= i){
phase1IncorrectLabels.add(new JLabel(new ImageIcon(complexStimulus[i])));
phase1IncorrectLabels.get(i).addMouseListener(this); //line 112
add(phase1IncorrectLabels.get(i),c);
}
else if(phase1Trial.getPositionOfCorrectImage() == i){
correctLabel = new JLabel(new ImageIcon(complexStimulus[i]));
add(correctLabel, c);
correctLabel.addMouseListener(this);
}
}
}
}
If i==phase1Trial.getPositionOfCorrectImage() you're not adding an element to phase1IncorrectLabels. So in the next iteration after adding one element to the array it's at position i-1 and not i. You should replace your get(i) by get(phase1IncorrectLabels.size() - 1).

Programming Chess Rook Movement

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
}