forked from zuliandres/BattleShipJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleship
More file actions
171 lines (142 loc) · 5.17 KB
/
Battleship
File metadata and controls
171 lines (142 loc) · 5.17 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
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
import java.util.Random;
import javax.swing.JOptionPane;
/**
*
* @author Katherine
*/
public class Battleship
{
//Instance Variables
int[][] game; // An int array to create the board
public static int hits = 0; // Number of hits
public static int misses = 0; // Number of misses
public static int games = -1; // Number of games played
public static int hitsGame = 0; // Total number of hits for all games
public static int missesGame = 0; // Total number of misses for all games
private String transcript; // Record of players guesses
//Creates an array with 5 rows and 5 columns, then places 4 random ships
//on the board.
public Battleship()
{
game = new int[5][5];
placeShips();
//Updates the number of games played
games++;
transcript = "\n\nYour previous guesses include:";
//Initializes hits and misses to start at 0
hitsGame = 0;
missesGame = 0;
}
//********************Accessors*******************************************//
//Accesses the current state of the transcript
//@return transcript: the current history of the players guesses
public String getTranscript()
{
return transcript;
}
//Accesses the number of hits throug the life of the current game
//@return hitsGame: number of hits per one game
public int hits()
{
return hitsGame;
}
//*********************************Methods************************************//
//Creates four random locations on the game board
public void placeShips()
{
Random locations = new Random();
int row; int column;
for(int i =0; i < 4; i++)
{ do
{
//Generates a random row & column integer from 0 to 4
row = locations.nextInt(5);
column = locations.nextInt(5);
}
while (game[row][column] == 1);
//Places the ship in the randomly generated location
game[row][column] = 1;
}
}
//Method that allows user to enter a guess consisting of a row number and
//column number
//@param row: the row number of the desired target
//@param column: the column rumber of the desired target
public void guess(int row, int column)
{
//Updates the transcript so the user can see their past guesses
//in format- row: a column: b
transcript += "\nRow: " + (row + 1) + " Column: " + (column + 1);
int guess = game[row][column];
if(guess == 1)
{
game[row][column] = 2;
//Displays the current state of the game board
printGame();
JOptionPane.showMessageDialog(null, "Battleship Down!", "Congrats",
JOptionPane.PLAIN_MESSAGE);
hits++;
hitsGame++;
}
else
{
game[row][column] = 3;
//Displays the current state of the game board
printGame();
JOptionPane.showMessageDialog(null, "Missed... Try again!",
"Missed", JOptionPane.ERROR_MESSAGE);
misses++;
missesGame++;
}
}
//Displays the number fo the game you just played
//the board of that game, the number of hits and the misses
public void print()
{
System.out.println("\nGame #" + games);
for (int[] game1 : game) {
for (int j = 0; j < game1.length; j++) {
System.out.print("\t" + game1[j]);
}
System.out.println();
}
System.out.println("\tHits \tMisses");
System.out.println("\t" + hitsGame + "\t" + missesGame + "\n");
}
//Displays the current state of the board throughout the life of the game,
//and the hits and misses so far.
public void printGame()
{
System.out.println(" Current Board Status");
System.out.println("======================");
for (int[] game1 : game) {
for (int j = 0; j < game1.length; j++) {
if (game1[j] == 1) {
System.out.print("\t0");
} else {
System.out.print("\t" + game1[j]);
}
}
System.out.println();
}
System.out.println("\tHits:" + "\tMisses:");
System.out.println("\t" + hitsGame + "\t" + missesGame);
}
//Displays the statistics at the end of the game.
public void stats()
{
System.out.println("\nHope you had fun! Here are your stats for"
+ " this round of play:");
System.out.println("You played " + games + " game(s).");
System.out.println("Throughout the game(s)you hit " + hits + " of " + (games*4)
+ " possible targets.");
System.out.println("Better luck next time!");
}
//Resets the hits, misses, and record of games played
public void reset()
{
hits = 0;
misses = 0;
games = 0;
}
}