Welcome Guest ( Log In | Register )

" width="8" height="8"/> debug this java code
Outline · Standard · [ Linear+ ]
Telum
post Mar 7 2006, 10:33 PM
Post #1


Admin
********

Group: Moderators
Posts: 7,429
Joined: 20-December 02
Member No.: 224



CODE
public class Ship
{
private String type;
private int length;
private boolean hit;

public Ship(String type, int length)
{
 this.type = type;
 this.length = length;
 hit = false;
}

public String getType()
{
 return this.type;
}

public int getSize()
{
 return length;
}

public boolean equals(Ship other)
{
 if(this.type.equals(other.getType()) && this.length == other.getSize()) return true;
 else return false;
}

public String getL()
{
 if(hit) return "*";
 else return type.substring(0,1);

}

public void hit()
{
 hit = true;
}
}



CODE

//boat is value, location is key
import java.util.*;
public class Board
{
private TreeMap board;
public Board()
{
 board = new TreeMap();
 for(int k=0; k>10;k++)
 {
  for(int j=0; j<10;j++)
  {
  board.put(new Location(j,k), null);
  }
 }
}

//start and end must be between 0,9
//ship must be in a straight line
public void place(Ship ship, Location start, Location end) throws Exception
{
 int stx = start.getX();
 int enx = end.getX();
 int sty = start.getY();
 int eny = end.getY();
 if(stx > 9 || enx > 9 || sty > 9 || eny >9)
 {
  if((stx != enx) && (sty != eny))
  {
   throw new Exception("Critical Error: Ship was not a straight line");
  }
 }
 if(stx == enx)
 {
  int temp;
  if(sty > eny)
  {
   temp = sty;
   sty = eny;
   eny = temp;
  }
  for(int k=sty; k<=eny; k++)
  {
   board.remove(new Location(stx, k));
   //System.out.println("Place1");
   board.put(new Location(stx, k), ship);
  }
 }

 else if(sty == eny)
 {
  int temp;
  if(stx > enx)
  {
   temp = stx;
   stx = enx;
   enx = temp;
  }
  for(int k=stx; k<=enx; k++)
  {
   board.remove(new Location(k, sty));
   //System.out.println("Place2");
   //System.out.println(sty);
   board.put(new Location(k, sty), ship);
  }
 }
}

//returns 0 for no ship, 1 for a hit, and 2 for an error(already shot at)
public int hit(Location shot)
{
 Set myBoard = board.keySet();
 Iterator iter = myBoard.iterator();
 Location L = new Location(-1,0,4);
 while(iter.hasNext())
 {
  L = (Location)iter.next();
  if ((L).equals(shot)) break;
 }
 if(board.get(shot).equals(null)) return 0;
 if(L.isHit()) return 2;
 else
  {
   (L).hit();
   ((Ship)board.get(L)).hit();
   return 1;
     }
}

//checks if the given ship is sunk
//if there are multiple ships of the same size, they must have different types
public boolean checkSunk(Ship boat)
{
 int count = 0;
 Set myBoard = board.keySet();
 Iterator iter = myBoard.iterator();
 while(iter.hasNext())
 {
  Location key = (Location)iter.next();
  if (((Ship)board.get(key)).equals(boat) && key.isHit())
  {
   count++;
  }
 }
 return (count==boat.getSize())

}

//board is sorted by order created
//board creates them 0,0 1,0 2,0 etc
public void print()
{
 //System.out.println(board);
 int count = 0;
 Set mySet = board.keySet();
 Iterator iter = mySet.iterator();
 System.out.println("------------------------------");
 for(int x=0; x<10; x++)
 {
  for(int y=0;y<10;y++)
  {
   Ship t = new Ship("x",6);
   Location L = new Location(x,y);
   if(board.get(L) instanceof Ship)
   {
    Ship q = (Ship)board.get(L);
  //  System.out.println(L);
  //   System.out.println(q);
    System.out.print("|"+q.getL()+"|");

    // System.out.print("Null");
   }
   else System.out.print("| |");
   {

    }
  }
  System.out.println();
  System.out.println("------------------------------");
 }
}

}




CODE

