/**
* @file
* Routines for dealing with Square objects that occur in the
* 'squares' array in the Qube object.
* Last Modified: Sun Aug 28 14:42:05 PDT 2016
* @author Kevin O'Gorman
*/
/*
* Copyright 2002 Santiago Canez <scanez@math.berkeley.edu>
* Copyright 2012,2013,2016 Kevin O'Gorman <kogorman@gmail.com>.
* Distributed under the GNU General Public License.
*
* This file is part of libqubist, the library of functions for playing
* 4x4x4 tic-tac-toe, also known by some other names outside the USA.
*
* Libqubist is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2.
*
* Libqubist is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* @file
* Routines for dealing with Square objects that occur in the
* 'squares' array in the Qube object.
*
* Each cell (or, in the code, Square) has a state and a list of rows
* that it belongs to. This list has 4 entries for most Squares, but
* has 7 for the 16 Squares on one of the 4 major diagonals.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "player.h"
#include "board.h"
#define BUFL 129
#undef NDEBUG
#define NDEBUG
#include <assert.h>
/**
* Predicate function to determine if a square is unmarked.
* @param sq a pointer to the Square object.
* @return a truth-value; TRUE if the square is empty, and FALSE otherwise.
*/
int
qb_is_square_empty(Square *sq)
{
return sq->square_state == EMPTY;
}
/**
* Predicate function to determine if a square is occupied (marked) by the computer.
* @param sq a pointer to the Square object.
* @return a truth-value; TRUE if the square occupied by the computer player, and FALSE otherwise.
*/
int
qb_is_marked_computer(Square *sq)
{
return sq->square_state == COMP;
}
/**
* Predicate function to determine if a square is occupied (marked) by the human player.
* @param sq a pointer to the Square object.
* @return a truth-value; TRUE if the square is occupied by the human player, and FALSE otherwise.
*/
int
qb_is_marked_human(Square *sq)
{
return sq->square_state == PLAYER;
}
/**
* Add rows to a Square. Used in initialization.
* @param sq a pointer to the Square object.
* @param r a row number.
*/
void
qb_add_rows_in(Square *sq, int r)
{
sq->rows_in[sq->num_rows] = r;
sq->num_rows++;
}
/**
* Get a string representation of a square, showing its board, row and column
* (in that order). Not thread-safe, and the result is overwritten on each call.
* @param s a pointer to the Square object.
* @return a pointer to a static string in the form "(b,r,c)".
*/
char*
qb_get_string_square(Square *s)
{
static char result[BUFL];
sprintf(result, "%i", qb_get_square_num(s)); /* this throws memcheck errors, but I ignore them. */
return result;
}
/* Initialize all 64 squares on the board to be empty and have an empty row list.
* The row list gets initialized later when the rows are built.
* @param board the Qube that contains these squares.
*/
void
qb_initialize_squares(Qube *board)
{
Square *sq = board->squares;
int q;
for (q = 0; q < 64; q++) {
sq->num = q;
sq->square_state = EMPTY;
sq->num_rows = 0;
sq++;
}
}
/**
* Get a pointer to a Square given its number.
* @param board a pointer to the Qube that contains the Square.
* @param num the number of the Square.
* @return a pointer to the Square.
*/
Square*
qb_get_square(Qube *board, int num)
{
return board->squares + num;
}
/**
* Get the number of a square given a pointer to it.
* @param sq a pointer to the Square.
* @return the number of the Square.
*/
int
qb_get_square_num(Square *sq)
{
return sq->num;
}
/**
* Empty all squares of the Qube.
* @param board a pointer to the Qube.
*/
void
qb_clear_squares_array(Qube *board)
{
int i;
for (i=0; i < 64; i++)
board->squares[i].square_state = EMPTY;
}
/**
* Getter for the row census.
* @param sq a pointer to the Square.
* @return the number of rows that include this Square.
*/
int
qb_get_row_count(Square *sq) {
return sq->num_rows;
}
/**
* Getter for one of the rows (by number) that go through this Square.
* @param sq a pointer to the Square.
* @param row an index to the row.
* @return the row number (in the Qube) of the Row.
*/
int
qb_get_row_from_square(Square *sq, int row) {
return sq->rows_in[row];
}
/* vim: set expandtab autoindent shiftround nosmartindent tabstop=8 softtabstop=2 shiftwidth=2: */