Jump to content
  • Advertisement
darrin k

C++ structure of a text based football game in C++

Recommended Posts

I am looking for some advice in how best to structure a text-based football game in C++. I am actually a trying to mimic an old DOS game called Armchair Quarterback but without any graphics for the time being. The original DOS game had very minimal graphics just showing a cursor moving across a static graphic (but I will leave the graphics  to the end). I attached what the original game looked like below.

So far, I wrote functions for the following :

- for the user to select a team

- for the user to select the opponent's which is always the CPU controlled

- for the user to select difficulty (enum)

 

Things left to do:

Set-up one overarching team class for all the teams in the game then each specific team would inherit the attributes from the overarching team class.

In the original game, each team only had four attributes that they were rated on.

I would also have to construct 17 offensive plays that the user has to choose from. Would each play be a separate function???

Also, the main logic and AI would need to be constructed (would need some variables for logic and AI).

-Bigger pass plays have lower % of completion than shorter pass plays

-CPU is guessing what the user selects to "play" defense

-The user is guessing what the CPU selects to "play" defense

-This will incorporate the attribute ratings for the offense and defense per team plus randomness and the situation in the game (down and distance???)

Any help would be appreciated in how the code base should be structured. Thanks!

DK

 

-

 

 

armchair UI.jpg

Share this post


Link to post
Share on other sites
Advertisement

This particular question seems like a good place to start:

Quote

I would also have to construct 17 offensive plays that the user has to choose from. Would each play be a separate function???

I would say that functions aren't the optimal tool for this task, conceptually speaking. Typically something like a set of plays would be conceptualized as data rather than being hard-coded behaviors (e.g. in separate functions). I think this preference for data can also be applied in general (not just to handling plays).

What that means exactly depends on the context, and I don't know enough about the context to be more specific. But maybe just the conceptual shift from functions to data will be enough to get you moving in the right direction.

Share this post


Link to post
Share on other sites
12 hours ago, Zakwayda said:

This particular question seems like a good place to start:

I would say that functions aren't the optimal tool for this task, conceptually speaking. Typically something like a set of plays would be conceptualized as data rather than being hard-coded behaviors (e.g. in separate functions). I think this preference for data can also be applied in general (not just to handling plays).

What that means exactly depends on the context, and I don't know enough about the context to be more specific. But maybe just the conceptual shift from functions to data will be enough to get you moving in the right direction.

With regards to data, what do you mean by that exactly? I am not following in how to implement the behavior. If I want to implement the play chosen, I figure that this will be separate from the other variables. For instance, if it is first down and 10 and I select a play action pass, how is that best implemented in the code? I figured that would remain separate from other variables such as the chances of the CPU guessing the correct play call, the chance of the actual play being completed (the longer the pass play the lower the chance of the play being completed), and the attributes/ratings for the user controlled offense and the AI controlled defense. I guess I could write functions for all this stuff or a load of switch statements, but that does seem correct like you stated and I would need to implement some randomness so it does not get too predictable.

 

 

Share this post


Link to post
Share on other sites

If the computer opponent AI is a big deal, you should organize the game around it: you should develop (starting simple, and aiming for a fun player experience rather than for complexity and "realism") a data abstraction that can model anything the AI can do.

Then you'd be able to author data for your system, defining plays and AI profiles, with minimum trouble and minimum turnaround time. Technically, you should be able to modify a human readable text file, reload it from the game and test the tweaked AI behaviour without compiling or restarting or interrupting the game in any way.

For example, elementary tactics could be defined by

  • initial location of each player (possibly not on the field)
  • where each player on the field should run (fixed locations, opponent players, the ball...)

Less trivial player behaviour could be obtained by delaying and waiting, sequences of behaviours, behaviour trees, etc. (for example, run towards the ball if close enough, otherwise keep a formation with other players, and if they are stopped disturb the closest opponent)

I'm not sure what a "play action pass" is, but its definition should have a designated player who passes the ball and one or more others who try to receive the pass after moving appropriately.

