File indexing completed on 2024-04-28 04:05:21

0001 /*
0002     SPDX-FileCopyrightText: 2015 Jakob Gruber <jakob.gruber@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "board.h"
0008 
0009 Board::Board(int width, int height)
0010     : m_width(width), m_height(height), m_size(width * height),
0011       m_state(width * height, Nothing)
0012 {
0013 }
0014 
0015 bool Board::outOfBounds(int x, int y) const {
0016     return (x < 0 || x >= m_width || y < 0 || y >= m_height);
0017 }
0018 
0019 void Board::assertInbounds(int x, int y) const {
0020     if (outOfBounds(x, y)) {
0021         throw OutOfBoundsException();
0022     }
0023 }
0024 
0025 Board::State Board::get(int x, int y) const {
0026     assertInbounds(x, y);
0027     return m_state[xy_to_i(x, y)];
0028 }
0029 
0030 int Board::xy_to_i(int x, int y) const {
0031     return y * m_width + x;
0032 }
0033 
0034 int Board::i_to_x(int i) const {
0035     return i % m_width;
0036 }
0037 
0038 int Board::i_to_y(int i) const {
0039     return i / m_width;
0040 }