Results 1 to 1 of 1
- 12-18-2007, 08:45 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 2
- Rep Power
- 0
linked list nodes all refernce same item.
I was running the program using an array, and it worked properly. However when I switched to using a linked list, every item in the list seems to be the same. The nodes in the Item_db_l all seem to be the same. When I run the program all the items are apparently the same. I created a copy routine in the Item_Class, because I thought that primitives are copied and not set as references.
Java Code:import java.awt.*; import java.net.*; import java.io.*; import java.applet.*; import java.awt.event.*; import java.util.*; class Item_Class{ public int Attack; public int Defense; public int Item_Type;//1: Armour 2:Weapon public int Item_UID;//Unique ID public int Value; public String Name; public void copy(Item_Class CopyFrom){ this.Attack = CopyFrom.Attack; this.Defense = CopyFrom.Defense; this.Item_Type = CopyFrom.Item_Type; this.Item_UID = CopyFrom.Item_UID; this.Value = CopyFrom.Value; this.Name = CopyFrom.Name; } } class GuyClass extends Applet{ private int Health; public int Damage; public int Foes; public int X; public int Y; public int Gold; public int Exp; public int UID; Item_Class Item[] = new Item_Class[2];//0: Weapon 1:Armor public String Debug; public String PicName; public String Name; Image CharImage; public boolean Collision(int X2,int Y2,int Width,int Height){ return !(X2 >= X + CharImage.getWidth (this) || X2+Width <= X || Y2 >= Y+CharImage.getHeight (this) || Y2+Height <= Y); } public void Hit(int Damage_Amount){ if (Damage_Amount > Item[1].Defense) Health = Health - Damage_Amount + Item[1].Defense; } public void Set_Health(int New_Health){ Health = New_Health; } public int Get_Health(){ return(Health); } public void copy(GuyClass CopyFrom){ Health = CopyFrom.Health; Damage = CopyFrom.Damage; Foes = CopyFrom.Foes; X = CopyFrom.X; Y = CopyFrom.Y; Gold = CopyFrom.Gold; Exp = CopyFrom.Exp; UID = CopyFrom.UID; Item[0].copy(CopyFrom.Item[0]); Item[1].copy(CopyFrom.Item[1]); Debug = CopyFrom.Debug; PicName = CopyFrom.PicName; Name = CopyFrom.Name; CharImage = CopyFrom.CharImage; } } public class Game5 extends Applet implements MouseListener,Runnable{ Image CharImage; Image VillImage; boolean debuggin = false; String Version = "Version 1.23"; Thread AIThread; boolean airunning = true; GuyClass Guy[] = new GuyClass[31]; int i; int Mouse_X; int Mouse_Y; InputStream inStream; // Input stream for reading instructions and high scores int places[]; // Storage area for high score places int scores[]; // Storage area for high score scores String names[]; // Storage area for high score names // Item_Class Item_db[] = new Item_Class[30]; GuyClass Monster_db[] = new GuyClass[30]; LinkedList<Item_Class> Item_db_l = new LinkedList<Item_Class>(); public void init() { // for(i=0;i<30;i++){ // Item_db[i] = new Item_Class(); // // } for(i=0;i<30;i++){ Monster_db[i] = new GuyClass(); Monster_db[i].Item[0] = new Item_Class();//Create first item a Weapon Monster_db[i].Item[1] = new Item_Class();//Create first item a Weapon } WeaponRead(); MonsterRead(); Guy[0] = new GuyClass();//need to add items to each guy Guy[0].PicName = "images/Character.gif"; Guy[0].CharImage = getImage(getCodeBase(), Guy[0].PicName); Guy[0].Set_Health(100); Guy[0].Damage = 5; Guy[0].X = 10; Guy[0].Y = 100; Guy[0].Foes = 0; Guy[0].Gold = 0; Guy[0].Exp = 0; Guy[0].Item[0] = new Item_Class();//Create first item a Weapon Guy[0].Item[0].copy(Item_db_l.get(2)); Guy[0].Item[1] = new Item_Class();//Create second item armor Guy[0].Item[1].copy(Item_db_l.get(2)); for(i=1;i<=30;i++){ Guy[i] = new GuyClass(); Guy[i].Item[0] = new Item_Class();//Create first item a Weapon Guy[i].Item[1] = new Item_Class();//Create second item armor Guy[i].Set_Health(0); } BadGuyRead(); for(i=0;i<=30;i++)if (Guy[i].PicName != "") { Guy[i].CharImage = getImage(getCodeBase(), Guy[i].PicName); } addMouseListener(this); AIThread= new Thread(this); // Create Thread AIThread.start(); //Run Thread } public void paint(Graphics g) { for (int i=0; i<=30;i++){ if (Guy[i].Get_Health() > 0) g.drawImage(Guy[i].CharImage, Guy[i].X,Guy[i].Y, this); } g.drawString(Version+" MX = "+Mouse_X+" MY = "+Mouse_Y,10,10); for (int i=0; i<=5;i++) g.drawString("Health "+Guy[i].Get_Health()+" Damage "+Guy[i].Item[0].Attack+" Weapon "+Guy[i].Item[0].Name+" Armor "+Guy[i].Item[1].Defense+" "+Guy[i].PicName+" "+Guy[i].Debug,10,280+i*10); } public void destroy() { removeMouseListener(this); } public void mouseReleased(MouseEvent e) { int x = e.getX(); int y = e.getY(); Mouse_X = x; Mouse_Y = y; for (int i=1; i<=30;i++) if ((Guy[0].Get_Health() > 0 ) && (Guy[i].Get_Health() > 0)&&(Guy[i].Collision(x,y,1,1) == true)){ //Guy[i].Set_Health(0); Guy[i].Hit(Guy[0].Damage); if (Guy[i].Get_Health() < 1) Guy[0].Foes++; Guy[i].Debug = Guy[i].Debug + " attack "+i; } repaint(); } public void mousePressed(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void WeaponRead() { Item_Class Item_Temp; Item_Temp = new Item_Class(); String line; String fileToRead = "Weapon.txt"; int num_w; URL url = null; try{ url = new URL(getCodeBase(), fileToRead); } catch(MalformedURLException e){} try{ InputStream in = url.openStream(); BufferedReader bf = new BufferedReader(new InputStreamReader(in)); Item_Temp = new Item_Class(); num_w = Integer.valueOf(bf.readLine()).intValue(); for (int counter=0;counter < num_w;counter ++){ Item_Temp.Attack = Integer.valueOf(bf.readLine()).intValue(); Item_Temp.Defense = Integer.valueOf(bf.readLine()).intValue(); Item_Temp.Item_Type = Integer.valueOf(bf.readLine()).intValue(); Item_Temp.Item_UID = Integer.valueOf(bf.readLine()).intValue(); Item_Temp.Value = Integer.valueOf(bf.readLine()).intValue(); Item_Temp.Name = bf.readLine(); Item_db_l.add(Item_Temp); } } catch(IOException e){ e.printStackTrace(); } Item_Temp = new Item_Class(); Item_Temp.Name = "test name"; } public void MonsterRead() { String line; String fileToRead = "Monsters.txt"; int num_w; URL url = null; try{ url = new URL(getCodeBase(), fileToRead); } catch(MalformedURLException e){} try{ InputStream in = url.openStream(); BufferedReader bf = new BufferedReader(new InputStreamReader(in)); num_w = Integer.valueOf(bf.readLine()).intValue(); for (int counter=0;counter < num_w;counter ++){ Monster_db[counter] = new GuyClass(); Monster_db[counter].Set_Health(Integer.valueOf(bf.readLine()).intValue()); Monster_db[counter].Damage = Integer.valueOf(bf.readLine()).intValue(); Monster_db[counter].Foes = Integer.valueOf(bf.readLine()).intValue(); Monster_db[counter].Gold = Integer.valueOf(bf.readLine()).intValue(); Monster_db[counter].Exp = Integer.valueOf(bf.readLine()).intValue(); Monster_db[counter].UID = Integer.valueOf(bf.readLine()).intValue(); Monster_db[counter].Item[0] = new Item_Class(); Monster_db[counter].Item[0].copy(Item_db_l.get(Integer.valueOf(bf.readLine()).intValue())); Monster_db[counter].Item[1] = new Item_Class(); Monster_db[counter].Item[1].copy(Item_db_l.get(Integer.valueOf(bf.readLine()).intValue())); Monster_db[counter].Debug = bf.readLine(); Monster_db[counter].PicName = bf.readLine(); Monster_db[counter].Name = bf.readLine(); } } catch(IOException e){ e.printStackTrace(); } } public void BadGuyRead() { String line; String fileToRead = "BadGuy.txt"; int num_w; URL url = null; try{ url = new URL(getCodeBase(), fileToRead); } catch(MalformedURLException e){} try{ InputStream in = url.openStream(); BufferedReader bf = new BufferedReader(new InputStreamReader(in)); num_w = Integer.valueOf(bf.readLine()).intValue(); for (int counter=1;counter <= num_w;counter ++){ Guy[counter] = new GuyClass(); Guy[counter].Item[0] = new Item_Class();//Create first item a Weapon Guy[counter].Item[1] = new Item_Class();//Create first item a Weapon Guy[counter].copy(Monster_db[Integer.valueOf(bf.readLine()).intValue()]); Guy[counter].X = Integer.valueOf(bf.readLine()).intValue(); Guy[counter].Y = Integer.valueOf(bf.readLine()).intValue(); } } catch(IOException e){ e.printStackTrace(); } } public void run() { // loop until told to stop while (airunning) { for (int i=1; i<=5;i++){ if ((Guy[i].Get_Health() > 0)&&(Guy[0].Get_Health() > 0)) Guy[0].Hit(Guy[i].Damage); } repaint(); try { // Wait 500milliseconds before continuing AIThread.sleep(250); } catch (InterruptedException e) { System.out.println(e); } // he has wait and will now restart his actions. } } }
Similar Threads
-
Circular Double Linked List
By theonly in forum Advanced JavaReplies: 3Last Post: 12-06-2009, 05:10 PM -
Linked List help
By neobie in forum New To JavaReplies: 8Last Post: 12-22-2007, 03:15 AM -
going from vectors to linked list?
By cbrown08 in forum New To JavaReplies: 3Last Post: 12-01-2007, 12:55 AM -
Linked List
By rnavarro9 in forum New To JavaReplies: 0Last Post: 11-29-2007, 03:42 AM -
Help with linked list
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks