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

BattleShips.java #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
324 changes: 106 additions & 218 deletions BattleShips.java
Original file line number Diff line number Diff line change
@@ -1,225 +1,113 @@
/** This project will help you get more familiar with arrays.
* You will be recreating the game of battleships.
* A player will place 5 of their ships on a 10 by 10 grid.
* The computer player will deploy five ships on the same grid.
* Once the game starts the player and computer take turns,
* trying to sink each other's ships by guessing the coordinates to "attack".
* The game ends when either the player or computer has no ships left.
* https://courses.edx.org/courses/course-v1:Microsoft+DEV277x+1T2018/courseware/76c11a375a0e495e83ab68121566fb12/8f250da826d7405d8fecf99aca3a5e9a/?child=first
*/

import java.util.*;

public class BattleShips {
public static int numRows = 10;
public static int numCols = 10;
public static int playerShips;
public static int computerShips;
public static String[][] grid = new String[numRows][numCols];
public static int[][] missedGuesses = new int[numRows][numCols];

public static void main(String[] args){
System.out.println("**** Welcome to Battle Ships game ****");
System.out.println("Right now, sea is empty\n");

//Step 1 – Create the ocean map
createOceanMap();

//Step 2 – Deploy player’s ships
deployPlayerShips();

//Step 3 - Deploy computer's ships
deployComputerShips();

//Step 4 Battle
do {
Battle();
}while(BattleShips.playerShips != 0 && BattleShips.computerShips != 0);

//Step 5 - Game over
gameOver();
}

public static void createOceanMap(){
//First section of Ocean Map
System.out.print(" ");
for(int i = 0; i < numCols; i++)
System.out.print(i);
System.out.println();

//Middle section of Ocean Map
for(int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = " ";
if (j == 0)
System.out.print(i + "|" + grid[i][j]);
else if (j == grid[i].length - 1)
System.out.print(grid[i][j] + "|" + i);
else
System.out.print(grid[i][j]);
}
System.out.println();
}

//Last section of Ocean Map
System.out.print(" ");
for(int i = 0; i < numCols; i++)
System.out.print(i);
System.out.println();
}

public static void deployPlayerShips(){
Scanner input = new Scanner(System.in);

System.out.println("\nDeploy your ships:");
//Deploying five ships for player
BattleShips.playerShips = 5;
for (int i = 1; i <= BattleShips.playerShips; ) {
System.out.print("Enter X coordinate for your " + i + " ship: ");
int x = input.nextInt();
System.out.print("Enter Y coordinate for your " + i + " ship: ");
int y = input.nextInt();

if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (grid[x][y] == " "))
{
grid[x][y] = "@";
i++;
}
else if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && grid[x][y] == "@")
System.out.println("You can't place two or more ships on the same location");
else if((x < 0 || x >= numRows) || (y < 0 || y >= numCols))
System.out.println("You can't place ships outside the " + numRows + " by " + numCols + " grid");
}
printOceanMap();
import java.util.ArrayList;
import java.util.Scanner;

