Question & Answer: The assignment is to create a text-based, turn-based Space Invaders style game……

JAVA

The assignment is to create a text-based, turn-based Space Invaders style game.

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: The assignment is to create a text-based, turn-based Space Invaders style game……
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

The game board is 6 wide by 10 high, though really it could be wider but that would make the game harder.

You type ‘f’ into the console to fire. This destroys all enemy ships in the column that the player is currently in.

To move you type ‘m’ followed by the number of the column to move to that column. For example if you want to move to the second column you type in “m 2”.

Each round the space invaders move down toward the ground. For each one that hits the ground you lose a life.

JAVA LANGUAGE

Expert Answer

 

SpaceInvader.java:

import java.util.Random;

import java.util.Scanner;

public class SpaceInvader {

static int ROWS = 10, COLS = 6;

static Random random = new Random();

static boolean ships[][] = new boolean[ROWS][COLS];

static int currentCol = random.nextInt(COLS);

static Scanner scanner = new Scanner(System.in);

public static void shiftDown() {

// loop for all but last row

for(int i=ROWS-2; i>=0; i–) {

for(int j=0; j<COLS; j++) {

// copy to next row

ships[i + 1][j] = ships[i][j];

}

}

// clear first row

for(int i=0; i<COLS; i++) {

ships[0][i] = false;

}

// next ship position,

int shipIndex = random.nextInt(COLS);

if(shipIndex < COLS) {

ships[0][shipIndex] = true;

}

}

public static void fireOnColumn(int col) {

for(int i=0; i<ROWS; i++) {

ships[i][col] = false;

}

}

public static void printShips() {

System.out.print(“n “);

for(int j=0; j<COLS; j++) {

System.out.print((j+1) + ” “);

}

System.out.println(“n=============================”);

for(int i=0; i<ROWS; i++) {

System.out.printf(“%2s |”, (i+1));

for(int j=0; j<COLS; j++) {

if(ships[i][j]) {

System.out.print(” X |”);

} else {

System.out.print(” |”);

}

}

System.out.println();

}

System.out.println(“=============================”);

System.out.print(” “);

for(int j=0; j<COLS; j++) {

if(j == currentCol) {

System.out.print(“Pl “);

}

System.out.print(” “);

}

System.out.println();

}

public static String getChoice() {

System.out.print(“Enter your choice(‘f’ to fire, ‘m <Col no>’ to move: “);

return scanner.nextLine();

}

private static boolean gameOver() {

boolean gameOver = false;

// check last row, if some ship has touched already

for(int i=0; i<COLS; i++) {

if(ships[ROWS-1][i]) {

gameOver = true;

break;

}

}

return gameOver;

}

public static void main(String[] args) {

shiftDown();

printShips();

while(true) {

String choice = getChoice();

if(choice.equalsIgnoreCase(“f”)) {

fireOnColumn(currentCol);

shiftDown();

printShips();

} else if(choice.startsWith(“m”)) {

String tokens[] = choice.split(” “);

int col = Integer.parseInt(tokens[1]) – 1;

if((col < 0) || (col >= COLS)) {

System.out.println(“invalid column index. Try again.”);

} else {

if(gameOver()) {

System.out.println(“Game Over.”);

break;

}

currentCol = col;

shiftDown();

printShips();

}

} else {

System.out.println(“Invalid choice. Try again.”);

}

}

System.out.println(“Better Luck next time!”);

}

}

Question & Answer: The assignment is to create a text-based, turn-based Space Invaders style game...... 1

Still stressed from student homework?
Get quality assistance from academic writers!