forked from sanyathisside/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Battleships.java
73 lines (63 loc) · 1.81 KB
/
Battleships.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
import java.util.Scanner;
public class BattleShips
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
String board[][]=new String[5][5];
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
board[i][j]="O";
}
}
int turn=0,guessRow=5,guessCol=5;
int row=(int)(Math.round(Math.random()*4));
int col=(int)(Math.round(Math.random()*4));
System.out.println();
System.out.println();
while(guessRow!=row || guessCol!=col)
{
System.out.println("\f");
System.out.println("BATTLESHIPS!");
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
System.out.print(board[i][j]+" ");
}
System.out.println();
}
System.out.println("Turn: "+ ++turn);
System.out.println("Guess a row (from 0 to 4): ");
guessRow=s.nextInt();
System.out.println("Guess a column (from 0 to 4): ");
guessCol=s.nextInt();
if(row==guessRow && col==guessCol)
{
board[row][col]="#";
System.out.println("You've sunk my ship!");
break;
}
else if(guessRow>4 || guessCol>4)
{
System.out.println("Coordinates are not in the ocean");
}
else
{
System.out.println("You've missed my ship!");
board[guessRow][guessCol]="X";
}
}
board[row][col]="#";
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
System.out.print(board[i][j]+" ");
}
System.out.println();
}
}
}