Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
IO 7.71 KB | None | 0 0
  1. /** Assignment 2:
  2.  *  File: Game.java .
  3.  *  Ho va ten: Dao Thuy Ngan
  4.  *  Ma so SV: 0902 0354
  5.  *  Lop: k54cc.2
  6.  *  Email: ngandt_54@vnu.edu.vn
  7.  */
  8.  
  9. import java.util.Scanner;
  10. import java.io.*;
  11.  
  12. public class Game {
  13.     private int m, n; // kich thuoc ban do bau troi  mxn
  14.     private Plane planes[] = new Plane[100]; // mang chua toi da 100 may bay
  15.     private int count = 0; // đem so may bay
  16.     private char map[][]; // ban đo hien thi trong giao dien nguoi dung
  17.     private int logMap[][]; // ban đo in ra tap tin nhat trinh
  18.        
  19.     // khoi tao game
  20.     public Game( String text, String log ) {
  21.        
  22.         Scanner input = null;
  23.         PrintWriter output = null;
  24.         try {
  25.             input = new Scanner( new File( text ) );
  26.             m = input.nextInt(); // so dong
  27.             n = input.nextInt(); // so cot    
  28.             map = new char[m][n];
  29.             logMap = new int[m][n];
  30.            
  31.             // nhap toa do may bay
  32.             while( input.hasNext() ) {
  33.                 String type = input.next(); // loai may bay
  34.                 int x = input.nextInt(); // toa do dong cua diem dau may bay
  35.                 int y = input.nextInt(); // toa do cot cua diem dau may bay
  36.                 String direction = input.next(); // huong bay
  37.                
  38.                 if( type.equals("F") ) // may bay tiem kich
  39.                     planes[count] = new Fighter( m, n, x, y, direction );
  40.                 else if( type.equals("H") ) // may bay truc thang
  41.                     planes[count] = new Helicopter( m, n, x, y, direction );
  42.                 else if( type.equals("B") ) // may bay nem bom
  43.                     planes[count] = new Bomber( m, n, x, y, direction );
  44.                
  45.                 // tao ban do giao dien va nhat trinh
  46.                 for( int i = 0; i < m; i++ )
  47.                     for( int j = 0; j < n; j++) {
  48.                         map[i][j] = '.';
  49.                         if( planes[count].getValue(i, j) != 0 )
  50.                             logMap[i][j]++;
  51.                     }
  52.                        
  53.                 count++;
  54.             }        
  55.             input.close();
  56.                        
  57.             // tao file nhat trinh moi cho game
  58.             output = new PrintWriter( new BufferedWriter( new FileWriter( log )));
  59.             output.close();
  60.         }
  61.         catch( IOException e) {
  62.             if( input != null )
  63.                 input.close();
  64.             e.printStackTrace();
  65.             System.exit(1);
  66.         }    
  67.     }
  68.    
  69.     // kiem tra toa do ban
  70.     public void check( int x, int y, String log ) {
  71.        
  72.         // toa do ban nam trong ban do
  73.         if( x >= 0 && x < m && y >= 0 && y < n ) {    
  74.             int flag = 0; // co bieu thi ket qua cua luot ban
  75.             for( int k = 0; k < count; k++ ) { // xet cac may bay
  76.            
  77.                 // ban trung
  78.                 if( planes[k].getValue( x, y ) != 0 ) {
  79.                     flag = 1; // ban trung
  80.                     planes[k].setZero( x, y ); // danh dau diem da bi ban trung
  81.                    
  82.                     // neu may bay thu k da bi diet thi xoa khoi ban do
  83.                     if( planes[k].isKilled() ) {
  84.                         deletePlane( planes[k] );
  85.                         count--; // so may bay giam 1
  86.                         for( int i = k; i < count; i++ ) // dam bao su lien tuc cua mang may bay sau
  87.                             planes[i]= planes[i+1];
  88.                         k--;
  89.                     }
  90.                 }    
  91.             }
  92.            
  93.             // thong bao ket qua luot ban
  94.             informResult( x, y, flag, log );    
  95.         }
  96.     }
  97.    
  98.     // thong bao ket qua luot ban
  99.     private void informResult( int x, int y, int flag, String log ) {
  100.    
  101.         PrintWriter output = null;
  102.         try {
  103.             // mo file nhat trinh
  104.             output = new PrintWriter( new BufferedWriter( new FileWriter( log, true )));
  105.            
  106.             // thong bao ban trung
  107.             if( flag == 1 ) {
  108.                 output.printf( "%d\t%d\thit\n", x, y );
  109.                 System.out.println( "Hit!" );
  110.                 map[x][y] = 'x';
  111.                 logMap[x][y] = 0;
  112.             }
  113.                
  114.             // thong bao ban truot
  115.             else {
  116.                 output.printf( "%d\t%d\tmiss\n", x, y );
  117.                 System.out.println( "miss!" );
  118.                 if( map[x][y] == '.' )
  119.                     map[x][y] = ' ';                        
  120.             }
  121.             output.close();
  122.            
  123.             // in ban do ra file nhat trinh
  124.             writeToLog(log);
  125.         }
  126.         catch(IOException e) {
  127.             e.printStackTrace();
  128.             if( output != null )
  129.                 output.close();
  130.             System.exit(1);
  131.         }
  132.     }
  133.    
  134.     // hien thi ban do bau troi
  135.     public void displayMap() {
  136.         for( int i = 0; i < n; i++) {
  137.             for( int j = 0; j < n; j++ )
  138.                 System.out.printf( "%c ", map[i][j] );
  139.             System.out.println();
  140.         }
  141.         System.out.println();
  142.     }
  143.    
  144.     // in ban do ra tap tin nhat trinh
  145.     public void writeToLog( String log ) {
  146.         PrintWriter output = null;
  147.         try {
  148.             // mo file nhat trinh
  149.             output = new PrintWriter( new BufferedWriter( new FileWriter( log, true )));
  150.            
  151.             for( int i = 0; i < n; i++ ) {
  152.                 for( int j = 0; j < n; j++ ) {
  153.                     if( logMap[i][j] != 0 ) // diem con phan tu may bay
  154.                         output.printf( "%d ", 1 );
  155.                     else // diem trong
  156.                         output.printf( "%d ", 0 );
  157.                 }
  158.                 output.println();
  159.             }
  160.             output.close();
  161.         }
  162.         catch(IOException e) {
  163.             e.printStackTrace();
  164.             if( output != null )
  165.                 output.close();
  166.             System.exit(1);
  167.         }
  168.     }
  169.    
  170.     // xoa may bay khoi ban do
  171.     public void deletePlane( Plane plane ) {
  172.         for( int i = 0; i < m; i++ )
  173.             for( int j = 0; j < m; j++ )            
  174.                 if( plane.getValue(i, j) != 0 ) {
  175.                     logMap[i][j]--;
  176.                     if( logMap[i][j] == 0 )
  177.                         map[i][j] = 'x';
  178.                 }
  179.     }
  180.    
  181.     // kiem tra so may bay con hay het
  182.     public boolean finish() {
  183.         for( int i = 0; i < m; i++ )
  184.             for( int j = 0; j < n; j++ )
  185.                 if( logMap[i][j] == 1 )
  186.                     return false;
  187.         return true;
  188.     }
  189.    
  190.     // main
  191.     public static void main( String args[] ) {
  192.    
  193.         String text = null; // tham so input
  194.         String log = null; // tham so output    
  195.         if( args.length == 2 ) {
  196.             text = args[0];
  197.             log = args[1];
  198.         }
  199.         else if( args.length == 1 ) {
  200.             text = args[0];
  201.             log = text + ".log";
  202.         }
  203.         else {
  204.             System.out.println( "Syntax Error!\n" );
  205.             System.exit(1);
  206.         }
  207.  
  208.         // khoi tao game
  209.         Game game = new Game( text, log );
  210.        
  211.         // thong bao nhap toa do ban
  212.         Scanner input = new Scanner( System.in );
  213.         while( !game.finish() ) {    
  214.             // hien thi ban do
  215.             game.displayMap();
  216.             System.out.print( "Enter shooting coordinates: \n" );
  217.             int x = input.nextInt(); // toa do dong
  218.             int y = input.nextInt(); // toa do cot
  219.             game.check(x, y, log); // kiem tra ket qua ban
  220.         }
  221.        
  222.         // ket thuc tro choi        
  223.         game.displayMap();
  224.         System.out.println( "You won. Congratulations!\n" );
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement