Thread: undo func in a tic tac toe game

  1. #1
    Registered User
    Join Date
    Dec 2020
    Posts
    1

    undo func in a tic tac toe game

    I want to program a version of the Tic-Tac-Toe game using C, in which we have 'n × n' board decided by the user, and the loser is decided by the first who get first 'n' X's or O's in a row or column etc..

    One of the requirements is to let the players be able to undo multiple steps, that means to get back to the board status as it was a couple of steps ago by entering a negative odd number.

    For example, if player 1 entered '-3' as the row index, the game needs to revert back how it was 3 steps before (in case there has already been 3 steps done within the game), show the board and give the turn to player 2.

    Any idea how I would be able to make such a function ill be very thankful if someone do it for me its just my 4th week with c https://dreamincode.net/forums/publi...helpsmilie.gif

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by moradkh View Post
    I want to program a version of the Tic-Tac-Toe game using C, in which we have 'n × n' board decided by the user, and the loser is decided by the first who get first 'n' X's or O's in a row or column etc..

    One of the requirements is to let the players be able to undo multiple steps, that means to get back to the board status as it was a couple of steps ago by entering a negative odd number.

    For example, if player 1 entered '-3' as the row index, the game needs to revert back how it was 3 steps before (in case there has already been 3 steps done within the game), show the board and give the turn to player 2.

    Any idea how I would be able to make such a function ill be very thankful if someone do it for me its just my 4th week with c https://dreamincode.net/forums/publi...helpsmilie.gif

    Thanks!

    Undo is always difficult to program. The easiest way is to have a list of all previous states. Fortunately a tic-tac-toe board is only a few bytes, so there's no real issue in memory or performance.

    Maximum number of moves is nine, so you need a stack with a capacity of nine boards. When a move is made, push the stack. When an undo is made, pop it and set stack top to the current board.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Malcolm McLean View Post
    Undo is always difficult to program. The easiest way is to have a list of all previous states. Fortunately a tic-tac-toe board is only a few bytes, so there's no real issue in memory or performance.

    Maximum number of moves is nine, so you need a stack with a capacity of nine boards. When a move is made, push the stack. When an undo is made, pop it and set stack top to the current board.
    With a game like tic-tac-toe (or even one like chess), you only need to store the moves themselves as board locations (like (0,0)) in the undo stack, rather than a whole copy of the board. To undo a move, pop off the top move from the stack and remove the player's mark from the location indicated by the move.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undo a pipe. It is possible?
    By JOCAAN in forum C Programming
    Replies: 4
    Last Post: 01-16-2011, 05:47 PM
  2. Undo virtual
    By User Name: in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2010, 09:42 PM
  3. undo bitshift
    By young turk in forum C Programming
    Replies: 6
    Last Post: 04-28-2009, 10:08 AM
  4. Implementing multiple undo's
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 09-20-2007, 11:21 PM
  5. Cut, copy, undo, AAGG
    By Lurker in forum Windows Programming
    Replies: 1
    Last Post: 05-30-2003, 12:31 AM

Tags for this Thread