-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.java
93 lines (85 loc) · 2.03 KB
/
Grid.java
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
import java.util.*;
/**
* Write a description of class Grid here.
*
* @author Jerrett Fowler
* @version 1.1 (September 2013)
*/
public class Grid
{
// instance variables
private int[][] grid1;
private int[][] grid2;
int size;
/**
* Constructor for objects of class Grid
*/
public Grid()
{
size = 10;
grid1 = new int[size][size];
grid2 = new int[size][size];
}
/**
* Add Ship from fleet
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void addShipFromFleet(Ship s)
{
//get x
int xCoord = s.getColumn();
//get y
int yCoord = s.getRow();
//get direction
int direction = s.getOrientation();
//get Size
int shipSize = s.getSize();
if(direction == 0)
{
for(int x = 0; x < shipSize; x++)
{
grid1[yCoord][xCoord + x] = 1;
}
}
else if(direction == 1)
{
for(int y = 0; y < shipSize; y++)
{
grid1[yCoord + y][xCoord] = 1;
}
}
else
{
throw new RuntimeException("Empty case.");
}
}
/**
* Print grid
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void printGrid()
{
//Loop through rows
for(int i = 0; i < size; i++)
{
//Loops and prints each column
for(int j = 0; j < size; j++)
{
System.out.print(grid1[i][j] == 1 ? "X" : "." );
System.out.print(" ");
}
System.out.print(" ");
for(int k = 0; k < size; k++)
{
System.out.print(grid2[i][k] == 1 ? "X" : "." );
System.out.print(" ");
}
System.out.println();
}
System.out.println();
}
}