Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA The assignment is to create a text-based, turn-based Space Invaders style g

ID: 3867847 • Letter: J

Question

JAVA

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

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.

Explanation / Answer

The following code does the required task. First initialise an array of 6 by 10 and set all elements to blank character ('')

Then set the layout of ships to 10 in first row, 8 in second row and 6 in third

Place the player in ar[5][4] by default and ask for user input

If the input is f then simply remove the ships of that column else move the player to the entered column.

import java.io.*;

import java.util.*;

class space

{

public static void main(String args[])throws Exception

{int p=4;

Scanner sc=new Scanner(System.in);

char ar[][]=new char[6][10];

for(i=0;i<6;i++)

for(j=0;j<10;j++)

ar[i][j]='';

for(i=0;i<10;i++)

ar[0][i]='S';

for(i=1;i<9;i++)

ar[1][i]='S';

for(i=2;i<8;i++)

ar[2][i]='S';

ar[5][4]='P';

for(i=0;i<6;i++)

{for(j=0;j<10;j++)

System.out.print(ar[i][j]);

System.out.println();

}

String m=""+sc.nextLine();

if(m.length()==3)

{

ar[p]='';

p=Integer.parseInt(m);

ar[5][Integer.parseInt(m)]='P';

}

if(m.length()==1)

{

for(i=0;i<=2;i++)

ar[i][p]='';

}

}

}