-
Notifications
You must be signed in to change notification settings - Fork 1
/
specific.cpp
113 lines (99 loc) · 3.67 KB
/
specific.cpp
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
#include "specific.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
namespace Battleships
{
//----------------------------------------------------------------------------------------------------------//
void Specific::choosePosition(Ship& shipToPlace)
{
unsigned row, column, i;
Cell* tempCell;
bool horizontal = false;
Coordinate* thePosition;
theInterface->showMyBoard(*theBoard);
theInterface->showFleet(*theBoard);
do
{
do
{
thePosition = NULL;
if(ismouseclick(WM_RBUTTONDOWN))
{
horizontal = abs(horizontal - 1);
clearmouseclick(WM_RBUTTONDOWN);
}
else if(ismouseclick(WM_LBUTTONDOWN))
{
thePosition = theInterface->getCoordinate(*theBoard, 59);
clearmouseclick(WM_LBUTTONDOWN);
}
}while(thePosition == NULL);
}while(getPositionValid(thePosition, shipToPlace.getShipLength(), horizontal) == false);
row = thePosition->getCoordinateRow();
column = thePosition->getCoordinateColumn();
for(i=0; i<shipToPlace.getShipLength(); i++)
{
if(horizontal == true)
{
tempCell = theBoard->getCellByLocation(row, column + i);
tempCell->setCellOccupant(&shipToPlace);
}
else
{
tempCell = theBoard->getCellByLocation(row + i, column);
tempCell->setCellOccupant(&shipToPlace);
}
theInterface->showFleet(*theBoard);
}
delete thePosition;
}
//----------------------------------------------------------------------------------------------------------//
bool Specific::getPositionValid(Coordinate* thePosition, unsigned shipLength, bool horizontal)
{
unsigned row, column, i;
Cell* tempCell;
bool isValid = true; // just a temporary measure to ensure conditionals work further down
row = thePosition->getCoordinateRow();
column = thePosition->getCoordinateColumn();
//make sure the location isn't out of bounds, this check should never be used, but just in case...
if(column > theBoard->getBoardColumns() || row > theBoard->getBoardRows())
{
isValid = false;
}
//make sure the ship doesn't go out of bounds, very important!
if(horizontal == true && (column + shipLength) > theBoard->getBoardColumns())
{
isValid = false;
}
else if(horizontal == false && (row + shipLength) > theBoard->getBoardRows())
{
isValid = false;
}
//finally check for other ships in the area
if(isValid == true)
{
for(i=0; i<shipLength; i++)
{
if(horizontal == true && isValid == true)
{
tempCell = theBoard->getCellByLocation(row, column + i);
if(tempCell->getOccupant() != NULL)
{
isValid = false;
}
}
else if(horizontal == false && isValid == true)
{
tempCell = theBoard->getCellByLocation(row + i, column);
if(tempCell->getOccupant() != NULL)
{
isValid = false;
}
}
}
}
return isValid;
}
//----------------------------------------------------------------------------------------------------------//
}