I have been trying too parse 2 parameter using for loop but it only loop once and locates one parameter. This is my code.
By providing rectangle 70,80 command in the form, I am trying to draw shapes but while passing through check variable for loop works only once. It does not increments but returns value only after first execution then goes to check number of parameter then rejects with error Array out of bounds.
public String[] ParameterSplit;
public IDictionary<string, int> VariableDictionary = new Dictionary<string, int>();
public IDictionary<string, int> MethodVariableDictionary = new Dictionary<string, int>();
public Boolean AlreadyInArray = false;
public Boolean Value1IsVariable = false;
public Boolean Value2IsVariable = false;
public int value = 0;
public int value1 = 0;
public int value2 = 0;
public CommandParser()
{
}
//This method is used for the validation of commands and parameters which passes thorough
//singleline command and multiline command method
public string[] CommandParsers(string input, int lineCount)
{
string[] text = { };
string[] inputcommand = input.Split(',', ' ');
if (inputcommand[0].ToUpper() == "MOVETO")
{
if(inputcommand.Length < 4)
{
Convert.ToInt32(inputcommand[1]);
Convert.ToInt32(inputcommand[2]);
text = inputcommand;
}
}
else
{
if (inputcommand[0].ToUpper() == "DRAWTO")
{
if (inputcommand.Length == 3)
{
Convert.ToInt32(inputcommand[1]);
Convert.ToInt32(inputcommand[2]);
text = inputcommand;
}
else
{
MessageBox.Show($"ERROR: Provide correct parameter for moveto in line no {lineCount}");
string[] strings = { "moveto,100,100" };
text = strings;
}
}
else if (inputcommand[0].ToUpper() == "RECTANGLE")
{
ParameterSplit = inputcommand[1].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
CheckForVariable();
if (ParameterSplit.Length == 2)
{
}
else
{
Convert.ToInt32(ParameterSplit[0]);
Convert.ToInt32(ParameterSplit[1]);
text = ParameterSplit;
}
}
else if (inputcommand[0].ToUpper() == "CIRCLE")
{
if (inputcommand.Length == 2)
{
Convert.ToInt32(inputcommand[1]);
text = inputcommand;
}
else
{
MessageBox.Show("ERROR: Provide correct parameter for circle in line no " + lineCount );
string[] strings = { "circle,20" };
text = strings;
}
}
else if (inputcommand[0].ToUpper() == "TRIANGLE")
{
if (inputcommand.Length == 3)
{
Convert.ToInt32(inputcommand[1]);
Convert.ToInt32(inputcommand[2]);
text = inputcommand;
}
else
{
MessageBox.Show("ERROR: Provide correct parameter for triangle in line no "+ lineCount);
string[] strings = { "triangle,20,60" };
text = strings;
}
}
else if (inputcommand[0].ToUpper() == "PEN")
{
Pen = true;
string[] validateinput = { "pen", inputcommand[1] };
text = validateinput;
switch (inputcommand[1])
{
case "red":
color = Color.Red;
break;
case "blue":
color = Color.Blue;
break;
case "yellow":
color = Color.Yellow;
break;
case "green":
color = Color.Green;
break;
case "violet":
color = Color.Violet;
break;
}
}
else if (inputcommand[0].ToUpper() == "FILL")
{
string[] validateinput = { "fill", inputcommand[1] };
text = validateinput;
switch (inputcommand[1])
{
case "on":
fill = true;
break;
case "off":
fill = false;
break;
}
}
else if (inputcommand.Length > 2)
{
text = inputcommand;
//checks if the middle element is a "="
if(inputcommand[1].ToUpper() == "=")
{
//check if the variable is already stored or not
//StoreVarialable();
foreach(KeyValuePair<string,int> variable in VariableDictionary)
{
if (variable.Key.Equals(inputcommand[0]))
{
AlreadyInArray = true;
}
}
foreach(KeyValuePair<string,int> variable in VariableDictionary)
{
if(variable.Key.Equals(inputcommand[2]))
{
value1 = variable.Value;
Value1IsVariable = true;
}
}
if(inputcommand.Length.Equals(5))
{
foreach(KeyValuePair<string,int> variable in VariableDictionary)
{
if (variable.Key.Equals(inputcommand[4]))
{
value2 = variable.Value;
Value2IsVariable = true;
}
}
if (AlreadyInArray)
{
VariableDictionary.Remove(inputcommand[0]);
}
}
try
{
if (Value1IsVariable.Equals(false))
{
value1 = Convert.ToInt32(inputcommand[2]);
}
if (Value2IsVariable.Equals(false))
{
if (inputcommand.Length.Equals(5))
{
value2 = Convert.ToInt32(inputcommand[4]);
}
}
if (inputcommand.Length > 3)
{
if (inputcommand[3].Equals("+"))
{
value = value1 + value2;
}
else if (inputcommand[3].Equals("-"))
{
value = value1 - value2;
}
else
{
//InvalidOperator();
}
}
else
{
try
{
value = int.Parse(inputcommand[2]);
}
catch (FormatException ex)
{
//If it is not an integer throw an error
//ValueIsIncorrect();
}
}
//Adds the variable with the inputted value if it is an integer
// VariableDictionary.Add(inputcommand[0], value);
}
catch (FormatException ex)
{
//If it is not an integer throw an error
//ValueIsIncorrect();
//return;
}
}
else
{
}
}
else
{
string[] validateinput = { "error" };
text = validateinput;
}
//end of condition
}
return text;
}
public void CheckForVariable()
{
//Loops through all the parameters
for (int i = 0; i<ParameterSplit.Length ; i++)
{
int index = i;
try
{
//Checks if the parameter is an int
int test = int.Parse(ParameterSplit[index]);
}
catch
{
Boolean foundVariable = false;
foreach (KeyValuePair<string, int> variable in MethodVariableDictionary)
{
if (variable.Key.Equals(ParameterSplit[index]))
{
//if it is then assign the parameter the integer value of the variable
ParameterSplit[index] = variable.Value.ToString();
//ErrorList = ErrorList + variable.Key + ParameterSplit[i] + Environment.NewLine; ;
foundVariable = true;
}
}
if (foundVariable == false)
{
//if it is not then loop through the VariableDictionary to check if the parameter is a variable name
foreach (KeyValuePair<string, int> variable in VariableDictionary)
{
if (variable.Key.Equals(ParameterSplit[i]))
{
//if it is then assign the parameter the integer value of the variable
ParameterSplit[i] = variable.Value.ToString();
}
}
}
}
}
}
//This method is used to get the color from pen by returning its values
internal Color getColor()
{
return color;
}
}
I tried this where I have to check 2 parameter using for loop inside CheckForVariable method
Related
When I'm going to draw the next stage at frame 2 my enemies did nothing.
When the stage1 finished I remove the listeners and at stage 2 I add it back.
So, How can I use the array that I have at frame 1 to another frame?
stop();
var enemy1Array:Array = new Array();
for (var e1:int = numChildren - 1; e1 >= 0; e1--)
{
var childe1:DisplayObject = getChildAt(e1);
if (childe1.name.indexOf("enemy")>-1)
{
enemy1Array.push(MovieClip(childe1));
MovieClip(childe1).hitPoints=enemykoufoueshitpoints;
}
}
stage.addEventListener(Event.ENTER_FRAME,loop);
function loop(event:Event):void
{
for(var e:int=enemy1Array.length-1;e>=0;e--)
{
if (playerrun == true)
{
enemy1Array[e].x -=speedall;
}
if (playerrunback == true)
{
enemy1Array[e].x +=speedall;
}
if (enemy1Array[e].hitTestPoint(player.x+5,player.y-40))
{
trace("i heart the player");
zoihit++;
enemy1Array[e].x +=70;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitTestPoint(player.x-5,player.y-40))
{
trace("i heart the player");
zoihit++;
enemy1Array[e].x -=70;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitTestObject(knife)&&attackright == true)
{
attackright = false;
enemy1Array[e].hitPoints -= knifedamage;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitTestObject(knife)&&attackleft == true)
{
attackleft = false;
enemy1Array[e].hitPoints -= knifedamage;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitPoints <= 0)
{
var thkiamantoui:thkiamanti= new thkiamanti;
diamondArray.push(thkiamantoui);
this.addChild(thkiamantoui);
thkiamantoui.gotoAndStop(2);
thkiamantoui.x = enemy1Array[e].x;
thkiamantoui.y = enemy1Array[e].y-5.05;
enemy1Array[e].parent.removeChild(enemy1Array[e]);
enemy1Array.splice(e,1);
}
if(enemy1Array[e].hitTestObject(dioswall))
{
enemy1Array[e].parent.removeChild(enemy1Array[e]);
enemy1Array.splice(e,1);
}
}
if(stage2.hitTestObject(playerhit))
{
stage.removeEventListener(Event.ENTER_FRAME,loop);
gotoAndPlay(2);
}
}
How can I sort an arraylist in Typescript by multiple fields.
For example, I have this object:
enter image description here
My single sort method works fine:
private sortFunction(a: RSFolderObject, b: RSFolderObject) {
var a_label = a.label.toLowerCase();
var b_label = b.label.toLowerCase();
if (a_label < b_label) {
return -1;
} else if (a_label > b_label) {
return 1;
} else {
return 0;
}
}
For multiple sorting I used that:
private sortFunction(a: RSFolderObject, b: RSFolderObject) {
var a_label = a.label.toLowerCase();
var b_label = b.label.toLowerCase();
var a_description = a.description[0].toLowerCase().replace("\\", "");
var b_description = b.description[0].toLowerCase().replace("\\", "");
if (a_label < b_label || a_description < b_description) {
return -1;
} else if (a_label > b_label || a_description > b_description) {
return 1;
} else {
return 0;
}
}
But it does not work.
OK, I solved it like this:
private sortFunction(a: RSFolderObject, b: RSFolderObject) {
var a_label = a.label.toLowerCase();
var b_label = b.label.toLowerCase();
var a_description = a.description[0].toLowerCase().replace("\\", "");
var b_description = b.description[0].toLowerCase().replace("\\", "");
if (a_label < b_label) {
return -1;
}
else if (a_label > b_label) {
return 1;
}
else {
if (a_description < b_description) {
return -1;
}
else if (a_description > b_description) {
return 1;
}
else {
return 0;
}
}
}
I like to sort an array with objects that have multiple properties. My objects have a string called name and a boolean called mandatory.
First i want to sort on age, next on the name.
How do I do this?
Ordering by age is easy...:
this.model.mylist.sort((obj1: IObj, obj2: IObj => {
if (obj1.age < obj2.age) {
return -1;
}
if (obj1.age > obj2.age) {
return 1;
}
return 0;
});
Well, you only add comparation for case when the two age values are the same. So something like this should work:
this.model.mylist.sort((obj1: IObj, obj2: IObj) => {
if (obj1.age < obj2.age) {
return -1;
}
if (obj1.age > obj2.age) {
return 1;
}
return obj1.name.localeCompare(obj2.name);
});
Something like this should work. The method compares the current and next values and adds comparison to the case when the two age values are the same. Then assume the column name in order based on age.
private compareTo(val1, val2, typeField) {
let result = 0;
if (typeField == "ftDate") {
result = val1 - val2;
} else {
if (val1 < val2) {
result = - 1;
} else if (val1 > val2) {
result = 1;
} else {
result = 0;
}
}
return result;
}
-
this.model.mylist.sort((a, b) => {
let cols = ["age", "name"];
let i = 0, result = 0, resultordem = 0;
while (result === 0 && i < cols.length) {
let col = cols[i];
let valcol1 = a[col];
let valcol2 = b[col];
let typeField = "string";
if (col == "age") {
typeField = "number";
}
if (valcol1 != "null" && valcol1 != "null") {
resultordem = this.compareTo(valcol1, valcol2, typeField);
if (resultordem != 0) {
break;
}
}
i++;
}
return resultordem;
});
The purpose of this code is is to define the root of the sum of the squares.
I cant figure out how to put i into j. Please help.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int input, som, i=0;
int j = 0;
double answer;
Boolean gaDoor= true;
int [] array = new int [24];
while (gaDoor)
{
Console.Write("Specify a positive integer");
input = Convert.ToInt32(Console.ReadLine());
if (input == -1)
{
gaDoor = false;
}
else
{
if (input >= 0)
{
array[i] = input;
i++;
}
else
{
Console.WriteLine("Specify a positive integer ");
}
}
}
while (j<i)
{
sum = array [j] ^ 2;
answer = Math.Sqrt(sum);
Console.Write(answer);
}
Console.ReadKey();
}
}
}
using System;
namespace Test
{
class MainClass
{
public static void Main (string[] args)
{
int[] invoer = new int[24];
double[] resultaat = new double[24];
double totaal = 0;
double wortel = 0;
int commando = 0;
int teller = -1;
try {
// Keep going until a negative integer is entered (or a 0)
while ((commando = Convert.ToInt32 (Console.ReadLine ())) > 0) {
teller++;
invoer [teller] = commando;
}
} catch (FormatException) {
// Not a number at all.
}
teller = -1;
foreach (int i in invoer) {
teller++;
resultaat [teller] = Math.Pow (invoer [teller], 2);
totaal += resultaat [teller];
if (invoer [teller] > 0) {
Console.WriteLine ("Invoer: {0}, Resultaat: {1}", invoer [teller], resultaat [teller]);
}
}
wortel = Math.Sqrt (totaal);
Console.WriteLine ("Totaal: {0}, Wortel: {1}", totaal, wortel);
}
}
}
Just as the name says, I want that for each certain name in an array a value is added to a int.
For example: if there are 3 strings of the same name in the array, then 3 times 50 will be added to the value.
This is my script I have now:
var lootList = new Array();
var interaction : Texture;
var interact = false;
var position : Rect;
var ching : AudioClip;
var lootPrice = 0;
function Update()
{
print(lootList);
if ("chalice" in lootList){
lootPrice += 50;
}
}
function Start()
{
position = Rect( ( Screen.width - interaction.width ) /2, ( Screen.height - interaction.height ) /2, interaction.width, interaction.height );
}
function OnTriggerStay(col : Collider)
{
if(col.gameObject.tag == "loot")
{
interact = true;
if(Input.GetKeyDown("e"))
{
if(col.gameObject.name == "chalice")
{
Destroy(col.gameObject);
print("chaliceObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("chalice");
}
if(col.gameObject.name == "moneyPouch")
{
Destroy(col.gameObject);
print("moneyPouchObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("moneyPouch");
}
if(col.gameObject.name == "ring")
{
Destroy(col.gameObject);
print("ringObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("ring");
}
if(col.gameObject.name == "goldCoins")
{
Destroy(col.gameObject);
print("coldCoinsObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("goldCoins");
}
if(col.gameObject.name == "plate")
{
Destroy(col.gameObject);
print("plateObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("plate");
}
}
}
}
function OnTriggerExit(col : Collider)
{
if(col.gameObject.tag == "pouch")
{
interact = false;
}
}
function OnGUI()
{
if(interact == true)
{
GUI.DrawTexture(position, interaction);
GUI.color.a = 1;
}
}
It's for a game I'm making where you can steal items for extra score points.
I've tried using the for(i = 0; i < variable.Length; i++) but that didn't seem to work.
The only thing I can think of now is using booleans to add it once. But that isn't memory friendly.
Help is appreciated and thanks in advance!
You could use the standard .forEach(callback) method:
lootList.forEach(function(value, index, array)
{
if (value === "chalice") { lootPrice += 50; }
});
If you don't have that method, you could implement it like this:
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (callback) {
for(var i = 0; i < this.length; i++) { callback(this[i], i, this); }
}
}