Seems to be working perfectly, only 50% on grader though.

This commit is contained in:
maxneedspats 2022-03-06 21:43:08 -06:00
parent 889975427e
commit 05e14983cf
2 changed files with 57 additions and 20 deletions

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View file

@ -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;
}
}