Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Assignment 2:
- * File: Game.java .
- * Ho va ten: Dao Thuy Ngan
- * Ma so SV: 0902 0354
- * Lop: k54cc.2
- * Email: ngandt_54@vnu.edu.vn
- */
- import java.util.Scanner;
- import java.io.*;
- public class Game {
- private int m, n; // kich thuoc ban do bau troi mxn
- private Plane planes[] = new Plane[100]; // mang chua toi da 100 may bay
- private int count = 0; // đem so may bay
- private char map[][]; // ban đo hien thi trong giao dien nguoi dung
- private int logMap[][]; // ban đo in ra tap tin nhat trinh
- // khoi tao game
- public Game( String text, String log ) {
- Scanner input = null;
- PrintWriter output = null;
- try {
- input = new Scanner( new File( text ) );
- m = input.nextInt(); // so dong
- n = input.nextInt(); // so cot
- map = new char[m][n];
- logMap = new int[m][n];
- // nhap toa do may bay
- while( input.hasNext() ) {
- String type = input.next(); // loai may bay
- int x = input.nextInt(); // toa do dong cua diem dau may bay
- int y = input.nextInt(); // toa do cot cua diem dau may bay
- String direction = input.next(); // huong bay
- if( type.equals("F") ) // may bay tiem kich
- planes[count] = new Fighter( m, n, x, y, direction );
- else if( type.equals("H") ) // may bay truc thang
- planes[count] = new Helicopter( m, n, x, y, direction );
- else if( type.equals("B") ) // may bay nem bom
- planes[count] = new Bomber( m, n, x, y, direction );
- // tao ban do giao dien va nhat trinh
- for( int i = 0; i < m; i++ )
- for( int j = 0; j < n; j++) {
- map[i][j] = '.';
- if( planes[count].getValue(i, j) != 0 )
- logMap[i][j]++;
- }
- count++;
- }
- input.close();
- // tao file nhat trinh moi cho game
- output = new PrintWriter( new BufferedWriter( new FileWriter( log )));
- output.close();
- }
- catch( IOException e) {
- if( input != null )
- input.close();
- e.printStackTrace();
- System.exit(1);
- }
- }
- // kiem tra toa do ban
- public void check( int x, int y, String log ) {
- // toa do ban nam trong ban do
- if( x >= 0 && x < m && y >= 0 && y < n ) {
- int flag = 0; // co bieu thi ket qua cua luot ban
- for( int k = 0; k < count; k++ ) { // xet cac may bay
- // ban trung
- if( planes[k].getValue( x, y ) != 0 ) {
- flag = 1; // ban trung
- planes[k].setZero( x, y ); // danh dau diem da bi ban trung
- // neu may bay thu k da bi diet thi xoa khoi ban do
- if( planes[k].isKilled() ) {
- deletePlane( planes[k] );
- count--; // so may bay giam 1
- for( int i = k; i < count; i++ ) // dam bao su lien tuc cua mang may bay sau
- planes[i]= planes[i+1];
- k--;
- }
- }
- }
- // thong bao ket qua luot ban
- informResult( x, y, flag, log );
- }
- }
- // thong bao ket qua luot ban
- private void informResult( int x, int y, int flag, String log ) {
- PrintWriter output = null;
- try {
- // mo file nhat trinh
- output = new PrintWriter( new BufferedWriter( new FileWriter( log, true )));
- // thong bao ban trung
- if( flag == 1 ) {
- output.printf( "%d\t%d\thit\n", x, y );
- System.out.println( "Hit!" );
- map[x][y] = 'x';
- logMap[x][y] = 0;
- }
- // thong bao ban truot
- else {
- output.printf( "%d\t%d\tmiss\n", x, y );
- System.out.println( "miss!" );
- if( map[x][y] == '.' )
- map[x][y] = ' ';
- }
- output.close();
- // in ban do ra file nhat trinh
- writeToLog(log);
- }
- catch(IOException e) {
- e.printStackTrace();
- if( output != null )
- output.close();
- System.exit(1);
- }
- }
- // hien thi ban do bau troi
- public void displayMap() {
- for( int i = 0; i < n; i++) {
- for( int j = 0; j < n; j++ )
- System.out.printf( "%c ", map[i][j] );
- System.out.println();
- }
- System.out.println();
- }
- // in ban do ra tap tin nhat trinh
- public void writeToLog( String log ) {
- PrintWriter output = null;
- try {
- // mo file nhat trinh
- output = new PrintWriter( new BufferedWriter( new FileWriter( log, true )));
- for( int i = 0; i < n; i++ ) {
- for( int j = 0; j < n; j++ ) {
- if( logMap[i][j] != 0 ) // diem con phan tu may bay
- output.printf( "%d ", 1 );
- else // diem trong
- output.printf( "%d ", 0 );
- }
- output.println();
- }
- output.close();
- }
- catch(IOException e) {
- e.printStackTrace();
- if( output != null )
- output.close();
- System.exit(1);
- }
- }
- // xoa may bay khoi ban do
- public void deletePlane( Plane plane ) {
- for( int i = 0; i < m; i++ )
- for( int j = 0; j < m; j++ )
- if( plane.getValue(i, j) != 0 ) {
- logMap[i][j]--;
- if( logMap[i][j] == 0 )
- map[i][j] = 'x';
- }
- }
- // kiem tra so may bay con hay het
- public boolean finish() {
- for( int i = 0; i < m; i++ )
- for( int j = 0; j < n; j++ )
- if( logMap[i][j] == 1 )
- return false;
- return true;
- }
- // main
- public static void main( String args[] ) {
- String text = null; // tham so input
- String log = null; // tham so output
- if( args.length == 2 ) {
- text = args[0];
- log = args[1];
- }
- else if( args.length == 1 ) {
- text = args[0];
- log = text + ".log";
- }
- else {
- System.out.println( "Syntax Error!\n" );
- System.exit(1);
- }
- // khoi tao game
- Game game = new Game( text, log );
- // thong bao nhap toa do ban
- Scanner input = new Scanner( System.in );
- while( !game.finish() ) {
- // hien thi ban do
- game.displayMap();
- System.out.print( "Enter shooting coordinates: \n" );
- int x = input.nextInt(); // toa do dong
- int y = input.nextInt(); // toa do cot
- game.check(x, y, log); // kiem tra ket qua ban
- }
- // ket thuc tro choi
- game.displayMap();
- System.out.println( "You won. Congratulations!\n" );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement