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

KBDiagonalShotStrategy::KBDiagonalShotStrategy(KBStrategy *tqparent) : KBStrategy(tqparent)
{
	m_column = 0;
	m_row = 0;
	m_vertical = 0;
	m_horizontal = 0;
}

const TQPoint KBDiagonalShotStrategy::nextShot()
{
	if(hasMoreShots())
		return TQPoint(m_column, m_row);

	return TQPoint(0,0);
}

bool KBDiagonalShotStrategy::advance()
{
	while (m_fieldRect.tqcontains(m_column, m_row))
	{
		if(enemyFieldStateAt(m_column, m_row) != KBStrategy::SHOT)
			return true;
		m_column += m_horizontal;
		m_row += m_vertical;
	}

	return false;
}

bool KBDiagonalShotStrategy::hasMoreShots()
{
	return advance();
}

void KBDiagonalShotStrategy::shotAt(const TQPoint &pos)
{
	m_prevShots.append(pos);
}

void KBDiagonalShotStrategy::startAt(int col, int row, Direction dir)
{
	m_column = col;
	m_row = row;

	switch(dir)
	{
		case LEFTUP:
			m_vertical = -1;
			m_horizontal = -1;
			break;

		case LEFTDOWN:
			m_vertical = 1;
			m_horizontal = -1;
			break;

		case RIGHTUP:
			m_vertical = -1;
			m_horizontal = 1;
			break;

		case RIGHTDOWN:
			m_vertical = 1;
			m_horizontal = 1;
			break;

		default:
			m_vertical = 0;
			m_horizontal = 0;
			break;
	}
}

TQPoint KBDiagonalShotStrategy::endPoint()
{
	int row = m_row;
	int col = m_column;

	if(m_vertical == 0 || m_horizontal == 0)
		return TQPoint(col, row);

	while(m_fieldRect.tqcontains(col, row))
	{
		row += m_vertical;
		col += m_horizontal;
	}

	row -= m_vertical;
	col -= m_horizontal;

	return TQPoint(col, row);
}