C++ text based football — lookup table and AI logic help

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
theprodigy
Posts: 5
Joined: December 9th, 2018, 9:51 pm

C++ text based football — lookup table and AI logic help

Post by theprodigy » December 9th, 2018, 9:58 pm

First off, thanks to Chili for the C++ beginner series tutorials, I am working through those while trying to work on my side project because I love old skool sports and racing games besides some of the classic platformers. However, I am struggling with some AI logic below and wanted to see if anyone more experienced could offer insight.

I am trying to develop a one-player text based football game in C++ strictly in the console without graphics. I am developing the game logic here below vs the CPU and I also need to do the vice versa (CPU vs User). How could I do this without a ton of if statements. I am not sure how to accomplish this same code below as a lookup table and I know I need some randomness so it is not predictable.

For this example below, the scenario would be which down are we in (4th in this example), how many yards to go (2 yards), the offensive's team running attribute (choice = Bears), the defense run attribute (o_choice = Bengals) and the play called (playbook_choice = 1 which is QB Sneak). Any help would be much appreciated, I have been banging my head against on the wall on this. I have not incorporated the attributes ratings below into the code below because I am not sure how and the numbers represent only the teams chosen. The Bears would have offensive running and pass ratings, and defensive running and pass ratings (so 4 ratings in total), same goes for the Bengals.

Code: Select all


    //test logic
if (playbook_choice == 1 && choice == 1 && o_choice == 2) { //if qb sneak is called by the user controlled bears vs the cpu bengals

    int current_down_yards = 0,down = 4 ,yards = 2; // current down is 4th and 2  --- setting the down and distance
    cout << "It is 4th down and 2 yards to go" << endl; // print it out to the user --- setting the down and distance

    if (down == 2 || down == 3 || down == 4 && yards < 3) { //if its 2nd , 3rd of 4th down  and less than 3 yards to go
        current_down_yards = rand() % 16; // give a rate of completion of randomness from 1-15 yards for a QB sneak --- need to take account for the strength
        // of the defense's attribute rating and offense's running rating -- to include for a loss as well as a gain in yardage
        cout << "The QB ran for " << current_down_yards << " " << "yards" << endl;
    }
}






cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: C++ text based football — lookup table and AI logic help

Post by cameron » December 10th, 2018, 12:22 am

Not sure I completely understand what you are trying to do but ill take a stab at it. It would help keep things clean if you put things into structs/classes and help make things a little more uniform for developing the logic. This could be done even nicer when you learn more but don't worry too much about that and just do it with the tools you know how to use. (structs/enums/const vars/functions will do for now)

Besides keeping things clean, the way to avoid tons of ifs/redundant statements/linear-like code is to use functions/objects for code reuse. You can make generic functions that do what you want given a list of parameters.

For example:

Code: Select all

const int PLAYER = 0;
const int AI = 1;

struct Team
{
const char* name;
int defensiveRating;
int offsensiveRating;
}

struct PlayerData
{
    int current_down_yards, down, yards;
}

PlayerData players[2];

void ExecuteTurn(Turn currentTurn, const Team& defense, const Team& offense)
{
PlayerData& data = players[currentTurn];
//Logic here
}

Turn ChangeTurn(Turn currentTurn)
{
if(currentTurn == PLAYER)
return AI;
else
return PLAYER;
}
You would likely want to make an array of Player structs and populate them with a bunch of teams and their respective ratings. User input would be parsed and converted into a reference to the player struct you want.

Note: The above is just pseudo code to help you structure your code and give you an idea of how to simply/keep things clean. Hope that helps and sorry I can't give you more info as I dont know a ton about the project and I dont know a whole ton about football either.
Computer too slow? Consider running a VM on your toaster.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: C++ text based football — lookup table and AI logic help

Post by albinopapa » December 10th, 2018, 2:05 am

I'm not too familiar with AI decision trees, me personally I would take the naive approach and use if/else or switch/case. I've heard chili throw around the term minimax before where the choice for the AI is maximize reward and minimize risk. For instance, if the Bears have a strong offense and the Bengals have a weak defense, the QB sneak would be low risk and high reward. If the Bengals have a strong defense, then perhaps a pass play would be lower risk. If the Bengals have good defense and a high rate of interceptions, then perhaps a field goal would be the lower risk.

I'm not sure how to assign points to plays or decisions the computer can take, or whatever though, just something to think about. Early on in the game, perhaps taking higher risks would be ok, unless the CPU is behind, then might play more conservatively, conversely might play more risky to try and catch up while if they have the lead, play less risky and just hold the lead.

I think the idea would be to come up with some plays given offense or defense, strength of the chosen team for AI and player and assign some point value for each play. Once you go through the min/max tree, you end up with the value of the play, and you use that play from the play book.

Minimax tree example
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: C++ text based football — lookup table and AI logic help

Post by albinopapa » December 10th, 2018, 2:16 am

Cameron does have a point about using structs/classes, not necessarily for code reuse, but to minimize clutter. I feel that having things in their place helps keep the mind focused on the problem at hand.

To add to what I posted, you would probably have to make trees for for each team facing off, Bears/Bengals, Bears/Lions, Bears/Packers, etc... You could probably do this at runtime based on which team was picked by each player ( CPU/Human, CPU/CPU, Human/CPU ). Perhaps have a playbook, and some plays only available based on team match ups. I don't know a huge amount about football either lol, so my knowledge here is limited as well.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

theprodigy
Posts: 5
Joined: December 9th, 2018, 9:51 pm

Re: C++ text based football — lookup table and AI logic help

Post by theprodigy » December 11th, 2018, 6:27 pm

thanks for the assistance and tips! I am going to try to make this work even though this project might be over my head. I might have to put this to the side until I have a better idea of other concepts.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: C++ text based football — lookup table and AI logic help

Post by albinopapa » December 11th, 2018, 8:52 pm

Well, if this is something you want to continue, you can always just rely on the branching if/else if/else or switch/case blocks.

You can finish the project with the naive approach first, then if desired, go back and replace or add stuff.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: C++ text based football — lookup table and AI logic help

Post by cameron » December 12th, 2018, 7:07 am

Honestly, if you haven't even gone through all the beginner tutorials don't worry about decision trees or any ai algorithms of the likes. I haven't even gotten into those yet. Simple if statements should suffice. Really if you want lots of advanced ai behavior then yeah... maybe come back to it another time. Otherwise, you should do fine with what you have learned thus far.
Computer too slow? Consider running a VM on your toaster.

theprodigy
Posts: 5
Joined: December 9th, 2018, 9:51 pm

Re: C++ text based football — lookup table and AI logic help

Post by theprodigy » December 12th, 2018, 8:01 pm

That is very true, I will try to just use straight if then else statements and if I run into any huge blocks, I might ask for some assistance here. I know one of the most important things is just finishing the project even if isn't that great.

Post Reply