Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 581577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:35:37+00:00 2026-05-13T14:35:37+00:00

I’m trying to design a function template which searches for the best move for

  • 0

I’m trying to design a function template which searches for the best move
for any game – of course the user of this function template has to implement
some game specific functions. What i’m trying to do is to generalize the
alpha beta search algorithm with a function template.

The declaration of this function template looks like this:

template<class GameState, class Move,
         class EndGame, class Evaluate, class GetMoves, class MakeMove)
int alphaBetaMax(GameState g, int alpha, int beta, int depthleft);

Among other things the function has to:

  • Determine if a game has ended: bool EndGame(g)
  • Evaluate the state of a game: int Evaluate(g)
  • Get the possible moves: std::vector<Move> moves = GetMoves(g)
  • Make a move: Gamestate gnew = MakeMove(g, moves[i])

Do you think the function has to many template arguments? Is there a way to
reduce the number of arguments? One idea is to extend the GameState class with members
that evaluate the gamestate or decide if the game has ended. But a alpha beta
search tree contains a lot of Gamestate instances which may leads to
unnecessary memory requirements, thus i like to keep Gamestate small. In general, is a function template actually the right way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:35:38+00:00Added an answer on May 13, 2026 at 2:35 pm

    You could define an abstract interface say game_traits and have specialized game_traits implementation for each game:

    template<typename Game>
    class game_traits {
      ...
    };
    
    class Chess {
      ...
    };
    
    template<>
    class game_traits<Chess> {
      static bool endGame(Chess game);
      ...
    };
    
    template <typename Game, typename traits = game_traits<Game> >
    int alphaBetaMax(Game game, int alpha, int beta, int depthleft) {
      ended = traits::endGame(game);
      ...
    }
    

    See char_traits in the C++ standard library how it is used there.

    Alternatively, you could make them just methods of the Game classes, you don’t need inheritence here from some abstract class since you supply it as a template argument. You will just get a, perhaps not so transparent, compile error when your template function tries to access, say game.has_ended(), when no such method exists. This kind of mechanism is also used a lot in the standard template library.

    btw, there was a new feature planned for this; Concepts:

    auto concept GameType<typename Game>
    {
      bool has_ended(Game&);
      ...
    };
    
    template<typename Game> requires GameType<Game>
    int alphaBetaMax(Game game, int alpha, int beta, int depthleft) {
      bool ended = game.has_ended();
      ...
    }
    

    Unfortunately Concepts have been postponed to a future version of the standard and will not yet appear in c++0x 🙁

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.