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
|
/***************************************************************************
kbstrategy.cpp
----------
Developers: (c) 2001 Kevin Krammer <kevin.krammer@gmx.at>
(c) 2001 Nikolas Zimmermann <wildfox@kde.org>
***************************************************************************/
/***************************************************************************
* *
* This program 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; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "kbstrategy.h"
KBStrategy::KBStrategy(KBStrategy *parent)
{
m_parent = parent;
m_viableShots = 0;
}
KBStrategy::~KBStrategy()
{
while ( !m_prevShots.empty() )
{
m_prevShots.remove( m_prevShots.last() );
}
if (m_parent == 0 && m_viableShots != 0)
{
delete[] m_viableShots;
}
}
/* Returns the master strategy's shot list. */
QValueList<QPoint> KBStrategy::masterShotList()
{
return (!m_parent) ? m_prevShots : m_parent->masterShotList();
}
/* the AI player decided to shoot at pos */
void KBStrategy::shotAt(const QPoint &pos)
{
m_prevShots.append(pos);
}
void KBStrategy::init(KBattleField *field, const QRect &field_rect)
{
m_battleField = field;
m_fieldRect = field_rect;
if (!m_parent)
{
if (m_viableShots == 0)
{
m_viableShots = new bool[(field_rect.width()*field_rect.height())];
}
for (int x = 0; x < field_rect.width(); ++x)
{
for (int y = 0; y < field_rect.height(); ++y)
{
//m_viableShots[x, y] = true;
setViablePos(x, y, true);
}
}
}
else
{
m_viableShots = m_parent->getViableShots();
}
}
/* Returns the field type of position (x, y) on the user player's field */
int KBStrategy::enemyFieldStateAt(int x, int y)
{
if (!isViablePos(x, y))
return SHOT; // faking SHOT if position is not possible ship position
switch(m_battleField->ownState(x, y))
{
case KBattleField::FREE:
return KBStrategy::FREE;
case KBattleField::WATER:
case KBattleField::HIT:
case KBattleField::DEATH:
return KBStrategy::SHOT;
default:
return KBStrategy::SHIP;
}
}
bool* KBStrategy::getViableShots()
{
return m_viableShots;
}
bool KBStrategy::isViablePos(int x, int y)
{
return m_viableShots[(m_fieldRect.width()*y + x)];
}
void KBStrategy::setViablePos(int x, int y, bool viable)
{
m_viableShots[(m_fieldRect.width()*y + x)] = viable;
}
|