BigInteger parsing on Silverlight - silverlight

I'm actually working on a IBAN key verification function.
To get the key i do something like :
string theKey = (98 - ((int64.Parse(value)) % 97)).ToString();
The problem is that my value is something longer than 19. So i need to use BigInteger from System.Numerics.
This references doesn't include the Parse() method.
I need a solution that would allow me to use 23char integer on Silverlight.

Yep i dont think BigInteger.Parse() is available in silverlight.
You could use Decimal just without a decimal point, as a decimal value can go up to 79,228,162,514,264,337,593,543,950,335.
(29 chars) if i counted correctly..
*Edit - the reason i chose decimal over double is that decimal has more significant figures and can therefore be more precise.

Someone on MSDN gave me a class that comes with Parse/TryParse, it works really good and i hope it'll help. Thanks for the decimal solution though, but it appears that i need to use 30 digits int as well, so biginteger was a must have :
public static class BigIntegerHelper
{
public static BigInteger Parse(string s)
{
return Parse(s, CultureInfo.CurrentCulture);
}
public static BigInteger Parse(string s, IFormatProvider provider)
{
return Parse(s, NumberStyles.Integer, provider);
}
public static BigInteger Parse(string s, NumberStyles style)
{
return Parse(s, style, CultureInfo.CurrentCulture);
}
public static BigInteger Parse(string s, NumberStyles style, IFormatProvider provider)
{
BigInteger result;
if (TryParse(s, style, provider, out result))
{
return result;
}
throw new FormatException();
}
public static bool TryParse(string s, out BigInteger b)
{
return TryParse(s, NumberStyles.Integer, CultureInfo.CurrentCulture, out b);
}
public static bool TryParse(string s, NumberStyles style, IFormatProvider formatProvider, out BigInteger value)
{
if (formatProvider == null)
{
formatProvider = CultureInfo.CurrentCulture;
}
if ((style & ~(NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowHexSpecifier)) != NumberStyles.None)
{
throw new NotSupportedException();
}
NumberFormatInfo numberFormatInfo = (NumberFormatInfo)formatProvider.GetFormat(typeof(NumberFormatInfo));
uint num = ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None) ? 16u : 10u;
int num2 = 0;
bool flag = false;
if ((style & NumberStyles.AllowLeadingWhite) != NumberStyles.None)
{
while (num2 < s.Length && IsWhiteSpace(s[num2]))
{
num2++;
}
}
if ((style & NumberStyles.AllowLeadingSign) != NumberStyles.None)
{
int length = numberFormatInfo.NegativeSign.Length;
if (length + num2 < s.Length && string.Compare(s, num2, numberFormatInfo.NegativeSign, 0, length, CultureInfo.CurrentCulture, CompareOptions.None) == 0)
{
flag = true;
num2 += numberFormatInfo.NegativeSign.Length;
}
}
value = BigInteger.Zero;
BigInteger bigInteger = BigInteger.One;
if (num2 == s.Length)
{
return false;
}
for (int i = s.Length - 1; i >= num2; i--)
{
if ((style & NumberStyles.AllowTrailingWhite) != NumberStyles.None && IsWhiteSpace(s[i]))
{
int num3 = i;
while (num3 >= num2 && IsWhiteSpace(s[num3]))
{
num3--;
}
if (num3 < num2)
{
return false;
}
i = num3;
}
uint num4;
if (!ParseSingleDigit(s[i], (ulong)num, out num4))
{
return false;
}
if (num4 != 0u)
{
value += num4 * bigInteger;
}
bigInteger *= num;
}
if (value.Sign == 1 && flag)
{
value = -value;
}
return true;
}
private static bool IsWhiteSpace(char ch)
{
return ch == ' ' || (ch >= '\t' && ch <= '\r');
}
private static bool ParseSingleDigit(char c, ulong radix, out uint result)
{
result = 0;
if (c >= '0' && c <= '9')
{
result = (uint)(c - '0');
return true;
}
if (radix == 16uL)
{
c = (char)((int)c & -33);
if (c >= 'A' && c <= 'F')
{
result = (uint)(c - 'A' + '\n');
return true;
}
}
return false;
}
}

Related

How do I sort string numbers? [duplicate]

