Results 1 to 11 of 11
  1. #1
    fdsdfg is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Making a board game, best way to initialize world?

    I'm working on a simple Risk-like game that will be the part of something larger. Right now I just need to get the objects interacting happily, but I'm a bit lost on how to do it.

    I have a class 'Country' with a constructor that defines its name, a 'setOwner' function that changes ownership, a 'setArmy' function that sets the army, and a 'setNeighbors' function used during initialization to define which countries are adjacent. It seems like I can't use 'setNeighbors' in the constructor because if I'm initializing all the countries, I can't set neighbors until all the other countries are defined.

    That seems like it'll work, but I really don't know how to organize the classes. Right now I have one file where all the classes (country, continent, player) are defined. I am trying to use arrays of 'Country' and 'Continent' defined globally so I can access them from any function. I remember from school this is fundamentally wrong, but I don't know how else to do it.

    When I get to the actual main class just to test basic output and modification, I can't run the initWorld function because I can't make a static reference to a non-static.

    I know I'm doing something wrong here, but I've read through tutorials and just don't know which concepts I need to apply here. What's the best way of organizing the very basic functionality of this program?

    Thanks

  2. #2
    bigka79 is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    it'll be a lot easier to help ya out if you can show us some code

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Someone else may be better at helping but here is my advice. Try creating a class called Board or some name of your choice.

    Have the board have an instance variable which is an array of countries. To start the game in main you would initialize an object of class board, which sets up and populates the array to be used.
    Java Code:
    class Board{
      private Country[] places = { create new countries };
    }
    If the country classes are stored in another file, importing them should make them accessible.

    I could be answering your question wrong, but I believe this is what you are looking for.

    Also, post some of your code(using [ code][/code] tags) and tell me if what I am saying is heading in the right direction.
    Last edited by sunde887; 02-03-2011 at 05:32 PM.

  4. #4
    fdsdfg is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Someone else may be better at helping but here is my advice. Try creating a class called Board or some name of your choice.

    Have the board have an instance variable which is an array of countries. To start the game in main you would initialize an object of class board, which sets up and populates the array to be used.
    Java Code:
    class Board{
      private Country[] places = { create new countries };
    }
    If the country classes are stored in another file, importing them should make them accessible.

    I could be answering your question wrong, but I believe this is what you are looking for.

    Also, post some of your code(using [ code][/code] tags) and tell me if what I am saying is heading in the right direction.

    I believe that makes sense. When you say the country classes exist in another file though, what exactly does that mean? I have a class 'board' that has a private array of Countries - and in that Board class I would define the list of countries?

    Here is my code so far. Note I'm just using a test board of a 2x2 grid:

    Java Code:
    public class Game {
    
    	public Continent[] continent;
    	public Country[] country;
    	public Player[] player;
    	
    	class Player{
    		Player(String newname,int newpid){
    			player_id=newpid;
    			name=newname;
    		}
    		String getName(){
    			return name;
    		}
    		int player_id;
    		String name;
    	}
    	
    	
    	class Country{
    		Country(String newname){
    			name=newname;
    		}
    		
    		void setNeighbors(Country ... newneighbor){
    			neighbor=newneighbor;
    		}		
    		
    		void own(Player newowner, int newarmy){
    			owner=newowner;
    			army=newarmy;
    		}
    		void draw(){
    			System.out.println(name+" "+owner.getName()+" "+army);
    		}
    		String name;
    		int army;
    		Player owner;
    		Country[] neighbor = new Country[10];
    	}
    	
    	
    	
    	class Continent{
    		String name;
    		Country[] members;
    		
    		Continent(String newname,Country ... newmembers){
    			name=newname;
    			members=newmembers;
    		}
    	}
    	
    
    	public void initWorld(){
    		country = new Country[4];
    		country[0] = new Country("Northeastland");
    		country[1] = new Country("Northwestland");
    		country[2] = new Country("Southwestland");
    		country[3] = new Country("Southeastland");
    		
    		Country ne = country[0];
    		Country nw = country[1];
    		Country sw = country[2];
    		Country se = country[3];
    		
    		
    		ne.setNeighbors(nw,se);
    		se.setNeighbors(ne,sw);
    		nw.setNeighbors(ne,sw);
    		sw.setNeighbors(se,nw);
    		
    		Player bob = new Player("Bob",1);
    		Player joe = new Player("Joe",2);
    		
    		Continent westcon=new Continent("West Continent",nw,sw);
    		Continent eastcon=new Continent("East Continent",ne,se);
    		
    		ne.own(bob,7);
    		nw.own(bob,7);
    		se.own(joe,7);
    		sw.own(joe,7);
    	}
    	
    	
    	public void displayWorld(){
    		for (int i=0; i<country.length; i++){
    			country[i].draw();
    		}
    	}
    	
    	public static void main(String args[]){
    		initWorld();
    		displayWorld();
    	}
    		
    }

  5. #5
    fdsdfg is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Someone else may be better at helping but here is my advice. Try creating a class called Board or some name of your choice.

    Have the board have an instance variable which is an array of countries. To start the game in main you would initialize an object of class board, which sets up and populates the array to be used.
    Java Code:
    class Board{
      private Country[] places = { create new countries };
    }
    If the country classes are stored in another file, importing them should make them accessible.

    I could be answering your question wrong, but I believe this is what you are looking for.

    Also, post some of your code(using [ code][/code] tags) and tell me if what I am saying is heading in the right direction.

    I believe that makes sense. When you say the country classes exist in another file though, what exactly does that mean? I have a class 'board' that has a private array of Countries - and in that Board class I would define the list of countries?

    UPDATE:
    I applied the above and here's what I ended up with:

    Java Code:
    public class Game {
    
    
    	
    	public static void main(String args[]){
    		Board board = new Board();
    		board.initWorld();
    		board.displayWorld();
    	}
    		
    }
    
    
    
    public class Board {
    
    	
    	private Continent[] continent;
    	private Country[] country;
    	
    
    	public void initWorld(){
    		country = new Country[4];
    		country[0] = new Country("Northeastland");
    		country[1] = new Country("Northwestland");
    		country[2] = new Country("Southwestland");
    		country[3] = new Country("Southeastland");
    		
    		Country ne = country[0];
    		Country nw = country[1];
    		Country sw = country[2];
    		Country se = country[3];
    		
    		
    		ne.setNeighbors(nw,se);
    		se.setNeighbors(ne,sw);
    		nw.setNeighbors(ne,sw);
    		sw.setNeighbors(se,nw);
    		
    		Player bob = new Player("Bob",1);
    		Player joe = new Player("Joe",2);
    		
    		Continent westcon=new Continent("West Continent",nw,sw);
    		Continent eastcon=new Continent("East Continent",ne,se);
    		
    		ne.own(bob,7);
    		nw.own(bob,7);
    		se.own(joe,7);
    		sw.own(joe,7);
    	}
    	
    	
    	public void displayWorld(){
    		for (int i=0; i<country.length; i++){
    			country[i].draw();
    		}
    	}
    }
    
    
    public class Player {
    	String name;
    	int player_id;
    	
    	Player(String newname,int newpid){
    			player_id=newpid;
    			name=newname;
    		}
    		
    		
    		String getName(){
    			return name;
    		}
    }
    
    public class Continent {
    	String name;
    	Country[] members;
    	
    	Continent(String newname,Country ... newmembers){
    		name=newname;
    		members=newmembers;
    	}
    }
    
    
    public	class Country{
    		Country(String newname){
    			name=newname;
    		}
    		
    		void setNeighbors(Country ... newneighbor){
    			neighbor=newneighbor;
    		}		
    		
    		void own(Player newowner, int newarmy){
    			owner=newowner;
    			army=newarmy;
    		}
    		void draw(){
    			System.out.println(name+" "+owner.getName()+" "+army);
    		}
    		String name;
    		int army;
    		Player owner;
    		Country[] neighbor = new Country[10];
    	}
    Note I know there are plenty more functions to add, but I'm still looking for the right fundamental organization.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I have two questions, do you need to use inner classes? If so, how familiar are you with them? If you don't need to use them and are not very familiar with them try putting them separately.

    The code looks mostly good, and other people may be better at helping you, but when you design the method to take
    Java Code:
    Country ... newMembers
    You may need to use something different then just
    Java Code:
    members = newMembers;
    check the following link
    http://www.deitel.com/articles/java_...mentLists.html
    Last edited by sunde887; 02-04-2011 at 01:48 AM.

  7. #7
    fdsdfg is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    I have two questions, do you need to use inner classes? If so, how familiar are you with them? If you don't need to use them and are not very familiar with them try putting them separately.
    I don't know what that is. Does that means like I define one class in another class or something?

    The five classes above are all in different .java files if that's what you mean.


    The code looks mostly good, and other people may be better at helping you, but when you design the method to take
    Java Code:
    Country ... newMembers
    You may need to use something different then just
    Java Code:
    members = newMembers;
    check the following link
    Java Tutorial: Variable Length Argument Lists in Java 5.0
    I will check that out, thanks. I wasn't too sure that was going to work myself

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Sorry, I misread the code you posted. It might be better to have the methods take arrays instead of use the elipse.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Yes, ditch the ellipses.
    However I would recommend using something like an ArrayList to hold these things. It at least means you don't have to figure out beforehand how big to make these arrays of yours. You want to be making your life as easy as possible at the moment.

    I would write this without worrying about display. Think of your model in terms of what the computer needs to know to play the game, so the display isn't part of it. You can then bolt a display on top that uses this model.

    You have the bulk of the model already, but the draw() method of Country should be removed. Add a Display class (or whatever name) which then takes this model and displays it. It'll have the draw method (or similar), and the displayWorld() method. The advantage of this is you should then be able to completely replace your Display class with (say) a Swing one later on and not have to change the actual game logic. At the moment you'd have to change your core model classes to incorporate that. I've been there, you really don't want to be doing that...:)

    You'll probably want to think about some format for a save file (or scenario file) which you can then load up. I would suggest that this loading thing is also separated from your model logic...of course at the moment it would simply create hard coded objects (as you are currently), but if it's separate then it'll be easier to swap out for a proper scenario loader.

  10. #10
    fdsdfg is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Yes, ditch the ellipses.
    However I would recommend using something like an ArrayList to hold these things. It at least means you don't have to figure out beforehand how big to make these arrays of yours. You want to be making your life as easy as possible at the moment.

    I would write this without worrying about display. Think of your model in terms of what the computer needs to know to play the game, so the display isn't part of it. You can then bolt a display on top that uses this model.

    You have the bulk of the model already, but the draw() method of Country should be removed. Add a Display class (or whatever name) which then takes this model and displays it. It'll have the draw method (or similar), and the displayWorld() method. The advantage of this is you should then be able to completely replace your Display class with (say) a Swing one later on and not have to change the actual game logic. At the moment you'd have to change your core model classes to incorporate that. I've been there, you really don't want to be doing that...:)

    You'll probably want to think about some format for a save file (or scenario file) which you can then load up. I would suggest that this loading thing is also separated from your model logic...of course at the moment it would simply create hard coded objects (as you are currently), but if it's separate then it'll be easier to swap out for a proper scenario loader.
    Thanks for the advice. A couple questions about how to organize this, because I'm a bit lost:

    If I have just a 'Display' class, then I assume it has a 'DisplayWorld(Board x)' function where I give it the board to display? How would that function look? If it doesn't call another display function in x, then it needs to get all the board information from x, right? I really don't know how that would work.

    Also, I'm sometimes confused where to put functions. For instance, 'Attack'. In the driver, a country is called to attack another. Do I put that in the Board class? Attack(Country attacker, Country defender, int units)
    Or do I put it in the Country class? Attack (Country victim, int units)? How does the driver call the right countries when it only knows the Board and not how the countries are laid out?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Quote Originally Posted by fdsdfg View Post
    Thanks for the advice. A couple questions about how to organize this, because I'm a bit lost:

    If I have just a 'Display' class, then I assume it has a 'DisplayWorld(Board x)' function where I give it the board to display? How would that function look? If it doesn't call another display function in x, then it needs to get all the board information from x, right? I really don't know how that would work.
    Pretty much. But note that it only needs access to the information required to display the data. There may well be other stuff in Board that is completely irrelevant to how it is displayed.

    Quote Originally Posted by fdsdfg View Post
    Also, I'm sometimes confused where to put functions. For instance, 'Attack'. In the driver, a country is called to attack another. Do I put that in the Board class? Attack(Country attacker, Country defender, int units)
    Or do I put it in the Country class? Attack (Country victim, int units)? How does the driver call the right countries when it only knows the Board and not how the countries are laid out?
    I'd advise using the proper naming conventions for Java. Methods start with a lower case (so attack(), or displayWorld()). Classes start with an upper case (Board, Display). If you start mixing them then people can get confused about whether you are talking about a method or a class.

    Now for your problem. You have a Game. In that Game you have Countries. Are Country(s) the spaces on your Board? If so what happens when a Country takes over another Country? You might want to think about having Country and Territory. Then a Board has Continents which have Territorys.

    That's a basic structure.

    Now, when a Country makes a Move your Driver (the Game in my mind) processes that Move. The Move says who is moving, and where they are moving to. Obviously if the Move is into another Countrys Territory then it's an attack. So long as a Territory knows who owns it and what is deployed there then you should have enough information to process that attack.

    This is all off the top of my head, but it's a start.

Similar Threads

  1. Developing an Hex-board strategy game
    By abegade in forum Java 2D
    Replies: 8
    Last Post: 05-03-2011, 11:16 AM
  2. Making a dart-board
    By MarsTeam in forum Advanced Java
    Replies: 6
    Last Post: 06-22-2010, 07:54 PM
  3. need help making a game board
    By Don k in forum New To Java
    Replies: 2
    Last Post: 04-30-2010, 12:09 AM
  4. game board design
    By tomitu in forum New To Java
    Replies: 12
    Last Post: 02-27-2010, 05:31 PM
  5. Programming a Board Game
    By makanti in forum New To Java
    Replies: 7
    Last Post: 03-10-2009, 02:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •