2

I am buiding a Tic Tac Toe solving robot. For practise, I wrote a Tic Tac Toe game using the minimax algorithm which worked very well. When I wanted to port my code to the controller, I found out that none of C/C++ compilers for this controller support recursive functions. Therefore, I need help converting this recursive minimax function to one that uses iteration or an internal stack :

int miniMax (char board[BOARD_DIM][BOARD_DIM], _Bool minNode, int *xBest, int *yBest)
{
    int possibleMoves[NSQUARES][2];
    int nPossibleMoves = generateMoves(board, possibleMoves);
    char boardChild [BOARD_DIM][BOARD_DIM];
    int ind, x_ind, y_ind;
    int minScore, maxScore;
    if (gameOver(board))
        return evaluateState(board);
    else if (minNode)
    {
        minScore = +INFINITY;
        for (ind = 0 ; ind < nPossibleMoves; ind++) 
        {
            duplicateBoard(board, boardChild);
            x_ind = possibleMoves[ind][0];
            y_ind = possibleMoves[ind][1];
            updateboard(boardChild, x_ind, y_ind, cPlayer);
            int score = miniMax(boardChild,!minNode ,&x_ind ,&y_ind);
            if (minScore > score)
                minScore = score;
        }
        return minScore;
    }
    else if (!minNode)
    {
        maxScore = -INFINITY;
        for (ind = 0 ; ind < nPossibleMoves; ind++) 
        {
            duplicateBoard(board, boardChild);
            x_ind = possibleMoves[ind][0];
            y_ind = possibleMoves[ind][1];
            updateboard(boardChild, x_ind, y_ind, cComputer);
            int score = miniMax(boardChild,!minNode ,&x_ind ,&y_ind);
            if (maxScore < score)
            {
                maxScore = score;
                *xBest = x_ind;
                *yBest = y_ind;
            }
        }
        return maxScore;
    }

I'm totally lost on how to do this. I appreciate any help :)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
lyxicon
  • 163
  • 3
  • 6
  • What do you mean under "none of C/C++ compilers for this controller support recursive functions"? – BЈовић Nov 14 '11 at 07:29
  • 1
    @VJo: I bet he means that stack space is cramped. Embedded software :) – sehe Nov 14 '11 at 07:32
  • @sehe: without profiling whether explicit or recursion stack takes up more memory, this is a case of premature optimization. – moshbear Nov 14 '11 at 07:33
  • @all commenters: No, user1045114 means that the compiler doesn't support recursion. This does not surprise me. Some microprocessors don't have push and pop instructions, so procedure-local data has to be allocated at link time. This obviously won't work for recursive functions. – TonyK Nov 14 '11 at 07:33
  • @VJo: it's no doubt some feature-deficient micro-controller with a 3Hz CPU and seven and a half bytes of RAM :-) – paxdiablo Nov 14 '11 at 07:33
  • 1
    Instead of thinking in terms of code conversion, think in terms of solving the problem from scratch. I think that's much easier here. To start, the initial `if` with early (recursive) return, becomes a loop. – Cheers and hth. - Alf Nov 14 '11 at 07:35
  • The platform is NXT Mindstorms. I tried both ROBOTC and NXC and none of them support this. @AlfP.Steinbach: I'd appreciate it you could expand on that. I'm still a little confused. – lyxicon Nov 14 '11 at 08:18

1 Answers1

7

If it's for embedded I would

  • encode positions in binary (bit matrices instead of 2dim byte arrays)
  • encode the full solution map, so everything is a Lookup only (linear lookup will do fine for this complexity)

enter image description here

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thanks for the answer. But how is it possible to represent 3 states ( O, X and empty) using 0s and 1s? And does using this method means a huge number of if statements which check all the 9 cells? – lyxicon Nov 14 '11 at 08:24
  • Oh and why are the moves for O so much more than moves for X? – lyxicon Nov 14 '11 at 08:25
  • @user1045114: You should bring some creativity! You could use two bits per cell - but that'd be wasteful. You could assign an unique ordinal 0..3^9 (requiring 15 bits only) to each possible position and use it to index a lookup table. Oh, and that is part of the answer: a lookup-table gets rid of any conditionals. – sehe Nov 14 '11 at 09:12
  • 'Oh and why are the moves for O so much more than moves for X' - because X starts? It should be quite obvious from the large red X in the first diagram :) – sehe Nov 14 '11 at 09:14
  • wow guys you're helping a very novice programmer here. I have no idea how to implement a look up table. and @sehe: you totally lost me at your last sentence. The project is due soon so I really don't have time to write the most optimized code. – lyxicon Nov 14 '11 at 18:10
  • If this is for homework, you should add a homework tag. Some users look specifically for that tag and you might get more help. – James Nov 15 '11 at 03:04