public class Location implements Comparable
{

private int x;
private int y;
private boolean hit;
private static int id=0;

public Location(int x,int y)
{
 this.x=x;
 this.y=y;
 this.id=id;
 id++;
 hit = false;
}

public Location(int x,int y, int noId)
{
 this.x=x;
 this.y=y;
 this.id=id;
 hit = false;
}

public void hit()
{
 hit = true;
}

public boolean isHit()
{
 return hit;
}

public int getX()
{
 return x;
}

public int getY()
{
 return y;
}

public int getId()
{
 return id;
}

public int compareTo(Object o)
{
 return x-((Location)o).getX();
}

public boolean equals(Location L)
{
 if(x==L.getX() && y==L.getY()) return true;
 else return false;
}
}


CODE

//Testing the print and place methods
public class Battlemain
{

public static void main(String[] args)
{
 //Testing Print
 Board p = new Board();
 try{p.place(new Ship("PT",2),new Location(8,8), new Location(6,8));}
 catch(Exception e){System.out.println(e);}

 try{p.place(new Ship("Destroyer", 4), new Location(1,1), new Location(1,5));}
 catch(Exception e){System.out.println(e);}
 p.print();
}

private class Player
{
 private Board board;
 private int player;

 public Player(Board b, int p)
 {
  board = b;
  player = p;
 }
}
}


This post has been edited by Telum: Mar 7 2006, 10:51 PM
Top
User is offlinePM
Quote Post

 
Reply to this topicStart new topicStart Poll

Replies(1 - 14)
Telum
post Mar 7 2006, 10:34 PM
Post #2


Admin
********

Group: Moderators
Posts: 7,429
Joined: 20-December 02
Member No.: 224



The problem is that either the place or print methods in Board.java are not working properly.
Top
User is offlinePM
Quote Post
Sovy Kurosei
post Mar 7 2006, 10:44 PM
Post #3


Authoritative Totalitarian Militant Bitch
*******

Group: Members
Posts: 4,555
Joined: 19-August 02
Member No.: 137



What error are you getting? Compile time error? Run time error?
Top
User is offlinePMEmail Poster
Quote Post
Telum
post Mar 7 2006, 10:51 PM
Post #4


Admin
********

Group: Moderators
Posts: 7,429
Joined: 20-December 02
Member No.: 224



QUOTE(Sovy Kurosei @ Mar 7 2006, 06:44 PM)
What error are you getting? Compile time error? Run time error?
*



Oh, its just not working properly.

What it should be doing, is printing out a ASCII graphic of a 10x10 board, with "P" in (6,6),(6,7),(6,8) and "D" in (1,1)(1,2)(1,3)(1,4).
Top
User is offlinePM
Quote Post
Deus Ex Machina
post Mar 8 2006, 05:53 AM
Post #5


age
******

Group: Members
Posts: 1,205
Joined: 24-November 03
From: Suburb of Denver
Member No.: 569



The problem is that your Location.compareTo() method doesn't work correctly. You only compare the X coordinate, when you should actually be comparing both the X and Y coordinates. This makes compareTo() return 0 (which TreeMap interperts as equivelent to equals() returning true) for places that have the same X, but different Y, coordinates. Your compareTo() method should look more like this:

CODE
   public int compareTo(Object o)
   {
       Location l = (Location) o;
       if (l.x==this.x)
           return this.y - l.y;
       else
           return this.x - l.x;
   }


This post has been edited by Deus Ex Machina: Mar 8 2006, 05:58 AM
Top
User is offlinePMEmail Poster
Quote Post
Telum
post Mar 8 2006, 12:23 PM
Post #6


Admin
********

Group: Moderators
Posts: 7,429
Joined: 20-December 02
Member No.: 224



That is a good point, but it shouldnt matter because im not using the structure of the treemap for anything. I changed the print method from returning a keyset and iterating over the elements to just a double for loop going from x[0,10) and y[0,10)
Top
User is offlinePM
Quote Post
Deus Ex Machina
post Mar 8 2006, 10:46 PM
Post #7


age
******

Group: Members
Posts: 1,205
Joined: 24-November 03
From: Suburb of Denver
Member No.: 569



The problem is that it does matter.

from the java.util.TreeMap API:
Note that the ordering maintained by a sorted map (whether or not an explicit comparator is provided) must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.

More to the point, (from the java.util.Map API):
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

