-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (88 loc) · 2.68 KB
/
main.cpp
File metadata and controls
103 lines (88 loc) · 2.68 KB
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
/********************************************************************
* Name : Kevin Shaffer
* Student ID : 106069743
* Class : CSCI 2312-001
* HW# : 3
* Due Date : Sep 26th, 2017
* Description : Main program. This is the driver program that takes
the user's input and uses it to create the submarine
object and handles the output.
********************************************************************/
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include "main.h"
#include "position.h"
#include "submarine.h"
#include "../grid/grid.cpp"
using namespace std;
int main()
{
Grid grid(10, 10, time(0));
int x, y, length, depth;
Position::Orientation orientation;
do
{
x = getInput("Enter the x coordinate: ");
y = getInput("Enter the y coordinate: ");
length = getInput("Enter the length: ");
depth = getInput("Enter the dive depth in meters: ");
orientation = getOrientation();
} while (!isValid(orientation, x, y, length, grid.getRows(), grid.getColumns()));
Submarine submarine(Position::Coordinates(x, y), orientation, length, depth);
grid.Randomize(15);
cout << "Fifteen torpedo shots: " << endl;
cout << grid.ToString() << endl;
for (int y = 0; y < grid.getRows(); y++)
for (int x = 0; x < grid.getColumns(); x++)
{
if (grid[y][x] && submarine.IsHit(Position::Coordinates(x, y)))
{
cout << "Hit at " << x << ", " << y << endl;
if (submarine.IsSunk())
cout << "You sunk my submarine!!!" << endl;
}
}
Submarine otherSub = submarine;
cout << otherSub.GetCoordinates().X << ", " << otherSub.GetCoordinates().Y << endl;
cout << (otherSub.GetOrientation() == Position::HORIZONTAL ? "Horizontal" : "Verical") << endl;
cout << otherSub.GetLength() << endl;
cout << otherSub.GetDepth() << endl;
cout << "Press a key to exit: ";
cin >> x;
return 0;
}
int getInput(string question)
{
int value = -1;
// loop until user provides valid integer input
while (value < 0)
{
cout << question << endl;
cin >> value;
cin.clear();
cin.ignore(256,'\n');
}
return value;
}
Position::Orientation getOrientation()
{
char letter = '\0';
do
{
cout << "Horizontal (h) or Verical (v) orientation: " << endl;
cin >> letter;
cin.clear();
cin.ignore(256, '\n');
} while (!letter || letter != 'h' && letter != 'H' && letter != 'v' && letter != 'V');
return ((letter == 'h' || letter == 'H') ? Position::HORIZONTAL : Position::VERTICAL);
}
bool isValid(Position::Orientation orientation, int x, int y, int length, int max_x, int max_y)
{
length--;
if (orientation == Position::HORIZONTAL)
return x + length < max_x && y < max_y;
else
return x < max_x && y + length < max_y;
}