This question already has answers here:
Sort on a string that may contain a number
(24 answers)
Closed 3 years ago.
The output of this string should be sorted as follows.
The output of this string should be sorted as follows.
public static void main(String[] args) {
String input="";
List<String> items = Arrays.asList(input.split("\\s*,\\s*"));
System.out.println("items: " + items);
Collections.sort(items, new Comparator<String>() {
public int compare(String o1, String o2) {
String o1StringPart = o1.replaceAll("\\d", "");
String o2StringPart = o2.replaceAll("\\d", "");
if (o1StringPart.equalsIgnoreCase(o2StringPart)) {
return extractInt(o1) - extractInt(o2);
}
return o1.compareTo(o2);
}
int extractInt(String s) {
String num = s.replaceAll("\\D", "");
// return 0 if no digits found
return num.isEmpty() ? 0 : Integer.parseInt(num);
}
});
for (String s : items) {
System.out.println(s);
}} }
I assume that all the numeric are integer and all the alphabets are A to Z, you can transform those strings which contains / or - into floating points first, then replace all the alphabets to empty strings. Finally, compare their values as Double.
For example, 51/1 will be 51.1 and 571-573B will be 571.573.
Code snippet
public int compare(String o1, String o2) {
String n1 = o1.replace("-", ".").replace("/", ".").replaceAll("[A-Z]", "");
String n2 = o2.replace("-", ".").replace("/", ".").replaceAll("[A-Z]", "");
// This equals above statements
//String n1 = o1.replaceAll("[-/]", ".").replaceAll("[A-Z]", "");
//String n2 = o2.replaceAll("[-/]", ".").replaceAll("[A-Z]", "");
int result = Double.compare(Double.valueOf(n1), Double.valueOf(n2));
return (result == 0) ? o1.compareTo(o2) : result;
}
This is not the most elegant way, but I think it should work!
Use the below snippet code. Firstly, you need to compare the integer part of the string and in case the integer part is equal compare the string part.
import java.io.*;
import java.util.*;
import java.util.regex.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
class Solution {
public static void main(String[] args) {
String input = "605,2A,401-2A,32C,21F,201A,605A,401-1A,200-2E,583-58D,583/58E,583-57D,542,2B,1,542/2E,605B,32D,3,603,4,6,5,60,201C,542/2D,40,20,50,200-2C,21C,800A,200A,571-573B,51/2,470/1,51/1,571-573C,454-1,444-446";
List < String > items = Arrays.asList(input.split("\\s*,\\s*"));
System.out.println("items: " + items);
Pattern pattern = Pattern.compile("^\\d+");
Collections.sort(items, new Comparator < String > () {
public int compare(String o1, String o2) {
int intDiff = extractInt(o1) - extractInt(o2);
if (intDiff == 0) {
return o1.compareTo(o2);
}
return intDiff;
}
int extractInt(String s) {
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
String num = matcher.group(0);
return Integer.parseInt(num);
}
return 0;
}
});
for (String s: items) {
System.out.println(s);
}
}
}
This answer handles comparison between numbers like 20-10A and 20-2A
public static void main(String[] args) {
String s = "605,2A,401-2A,32C,21F,201A,605A,401-1A,200-2E,583-58D,583/58E,583-57D,542,2B,1,542/2E," +
"605B,32D,3,603,4,6,5,60,201C,542/2D,40,20,50,200-2C,21C,800A,200A,571-573B,51/2,470/1,51/1," +
"571-573C,454-1,444-446";
String[] strings = s.split(",");
Arrays.sort(strings, App::compare);
System.out.println(Arrays.deepToString(strings));
}
public static int compare(String o1, String o2) {
if (startsWithDelim(o1)) return compare(o1.substring(1), o2);
if (startsWithDelim(o2)) return compare(o1, o2.substring(1));
List<String> n1 = extractInt(o1);
List<String> n2 = extractInt(o2);
if (n1 != null && n2 != null) {
Integer n1int = Integer.parseInt(n1.get(0));
Integer n2int = Integer.parseInt(n2.get(0));
String n1Remaining = n1.get(1);
String n2Remaining = n2.get(1);
int intCompare = n1int.compareTo(n2int);
return intCompare == 0 ? compare(n1Remaining, n2Remaining) : intCompare;
}
if (n1 == null && n2 == null)
return o1.compareTo(o2);
else if (n1 == null) return -1;
else return 1;
}
static List<String> extractInt(String s) {
Pattern pattern = Pattern.compile("^\\d+");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
String num = matcher.group(0);
return List.of(num, s.substring(matcher.end(0)));
}
return null;
}
static boolean startsWithDelim(String s) {
return (s.startsWith("/") || s.startsWith("-"));
}
I will provide some tips
In order compare the items 1,20,200-2C,3,32C,32D,4 (this is the default sort order, as string) you need to check if any number is included in the string. This can be done using a regular expression. You can find a lot of examples online with regular expressions that can bring you the numbers in the string back.
Modify your comparator and check if any of the two string that are to be compared include any number and return the appropriate result.
the extractInt method can be similar to as #Pankaj Saini 's suggestion
Integer extractInt(String s) {
Pattern pattern = Pattern.compile("^\\d+");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
String num = matcher.group(0);
return Integer.parseInt(num);
}
return null;
}
The compare method can be like this
public int compare(String o1, String o2) {
if(extractInt(o1)!=null && extractInt(o2)!=null){
if(extractInt(o1).equals(extractInt(o2)))
{
return o1.substring(extractInt(o1).toString().length())
.compareTo(o2.substring(extractInt(o2).toString().length()));
}
return extractInt(o1).compareTo(extractInt(o2));
}
else if(extractInt(o1)!=null)
{
return -1;
}
else if(extractInt(o2)!=null)
{
return 1;
}
else{
return o1.compareTo(o2);
}
}

