-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattleship.java
164 lines (159 loc) · 4.19 KB
/
battleship.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
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
import java.lang.*;
import java.util.*;
import java.io.*;
class battleship{
public int size;
public player Player1;
public player Player2;
public int[] shipsize;
public battleship(int size)
{
this.size = size;
Player1 = new player(size);
Player2 = new player(size);
shipsize = new int[size/2];
}
public void setShipSizes(int maxLength)
{
Random ran = new Random();
for(int i=0; i<shipsize.length; i++)
shipsize[i] = ran.nextInt(maxLength) + 1;
Arrays.sort(shipsize);
}
public void randomGenerate(player Player)
{
int curr = 0;
Random ran = new Random();
for(int i = 0; i<shipsize.length; i++)
{
int success =0;
while(success == 0)
{
int runfurther = 1;
if(curr == 0)
{
int y;
int row = ran.nextInt(size) + 0;
int col = ran.nextInt(size-shipsize[i]) + 0;
for(y = col; y<col+shipsize[i]; y++)
{
if(Player.fixedBoard[row][y] == 1)
runfurther=0;
}
if(y == col+shipsize[i] && runfurther==1)
{
success = 1;
curr = 1-curr;
for(int x = col; x<col+shipsize[i]; x++)
{
Player.fixedBoard[row][x] = 1;
Pair<Integer,Integer> q1 = new Pair<Integer,Integer>(row,x);
Pair<Integer,Integer> q2 = new Pair<Integer,Integer>(row,col);
Pair<Integer,Integer> q3 = new Pair<Integer,Integer>(row,col+shipsize[i]-1);
Pair<Pair<Integer,Integer>,Pair<Integer,Integer>> q4 = new Pair<Pair<Integer,Integer>,Pair<Integer,Integer>>(q2,q3);
Player.targets.put(q1,q4);
}
}
}
else
{
int y;
int col = ran.nextInt(size) + 0;
int row = ran.nextInt(size-shipsize[i]) + 0;
for(y = row; y<row+shipsize[i]; y++)
{
if(Player.fixedBoard[y][col] == 1)
runfurther=0;
}
if(y == row+shipsize[i] && runfurther == 1)
{
success =1;
for(int x = row; x<row+shipsize[i]; x++)
{
Player.fixedBoard[x][col] = 1;
Pair<Integer,Integer> q1 = new Pair<Integer,Integer>(x,col);
Pair<Integer,Integer> q2 = new Pair<Integer,Integer>(row,col);
Pair<Integer,Integer> q3 = new Pair<Integer,Integer>(row+shipsize[i]-1,col);
Pair<Pair<Integer,Integer>,Pair<Integer,Integer>> q4 = new Pair<Pair<Integer,Integer>,Pair<Integer,Integer>>(q2,q3);
Player.targets.put(q1,q4);
}
curr = 1-curr;
}
}
}
}
}
public void setBoards()
{
int[] shipSize = new int[size-2];
Random ran = new Random();
int maxLength = size/2;
setShipSizes(maxLength );
randomGenerate(this.Player1);
//print statements
randomGenerate(this.Player2);
//setShip();
System.out.println("Player1 fixedBoard: ");
Player1.printFixedBoard();
System.out.println("-----------");
System.out.println("Player2 fixedBoard: ");
Player2.printFixedBoard();
System.out.println("-----------");
System.out.println("Game begins");
}
public void playGame()
{
int p1 = 0, p2 = 0;
int curr=0;
int row=0,col=0;
Random ran = new Random();
while( p1!=shipsize.length && p2!=shipsize.length)
{
if(curr == 1)
{
System.out.println("Player2 playing ... Player1 board:");
int l=1;
while(l==1)
{
row = ran.nextInt(size) + 0;
col = ran.nextInt(size) + 0;
if(Player1.getPlayingBoardVal(row,col) == 'o')
l=0;
}
Player1.setPlayingBoard(row,col);
Player1.printPlayingBoard();
p1 = Player1.checkCurrent();
curr =1-curr;
}
else
{
System.out.println("Player1 playing ... Player2 board:");
int l=1;
while(l==1)
{
row = ran.nextInt(size) + 0;
col = ran.nextInt(size) + 0;
if(Player2.getPlayingBoardVal(row,col) == 'o')
l=0;
}
Player2.setPlayingBoard(row,col);
p2=Player2.checkCurrent();
Player2.printPlayingBoard();
curr = 1-curr;
}
}
if(p1>p2)
System.out.println("Player2 won");
else
System.out.println("Player1 won");
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Welcome to Battleship\n" + "Rules:\n1)Unplayed position: o\n2)Target on point: x\n3)Ship found: F\n"+"Enter the size of the board that you want");
int a = s.nextInt();
battleship game = new battleship(a);
game.setBoards();
game.playGame();
}
}