2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
16 Board::Board(const Point
& size
)
18 m_data
.resize(m_size
.x
* m_size
.y
);
21 Board::Board(const Board
* other
)
22 : m_size(other
->m_size
)
23 , m_data(other
->m_data
) { }
25 bool Board::equals(const Board
* other
) const {
26 if (m_size
!= other
->m_size
)
29 const unsigned int total
= m_data
.size();
30 for (unsigned int i
= 0; i
< total
; i
++) {
31 if (m_data
[i
] != other
->m_data
[i
])
38 Point
Board::size() const { return m_size
; }
40 const Piece
* Board::get(const Point
& p
) const {
42 return &m_data
[p
.x
+ p
.y
* m_size
.x
];
49 // FIXME: solve this code duplication
50 Piece
* Board::get(const Point
& p
) {
52 return &m_data
[p
.x
+ p
.y
* m_size
.x
];
59 void Board::set(const Point
& p
, const Piece
& piece
) {
61 m_data
[p
.x
+ p
.y
* m_size
.x
] = piece
;
64 kError() << "point is not valid" << p
;
67 bool Board::valid(const Point
& p
) const {
68 return p
.x
< m_size
.x
&&
74 PathInfo
Board::path(const Point
& from
, const Point
& to
) const {
75 if (!valid(from
) || !valid(to
))
76 return PathInfo(PathInfo::Undefined
, 0);
78 Point delta
= to
- from
;
79 PathInfo::Direction direction
;
82 direction
= PathInfo::Vertical
;
83 else if (delta
.y
== 0)
84 direction
= PathInfo::Horizontal
;
85 else if (delta
.x
== delta
.y
)
86 direction
= PathInfo::Diagonal1
;
87 else if (delta
.x
== -delta
.y
)
88 direction
= PathInfo::Diagonal2
;
90 direction
= PathInfo::Undefined
;
93 if (direction
!= PathInfo::Undefined
) {
94 Point step
= delta
.normalizeInfinity();
95 Point position
= from
;
96 while ((position
+= step
) != to
) {
97 if (get(position
) == NULL
||
98 *get(position
) == Piece())
104 return PathInfo(direction
, num_obs
);
107 Point
Board::find(const Piece
& piece
) const {
108 for (int i
= 0; i
< m_size
.x
; i
++) {
109 for (int j
= 0; j
< m_size
.y
; j
++) {
111 if (*get(p
) == piece
)
116 return Point::invalid();
119 QStringList
Board::borderCoords() const {
122 for (int i
= 0; i
< p
.x
; i
++)
123 retv
<< Point(i
, 0).col();
124 for (int i
= 1; i
<= p
.y
; i
++)
125 retv
<< QString::number(i
);