logo
Tags down

shadow

TicTacToe with MiniMax algorithm 4x4


By : Kishan Sunar
Date : September 16 2020, 01:00 PM
I wish did fix the issue. You set board_size way down in main. But board has already been allocated at that time. You need to allocate board after you change board_size.
Also, this is very weird:
code :
    cin>>tablechoice;
    while(tablechoice==1 || tablechoice ==2)


Share : facebook icon twitter icon

Return bestMove in minimax algorithm for tictactoe


By : griffey
Date : March 29 2020, 07:55 AM
may help you . In the simplest version of minimax, the first player wishes to maximize his score, and the second player wishes to minimize the first player's score. Since both first and second player only care about the first player's score, EvaluateStaticPosition should return a value indicating how good the board state is for the first player. Whose turn it is is not relevant.
code :
int EvaluateStaticPosition(stateT state)
{
        if(CheckForWin(state, FIRST_PLAYER))
        {
                return WINNING_POSITION;
        } 
        if(CheckForWin(state, Opponent(FIRST_PLAYER)))
        {
                return LOSING_POSITION;
        } 
        return NEUTRAL_POSITION;
}
moveT MiniMax(stateT state)
{
    moveT bestMove;
    int i = 0;
    if (state.whoseTurn == FIRST_PLAYER){
        i = MaxMove(state, bestMove);
    }
    else{
        i = MinMove(state,bestMove);
    }
    cout<<"i is "<<i<<endl;
    return bestMove;
}
int MaxMove(stateT state, moveT &bestMove)
{
        if(GameIsOver(state))
        {
            return EvaluateStaticPosition(state);
        }
        vector<moveT> moveList;
        GenerateMoveList(state, moveList);
        int nMoves = moveList.size();
        int v = -1000;
        for(int i = 0 ;i<nMoves; i++)
        {
                moveT move = moveList[i];
                MakeMove(state, move);
                moveT opponentsBestMove;
                int curRating = MinMove(state, opponentsBestMove);
                if (curRating > v)
                {
                        v = curRating;
                        bestMove = move;
                }
                RetractMove(state, move);
        }
        return v;

}
int MinMove(stateT state,  moveT &bestMove)
{
        if(GameIsOver(state))
        {
                return EvaluateStaticPosition(state);
        }
        vector<moveT>moveList;
        GenerateMoveList(state, moveList);
        int nMoves = moveList.size();
        int v = 1000;
        for(int i = 0 ; i<nMoves; i++)
        {
                moveT move = moveList[i];
                MakeMove(state , move);
                moveT opponentsBestMove;
                int curRating = MaxMove(state,opponentsBestMove);
                if(curRating < v)
                {
                        v = curRating;
                        bestMove = move;
                }
                RetractMove(state, move);
        }
        return v;
}
The final position looks like this:

 O | O | X
---+---+---
 X | X | O
---+---+---
 O | X | X

Cat's game.
int EvaluateStaticPosition(stateT state)
{
        if(CheckForWin(state, state.whoseTurn))
        {
                return WINNING_POSITION;
        }
        if(CheckForWin(state, Opponent(state.whoseTurn)))
        {
                return LOSING_POSITION;
        }
        return NEUTRAL_POSITION;
}
int MaxMove(stateT state, moveT &bestMove)
{
        if(GameIsOver(state))
        {
                return EvaluateStaticPosition(state);
        }
        vector<moveT> moveList;
        GenerateMoveList(state, moveList);
        int nMoves = moveList.size();
        int v = -1000;
        for(int i = 0 ;i<nMoves; i++)
        {
                moveT move = moveList[i];
                MakeMove(state, move);
                moveT opponentsBestMove;
                int curRating = -MaxMove(state, opponentsBestMove);
                if (curRating > v)
                {
                        v = curRating;
                        bestMove = move;
                }
                RetractMove(state, move);
        }
        return v;

}
moveT MiniMax(stateT state)
{
    moveT bestMove;
    int i = 0;
    i = MaxMove(state, bestMove);
    cout<<"i is "<<i<<endl;
    return bestMove;
}

TicTacToe minimax algorithm. Could use some input


By : Michael Schwarz
Date : March 29 2020, 07:55 AM
around this issue All your non-terminal positions have the same score of 0. Take a closer look at pre-terminal positions, that is, those where one move can lead to instant winning.
Try, for instance, to score +1 for each winning move you have in the position, and -1 or -2 for each winning move the opponent has in the position. This will make the algorithm more readily prune branches where opponent has many ways to win and you have few.

