Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

TextFighter.java
Wednesday, October 17th, 2007 at 7:21:51am MDT 

  1. /**
  2.   Main class for Text Fighter
  3.   - 20070928, Cheung Ho Yin
  4. */
  5.  
  6. import java.util.Scanner;
  7. import java.util.Random;
  8.  
  9. public class TextFighter {
  10.  
  11.   private static Scanner input = new Scanner( System.in );
  12.   private static Player [] p
  13.     = { new Player(), new Player("Florence"),
  14.       new Player("Janice"), new Player("Justin") };
  15.   private static Random r = new Random();
  16.  
  17.   // Methods
  18.   public static boolean gameOver() {
  19.     if(p[0].isDead()) return true;
  20.     int dead=0;
  21.     for(int i=0; i<p.length; i++){
  22.       if(p[i].isDead()) dead++;
  23.     }
  24.     return (dead>=p.length-1)?true:false;
  25.   }
  26.   // Action taken by a human player
  27.   public static void humanAction( Player player ) {
  28.  
  29.     System.out.printf( "\n%s's command > ", player.getName());
  30.     int command = input.nextInt();
  31.  
  32.     if( command < 0 || command >= p.length ){
  33.       System.out.println("--> Invalid option!");
  34.       return;
  35.     }
  36.     player.attack( p[command] );
  37.   }
  38.   // Action taken by a computer player
  39.   public static void computerAction( Player player ) {
  40.     if(player.isDead()) return; // do nothing if already dead
  41.     int command = r.nextInt()%2+1;
  42.  
  43.     System.out.println();
  44.     System.out.printf( "%s's command > Thinking... /\r", player.getName() ); sleep();
  45.     System.out.printf( "%s's command > Thinking... -\r", player.getName() ); sleep();
  46.     System.out.printf( "%s's command > Thinking... \\\r", player.getName() ); sleep();
  47.     System.out.printf( "%s's command > Thinking... |\r", player.getName() ); sleep();
  48.     System.out.printf( "%s's command > Thinking... /\r", player.getName() ); sleep();
  49.     System.out.printf( "%s's command >              \r", player.getName() );
  50.     System.out.printf( "%s's command > %d\n", player.getName(), command );
  51.     player.attack( p[command] );
  52.   }
  53.  
  54.   private static void sleep() {
  55.     try {
  56.       Thread.sleep(300);
  57.     } catch (InterruptedException e) {
  58.       e.printStackTrace();
  59.     }
  60.   }
  61.  
  62.   public static void printStatus() {
  63.     for(int i=0; i<p.length; i++){
  64.       if(!p[i].isDead()) System.out.printf("[%d] %s (%d)\n", i, p[i].getName(), p[i].getPower() );
  65.     }
  66.   }
  67.  
  68.   // Main program
  69.   public static void main( String [] args ){
  70.  
  71.     int counter=0;
  72.     System.out.print("Java Text Fighter! v20070928\n\n");
  73.  
  74.     System.out.print("Enter your name: ");
  75.     Scanner input = new Scanner(System.in);
  76.     p[0].setName( input.nextLine() );
  77.  
  78.     do {
  79.       // Print heading information
  80.       System.out.println("\n== Round " + (++counter) + " ==");
  81.       printStatus();
  82.       // Human player
  83.       humanAction(p[0]);
  84.       // Computer players
  85.       for(int i=1; i<p.length; i++) { computerAction(p[i]); }
  86.       System.out.println();
  87.  
  88.     } while(!gameOver());
  89.  
  90.     System.out.println("Game over! The survivor(s):");
  91.     printStatus();
  92.  
  93.   }
  94. }

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

worth-right