summaryrefslogtreecommitdiffstats
path: root/kbattleship/kbattleship/kbchooserstrategy.cpp
blob: fc8320864ff528da15133122761532d8d2e86be3 (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
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
/***************************************************************************
                             kbchooserstrategy.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 "kbchooserstrategy.h"

#include <kapplication.h>

#include "kbrandomshotstrategy.h"
#include "kbdiagonalwrapstrategy.h"
#include "kbhorizontalstepstrategy.h"
#include "kbverticalstepstrategy.h"

#define MAX_CHILD_NUM 4

KBChooserStrategy::KBChooserStrategy(KBStrategy *tqparent) : KBStrategy(tqparent)
{
	m_destroyer = new KBDestroyShipStrategy(this);
	m_destroying = false;

	m_child = 0;

	m_random = new KRandomSequence(KApplication::random());
}

KBChooserStrategy::~KBChooserStrategy()
{
	delete m_destroyer;
	delete m_child;
	delete m_random;
}

void KBChooserStrategy::init(KBattleField *field, const TQRect &field_rect)
{
	KBStrategy::init(field, field_rect);

	if(m_destroyer != 0)
		m_destroyer->init(field, field_rect);

	advance();
}

const TQPoint KBChooserStrategy::nextShot()
{
	if(hasMoreShots())
	{
		if(m_destroying)
			return m_destroyer->nextShot();
		else if(advance())
			return m_child->nextShot();
	}

	return TQPoint(0, 0);
}

bool KBChooserStrategy::advance()
{
	if(!m_destroying && m_prevShots.count() % 5 == 0)
	{
		delete m_child;

		switch(m_random->getLong(MAX_CHILD_NUM))
		{
			case 0:
				m_child = new KBVerticalStepStrategy(this);
				break;

			case 1:
				m_child = new KBHorizontalStepStrategy(this);
				break;

			case 2:
				m_child = new KBDiagonalWrapStrategy(this);
				break;

			default:
				m_child = new KBRandomShotStrategy(this);
				break;
		}

		m_child->init(m_battleField, m_fieldRect);
	}

	return true;
}

bool KBChooserStrategy::hasMoreShots()
{
	if(m_parent == 0)
	{
		if((!m_destroying) && m_prevShots.count() > 0)
		{
			TQPoint 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 KBChooserStrategy::shotAt(const TQPoint &pos)
{
	m_prevShots.append(pos);
	m_child->shotAt(pos);
}