blob: a3748a690c594a4740035614d3f87fbc7be158fa (
plain)
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
|
/***************************************************************************
kbrandomshotstrategy.cpp
----------
Developers: (c) 2001 Kevin Krammer <kevin.krammer@gmx.at>
***************************************************************************/
/***************************************************************************
* *
* 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 "kbrandomshotstrategy.h"
KBRandomShotStrategy::KBRandomShotStrategy(KBStrategy *parent) : KBStrategy(parent)
{
m_destroyer = new KBDestroyShipStrategy(this);
m_destroying = false;
}
KBRandomShotStrategy::~KBRandomShotStrategy()
{
delete m_destroyer;
}
void KBRandomShotStrategy::init(KBattleField *field, const QRect &field_rect)
{
KBStrategy::init(field, field_rect);
KRandomSequence rand;
m_column = (int) rand.getLong(m_fieldRect.width());
m_row = (int) rand.getLong(m_fieldRect.height());
if(m_destroyer != 0)
m_destroyer->init(field, field_rect);
}
const QPoint KBRandomShotStrategy::nextShot()
{
if(hasMoreShots())
{
if(m_destroying)
return m_destroyer->nextShot();
else if(advance())
return QPoint(m_column, m_row);
}
return QPoint(0, 0);
}
bool KBRandomShotStrategy::advance()
{
while(enemyFieldStateAt(m_column, m_row) == KBStrategy::SHOT)
{
m_column = m_randomSeq.getLong(m_fieldRect.width());
m_row = m_randomSeq.getLong(m_fieldRect.height());
}
return true;
}
bool KBRandomShotStrategy::hasMoreShots()
{
if(m_parent == 0)
{
if((!m_destroying) && m_prevShots.count() > 0)
{
QPoint pos = m_prevShots.last();
int state = m_battleField->ownState(pos.x(), pos.y());
if(state == KBattleField::HIT)
{
m_destroying = true;
m_destroyer->destroyShipAt(pos);
}
}
if(m_destroying)
{
if(m_destroyer->hasMoreShots())
return true;
else
m_destroying = false;
}
}
for(int row = 0; row < m_fieldRect.height(); row++)
{
for(int col = 0; col < m_fieldRect.width(); col++)
{
if(enemyFieldStateAt(col, row) != KBStrategy::SHOT)
return true;
}
}
return false;
}
void KBRandomShotStrategy::shotAt(const QPoint &pos)
{
m_prevShots.append(pos);
}
|