What is wrong with my minimax algorithm for tictactoe


By : Sergey Solovyov
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The concept of the minmax algorithm is to « minimize the maximum loss » (Wikipedia), so the first thing wrong with your algorithm is your sum.
For any state S of the game, and for any move M avaialble for the current player (let's say player 1 P1), the value of minmax (S + M, P2) is the maximum possible output for P2 if P1 plays M. So if P1 wants to maximize his chance to win, he should reduce as much as possible the maximum output for P2, i.e. he must find the minimum of the outputs.

C++ Exception Minimax algorithm for Tictactoe


By : Simon Kolotov
Date : March 29 2020, 07:55 AM
hope this fix your issue Your vector is empty (as it should be, there are no remaining legal moves).
Calling at(0) on an empty vector throws an exception, because there is no element 0.

Minimax algorithm not working for 4x4 TicTacToe


By : Juraj Marusiak
Date : March 29 2020, 07:55 AM
around this issue
I have used the traditional minimax algorithm without pruning .
Related Posts Related Posts :
  • par_unseq and "vectorization-unsafe" functions
  • libgphoto2 and Visual Studio 2019
  • What's the difference between "double* grade", "double *grade", and "double* fn()"?
  • How to license C++ software
  • Meaning of declaration float ***c
  • character array validation in C++
  • Why can't casting an address to int* be an lvalue but casting to a struct pointer can?
  • Can names in unnamed namespaces in different C++ files refer to the same named thing?
  • How to get variable no of argument with its size in C++ using variadic template
  • Understanding SHT_NOTE section ".note.ABI-tag" of an ELF exectable
  • Displaying all prefixes of a word in C++
  • Does Erase deletes heap memory used by element of stl unordered map
  • Can you cast a vector<int64> to a vector<uint8>
  • Do function parameter variables always require a & or * operator?
  • File Append In Such A Way That It Ends Line After Each Save C++
  • Calling Derived class function from a Base class pointer after typecasting it to Derived class pointer
  • is there a std::optional_function like the given
  • Access array in main by pointers by method in class
  • Is there a method/function in c++ which later constant parameters are based on the first ones?
  • How to find out which functions the compiler generated?
  • Hourglass in C++ adding arguments
  • I'm trying to encrypt a message for my homework assignment
  • Object instantiation with curly braces and : symbols
  • c++ How to add value at the beginning of the array and index it?
  • Data structure that stays sorted, allows log N insertion time, and can return the index of an element that I look for in
  • What is diffrence between return reference instance and non reference instance(return mystr & vs mystr)?
  • CListCtrl is showing different theme for Unicode and Multi byte character set
  • Delete Inherited class Pointer Array in C++
  • error: constexpr variable 'struct2Var' must be initialized by a constant expression
  • Possible problem of gcc with sleep_for and sleep_until functions
  • why does argv remove some of my characters?
  • Should I call processEvents() on a thread?
  • Is it possible / desirable to create non-copyable shared pointer analogue (to enable weak_ptr tracking / borrow-type sem
  • pthread works fine only if the number of threads is small
  • In C++ given one std::variant type, can one add additional types to make another variant type?
  • C++17 post increment operation
  • How to check if variable is of string type in template class?
  • C++ temporary variable lifetime?
  • Remove blank line in c++
  • I have to find the maximum sum of 4 numbers from an array of 5. My code fails for bigger numbers
  • What is the difference between std::stable_partition() and std::partition()?
  • Filter out breaks based on stack trace
  • stable_partition on forward iterators
  • Problem with creating and accesing a 4D vector in c++
  • Linking DirectX
  • How to change and delete a variable in stack?
  • are there other ways to write a multidimensional array in an array?
  • Partial Specialization using a qualified name
  • Warning C6385 in Visual Studio
  • 'this' cannot be used in a constant expression C++
  • Why is my print function not working? Linked list
  • fill vector with random elements
  • Difference between return 0 and -1
  • Is it possible to store lambdas with different signatures in a std::vector and execute them (with their respective argum
  • Why isn't my GetProcessID function compiling in VS?
  • Implementing a non-copyable C++ class
  • Understanding index++ in single line of code
  • high performance calculations and saving of the threads identificators
  • Typedefs with tweaked alignment
  • Get memory address of an R data frame
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk