my attempt at a game. [c++]

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
User avatar
[cc]z@nd!




Literarian 500

Posts: 2297
Joined: Tue May 04, 2004 1:52 pm
Location: michigan

my attempt at a game. [c++]

Post by [cc]z@nd! »

alright, so i've gotten to the point in c++ where i feel comfortable to attempt to make a game. I figured i'd go ahead and try a simple version of risk for my alpha-alpha build (it'll be played on a tic-tac-toe board), and pray to the gods that by the end of the line i'll have a kickass hardcore version of risk. but first i've got to get down how i'm going to do things gameplay wise, like attacking, checking territory ownership and whether two territories are 'connected' for troop movement at the end of a turn. i've read enough programming material to know the importance of planning, so i've gotten my own notebook to design this in before i even touch my IDE. however, i will admit i'm running into walls just in some design challenges. i don't want to go on forever with this, so i'll just summarize some current design ideas and problems and see if you guys could help me brainstorm on this some, like what function you think i should put where, or how to implement something.


=current class design (psuedo-code)=

Code: Select all

class player
{
string name
char symbol //used for displaying ownership on the board
bool still_playing //to tell if they've lost yet so we can skip their turn
bool first_turn //because you don't get reinforcements on the first turn

void turn(){} /*defines the player's turn, so main will only have to call player.turn(). will contain calls to other member functions.*/
void reinforce(){} //checks if it's the first turn. if not, compute reinforcement amount and give.
void redistribute(){} //redistributes troops at the end of a turn.
void attack(&attacker.troops, &defender.troops){} //contains the attack process, like rolling dice and reducing troop count accordingly. should call by reference to alter the values.

}

Code: Select all

class NPC
{
/* this should essentially be a carbon-copy of the player class, but all cin and cout statements should be stripped, and replaced with some facility for the AI to make decisions. alpha-alpha will have no AI, alpha will have shitty AI using rand() and modulus, and after that i'll try working on AI  that evaluates the situation and tries to formulate the best solution. if i don't run into too much trouble i might also have personality variables like 'aggressive' or 'vengeance' to affect decisions. but really, i'm betting on it being complete shit.*/
}

Code: Select all

class territory
{
string name //name of the territory. ie: kamchatka, egypt, chile
int troops //how many troops are currently in the territory
string owner // to determine ownership of the troops

/* some way to list the other territories it touches/can attack? i dunno, still workin' on this one. */
}
things i left out: functions to display the map and other statistics.



=problems=

1: how to represent/compute what territories a certain one is touching, so i can have a proper attack function that follows the rules.
2: similar to #1; how to check ownership & find paths between two territories, which i'd use in the troop movement function to enforce rules while moving troops
3: how am i going to create different territories? they've got their name member variable, but i'm not sure how i'd set their identifiers, since in the long run i'd like a dynamic solution that doesn't need hard-coded values. for example, if i make a map-making feature, having to set identifiers for different territories then recompile is just stupid.
4: i can't avoid it forever: AI. i'll settle for rand()%choices for a while, but i'd like something at least semi-intelligent in the final build, so i won't have a bot attack from it's weakest flank then end it's turn after it reinforces it's innermost country. i can put it off for a while though while i learn more about the subject.
5: there's bound to be more issues popping up, i'll come back here when i find 'em since it's been a good day since i've sat down and really thought about this.
ASPARTAME: in your diet soda and artificial sweeteners. also, it's obviously completely safe. it's not like it will cause tumors or anything. >.>
always remember: guilty until proven innocent
User avatar
grimdoomer




System Engineer

Posts: 1440
Joined: Mon Oct 09, 2006 4:36 pm

Post by grimdoomer »

For the owner ship problem, I would make one class that handles the player and NPC's. Then you could add a variable like this:

Code: Select all

Player Owner;
Player being the main class for the player and NPC's. Then you could add a array of some sort, or even a list of each territory that Player/NPC owns. Sorry if im worng, I have never seen the game you are talking about just what I would do.
Image
AI Zones in MP | Ambiance | Gravemind Beta v1.1
Aumaan Anubis wrote:Grimdoomer. The first person ever to mod Halo 2 Vista.
User avatar
[cc]z@nd!




Literarian 500

Posts: 2297
Joined: Tue May 04, 2004 1:52 pm
Location: michigan

Post by [cc]z@nd! »

i think i get what you mean. in fact, i'll bet i can fashion a list class that adds territories and takes them away similar to a stack, making things easy. then i'd just declare a list object inside every player object. however, i'm thinking maybe it'd be easier to have a master list set up like a table, with territories in the 0 column, and their owner's name in the 1 column. then i'd just use a function to search through the list for a territory, then when i find it i can alter the ownership.

i think i'll try out the second approach, actually. why bother with another class? that'd just over-complicate things.

next up would be dynamically producing identifiers. for example, i could get the number of territories (x), then declare an array of territory objects with x elements, but i'm not so sure how arrays of objects works. it should, but i'll have to play with it and figure it out. after that there's still things like finding paths between two territories with respect to ownership, and determining what territories border what others. AI can wait for a long time, i haven't even started actual coding yet.

and if you don't know what risk is: http://en.wikipedia.org/wiki/Risk_%28game%29
ASPARTAME: in your diet soda and artificial sweeteners. also, it's obviously completely safe. it's not like it will cause tumors or anything. >.>
always remember: guilty until proven innocent
easynerd





Posts: 4
Joined: Fri Jul 21, 2006 11:54 am

Post by easynerd »

try a 2D array for the board.
example:

Code: Select all

unsigned char Map[5][5] =
{{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0},
{0,0,0,0,0}};
You can preset territories on the map board by changing the 2D array.
To read the array:

Code: Select all


// Prints map (windows.h)

template <typename T>
void textAt(int x, int y, int colour, T output) { 
    static const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position = {x, y};
    SetConsoleCursorPosition(hConsole, position);
    SetConsoleTextAttribute(hConsole, colour);
    cout << output;
}


// Reads map

  for(int y = 0; y < 5; y++)
  {
    for(int x = 0; x < 5; x++)
    {
      if(Maze[y][x] == 1)
        textAt(x, y, 6, "o");
        else if(Maze[y][x] == 2)
        textAt(x, y, 7, "+");
      else
        textAt(x, y, 0, ' ');
    }
  }
You can check if a piece is touching by finding a pieces coordinates on the 2D array and then check the coordinates around it.

Example:

Code: Select all

if(Map [Player_y-1] [Player_x] == 1)
//Then do action
Hope that will get the basics of the game down.

- Crypt/Easynerd -
Signature exceeded 500x120 pixels.
Post Reply