class Main {
public static void main(String[] args) {

//creates multiple boards

String[][][]userBoard = new String[3][10][10];
String[][][]boardAI = new String[3][10][10];
intFillBoard(userBoard,0);
intFillBoard(boardAI,1);

/*
5 aircraft carrier - a
4 battleship - b
3 cruser - c
3 submarine - s
2 destroyer - d
*/

//creates history of moves made by user and the AI
ArrayList<Move> moveHistAI= new ArrayList<Move>();
ArrayList<String> usersShips= new ArrayList<String>();
ArrayList<String> aiShips= new ArrayList<String>();
shipLeftFill(usersShips);
shipLeftFill(aiShips);
Scanner enter = new Scanner(System.in);
space();


System.out.print("\n\n\t\u274CWelcome to battleship\u274C \n\t\tBy Michael F\n\n\n\n\n\tThis is your battleship board. Let's place your ships.\n\n");
printBoard(userBoard,0);
System.out.print("\n\nPRESS ENTER TO PLAY\n");
String wait = enter.nextLine();

//hide ships in hiddenAI board
//hides user ships

CompInputShips.autoFill(boardAI);
UserInputShips.userFill(userBoard);
int hit=-1;
boolean gameWon=false;

for(int r = 0;r<10;r++){
for(int c = 0; c<10;c++){
boardAI[2][r][c]=boardAI[0][r][c];
userBoard[2][r][c]=userBoard[1][r][c];
}
}

public static void deployComputerShips(){
System.out.println("\nComputer is deploying ships");
//Deploying five ships for computer
BattleShips.computerShips = 5;
for (int i = 1; i <= BattleShips.computerShips; ) {
int x = (int)(Math.random() * 10);
int y = (int)(Math.random() * 10);

if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (grid[x][y] == " "))
{
grid[x][y] = "x";
System.out.println(i + ". ship DEPLOYED");
i++;
}

while(!gameWon){
hit=UserGuess.UserInput(userBoard,boardAI,usersShips,aiShips,moveHistAI,hit);
if(aiShips.size()==0){
gameWon= true;
}
if(!gameWon){
AI.aiMove(userBoard,boardAI,usersShips,aiShips,moveHistAI);
if(usersShips.size()==0){
gameWon= true;
}
printOceanMap();
}
}

public static void Battle(){
playerTurn();
computerTurn();

printOceanMap();

System.out.println();
System.out.println("Your ships: " + BattleShips.playerShips + " | Computer ships: " + BattleShips.computerShips);
System.out.println();
}


//Fills board with "-" for ships to be placed on
public static void intFillBoard(String[][][] grid, int e){
for(int r = 0;r<10;r++){
for(int c = 0; c<10;c++){
grid [e][r][c] = "-";
}
}

public static void playerTurn(){
System.out.println("\nYOUR TURN");
int x = -1, y = -1;
do {
Scanner input = new Scanner(System.in);
System.out.print("Enter X coordinate: ");
x = input.nextInt();
System.out.print("Enter Y coordinate: ");
y = input.nextInt();

if ((x >= 0 && x < numRows) && (y >= 0 && y < numCols)) //valid guess
{
if (grid[x][y] == "x") //if computer ship is already there; computer loses ship
{
System.out.println("Boom! You sunk the ship!");
grid[x][y] = "!"; //Hit mark
--BattleShips.computerShips;
}
else if (grid[x][y] == "@") {
System.out.println("Oh no, you sunk your own ship :(");
grid[x][y] = "x";
--BattleShips.playerShips;
++BattleShips.computerShips;
}
else if (grid[x][y] == " ") {
System.out.println("Sorry, you missed");
grid[x][y] = "-";
}
}
else if ((x < 0 || x >= numRows) || (y < 0 || y >= numCols)) //invalid guess
System.out.println("You can't place ships outside the " + numRows + " by " + numCols + " grid");
}while((x < 0 || x >= numRows) || (y < 0 || y >= numCols)); //keep re-prompting till valid guess
}

public static void computerTurn(){
System.out.println("\nCOMPUTER'S TURN");
//Guess co-ordinates
int x = -1, y = -1;
do {
x = (int)(Math.random() * 10);
y = (int)(Math.random() * 10);

if ((x >= 0 && x < numRows) && (y >= 0 && y < numCols)) //valid guess
{
if (grid[x][y] == "@") //if player ship is already there; player loses ship
{
System.out.println("The Computer sunk one of your ships!");
grid[x][y] = "x";
--BattleShips.playerShips;
++BattleShips.computerShips;
}
else if (grid[x][y] == "x") {
System.out.println("The Computer sunk one of its own ships");
grid[x][y] = "!";
}
else if (grid[x][y] == " ") {
System.out.println("Computer missed");
//Saving missed guesses for computer
if(missedGuesses[x][y] != 1)
missedGuesses[x][y] = 1;
}
}
}while((x < 0 || x >= numRows) || (y < 0 || y >= numCols)); //keep re-prompting till valid guess
}

//Prints coordinates on the side of the board
public static void printBoard(String[][][] grid, int a){
for(int r = 0;r<10;r++){
System.out.print(10-r +" ");
if(r!=0){
System.out.print(" ");
}
for(int c = 0; c<10;c++){
System.out.print(grid[a][r][c] + " ");
}
System.out.print("\n");
}

public static void gameOver(){
System.out.println("Your ships: " + BattleShips.playerShips + " | Computer ships: " + BattleShips.computerShips);
if(BattleShips.playerShips > 0 && BattleShips.computerShips <= 0)
System.out.println("Hooray! You won the battle :)");
else
System.out.println("Sorry, you lost the battle");
System.out.println();
}

public static void printOceanMap(){
System.out.println();
//First section of Ocean Map
System.out.print(" ");
for(int i = 0; i < numCols; i++)
System.out.print(i);
System.out.println();

//Middle section of Ocean Map
for(int x = 0; x < grid.length; x++) {
System.out.print(x + "|");

for (int y = 0; y < grid[x].length; y++){
System.out.print(grid[x][y]);
}

System.out.println("|" + x);
}

//Last section of Ocean Map
System.out.print(" ");
for(int i = 0; i < numCols; i++)
System.out.print(i);
System.out.println();
System.out.print("\n 1 2 3 4 5 6 7 8 9 10");
}

//Method for spacing screen out
public static void space(){
for (int i=1;i<=54;i++){
System.out.print("\n");
}
}
//fills ship list
public static void shipLeftFill(ArrayList<String> list){
list.add("a");
list.add("b");
list.add("c");
list.add("s");
list.add("d");
}

//Returns coordinates of grid since indexes are +1
public static int gTOaX(int graphX){
return graphX-1;
}
public static int gTOaY(int graphY){
return 10-graphY;
}
}