/*
 * This file is part of openKropki.
 *
 * Copyright (C) 2014-2020 Mateusz Viste
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef KROPKI_CORE_H_SENTINEL
  #define KROPKI_CORE_H_SENTINEL

  #define kropki_EMPTY 0     /* These values */
  #define kropki_PL1_DOT 1   /* must all fit */
  #define kropki_PL2_DOT 2   /* in 2 bits!   */

  /* a linked list node used to store walls within a kropki_game struct */
  struct kropki_wallelement {
    int x1, y1, x2, y2;
    int player;
    struct kropki_wallelement *next;
  };

  /* dots are stored as 1 byte per dot in the play field, where:
      bits ......XX = Indicates whether it's a dot for player 1, player 2 or an empty space.
      bits ....XX.. = Indicates whether the field is in captivity (captured by who)
      bits ..XX.... = Indicates whether the dot is part of a wall (wall's owner)
      bits XX...... = used for internal processing (mostly for applying the flooding algorithm) */

  struct kropki_game {
    struct kropki_wallelement *walls;
    int lastplayer; /* who played last (1 or 2) - 0 means nobody played yet */
    int lastmovex;  /* lastmove X coordinate */
    int lastmovey;  /* lastmove Y coordinate */
    int field_width;
    int field_height;
    unsigned char field[1]; /* This element MUST be the last in the struct. The struct */
  };                        /* will be allocated at runtime to accomodate field's size */
                            /* YES, this is safe - see ISO 9899-1990, 6.5.2.1.         */

  struct kropki_game *kropki_newgame(int width, int height);
  void kropki_freegame(struct kropki_game *game);
  int kropki_getpos(const struct kropki_game *game, int x, int y);
  int kropki_setpos(struct kropki_game *game, int x, int y, int player);
  int kropki_setposforce(struct kropki_game *game, int x, int y, int player);
  void kropki_getpoints(struct kropki_game *game, int *scores);
  struct kropki_game *kropki_copygame(struct kropki_game *game);
  int kropki_findfirstfreefield(struct kropki_game *game, int *resx, int *resy);
#endif
