Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit8 #15

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions src/main/java/Unit8/Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package Unit8;

// Have to get rid of package statement

public class Board {

private String[][] squares;

public Board() {
squares = new String[10][10];
for (int r = 0; r < squares.length; r++) {
for (int c = 0; c < squares[r].length; c++) {
squares[r][c] = "-";
}
}
}

public String toString() {
String boardString = "";

for (int r = 0; r < squares.length; r++) {
for (int c = 0; c < squares[r].length; c++) {
boardString += squares[r][c] + " ";
}
boardString += "\n";
}

return boardString;
}

public boolean addShip(int row, int col, int len, boolean horizontal) {

if (!isCoordinatesValid(row, col, len, horizontal))
return false;

if (isShipInWay(row, col, len, horizontal))
return false;

placeShip(row, col, len, horizontal);

return true;
}

private boolean isCoordinatesValid(int row, int col, int len, boolean horizontal) {
boolean validCoordinates = (row >= 0 && row < squares.length) &&
(col >= 0 && col < squares[row].length);

if (horizontal) {
validCoordinates = validCoordinates && col + len <= squares[row].length;
} else {
validCoordinates = validCoordinates && row + len <= squares.length;
}

return validCoordinates;

}

private boolean isShipInWay(int row, int col, int len, boolean horizontal) {
if (horizontal) {
for (int c = col; c < col + len; c++) {
if (squares[row][c].equals("b")) {
return true;
}
}
} else {
for (int r = row; r < row + len; r++) {
if (squares[r][col].equals("b")) {
return true;
}
}
}

return false;
}

private void placeShip(int row, int col, int len, boolean horizontal) {
if (horizontal) {
for (int c = col; c < col + len; c++) {
squares[row][c] = "b";
}
} else {
for (int r = row; r < row + len; r++) {
squares[r][col] = "b";
}
}
}

public boolean foundShip(int len) {
// Searching row-by-row
for (int r = 0; r < squares.length; r++) {
int count = 0;
for (int c = 0; c < squares[r].length; c++) {
if (squares[r][c].equals("b")) {
count++;
} else {
if (count == len) {
return true;
}
count = 0;
}
}
if (count == len) {
return true;
}
}

// Searching column by column
for (int c = 0; c < squares[0].length; c++) {
int count = 0;
for (int r = 0; r < squares.length; r++) {
if (squares[r][c].equals("b")) {
count++;
} else {
if (count == len) {
return true;
}
count = 0;
}
}
if (count == len) {
return true;
}
}

return false;
}

public int shoot(int row, int col) {

if (!isCoordinatesValid(row, col, 0, false))
return -1;

if (squares[row][col].equals("-")) {
squares[row][col] = "m";
return 0;
} else if (squares[row][col].equals("b")) {
squares[row][col] = "x";
return 1;
}

return 2;
}

public boolean gameOver() {
if (foundShip(1)) {
return false;
}

return true;
}

}
68 changes: 68 additions & 0 deletions src/main/java/Unit8/TemperatureMonth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package Unit8;

// Have to get rid of package statement

public class TemperatureMonth {

private double[][] temperatures;

public TemperatureMonth(double[][] t) {
temperatures = t;
}

public double[] getMaxTempWeekly() {
double[] week = new double[temperatures.length];

for (int w = 0; w < week.length; w++) {
double max = temperatures[w][0];
for (int d = 1; d < temperatures[w].length; d++) {
if (temperatures[w][d] > max) {
max = temperatures[w][d];
}
}
week[w] = max;
}

return week;
}

public double[] getMinTempWeekly() {
double[] week = new double[temperatures.length];

for (int w = 0; w < week.length; w++) {
double min = temperatures[w][0];
for (int d = 1; d < temperatures[w].length; d++) {
if (temperatures[w][d] < min) {
min = temperatures[w][d];
}
}
week[w] = min;
}

return week;
}

public double getRange() {
double[] mins = getMinTempWeekly();
double[] maxes = getMaxTempWeekly();

double min = mins[0];
double max = maxes[0];

for (int w = 1; w < mins.length && w < maxes.length; w++) {
if (mins[w] < min) {
min = mins[w];
}
if (maxes[w] > max) {
max = maxes[w];
}
}

return max - min;
}

public double getCertainTemp(int week, int day) {
return temperatures[week][day];
}

}
16 changes: 16 additions & 0 deletions src/main/java/Unit8/U8_L1_Activity_One.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package Unit8;

// Have to get rid of package statement

public class U8_L1_Activity_One {

public static int sumOfDiag(int[][] arr) {
int sum = 0;
for (int i = 0; i < arr.length && i < arr[i].length; i++) {
sum += arr[i][i];
}

return sum;
}

}
18 changes: 18 additions & 0 deletions src/main/java/Unit8/U8_L1_Activity_Two.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Unit8;

// Have to get rid of package statement

public class U8_L1_Activity_Two {

public static int[][] productTable(int rows, int cols) {
int[][] table = new int[rows][cols];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
table[i][j] = i * j;
}
}

return table;
}
}
80 changes: 80 additions & 0 deletions src/test/java/Unit8/Battleship.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package Unit8;

// Have to get rid of package statement

import java.util.Scanner;

public class Battleship {
public static void main(String[] args) {
// Set up board and print welcome
Board b = new Board();
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to Battleship!\n");
boolean addNew = true;

while (addNew) {
System.out.println("Type \"a\" to add new ship, \"b\" to see the board, \"p\" to play or \"q\" to quit.");
String ans = scan.nextLine();
if (ans.toLowerCase().equals("q"))
return;
if (ans.toLowerCase().equals("a")) {
// Get parameters for new ship
System.out.println("Starting in which row?");
int r = scan.nextInt();
System.out.println("Starting in which column?");
int c = scan.nextInt();
System.out.println("How long?");
int l = scan.nextInt();
scan.nextLine();
System.out.println("Horizontal (h) or vertical (v)?");
String d = scan.nextLine();
boolean h = (d.toLowerCase().equals("h"));

// Call addShip method and return message based on true/false value
if (b.addShip(r, c, l, h)) {
System.out.println("\nNew ship added!\n");
} else
System.out.println("\nCan't put a ship there!\n");
} else if (ans.toLowerCase().equals("b"))
System.out.println("\n" + b + "\n");
else if (ans.toLowerCase().equals("p")) {
if (b.foundShip(3) && b.foundShip(4)) {
addNew = false;
System.out.println("\nOk, let's play!\n");
} else
System.out.println("\nYou need ships of length 3 and 4 to play!\n");
}
}

// As long as ships remain, play game
while (!b.gameOver()) {
System.out.println("Press \"s\" to shoot at a square, \"b\" to see the board, \"q\" to quit.");
String ans = scan.nextLine();
if (ans.toLowerCase().equals("q"))
return;
else if (ans.toLowerCase().equals("s")) {
// Get row and column to shoot
System.out.println("Input row.");
int r = scan.nextInt();
System.out.println("Input column.");
int c = scan.nextInt();

// Perform shot and store result
int result = b.shoot(r, c);

// Choose message based on result
if (result == 1)
System.out.println("\nHit!\n");
else if (result == 0)
System.out.println("\nMiss!\n");
else if (result == 2)
System.out.println("\nYou already tried that.\n");
else if (result == -1)
System.out.println("\nInvalid coordinates.\n");
scan.nextLine();
} else if (ans.toLowerCase().equals("b"))
System.out.println("\n" + b + "\n");
}
System.out.println("Game over!");
}
}
Loading
Loading