0

I had an assignment to do an N by N tic tac toe where you win by losing, and it has a rewind mechanism (goes back an odd number of turns.) I've tried implementing the rewind mechanism, by using a 2 by N squared matrix but it didn't work - it erases things "from the beginning" and notifies me of "core dumps". here's my revised code (fixed a different issue) - I would love a few tips on how to solve this issue.

int undo_most_recent(char board[N][N], int n, int log[N * N][2], int* p_of_m, int i)
{
    if ( (i % 2 == 0) && (*p_of_m + i <= 0))
    {
        print_error();
        return(Error);
    }
    while ((*p_of_m > 0) && (i < 0))
    {
        board[(log[*p_of_m][0])][(log[*p_of_m][1])] = '_';
        log[*p_of_m][0] = 0;
        log[*p_of_m][1] = 0;
        (*p_of_m)--;
        i++;
        printf(" p_of_m is: %d", *p_of_m);
    }
    print_board(board, n);
    return(Success);
}

Here's a link to the entire code - https://pastebin.com/qGHbWB56

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kal_elk122
  • 143
  • 6
  • 1
    Add the code to the question - and make it as small as possible to demonstrate the problem. e.g. like a unit test – Ed Heal Dec 27 '20 at 13:56
  • 1
    Welcome to StackOverflow! On your first post you might benefit by reading the [tour](https://stackoverflow.com/tour) and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) Usually, it's best to post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), the shortest *complete* code that shows the problem (not with external links). – Weather Vane Dec 27 '20 at 13:58
  • are you sure `*p_of_m++;`and `*p_of_m--;`do what you expect ? operators have priority – bruno Dec 27 '20 at 14:00
  • I wanted *p_of_m-- and ++ to reduce/add the value of moves (to which p_of_m points to) by 1. – kal_elk122 Dec 27 '20 at 14:07
  • `*p_of_m--` reduces the pointer, `(*p_of_m)--` reduces what it is pointing to. – Weather Vane Dec 27 '20 at 14:12
  • thank you! I'll fix that and see if it works. edit: there are more bugs- it still doesn't work, but now there's one less, thank you – kal_elk122 Dec 27 '20 at 14:15
  • I encourage you to compile with high warning level and to read the generated warnings, I just compiled your code using *gcc* with option `-Wall` to be warn about these two lines (*value computed is not used [-Wunused-value]*) – bruno Dec 27 '20 at 14:20
  • thank you - I'll try that, how do you toggle -Wall on? – kal_elk122 Dec 27 '20 at 14:21
  • you access non initialized values, the behavior is undefined. Are you under Linux ? install *valgrind* and execute your code with it – bruno Dec 27 '20 at 14:23
  • in *sec_diag_check* you missed to initialize *count* (probably with 0) – bruno Dec 27 '20 at 14:25
  • I'm using windows, my IDE is visual studio- currently trying to use gcc with wall but I don't know how. about sec_diag_check you're right - fixed that. thanks about the initialising too, I'll try something – kal_elk122 Dec 27 '20 at 14:29
  • Visual studio very probably allows to have high level warning, see its doc – bruno Dec 27 '20 at 14:29
  • I couldn't find a way to activate it and VS started acting up so I switched to an online compiler and removed the clause that prevents people from replacing the already placed signs (plus other things it pointed out) and the diagonal worked - I'll have to find a way to place it back and figure out what's up with the rewind. thank you! – kal_elk122 Dec 27 '20 at 15:00
  • buy a Raspberry PI 4 at few $ / €, use it under raspbian and you will have a serious environment to program ... I am currently in this condition ;-) – bruno Dec 27 '20 at 15:04

0 Answers0