# Copyright 2022 Mattia Giambirtone & All Contributors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import game import util/matrix type Move = ref object ## A specific state in a ## tic-tac-toe match, with ## all of its children states state: Matrix[int] # Since tic-tac-toe is a rather # simple game, we can generate all # possible (legal) board configurations # and then look up the current board when # playing against the user in order to find # the best move to win the match next: array[9, Move] parent: Move depth: int proc generateMoves*(map: Matrix[int], turn: TileKind, depth: int = 0): Move = ## Generates the full tree of all ## possible tic tac toe moves starting ## from a given board state new(result) result.state = map.copy() result.depth = depth if result.state.asGame().get() != Playing: # The game is over, no need to generate # any more moves! return for row in 0..