[go: up one dir, main page]

Menu

[190a4b]: / lib / square.c  Maximize  Restore  History

Download this file

189 lines (168 with data), 4.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @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: */