From 05e14983cfef1a2465f3aae13ae99170888ec9f0 Mon Sep 17 00:00:00 2001 From: maxneedspats Date: Sun, 6 Mar 2022 21:43:08 -0600 Subject: [PATCH] Seems to be working perfectly, only 50% on grader though. --- .idea/vcs.xml | 6 +++++ src/Board.java | 71 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..c8397c9 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Board.java b/src/Board.java index 048001b..2592b35 100644 --- a/src/Board.java +++ b/src/Board.java @@ -13,7 +13,7 @@ public class Board{ public String toString(){ String toReturn = ""; - for (int i = 0; i < squares[0].length; i++){ + for (int i = 0; i < 10; i++){ for (int c = 0; c < 10; c++){ // C++ :) toReturn += squares[i][c] + " "; } @@ -23,42 +23,73 @@ public class Board{ } public boolean addShip(int row, int col, int len, boolean horizontal){ - if (len > 9 || len < 0 || row > 9 || row < 0 || col > 9 || col < 0)return false; + if (len > 10 || len < 0 || row > 9 || row < 0 || col > 9 || col < 0)return false; if (horizontal){ - if (col + len > 9){ + if (col + len > 10){ + return false; + } + for (int i = 0; i < squares[0].length; i++){ + for (int c = 0; c < 10; c++){ // C++ :) + if (c == row && i >= col && i <= col + len){ + if (squares[c][i].equals("x") || squares[c][i].equals("b"))return false; + squares[c][i] = "b"; + } + } + } + } else { + if (row + len > 10){ return false; } for (int i = 0; i < squares[0].length; i++){ for (int c = 0; c < 10; c++){ // C++ :) if (i == col && c >= row && c <= row + len){ - squares[i][c] = "b"; - } - } - } - } else { - if (row + len > 9){ - return false; - } - for (int i = 0; i < squares[0].length; i++){ - for (int c = 0; c < 10; c++){ // C++ :) - if (c == row && i >= col && i <= row + col){ - squares[i][c] = "b"; + if (squares[c][i].equals("x") || squares[c][i].equals("b"))return false; + squares[c][i] = "b"; } } } } - } - - public boolean foundShip(int len){ return true; } + public boolean foundShip(int len){ + for (String[] square : squares) { + int bInColumn = 0; + for (int c = 0; c < squares[0].length; c++) { + if (square[c].equals("b")) bInColumn++; + if (bInColumn == len && !(square[c + 1].equals("b"))) return true; + } + } + for (int i = 0; i < squares[0].length; i++){ + int bInColumn = 0; + for (String[] square : squares) { + if (square[i].equals("b")) bInColumn++;// May need to switch this c and i, not sure + if (bInColumn == len && !(square[i + 1].equals("b"))) return true; + } + } + return false; + } + public int shoot(int row, int col){ + if (row > 9 || row < 0 || col > 9 || col < 0)return -1; + if (squares[row][col].equals("-")){ + squares[row][col] = "m"; + return 0; + } + if (squares[row][col].equals("x") || squares[row][col].equals("m"))return 2; + if (squares[row][col].equals("b")){ + squares[row][col] = "x"; + return 1; + } return 0; } public boolean gameOver(){ - return false; + for (String[] arr : squares){ + for (String e : arr){ + if (e.equals("b"))return false; + } + } + return true; } - }