How do I repeat an action in c# to fix the jumping mechanism

I'm creating a basic 2D platformer game however the jumping mechanism will only run once and land directly afterwards. How do I loop this?
I tried detection collisions (from tag: Terrain) and this does help a lot however it's still not working correctly.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController2D : MonoBehaviour
{
private Rigidbody2D rb2D;
private Vector2 velocity;
Vector2 xvelocity = new Vector2(10, 0);
Vector2 yvelocity = new Vector2(0, 10);
public float jumptime;
bool grounded = true;
bool jump = false;
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetButton("Jump"))
{
jump = true;
}
}
private void FixedUpdate()
{
//Debug.Log(Input.GetAxisRaw("Vertical"));
if (left == true)
{
//Debug.Log("Left");
rb2D.MovePosition(rb2D.position + -xvelocity * Time.fixedDeltaTime);
}
if (right == true)
{
//Debug.Log("Right");
rb2D.MovePosition(rb2D.position + xvelocity * Time.fixedDeltaTime);
}
//if (jump == true && grounded == true)
//{
// jumptime -= Time.fixedDeltaTime;
// if (jumptime > 0)
// {
// rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
// }
// if (jumptime <= 0)
// {
// jump = false;
// jumptime = 2;
// }
if (jump == true && grounded == true && jumptime > 0)
{
jumptime -= Time.fixedDeltaTime;
rb2D.MovePosition(rb2D.position + yvelocity * Time.fixedDeltaTime);
} else if (jumptime <= 0)
{
jump = false;
jumptime = 2f;
}
//if (Time.fixedDeltaTime >= 2)
//{
// jump = false;
// rb2D.MovePosition(rb2D.position + -yvelocity * Time.fixedDeltaTime);
//}
}
void OnCollisionExit2D(Collision2D other)
{
Debug.Log("No longer in contact with " + other.transform.name);
jump = true;
grounded = false;
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Terrain")
{
Debug.Log("Landed");
jump = false;
grounded = true;
}
}
}
The expected outcome is that the action 'jump' will loop for ~1/1.5 seconds with a good height (vector2 yvelocity) so it will jump higher and will come down afterwards (thanks to the gravity from the Rigidbody (2D))
As stated in the comments I think the main issue is coming from this line of code.
if (jump == true && grounded == true && jumptime > 0)
It is much likely that one of those bool is not what you expect it to be. Anyway I suggest you to convert the line like so:
if (jump && grounded && jumptime > 0)
You do not need == true for booleans.
Anyway, to solve your question in an easier way, I would suggest you to use AddForce instead of move (because you're using a rigibody2d anyway).
rb2D.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
A small note about horizontal velocity. If you're using a rigibody it would be better to move it with the same rigidbody and not with the transform:
rb2D.MovePosition(rb2D.position + Vector2.left * xspeed * Time.fixedDeltaTime);
Your code will become:
public class PlayerController2D : MonoBehaviour
{
private Rigidbody2D rb2D;
private Vector2 velocity;
public float jumpForce = 5;
bool grounded = true;
void Start() { rb2D = GetComponent<Rigidbody2D>(); }
void Update()
{
if (Input.GetButton("Jump") && grounded)
rb2D.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
//calculate horizontal speed here
xspeed = ...;
}
private void FixedUpdate()
{
rb2D.MovePosition(rb2D.position + Vector2.left * xspeed * Time.fixedDeltaTime);
}
void OnCollisionExit2D(Collision2D other)
{
Debug.Log("No longer in contact with " + other.transform.name);
grounded = false;
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Terrain")
{
Debug.Log("Landed");
grounded = true;
}
}
}

Adding up integer values and using them in different clas

I am making a fun practice script for some review but I have come across some problems. The script uses random numbers to decide between the letters "A, B or C" and when you get a set of 3 shows Yahtzee! on the console. I can get that to work just fine but decided to add how many Yahtzees you got out of 25 as well. Here is what I have so far.
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import ArrayList.EnhancedLoop2;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("TIME TO PLAY JAVA YAHTZEE");
System.out.println("Type 1 when ready");
in.nextInt();
ArrayList<NewClass> al = new ArrayList<NewClass>();
for(int i = 0; i <= 25; i++)
{
NewClass nw = new NewClass();
al.add(nw);
}
for(NewClass enhanced : al)
{
System.out.println("You got " + enhanced.m + " Yahtzees. Good Job");
}
}
}
import java.util.ArrayList;
import java.util.Random;
public class NewClass {
public String a;
public String b;
public String c;
public static int m;
public NewClass()
{
getLetter();
}
public static String getLetter()
{
String rv = "";
System.out.println("");
String a = method1();
String b = method1();
String c = method1();
System.out.println("Your letters are");
System.out.println(a + "\n" + b + "\n" + c);
System.out.print("your set is: " + a + b + c + "\n");
getLetter2(a, b, c);
return rv;
}
public static String getLetter2(String a, String b, String c)
{
String rv = "";
if(a == "A" && b == "A" && c == "A")
{
System.out.println("YAHTZEE!");
}
else if(a == "B" && b == "B" && c == "B")
{
System.out.println("YAHTZEE!");
}
else if(a == "C" && b == "C" && c == "C")
{
System.out.println("YAHTZEE!");
m = yahtzeeCount(a, b, c);
}
return rv;
}
public static String method1()
{
String letter = "";
Random r = new Random();
for(int i = 0; i <= 2; i++)
{
int cv = r.nextInt(9) + 1;
if(cv <= 3)
{
letter = "A";
}
else if(cv >= 4 && cv <= 6)
{
letter = "B";
}
else if(cv >=7 && cv <=9)
{
letter = "C";
}
}
return letter;
}
public static int yahtzeeCount(String a, String b, String c)
{
int rv = 0;
if(a == "A" && b == "A" && c == "A" || a == "B" && b == "B" && c == "B" || a == "C" && b == "C" && c == "C")
{
rv = 1;
}
return rv;
}
}
I am also having a problem with the script showing "you got # yahtzees. Good job." 25 times instead of once and I can't seem to figure out how to make it show only once.
All help is much appreciated. Thank You.
I ended up changing the script quite a bit and in the end had no need for an ArrayList(I didn't really understand enhanced loops). Here is what I ended up with
package Yahtzee;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
System.out.println("TIME TO PLAY JAVA YAHTZEE");
System.out.println("Type 1 when ready");
in.nextInt();
for(int i = 0; i <= 25; i++)
{
Test2 nw = new Test2();
}
System.out.println("Congratulations you got " + Test2.rv + " Yahtzees");
}
}
package Yahtzee;
import java.util.Random;
public class Test2 {
public String a;
public String b;
public String c;
public static int rv;
public Test2()
{
System.out.println("");
a = method1();
b = method1();
c = method1();
System.out.println("Your letters are");
System.out.println(a + "\n" + b + "\n" + c);
System.out.print("your set is: " + a + b + c + "\n");
if(a == "A" && b == "A" && c == "A")
{
System.out.println("YAHTZEE!");
rv = (rv + 1);
}
else if(a == "B" && b == "B" && c == "B")
{
System.out.println("YAHTZEE!");
rv = (rv + 1);
}
else if(a == "C" && b == "C" && c == "C")
{
System.out.println("YAHTZEE!");
rv = (rv + 1);
}
}
public static String method1()
{
String letter = "";
Random r = new Random();
for(int i = 0; i <= 2; i++)
{
int cv = r.nextInt(9) + 1;
if(cv <= 3)
{
letter = "A";
}
else if(cv >= 4 && cv <= 6)
{
letter = "B";
}
else if(cv >=7 && cv <=9)
{
letter = "C";
}
}
return letter;
}
}

