summaryrefslogtreecommitdiffstats
path: root/kbattleship/kbattleship/kshiplist.cpp
blob: e490fd0ad5f4bc3ed9a365e404ae9d6479950732 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/***************************************************************************
                                kshiplist.cpp
                             -------------------
    Developers: (c) 2000-2001 Nikolas Zimmermann <wildfox@kde.org>
                (c) 2000-2001 Daniel Molkentin <molkentin@kde.org>
                (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 "kshiplist.moc"
#include <kdebug.h>

KShipList::KShipList() : TQObject()
{
	m_shiplist.setAutoDelete(true);
	m_shipsadded = 4;

	m_fieldx = 10;
	m_fieldy = 10;
}

void KShipList::clear()
{
	m_shipsadded = 4;
	m_shiplist.clear();
}

int KShipList::shipTypeAt(int x, int y)
{
	int tempx, tempy;
	KShip *shipiterator;
	for(shipiterator = m_shiplist.first(); shipiterator != 0; shipiterator = m_shiplist.next())
	{
		if(shipiterator->shipystart() != shipiterator->shipystop())
		{
			for(tempy = shipiterator->shipystart(); tempy <= shipiterator->shipystop(); tempy++)
			{
				if(tempy == y)
				{
					if(shipiterator->shipxstart() != shipiterator->shipxstop())
					{
						for(tempx = shipiterator->shipxstart(); tempx <= shipiterator->shipxstop(); tempx++)
						{
							if(tempx == x)
								return shipiterator->shiptype();
						}
					}
					else
					{
						tempx = shipiterator->shipxstart();
						if(tempx == x)
							return shipiterator->shiptype();
					}
				}
			}
		}
		else
		{
			tempy = shipiterator->shipystart();
			if(tempy == y)
			{
				if(shipiterator->shipxstart() != shipiterator->shipxstop())
				{
					for(tempx = shipiterator->shipxstart(); tempx <= shipiterator->shipxstop(); tempx++)
					{
						if(tempx == x)
							return shipiterator->shiptype();
					}
				}
				else
				{
					tempx = shipiterator->shipxstart();
					if(tempx == x)
						return shipiterator->shiptype();
				}
			}
		}
	}
	return 99;
}

KShip *KShipList::shipAt(int x, int y)
{
	int tempx, tempy;
	KShip *shipiterator;
	for(shipiterator = m_shiplist.first(); shipiterator != 0; shipiterator = m_shiplist.next())
	{
		for(tempy = shipiterator->shipystart(); tempy <= shipiterator->shipystop(); tempy++)
		{
			if(tempy == y)
			{
				for(tempx = shipiterator->shipxstart(); tempx <= shipiterator->shipxstop(); tempx++)
				{
					if(tempx == x)
						return shipiterator;
				}
			}
		}
	}
	return 0;
}

bool KShipList::canAddShips()
{
	if(m_shipsadded >= 1)
		return true;
	return false;
}

void KShipList::addNewShip(int button, int fieldx, int fieldy)
{
	if(!addNewShip(!(button == Qt::LeftButton), fieldx, fieldy))
		KMessageBox::information(0L, i18n("You cannot place the ship here."));
}

bool KShipList::addNewShip(bool vertical, int fieldx, int fieldy)
{
	TQRect ship = vertical ? TQRect(fieldx, fieldy, 1, m_shipsadded) : TQRect(fieldx, fieldy, m_shipsadded, 1);
	TQRect field = TQRect(0, 0, m_fieldx, m_fieldy);
	if(!field.contains(ship))
		return false;

	for(KShip *placedShip = m_shiplist.first(); placedShip != 0; placedShip = m_shiplist.next())
	{
		for(int i = fieldx-1; i < (fieldx + ship.width()+1); i++)
		{
			if(placedShip->contains(i, fieldy - 1) || placedShip->contains(i, fieldy + ship.height()))
				return false;
		}

		for(int i = fieldy-1; i < (fieldy + ship.height()+1); i++)
		{
			if(placedShip->contains(fieldx - 1, i) || placedShip->contains(fieldx + ship.width(), i))
				return false;
		}
	}

	m_shipsadded--;

	if(!vertical)
		m_shiplist.append(new KShip(fieldx, fieldx + shipCount(), fieldy, fieldy, shipCount(), true));
	else
		m_shiplist.append(new KShip(fieldx, fieldx, fieldy, fieldy + shipCount(), shipCount(), false));

	for(int i = 0; i < shipCount() + 1; i++)
	{
		int start = 0;
		if(shipCount() == 3)
			start = KBattleField::SHIP4P1;
		else if(shipCount() == 2)
			start = KBattleField::SHIP3P1;
		else if(shipCount() == 1)
			start = KBattleField::SHIP2P1;
		else if(shipCount() == 0)
			start = KBattleField::SHIP1P1;

		if(!vertical)
			emit sigOwnFieldDataChanged(fieldx + i, fieldy, start + i);
		else
			emit sigOwnFieldDataChanged(fieldx, fieldy + i, start + i);
	}

	if(m_shipsadded == 0)
		emit sigLastShipAdded();
	return true;
}