Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Best Way to Code Random Percentage of Something on: 2013-04-09 11:01:01
I'm trying to figure out the best way to code the random occurance of something based on percent without hard coding if-else for each percentage since it will be dynamic.
Ex:

Item 1 drops 20% of the time
Item 2 drops 60% of the time
Item 3 drops 20% of the time

I'm storing the itemID and percentage as HashMaps
// itemID, percentage
HashMap<Integer, Integer>

But is there a better way to store these pairs? I could create a new class with only 2 variables called Pair. Would a Pair class be more efficient than a HashMap for storing multiple itemID and percentage?

I somehow have to go through each item in the HashMap and get the percentages, then add all them up (20 + 60 + 20) to get 100.
I then have to do some random number generation to get a number then check which item will drop. To do this I may have to order check from lowest percentage to highest, but if 2 percentages are the same (20 + 20), then I'm not sure how to pick which one should be dropped.
2  Game Development / Newbie & Debugging Questions / Java Object Variable Size of another Object? on: 2012-08-06 20:46:25
If I have an object:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class Element {
   public int id; // 4 byte
 
   public Element(int id) {
      this.id = id;
   }
}
// This would be a total of 12 bytes from my understanding (8 for header, 4 for int id)

// and then have another class
public class Unit {
   public int uid; // 4 byte
  public Element element; // ?? byte
}


would the the second object's size be: 16 bytes?
8 header, 4 int uid, 4 Element (reference?)
or would it contain the size of the Element object as well thus end up being 24 bytes total?
8 header, 4 int uid, 12 Element
What I'm asking is how many bytes is ?? byte within the Unit object?

I'll end up using a lot of objects so I need to figure out the best way to store an object.
Would it be better to store variable element within Unit as an int elementId field or use Element element? With int I'd be doing a search to grab the element but I will be storing all of these into a HashMap so it wouldn't be a linear search.

How it'll work is both are stored in 2 separate HashMap, but when I need to read the Element of Unit, I can use just unit.element to return Element or do elementHashMap.get(unit.elementId) depending on how I structure the data
3  Game Development / Newbie & Debugging Questions / Error reading file on: 2012-01-24 23:09:37
I have a file called privatekey.der in the same directory as a static class called KeyReader.

I'm having issues with reading the file. It's giving me invalid stream header. I was wondering how you can go about reading the file.

This is my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
package server.encryption;

import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class KeyReader {

   public static PrivateKey getPrivateKey(InputStream in) throws Exception {
//      System.err.println(filename);
//      File f = new File(filename);
     //DataInputStream dis = new DataInputStream(is);
     ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));
      BigInteger m = (BigInteger) oin.readObject();
      BigInteger e = (BigInteger) oin.readObject();
      RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
       KeyFactory fact = KeyFactory.getInstance("RSA");
       PrivateKey privateKey = fact.generatePrivate(keySpec);
       oin.close();
       return privateKey;
//      PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
//      KeyFactory kf = KeyFactory.getInstance("RSA");
//      return kf.generatePrivate(spec);
  }
   
   public static byte[] rsaDecrypt(byte[] data) throws Exception {
      System.err.println(KeyReader.class.getResourceAsStream("privatekey.der").toString());
      PrivateKey privateKey = getPrivateKey(KeyReader.class.getResourceAsStream("privatekey.der"));
      Cipher cipher = Cipher.getInstance("RSA");
      cipher.init(Cipher.DECRYPT_MODE, privateKey);
      byte[] cipherData = cipher.doFinal(data);
      return cipherData;
   }
   
   public static byte[] aesDecrypt(byte[]data, byte[]key) throws Exception {
      SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
      Cipher cipher = Cipher.getInstance("AES");
      cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, cipher.getParameters());
      byte[] cipherData = cipher.doFinal(data);
      return cipherData;
   }
   
}
4  Game Development / Networking & Multiplayer / Server acting as both server and client on: 2012-01-05 21:32:56
I have 2 servers - a chat server and world server. Both are connected to clients on different ports. I want to set up a unique port for just the world server to communicate with the chat server. I want the world server acting as a client to the chat server.

How would I go about doing this? I know how to set up 1 port but not mulltiple

This is the basic setup of the servers I have:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
      try {
         this.serverSocket = new ServerSocket(this.port);
         this.listening = true;
            while (listening) {
                Socket socket = this.serverSocket.accept();
                socket.setTcpNoDelay(true);
                DebugConsole.print("Client connection from " + socket.getRemoteSocketAddress(), "General");
                PacketAction socketConnection = new PacketAction(socket, this);
                socketConnection.start();
                this.clientConnections.add(socketConnection);
            };
        }
        catch (Exception e) {
            DebugConsole.print("Exception (run): " + e.getMessage(), "Error");
        }
5  Game Development / Newbie & Debugging Questions / Storing a reference of an object vs using a 4 byte int ID on: 2012-01-04 09:42:45
Hello suppose I have an object say: Equip which contains a bunch of variables in the class.
I have already created the object and stored it into a Library object so it can be fetched later by an int ID.

Now suppose I create a Character object that wants to wear the Equip. The character object has a field say equipWeared.
I can either make equipWear an int to store the ID of the Equip I made or make it Equip type which is the class Equip is.
I am not making a new instance of Equip but just referencing it by assigning the variable the Equip from the Library object.

Which would be better and more efficient? Should I just put in an int ID number or put in a reference to the Equip?
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (33 views)
2014-04-15 18:08:23

BurntPizza (30 views)
2014-04-15 03:46:01

UprightPath (45 views)
2014-04-14 17:39:50

UprightPath (28 views)
2014-04-14 17:35:47

Porlus (45 views)
2014-04-14 15:48:38

tom_mai78101 (66 views)
2014-04-10 04:04:31

BurntPizza (126 views)
2014-04-08 23:06:04

tom_mai78101 (226 views)
2014-04-05 13:34:39

trollwarrior1 (191 views)
2014-04-04 12:06:45

CJLetsGame (199 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!