The Function always take the last return - simulation

I create a Function for the setuptime. In the delay time in my service Block i write: f_getDuration(agent, v_Predecessor)
v_Predecessor is my variable typ agent and i write this in on exit: v_Predecessor = agent;
But the if function are not working. The service Block does only take the last return 10. why does it not accept my function?
Oberteil, Unterteil, Ring and Halteteil are my Agents.
CurrentAgent and predecessor are Arguments Type Agent
This is my function:
if (currentAgent == Oberteil && predecessor == Unterteil) {
return 45;
} else if (currentAgent == Oberteil && predecessor == Halteteil) {
return 40;
}else if (currentAgent == Oberteil && predecessor == Ring) {
return 45;
}else if (currentAgent == Unterteil && predecessor == Oberteil) {
return 45;
}else if (currentAgent == Unterteil && predecessor == Halteteil) {
return 40;
}else if (currentAgent == Unterteil && predecessor == Ring) {
return 45;
}else if (currentAgent == Halteteil && predecessor == Oberteil) {
return 40;
}else if (currentAgent == Halteteil && predecessor == Unterteil) {
return 40;
}else if (currentAgent == Halteteil && predecessor == Ring) {
return 45;
}else if (currentAgent == Ring && predecessor == Oberteil) {
return 45;
}else if (currentAgent == Ring && predecessor == Unterteil) {
return 45;
}else if (currentAgent == Ring && predecessor == Halteteil) {
return 45;
}
return 10;

