简体   繁体   中英

How to undo multiple steps in a Tic-Tac-Toe game in C

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 or at least a tip how would I start programming it?

Thanks!

idea how I would be able to make such a function or at least a tip how would I start programming it?

An alternative to undoing a step it to instead keep a history (linked list) of all actively. Replay the list sans the last one to "undo". Less efficient yet less complicated and less error prone to undo complicated steps - which may be present in more advanced games. Sometimes, "backing up" is wrought with subtle complexities where the prior state is not truly fully restored. Akin to @Tom Karzes @David C. Rankin

As a bonus, you have a game record.


Consider your game has events like:

  • Default setup-up
  • Select size n .
  • Select X or O to go first
  • Make a move
  • Rotate view
  • Save game
  • Restore game
  • Undo last command <-- now one can undo more than just the last "move"
  • etc.

通常,您会有一个堆栈,可以在其中放置每个新动作,并且要撤消某个动作时,只需从堆栈中弹出它,然后对游戏模型进行相反的动作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM