forked from PhiNotPi/NanoCoreWar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
69 lines (65 loc) · 1.93 KB
/
Player.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.ArrayList;
import java.util.HashMap;
import java.nio.IntBuffer;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
/**
* This contains the compiled code for each warror and a method for adding a line with three arguments.
*
* @author PhiNotPi, Ilmari Karonen
* @version 3/17/15
*/
public class Player
{
private String name, hash;
private ArrayList<Instruction> codeList;
public Player(String name)
{
this.name = name;
codeList = new ArrayList<Instruction>();
hash = null;
}
public void addLine(Instruction command)
{
codeList.add(command);
hash = null; // invalidate old hash
}
public ArrayList<Instruction> getCode()
{
return codeList;
}
public String getName()
{
return name;
}
public String getUniqueHash()
{
if (hash != null) return hash;
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
for (Instruction command : codeList)
{
ByteBuffer byteBuf = ByteBuffer.allocate(3 * 4);
IntBuffer intBuf = byteBuf.asIntBuffer();
intBuf.put(command.packedOp);
intBuf.put(command.field1);
intBuf.put(command.field2);
md.update(byteBuf.array());
}
byte[] digest = md.digest();
char[] hexDigest = new char[digest.length * 2];
char[] hexChars = "0123456789abcdef".toCharArray();
for(int i = 0; i < digest.length; i++)
{
hexDigest[2*i] = hexChars[(digest[i] >> 4) & 0xF];
hexDigest[2*i+1] = hexChars[digest[i] & 0xF];
}
hash = new String(hexDigest);
return hash;
}
catch (java.security.NoSuchAlgorithmException e)
{
return null; // should never happen
}
}
}