I don't understand this very much.. but if Oberteil is an agent Type this means that the == is wrong
instead you should do
if (currentAgent instanceof Oberteil && predecessor instanceof Unterteil) {
etc...
Otherwise if this belongs to some kind of loop and Oberteil and Unterteil are actual agents that are defined, then you should do
if (currentAgent.equals(Oberteil) && predecessor.equals(Unterteil)) {
Try those
The answer from #felix is wrong, and you don't need the else before the return 10... since the return is only executed if all the previous conditions are false

Right now, the function will always return 10 because this is not part of the "if-else" block (it overwrites the return values). If you only want to return "10" if the other conditions do not hold, put an "else" before "return 10;"

Related

Is there any way to simplify complex if else statement in swift?

I have a long if else condition to assign a text to a UILabel. So i am looking for a better option/logic to write less number of lines.
Below is my condition,
if numberOfTeachers == 4 && numberOfStudents == 27 {
if String(interval.day! + 1) == "1" {
self.daysLabel.text = "1st"
self.testLabel.isHidden = true
self.subjectLabel.text = "Physics"
self.dayTitleLabel.text = "Today is your first day"
} else if String(interval.day! + 1) == "2" {
self.daysLabel.text = "2nd"
self.testLabel.isHidden = true
self.subjectLabel.text = "Chemistry"
self.dayTitleLabel.text = "Today is your Second day"
} else if String(interval.day! + 1) == "3" {
self.daysLabel.text = "3rd"
self.subjectLabel.isHidden = true
self.unitLabel.text = "Mathematics"
self.dayTitleLabel.text = "Today is your Third day"}
else if String(interval.day! + 1) == "4" {
self.daysLabel.text = "4th"
self.testLabel.isHidden = false
self.subjectLabel.text = "Physics"
self.dayTitleLabel.text = "Today is your fourth day" }
else if numberOfTeachers == 4 && numberOfStudents == 28 {
} else if numberOfTeachers == 4 && numberOfStudents == 29 {
} else if numberOfTeachers == 4 && numberOfStudents == 30 {
} else if numberOfTeachers == 5 && numberOfStudents == 27 {
} else if numberOfTeachers == 5 && numberOfStudents == 28 {
} else if numberOfTeachers == 5 && numberOfStudents == 29 {
} else if numberOfTeachers == 5 && numberOfStudents == 30 {
} else if numberOfTeachers == 6 && numberOfStudents == 27 {
} else if numberOfTeachers == 6 && numberOfStudents == 28 {
} else if numberOfTeachers == 6 && numberOfStudents == 29 {
} else if numberOfTeachers == 6 && numberOfStudents == 30 {
} else if numberOfTeachers == 7 && numberOfStudents == 27 {
} else if numberOfTeachers == 7 && numberOfStudents == 28 {
} else if numberOfTeachers == 7 && numberOfStudents == 29 {
} else if numberOfTeachers == 7 && numberOfStudents == 30 {
}
there are atleast 4 more conditions inside every if condition like (4,27), (4,28) , (5,27), (5,28)....so on.
So this whole condition is getting too long.
You could nest the if conditions like:
if numberOfTeachers == 4
{
if numberOfStudents == 29 {}
else if ...
}
else if ...
You could also use a switch statement which is a great alternative for long chains of if-else:
switch numberOfTeachers
{
case value 4:
// handle number of students
case value 5:
...
default:
// error?
}
Edit as Martin R noted, one could also use:
switch (numberOfTeachers, numberOfStudents)
{
case (4, 27):
// handle number of students
case (5, 28):
...
default:
// error?
}

swift if condition with multiple cases

In c# or others we can use this in IF
ex.
if((a=1 && b=2)
(c=2 && d=3)
){}
using ( inside ( to check bool in that statement.
But in swift
I'm trying to make something like this.
func IsWorkDay() -> Bool {
if (monWorkYN == "Y" , weekday == 2)
|| (tueWorkYN == "Y" , weekday == 3)
|| (wedWorkYN == "Y" , weekday == 4)
{
return true
}
}
But the ( ) doesn't work here. Is there a way to do this?
In this case you have to use the && operator, with a comma the compiler treats the expression in parentheses as a tuple.
And please name functions (and variables) with starting lowercase letter
func isWorkDay() -> Bool {
if (monWorkYN == "Y" && weekday == 2)
|| (tueWorkYN == "Y" && weekday == 3)
|| (wedWorkYN == "Y" && weekday == 4)
{
return true
}
return false
}
or simpler
func isWorkDay() -> Bool {
return (monWorkYN == "Y" && weekday == 2)
|| (tueWorkYN == "Y" && weekday == 3)
|| (wedWorkYN == "Y" && weekday == 4)
}
You can use this property instead of your function
var isWorkDay: Bool {
return (monWorkYN == "Y" && weekday == 2)
|| (tueWorkYN == "Y" && weekday == 3)
|| (wedWorkYN == "Y" && weekday == 4)
}
Resource
You can check this for learn more
Swift Documentation

Passing parameters for methods in Scala (alternatives to change variables values)

I need translate a Java code to Scala, but the compiler show me error. I understand that parameter input on methods are val type. Which alternative i can adopt if i need transform these values? I think to apply case class or class... Below the snippet code (in Scala):
def pmerge_FA(x: Pennant,y: Pennant): Pennant={
if(x == null && y == null && this.root == null){
return null
}else if(x == null && y == null){
return this
}else if(this.root == null && x == null){
return y
}else if(this.root == null && y == null){
return x
}else if(x == null){
y = y.pmerge(this) //error
null
}else if(this.root == null){
y = y.pmerge(x) //error
null
}else if (y == null){
y = this.pmerge(x) // error
null
}else{
y = y.pmerge(x)
this
}
}
Note that error is showed where y parameter is updated.
Thanks
Yes, the error is shown because you cannot reassign something to val, and parameters to methods in Scala are only sent as vals (immutables).
Because you don't provide the full definition of this, it's difficult to suggest an alternative solution, but:
In general, instead of if-else "Java" style, in Scala you can use pattern matching, and instead of null you can use Option, which is very powerful.
For Example, I suggest refactoring your method in this "Scala" Style (partial implementation)
def pmerge_FA(x: Pennant, y: Pennant): Option[Pennant] = {
(Option(x),Option(y), Option(this.root)) match {
case (None, None, None) => None
case (None, None, _) => Option("")
case (None, _, None) => Option(y)
case (_, None, None) => Option(x)
case (None, _, _) =>
....
}
}
Such that you will return the x, y as their new values, or create a case class like:
case class PennantCaseClass (x:Pennant, y:Pennant)
And returning it when needed.
Again, If you will provide some more info about Pennant class it will be easier to give a better alternative implementation for this method.
New values of y (namely y.pmerge(...)) are never used after assignments. So I guess all assignments y = y.pmerge(...) can be replaced with just invocations y.pmerge(...).
Does y.pmerge(...) do any side effects? Just in case, if not then values y.pmerge(...) are never used (only null or this is returned), so in such case lines y = y.pmerge(...) can be removed at all.
So the code can be either (if there are side effects)
def pmerge_FA(x: Pennant,y: Pennant): Pennant={
if(x == null && y == null && this.root == null){
null
}else if(x == null && y == null){
this
}else if(this.root == null && x == null){
y
}else if(this.root == null && y == null){
x
}else if(x == null){
y.pmerge(this)
null
}else if(this.root == null){
y.pmerge(x)
null
}else if (y == null){
this.pmerge(x)
null
}else{
y.pmerge(x)
this
}
}
or (if there are no side effects)
def pmerge_FA(x: Pennant,y: Pennant): Pennant={
if(x == null && y == null && this.root == null){
null
}else if(x == null && y == null){
this
}else if(this.root == null && x == null){
y
}else if(this.root == null && y == null){
x
}else if(x == null){
null
}else if(this.root == null){
null
}else if (y == null){
null
}else{
this
}
}
Oh, right! There are three classes to build a Bag data structure object, with nodes added in perfectly balanced tree. These methods works in that one. Below the complete code (in Java). The Pennant class build a forest using the node of graph object.
Node Class:
public class Node {
private Node left;
private Node right;
private int item;
public Node() {
left = null;
right = null;
item = 0;
}
public Node(int value) {
left = null;
right = null;
item = value;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public int getItem() {
return this.item;
}
public void setItem(int item) {
this.item = item;
}
}
public class Pennant{
private Node root;
public Pennant() {
this.root = null;
}
public Pennant(int value) {
this.root = new Node(value);
}
public void setRoot(Node root) {
this.root = root;
}
public Node getRoot() {
return this.root;
}
public Pennant pmerge(Pennant y) {
if(this.getRoot() == null) {
return y;
}else {
this.getRoot().setRight(y.getRoot().getLeft());
y.getRoot().setLeft(this.getRoot());
}
return y;
}
public Pennant pmerge_FA(Pennant x, Pennant y) {
if(x == null && y == null && this.getRoot() == null) {
return null;
}else if(x == null && y == null) {
return this;
}else if(this.getRoot() == null && x == null) {
return y;
}else if(this.getRoot() == null && y == null) {
return x;
}else if(x == null) {
y = y.pmerge(this);
return null;
}else if(this.getRoot() == null) {
y = y.pmerge(x);
return null;
}else if (y == null) {
y = this.pmerge(x);
return null;
}else {
y = y.pmerge(x);
return this;
}
}
public Pennant psplit() {
if(this.getRoot() != null && this.getRoot().getLeft() != null) {
Pennant y = new Pennant();
y.setRoot(this.getRoot().getLeft());
this.getRoot().setLeft(y.getRoot().getRight());
y.getRoot().setRight(null);
return y;
}
return null;
}
public void remove_all(Node node) {
if (node.getLeft() != null) {
remove_all(node.getLeft());
}
if(node.getRight() != null) {
remove_all(node.getRight());
}
node = null;
}
}

My logic for seeing if a user has won a tic-tac-toe match isn't working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Below is some code to check if somebody has won on a tic tac toe board. (board[0] - board[8] represent the tic tac toe board from top to bottom, left to right):
func checkWin(board: [Int]) -> Bool{
if board[0] != 0 {
if board[0] == board[1] && board[1] == board[2] {
return true
} else if board[0] == board[3] && board[3] == board[6] {
return true
}
} else if board[4] != 0 {
if board[1] == board[4] && board[4] == board[7] {
return true
} else if board[3] == board[4] && board[4] == board[5] {
return true
} else if board[2] == board[4] && board[4] == board[6] {
return true
} else if board[0] == board[4] && board[4] == board[8] { //
return true
}
} else if board[8] != 0 {
if board[2] == board[5] && board[5] == board[8] { //
return true
} else if board[6] == board[7] && board[7] == board[8] { //
return true
}
}
return false
}
However, the three lines with // at the end will not return true even if the conditions are met. I have noticed that they all share board[8], however I know this is not a problem with the storyboard, as if I hard code a win that satisfies one of faulty conditions it still doesn't work. Can anyone see what's going wrong?
Your logic is flawed. Once you check with the outer ifs, you have committed to just checking a few of the possible cases.
For instance, if space 0 is not empty, then you are only checking cases 0-1-2 and 0-3-6, but you aren't checking 0-4-8 so you'll miss that possibility. The 0-4-8 case is handled inside the first else if case, but you'll never get there if board[0] != 0.
You can fix this by using 3 ifs instead of the else ifs.
func checkWin(board: [Int]) -> Bool{
print(board)
if board[0] != 0 {
if board[0] == board[1] && board[1] == board[2] {
return true
} else if board[0] == board[3] && board[3] == board[6] {
return true
}
}
if board[4] != 0 {
if board[1] == board[4] && board[4] == board[7] {
return true
} else if board[3] == board[4] && board[4] == board[5] {
return true
} else if board[2] == board[4] && board[4] == board[6] {
return true
} else if board[0] == board[4] && board[4] == board[8] { //
return true
}
}
if board[8] != 0 {
if board[2] == board[5] && board[5] == board[8] { //
return true
} else if board[6] == board[7] && board[7] == board[8] { //
return true
}
}
return false
}
a little bit more compact:
func checkWin(board: [Int]) -> Bool{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return true
}
}
return false
}
better to get also the winner-id or 0 for no winner:
func getWinner(board: [Int]) -> Int{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return board[check[0]]
}
}
return 0
}
or if you like to put it in an enum:
enum Winner {
case none
case player(id: Int)
}
func checkWin(board: [Int]) -> Winner{
let checks = [
//rows
[0,1,2],
[3,4,5],
[6,7,8],
//columns
[0,3,6],
[1,4,7],
[2,5,8],
//cross
[0,4,8],
[2,4,6]
]
for check in checks{
if board[check[0]] != 0
&& board[check[0]] == board[check[1]]
&& board[check[1]] == board[check[2]]
{
return .player(id: board[check[0]])
}
}
return .none
}

Swift: Error when updated Xcode: 'could not find overload for '&&' that accepts the supplied arguments'

I just updated to the non beta version of Xcode 6 (finally) and, coming from beta 5, got a few errors that I didn't get before, one being "could not find overload for '&&' that accepts the supplied arguments"
I am following a tutorial here and from another question, I know that this error is because "the expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions."
I'm a beginner; how do I break up an expression into sub-expressions?
My code:
func checkForWin(){
//first row across
var youWin = 1
var theyWin = 0
var whoWon = ["Lost":0,"Won":1]
for (key,value) in whoWon {
if ((plays[6] == value && plays[7] == value && plays[8] == value) || //across the bottom
(plays[3] == value && plays[4] == value && plays[5] == value) || //across the middle
(plays[0] == value && plays[1] == value && plays[2] == value) || //across the top
(plays[6] == value && plays[3] == value && plays[0] == value) || //down the left side
(plays[7] == value && plays[4] == value && plays[1] == value) || //down the middle
(plays[8] == value && plays[5] == value && plays[2] == value) || //down the right side
(plays[6] == value && plays[4] == value && plays[2] == value) || //diagonal
(plays[8] == value && plays[4] == value && plays[0] == value)){//diagonal
userMessage.hidden = false
youLabel.hidden = false
userMessage.text = "\(key)!"
done = true;
}
}
}
To break it up, you just need to add more ()s. Like this:
if (((plays[6] == value) && (plays[7] == value) && (plays[8] == value)) ||
((plays[3] == value) && (plays[4] == value) && (plays[5] == value)) ||
((plays[0] == value) && (plays[1] == value) && (plays[2] == value)) ||
((plays[8] == value) && (plays[4] == value) && (plays[0] == value)))