A dumb AI could play exactly the same way every time, a less dumb AI could choose from a weighted distribution of plays, a somewhat smart AI could take into account known information (e.g. what to do depending on remaining downs, positions, score) or guessed opponent behaviour (e.g. where did the opponent's quarterback go, previously in the game, in similar situations?).

You would develop a language of elementary and aggregate moves, complex moves, weighted random choices, interpolation, decision trees of rules, etc. and general implementations of each AI building block.

Note that the difference between defensive and offensive plays is that one player in the offensive team starts with the ball in hand while defensive players can only have the ball after taking it, and maybe some additional rule subtleties; the player roles and plays are going to be completely different, but represented and implemented uniformly, for the two teams.

Share this post


Link to post
Share on other sites

If the game is supposed to be like the one in the screenshot, there is no need for modelling individual players' movements. The only thing that is needed is a table of "which offensive tactics works against which defensive tactics with what percentage of success". Maybe a bit more than a single percentage value if there are some other stats involved. At its simplest, it's just a lookup of the data. So you wouldn't need separate functions for resolve each play, just one function and separate data.

Share this post


Link to post
Share on other sites
4 hours ago, 1024 said:

If the game is supposed to be like the one in the screenshot, there is no need for modelling individual players' movements. The only thing that is needed is a table of "which offensive tactics works against which defensive tactics with what percentage of success". Maybe a bit more than a single percentage value if there are some other stats involved. At its simplest, it's just a lookup of the data. So you wouldn't need separate functions for resolve each play, just one function and separate data.

1024, exactly correct...I want to first model it the way your describing to keep it simple and eventually be able to build like LorenzoGatti described on another more complex project. I am definitely not an AI expert, so I want to keep for simple for now. My main question, how do I implement a lookup table in C++. Do I have to just construct a csv excel spreadsheet with the teams and attribute ratings and percentages etc....and write a function to call it...depending on the play call??? I am trying to understand the implementation. Is there an easy way to read csv files into C++ or libraries???

 

 

Share this post


Link to post
Share on other sites
Quote

My main question, how do I implement a lookup table in C++. Do I have to just construct a csv excel spreadsheet with the teams and attribute ratings and percentages etc....and write a function to call it...depending on the play call??? I am trying to understand the implementation. Is there an easy way to read csv files into C++ or libraries???

There are many ways you could do it, but if you want to keep it simple, reading a CSV file might be a good starting point.

C++ is fairly low-level and doesn't include direct support for such things. There are third-party libraries for dealing with formats like CSV, XML, JSON, etc., but it should be fairly manageable to implement something like this from scratch if you prefer. The steps might be:

- Figure out how to read a text file into a std::string instance.

- Figure out how to split a string given a character separator so you can break the file up into lines and tokens. (Depending on the contents of the file there may be other issues to deal with here, such as string trimming or quoted strings.)

- Figure out how to convert strings to numbers.

You would read the file, parse it, and build some suitable representation from the results (e.g. an array or map of class/struct instances, where each instance represents all or part of a row of the database).

You can find plenty of info on these topics online, and can of course ask here if you get stuck on anything.

Again, this is just one approach you could take.

Share this post


Link to post
Share on other sites
18 hours ago, Zakwayda said:

There are many ways you could do it, but if you want to keep it simple, reading a CSV file might be a good starting point.

C++ is fairly low-level and doesn't include direct support for such things. There are third-party libraries for dealing with formats like CSV, XML, JSON, etc., but it should be fairly manageable to implement something like this from scratch if you prefer. The steps might be:

- Figure out how to read a text file into a std::string instance.

- Figure out how to split a string given a character separator so you can break the file up into lines and tokens. (Depending on the contents of the file there may be other issues to deal with here, such as string trimming or quoted strings.)

- Figure out how to convert strings to numbers.

You would read the file, parse it, and build some suitable representation from the results (e.g. an array or map of class/struct instances, where each instance represents all or part of a row of the database).

You can find plenty of info on these topics online, and can of course ask here if you get stuck on anything.

Again, this is just one approach you could take.

So, I am just trying to get on the right track, would be it just constructing an array like this below and I am not even sure if my logic is right.

1st column = 1 for Chicago Bears offense
2nd column = # of Down 
3rd column = Yards to Go
4th column = team 23 --- seattle seahawks defense


columns 5 through 21 would be percentages that the play
would be completed based upon the bears offense ratings vs 
whatever defense they would face (I would have to just make up percentages?)
Is this correct way to do it, I have no idea, seems inefficient with
repeated values.

{1,1,10,23,...................}; 
{1,2,10,23,...................};
{1,3,10,23,...................};
{1,4,10,23,...................};
{1,1,5,23,....................};
{1,2,5,23,....................};
{1,3,5,23,....................};
{1,4,5,23,....................};

 

But then yards to go could go from a theoretical range from 1-100 not discrete increments like I listed above.

Edited by darrin k
include yards

Share this post


Link to post
Share on other sites

I don't know enough about the sport to be of specific help here :( I think all I can offer is a couple general comments.

If a lot of your data elements are in fixed relationships with each other (e.g. a linear relationships between 'yards to go' and 'probability of completion'), then you might consider handling those relationships algorithmically rather than as raw data. The algorithms/relationships themselves could also be data, or could be implemented in code if that seemed more practical. Separate records for each combination of teams, down, and 'yards to go' does seem a bit suspicious and like a lot of data to generate manually, so I'm wondering if some sort of algorithmic approach based on coarser data might be preferable.

Also, repeated records with the same pairs of teams does seem to suggest that maybe some other data representation might be more appropriate. Keep in mind that you don't have to use CSV or any other particular format. You could, for example, create a bespoke format that organizes the data exactly how you want it (for example in some sort of hierarchical format).

Share this post


Link to post
Share on other sites
11 hours ago, darrin k said:

So, I am just trying to get on the right track, would be it just constructing an array like this below and I am not even sure if my logic is right.

1st column = 1 for Chicago Bears offense
2nd column = # of Down 
3rd column = Yards to Go
4th column = team 23 --- seattle seahawks defense


columns 5 through 21 would be percentages that the play
would be completed based upon the bears offense ratings vs 
whatever defense they would face (I would have to just make up percentages?)
Is this correct way to do it, I have no idea, seems inefficient with
repeated values.

With my limited knowledge of American Football, I'd suggest you start with something much simpler.

Say your field is 100 units long. For each 'run' (the ball becomes available, people play a bit, and the game is stopped again), the most basic progress is that everything moves a few units on the field, and possibly the defending team can take possession of the ball(???). For simplicity, let's assume that the team scores a point if they reach the opposite side of the field.

This means you have somewhere near 3 numbers for a team. "average progress while in ball possession" (on the field), "average ability to defend against progress", "chance of getting in ball possession".

In a game, you have a fixed number of runs (just keeping things simple at this stage), and for a run, from the 6 numbers that you have from above, you need to compute the amount of progress in the field, and who owns the ball afterwards. Special case is reaching either side of the field.
 

 

I agree it's not football as you see it on TV, it lacks a zillion number of details. There are however two reasons why this is a good first start.

- Currently you have nothing at all, and you're struggling to decide what data you need. You have that struggle, because you lack knowledge of what works. By building this simple thing first, you have a start (ie something is working), and you can better judge what is lacking.

- Secondly (but probably just as important as the first point if not more), a game is a game, In particular, it's not reality. The only thing that the game has to produce is convincing end scores (right?). It's all smoke and mirrors. The game only has to pretend a game is being played. It doesn't necessarily actually need to  simulate every little detail.

I got into games because I am fascinated by this phenomenon. You can pretend very complicated behavior by doing something totally different (not feasible in reality) and solve the whole problem in a very simple way. As long as the smoke and mirrors are done well, players are easily convinced the real thing is being done, while the actual code is just a few trivial calculations and a few random die throws to make the decision.

Of course, like a good magician you never tell the public your secret for writing such a good game :)

Edited by Alberth

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!