What's happening is that whenever you store a Location with the coordinates (x,y), it overwrites any previous locations with the same x coordinate. If you don't believe me, just change the compareTo method, and it should work (at least, it does for me).
Top
User is offlinePMEmail Poster
Quote Post
Llywelyn
post Mar 9 2006, 03:01 AM
Post #8


Mezameru Kotonaikedo
********

Group: Members
Posts: 5,735
Joined: 26-June 02
From: New Orleans, LA
Member No.: 64



QUOTE(Telum @ Mar 8 2006, 05:23 AM)
That is a good point, but it shouldnt matter because im not using the structure of the treemap for anything.  I changed the print method from returning a keyset and iterating over the elements to just a double for loop going from x[0,10) and y[0,10)
*



Then the question becomes why are you using a TreeMap and not some other form of collection.


This post has been edited by Llywelyn: Apr 10 2006, 04:04 AM
Top
User is offlinePMEmail Poster
Quote Post
Telum
post Mar 9 2006, 04:11 AM
Post #9


Admin
********

Group: Moderators
Posts: 7,429
Joined: 20-December 02
Member No.: 224



QUOTE(Llywelyn @ Mar 8 2006, 11:01 PM)
Then the question becomes why are you using a TreeMap and not some other form of collection.
*



Becuause I oringally had it planned out in my head that it would be sorted for me by the treemap. Then I realized that I would have to implement my own, 2d treemap.
Top
User is offlinePM
Quote Post
Deus Ex Machina
post Mar 10 2006, 04:23 AM
Post #10


age
******

Group: Members
Posts: 1,205
Joined: 24-November 03
From: Suburb of Denver
Member No.: 569



I don't see why TreeMap is necessarily a bad choice. If he fixes his Location class, he'll have quick access to any one of his cells, and the structure is more flexible than an array of arrays. Plus, he can get an iterator which will go through the cells in order.
Top
User is offlinePMEmail Poster
Quote Post
Telum
post Mar 10 2006, 04:27 AM
Post #11


Admin
********

Group: Moderators
Posts: 7,429
Joined: 20-December 02
Member No.: 224



QUOTE(Deus Ex Machina @ Mar 10 2006, 12:23 AM)
I don't see why TreeMap is necessarily a bad choice. If he fixes his Location class, he'll have quick access to any one of his cells, and the structure is more flexible than an array of arrays. Plus, he can get an iterator which will go through the cells in order.
*



I changed my compareTo method, and now it works.

Thanks for the help. Now I just need to finish the driver and maybe, if im feeling ambitious, add a internet option to it.
Top
User is offlinePM
Quote Post
The Poster Formerly Known as Y2A
post Mar 10 2006, 04:28 AM
Post #12


New Jersey Over All
*******

Group: Members
Posts: 4,642
Joined: 14-June 03
From: The Great State of New Jersey
Member No.: 388



Top
User is offlinePM
Quote Post
Llywelyn
post Mar 11 2006, 05:24 PM
Post #13


Mezameru Kotonaikedo
********

Group: Members
Posts: 5,735
Joined: 26-June 02
From: New Orleans, LA
Member No.: 64



QUOTE(Deus Ex Machina @ Mar 9 2006, 09:23 PM)
I don't see why TreeMap is necessarily a bad choice. If he fixes his Location class, he'll have quick access to any one of his cells, and the structure is more flexible than an array of arrays. Plus, he can get an iterator which will go through the cells in order.
*



It isn't, it is a workable choice for the application, but whenever one works with a class that does a lot for you, it pays to know why you are using that particular class and what the restrictions of that particular type are. TreeMaps are backed by red black trees, and so have a Lg(n) access time and require a good CompareTo function.

So, if one refactors the code so that it no longer needs to use a TreeMaps, it pays to also switch which collection is being used simply because of murphy's law of coding.
Top
User is offlinePMEmail Poster
Quote Post
Telum
post Mar 11 2006, 07:50 PM
Post #14


Admin
********

Group: Moderators
Posts: 7,429
Joined: 20-December 02
Member No.: 224



I realized later that I could switch it to a 3d array. X,Y, and then the 2nd level would be the mapped value
Top
User is offlinePM
Quote Post

Reply to this topicTopic OptionsStart new topic

 


Lo-Fi Version
Time is now: 15th June 2006 - 04:32 AM