Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Ship {
- Position p;
- int size;
- boolean o; //true if ship is horizontal
- boolean[] destroyed;
- int totalDestroyed;
- public boolean isDestroyed() {
- return totalDestroyed == size;
- }
- public boolean destroyOneCoordinate(Position guess) {
- System.out.println(guess.r + "," + guess.c);
- if (o) {
- if (guess.r != p.r) {
- return false;
- }
- if (guess.c - p.c > size) {
- return false;
- }
- if (destroyed[guess.c - p.c]) {
- return false;
- }
- destroyed[guess.c - p.c] = true;
- totalDestroyed++;
- System.out.println("destroyed" + guess.r + "," + guess.c);
- return true;
- }
- else {
- if (guess.c != p.c) {
- return false;
- }
- if (guess.r - p.r > size) {
- return false;
- }
- if (destroyed[guess.r - p.r]) {
- return false;
- }
- System.out.println("destroyed" + guess.r + "," + guess.c);
- destroyed[guess.c - p.c] = true;
- totalDestroyed++;
- return true;
- }
- }
- Ship(Position p, int size, boolean o) {
- this.p = p;
- this.o = o;
- this.size = size;
- destroyed = new boolean[size];
- totalDestroyed = 0;
- }
- }
- class Position {
- int r;
- int c;
- Position(int r, int c) {
- /*if (r <0 || c <0 || r > 9 || c >9) {
- throw new Exception("position not defined");
- }*/
- this.r = r;
- this.c = c;
- }
- }
- class Solution {
- public static void main(String[] args) {
- Ship a = new Ship(new Position(0,0), 2, true);
- Ship b = new Ship(new Position(1,0), 2, true);
- int turn = 0; // tell me who's turn
- Position[] guesses = new Position[5];
- guesses[0] = new Position(0, 0);
- guesses[1] = new Position(0, 0);
- guesses[2] = new Position(0, 1);
- guesses[3] = new Position(0, 1);
- guesses[4] = new Position(0, 2);
- int idx = 0;
- while (!a.isDestroyed() && !b.isDestroyed()) {
- idx ++;
- if (idx > 4) {
- break;
- }
- if (turn == 0) {
- a.destroyOneCoordinate(guesses[idx]);
- } else {
- b.destroyOneCoordinate(guesses[idx]);
- }
- turn = (turn + 1)%2;
- }
- if (a.isDestroyed()) {
- System.out.println("A destroyed");
- }
- if (b.isDestroyed()) {
- System.out.println("B destroyed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement