0

I am having some questions on how I would work through some problems. I am trying to setup a tic tac toe game with assembly and I am trying to implement the computer playing as a player. I am looking for ways to save data primarily to variable that I can append for example a variable string where I would add all the variables of the cell that each player played.

How would I do this, is there a way I can say create an array that would hold this information that I can dynamically change and allocate?.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Felcannon
  • 25
  • 4

1 Answers1

0

I'd recommend using the call stack as either a stack data structure, or more simply to create a recursive function.

Recursion naturally lends itself to an exhaustive search over the possible moves, pruning any branches that lead to forced loses. Formally this is called a Minimax search (to find the next move). https://www.cs.cornell.edu/courses/cs312/2002sp/lectures/rec21.htm

If you find a winning move, you return that fact, and the caller knows the board position it was considering lets the opponent force a win. You want your function to avoid letting the opponent force a win, and play moves that can lead to a forced win for itself if there is one. e.g. Tic Tac Toe recursive algorithm.


I'd recommend representing the board state with a fixed-size data structure. Your good options are

  • a 9-byte array of characters like space, 'X', and 'O' (padded to 12 so you can copy it with 3x lw). i.e. in C, struct board { alignas(4) char b[9]; };. You make copies of this whole struct.

  • 2x bitmaps (one for X and one for O), for what's called a "bitboard". (In chess engines, a 64-bit bitmaps has one bit for every square on the board. In 3x3 tic-tac-toe, you only need 9 bits of a register for a full board). Then you could come up with bit-hacks to test for 3-in-a-row, like b & 0b111 == 0b111 (with andi and beq) to check for the top row being all one marker.

    You can find open spaces left on the board by doing X|O and the looking for bits that are still zero.

  • a single bitboard with 2 bits per position (18 bits total, larger than a MIPS immediate). It might be easier to look for 2 in a row with an empty 3rd position this way.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Sorry, i forgot to mention this is for a 3d tic tac toe game – Felcannon Apr 09 '19 at 23:59
  • @Felcannon: Ok, then that would be 3x3x3 = 27 bits (fits in a word), or 27 bytes (pad to 28 bytes), and looping over the board state would take longer. But how you implement Minimax doesn't really change. (Except that it might be hard to make it run in reasonable time, with so many more possible moves and a much larger game space.) I still think copying around fixed-size game states is the way to go, with a stack data structure or recursion. Or maybe a tree if it's too slow to exhaustively search it in order. – Peter Cordes Apr 10 '19 at 00:41