@phoopio/

Assignment 12: Battleship Game-1

C++

No description

fork
loading
Files
  • main.cpp
  • Board.cpp
  • Board.h
  • Point.h
  • PointCollection.cpp
  • PointCollection.h
  • Ship.cpp
  • Ship.h
main.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
#include "PointCollection.h"
#include "Ship.h"
#include <iostream>

using namespace std;

void testContains(const Ship &s, int x, int y, bool desiredResult, int num) {
	point p(x, y);

	if (s.containsPoint(p) != desiredResult)
		cout << "Problem with contains test " << num << endl;
}

void testThreeArgConstructorAndContainsPoint() {
	point origin(0, 0);
	Ship s1(origin, HORIZONTAL, 3);
	Ship s2(origin, VERTICAL, 4);

	testContains(s1, 0, 0, true, 0);
	testContains(s1, 1, 0, true, 1);
	testContains(s1, 2, 0, true, 2);
	testContains(s1, 3, 0, false, 3);
	testContains(s2, 0, 0, true, 4);
	testContains(s2, 0, 1, true, 5);
	testContains(s2, 0, 2, true, 6);
	testContains(s2, 0, 3, true, 7);
	testContains(s2, 0, 4, false, 8);
	testContains(s2, 1, 1, false, 9);
	testContains(s1, -1, 0, false, 10);
	testContains(s2, 0, -1, false, 11);
}

void testCopyAssignmentAndConstructor() {
	point origin(0, 0);
	Ship s1(origin, HORIZONTAL, 3);
	Ship s2(s1);

	testContains(s2, 0, 0, true, 20);
	testContains(s2, 1, 0, true, 21);
	testContains(s2, 2, 0, true, 22);
	testContains(s2, 3, 0, false, 23);
}

void testCollidesWith() {
	point origin(0, 0);

	Ship s1(origin, HORIZONTAL, 3);
	Ship collisionShip(origin, VERTICAL, 4);

	if (!s1.collidesWith(collisionShip))
		cout << "problem with collides with 1" << endl;

	point oneOne(1, 1);
	Ship noCollisionShip(oneOne, HORIZONTAL, 3);

	if (s1.collidesWith(noCollisionShip))
		cout << "problem with collides with 2" << endl;
}

void testHitMethods() {
	point origin(0, 0);
	Ship s1(origin, HORIZONTAL, 3);

	point p = origin;

	s1.shotFiredAtPoint(p);
	if (s1.hitCount() != 1)
		cout << "Problem: hitCount 1" << endl;
	if (s1.isSunk())
		cout << "Problem with isSunk 1" << endl;

	// Further testing required
}

int main() {
	testHitMethods();
	//testCollidesWith();
	//testCopyAssignmentAndConstructor();
	//testThreeArgConstructorAndContainsPoint();
}