Sign in to follow this  
Flying_Dagger

Need help with Program Structure

Recommended Posts

Hello, I'm working on a turn-based game project for school assignment whereby players can issue out orders to his units(eg. Attack, Move, Defend) on the field during a command phrase and then the units will carry out their respective orders during an action phrase. I planned to have a class called Orders which will have all the orders a unit can do and implement a Linked List to store all the commands via function pointers for the unit to carry out. The units itself will have a class of its own to run rendering and game logic. By instancing the linked list in the unit I can have it to "read" the orders issued to him stored in the linked list and it will carry them out in the correct sequence. So with this concept in mind the way the classes will be linked should be something like this: Orders class ---> LinkedListNode class ---> LinkedList Class ---> Unit Class Now here's the problem.... Some of the commands in the Order class requireds input from the Unit class to be carried out (eg. Attack command requires the coordinate of the unit carrying it out to determine wat the particular unit can attack). So I have to make the Order class know what is a Unit somehow. I have thought of including the Unit class in the Order class but that will make the above linkage a merry go round. Is there some flaw in my structure? Also, is there another better way to do this kind of game?

Share this post


Link to post
Share on other sites
no there is nothing wrong with your design and you can have the circle you have to use forward declerations heres an article that explains how (its the second problem that they mention but i recomend reading the whole thing.

Share this post


Link to post
Share on other sites
Your design seems over complicated for what you are trying to accomplish. Why dont you simply add a stack to Unit to hold your orders.

In psuedo world something like this


class Unit {
Stack orders;
AddOrder()
{
orders.Add(new order(this));
}

ExecuteOrders()
{
// pop each order off stack and call its execute method
}
}

class Order {
Order(Unit* issuer)

Execute()
{
//dostuff
}
}

Edit: Released my pseudo code didnt make any damned sense... heres a 2 minute version in code. Its C#, but easy enough to translate to any language. Atleast its better then my pseudo code. Also, you would obviously want to implement Order as a base class or interface, as you will have multiple different types of orders.


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Order
{
private Unit unit;

public Order(ref Unit theUnit)
{
unit = theUnit;
}

public void Execute()
{
Console.WriteLine("Some order doing some thing");
}
}

class Unit
{
private Stack<Order> orders;

public Unit()
{
orders = new Stack<Order>();
}


public void AddOrder(Order anOrder)
{
orders.Push(anOrder);
}

public void ExecuteOrders()
{
while (orders.Count != 0)
{
((Order)orders.Pop()).Execute();
}
}
}

class Program
{
static void Main(string[] args)
{
Unit myUnit = new Unit();
Order doSomething = new Order(ref myUnit);
myUnit.AddOrder(doSomething);
myUnit.ExecuteOrders();
}
}
}

Share this post


Link to post
Share on other sites
I agree with the above – orders shouldn't do anything, they should just be instructions to a unit. It should be the Unit class that, on executing an 'attack' order, chooses an appropriate target.

Share this post


Link to post
Share on other sites
Thanks for all the input, I had came out a simple code of what I mentioned earier. It is quite long to be posted here so if you want to take a look I can send you the codes. I could use some advice/pointers on this since I think this way could achive the most of what I want to do.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this