Putting array with unknown variables into another array

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);
}
}
}

All elements change in Arraylist - Processing

My program is a zombie survival game, set in a 2d array of blocks.
I use an arraylist - my first attempt - to store the zombies, and each in tern "checks" if there is a block above, below and around it, to detect it's movement.
I'll post the relevant code here, and upload the sketch folder separately.
ArrayList zombies;
void setup() {
zombies = new ArrayList();
}
void draw() {
for (int i = 0; i < zombies.size(); i++) {
Zombie zombie = (Zombie) zombies.get(i);
zombie.draw();
}
}
void keyPressed() {
if (key == 'z') {
zombies.add(new Zombie());
}
}
class Zombie
{
int posX = 20;
int posY = 10;
boolean fall;
boolean playerOnBlock;
Zombie() {
posX = 10;
posY = 590;
fall = false;
}
void draw() {
grid.blockCheck(posX, posY, 2);
fill(0, 255, 0);
rect(posX, posY, 10, 10);
}
void fall() {
posY += 5;
println("zombiefall"+posY);
}
void move(boolean left, boolean right, boolean above) {
if (left == true && player.playerX < posX) {
posX -= 1;
}
if (right == true && player.playerX > posX) {
posX += 1;
}
}
}
class Grid {
void blockCheck(int x, int y, int e) {
for (int i = 0; i < l; i++)
for (int j = 0; j < h; j++)
{
grid[i][j].aroundMe (x, y, i, j, e);
}
}
}
class Block {
void aroundMe(int _x, int _y, int _i, int _j, int entity) {
int pGX = _x/10;
int pGY = _y/10;
if (entity == 1) {
if (pGY+1 == _j && pGX == _i && state == 4) {
player.fall();
}
if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
leftOfMe = true;
}
else
{
leftOfMe = false;
}
if (pGX+1 == _i && _y == posY && state == 4) {
rightOfMe = true;
}
else
{
rightOfMe = false;
}
if (pGY-1 == _j && _x == posX && state ==4) {
aboveOfMe = true;
}
else
{
aboveOfMe = false;
}
player.controls(leftOfMe, rightOfMe, aboveOfMe);
}
if (entity == 2) {
if (pGY+1 == _j && pGX == _i && state == 4) {
for (int i = 0; i < zombies.size(); i++) {
Zombie zombie = (Zombie) zombies.get(i);
zombie.fall();
}
}
if (pGX == _i && pGX-1 <= _i && _y == posY && state == 4) {
ZleftOfMe = true;
}
else
{
ZleftOfMe = false;
}
if (pGX+1 == _i && _y == posY && state == 4) {
ZrightOfMe = true;
}
else
{
ZrightOfMe = false;
}
if (pGY-1 == _j && _x == posX && state ==4) {
ZaboveOfMe = true;
}
else
{
ZaboveOfMe = false;
}
for (int i = 0; i < zombies.size(); i++) {
Zombie zombie = (Zombie) zombies.get(i);
zombie.move(ZleftOfMe, ZrightOfMe, ZaboveOfMe);
}
}
Sketch is here: http://www.mediafire.com/?u5v3117baym846v
I believe the problem lies in specifying which element of an arraylist I am referring to, as I can observe the issues to be:
All "zombies" fall when one detects that it should fall.
Zombie's speed increases with each additional zombie added - somehow treating all the zombie elements as one zombie object?
This might be a similar issue:
All elements of An ArrayList change when a new one is added?
But I've fiddled with my project and I can't seem to get it working still.
Please don't hesitate to ask for more information on my project. I will be with my computer all evening so should be able to reply quickly. Thanks in advance.
Thanks for your help.
I'm using it like this:
ArrayList <Zombie> zombies = new ArrayList <Zombie>();
-------------------------------------------
void setup(){
zombies = new ArrayList();
-------------------------------------------
void draw(){
for (Zombie z:zombies) {
z.draw();
}
}
-------------------------------------------
void keyPressed() {
if (key == 'z') {
for (int i = 0; i< 1; i++) {
zombies.add(new Zombie(i));
}
}
-------------------------------------------
class Zombie
{
int posX = 20;
int posY = 10;
boolean fall;
boolean playerOnBlock;
int z;
Zombie(int _z) {
posX = 10;
posY = 590;
fall = false;
z = _z;
}
void draw() {
grid.blockCheck(posX, posY, 2);
fill(0, 255, 0);
rect(posX, posY, 10, 10);
}
void fall() {
posY += 5;
println("zombiefall"+posY);
}
void move(boolean left, boolean right, boolean above) {
if (left == true && player.playerX < posX) {
posX -= 1;
}
if (right == true && player.playerX > posX) {
posX += 1;
}
}
}
Here is the idea :)
//use generics to keep it simple. Only Zoombies in this list
ArrayList <Zoombie> samp = new ArrayList <Zoombie>();
void setup() {
//a regular for loop to use the i as id
for (int i = 0; i< 100; i++) {
samp.add(new Zoombie(i));
}
//run just once
noLoop();
}
void draw() {
//a enhanced for, no need for use get()
// z will represent each element in the collection
for (Zoombie z:samp) {
z.d();
}
println("done");
}
// a dummy class ;)
class Zoombie {
int id;
Zoombie (int _id)
{
id =_id;
}
void d()
{
println(id);
}
}

Resources