Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By HelloWorld1234

Thread: My first game! :D

  1. #1
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Exclamation My first game! :D

    Hey guys, I made my own game, it is very bad and it is in the command prompt, but still it is my first game :P
    so yeah, my only problem is I have no idea how to make a AI. So if anyone can help with that I would be thankfull.

    Java Code:
    package com.ole.firstgame;
    
    import java.util.Random;
    import java.util.Scanner;
    
    public class FirstGame {
    	static Scanner scan = new Scanner(System.in);
    	static int cHp;
    	static int pHp;
    	static int cstrength = 5;
    	static int pstrength = 5;
    	static String starts;
    	static String chose;
    	static String YesOrNo;
    	static boolean won = false;
    	static boolean lost = false;
    	static boolean tie = false;
    	
    	
    	public static void main(String[] args) {
    		cHp = 20;
    		pHp = 20;
    		System.out.println( "In this game you are supposed to kill the opponent" +
    							"\nYou must kill the enemy with training and then hitting the enemy." +
    							"\nYou can either Train, fight or pass." +
    							"\nNow \"Let The Game Begin!!\"");
    		
    		Lsleep();
    		WhoStarts();
    		EndingCredits();
    	}
    	
    	public static void WhoStarts(){
    		String temp;
    		System.out.print("\nWho should start?" +
    						"\nChose from player, computer, random." +
    						"\nYour choice: ");
    		temp = scan.nextLine();
    		chose = temp.toLowerCase();
    		
    		switch(chose){
    		case "player":
    			Qsleep();
    			System.out.print("\n\nyou want to start yourself.");
    			System.out.print("\nDo you want to start?\n");
    			YesOrNo = scan.nextLine();
    			if(YesOrNo.equals("yes")){
    				starts = "player";
    				sleep();
    				Start();
    			}else{
    				reWhoStarts();
    			}
    			break;
    		case "computer":
    			Qsleep();
    			System.out.print("\n\nYou chose computer to start.");
    			System.out.print("\nDo you want the computer to start?\n");
    			YesOrNo = scan.nextLine();
    			if(YesOrNo.equals("yes")){
    				starts = "computer";
    				sleep();
    				Start();
    			}else{
    				reWhoStarts();
    			}
    			break;
    		default:
    			Qsleep();
    			System.out.print("\n\nYou chose a random user to start.");
    			System.out.print("\nDo you want a random user to start?\n");
    			YesOrNo = scan.nextLine();
    			if(YesOrNo.equals("yes")){
    				starts = "random";
    				sleep();
    				Start();
    			}else{
    				reWhoStarts();
    			}
    		}
    	}
    	
    	public static void reWhoStarts(){
    		String temp;
    		System.out.print("\nSince it seemed you didn't like your chose, \nyou can choose again. :)"+
    						"\nchoose again: ");
    		temp = scan.nextLine();
    		chose = temp.toLowerCase();
    		
    		switch(chose){
    		case "player":
    			System.out.print("\n\nYou want to start yourself.");
    			System.out.print("\nDo you want to start?\n");
    			YesOrNo = scan.nextLine();
    			if(YesOrNo.equals("yes")){
    				starts = "player";
    				sleep();
    				Start();
    			}else{
    				reWhoStarts();
    			}
    			break;
    		case "computer":
    			System.out.print("\n\nYou chose the computer to start.");
    			System.out.print("\nDo you want the computer to start?\n");
    			YesOrNo = scan.nextLine();
    			if(YesOrNo.equals("yes")){
    				starts = "computer";
    				sleep();
    				Start();
    			}else{
    				reWhoStarts();
    			}
    			break;
    		default:
    			System.out.print("\n\nYou chose a random user to start.");
    			System.out.print("\nDo you want a random user to start?\n");
    			YesOrNo = scan.nextLine();
    			if(YesOrNo.equals("yes")){
    				starts = "random";
    				sleep();
    				Start();
    			}else{
    				reWhoStarts();
    			}
    		}
    	}
    	
    	public static void Start(){
    		Random r = new Random();
    		int starting;
    		switch(starts){
    		case "random":
    			starting = r.nextInt(2)+1;
    			if(starting==1){
    				System.out.print("\n\nYou start!\n");
    				TrueStart(1);
    			}
    			if(starting==2){
    				System.out.print("\n\nThe computer starts!\n");
    				TrueStart(2);
    			}
    			break;
    		case "player":
    			System.out.print("\n\nYou start!\n");
    			TrueStart(1);
    			break;
    		case "computer":
    			System.out.print("\n\nThe computer starts!\n");
    			TrueStart(2);
    			break;
    		}
    	}
    	public static void TrueStart(int x){
    		switch(x){
    		case 1:
    			while(cHp>0 && pHp>0){
    				PTurn();
    				Qsleep();
    				CTurn();
    				Qsleep();
    			}
    			System.out.println("\n\n\nThe match is done!");
    			break;
    		case 2:
    			while(cHp>0 && pHp>0){
    				CTurn();
    				Qsleep();
    				PTurn();
    				Qsleep();
    			}	
    			System.out.println("\n\n\nThe match done!");
    			break;
    		}
    		if(pHp>cHp){
    			System.out.println("\nYou won!! :D");
    			won = true;
    		}
    		if(cHp>pHp){
    			System.out.println("\nYou lost... :'(");
    			lost = true;
    		}
    		if(pHp==cHp){
    			System.out.println("\nIt's tie!");
    			tie = true;
    		}
    	}
    
    	private static void PTurn() {
    		String PChose;
    		int dmgDealt;
    		Random rand = new Random();
    		System.out.print("\n\n\nIt's your turn," +
    						"\nyou can choose to train, Hit or pass.");
    		System.out.println("\nWhat do you want to do? ");
    		PChose = scan.nextLine();
    		
    		switch(PChose){
    		case "train":
    			System.out.print("\n\nYou have trained, and now your stronger");
    			
    			pstrength++;
    			break;
    			
    		case "Train":
    			System.out.print("\n\nYou have trained, and now your stronger");
    			
    			pstrength++;
    			break;
    			
    		case "hit":
    			int r = rand.nextInt(pstrength);
    			dmgDealt = r+(r<3&&r>0 ? r:2);
    			
    			System.out.print("\n\nYou hit the enemy with your fist, and dealt " + dmgDealt +" damage");
    			
    			cHp -= dmgDealt;
    
    			System.out.print("\nThe computer's health is now " + cHp);
    			break;
    		case "Hit":
    			int R = rand.nextInt(pstrength);
    			dmgDealt = R+(R < 3 ? R:1);
    			
    			System.out.println("\n\nYou hit the enemy with your fist," +
    					" and dealt " + dmgDealt +" damage");
    			
    			cHp -= dmgDealt;
    			
    			System.out.print("\nThe computer's health is now " + cHp);
    			break;
    		default:
    			System.out.print("\nYou have chosen to pass!");
    			break;
    		}
    		
    	}
    
    	private static void CTurn() {
    		String CChose = "hit";
    		int ChoseC;
    		int dmgDealt=0;
    		Random rand = new Random();
    		
    		ChoseC = rand.nextInt(100)+1;
    		if(ChoseC<=35)
    			CChose = "train";
    		if(ChoseC>=36 && ChoseC<=94)
    			CChose = "hit";
    		if(ChoseC>=95)
    			CChose = "pass";
    		
    		
    		switch(CChose){
    		case "train":
    			System.out.print("\n\nThe computer trained, and became stronger.");
    			cstrength++;
    			break;
    		case "Train":
    			System.out.print("\n\nThe computer trained, and became stronger.");
    			cstrength++;
    			break;
    		case "hit":
    			int r = rand.nextInt(cstrength);
    			dmgDealt = r+(r<3&&r>0 ? r:2);
    			
    			System.out.print("\n\nThe computer hit you with his fist,"+
    					"\nand dealt " + dmgDealt +" damage");
    			
    			pHp -= dmgDealt;
    			
    			System.out.print("\nYour health is " + pHp);
    			break;
    		case "Hit":
    			int R = rand.nextInt(cstrength);
    			dmgDealt = R+(R<3&&R>0 ? R:2);
    			
    			System.out.println("\n\nThe computer hit you with his fist,"+
    					"\nand dealt " + dmgDealt +" damage");
    			
    			pHp -= dmgDealt;
    
    			System.out.print("Your healt is " + pHp);
    			break;
    		default:
    			System.out.print("\nThe computer passed!");
    			break;
    		}
    	}
    	
    	public static void EndingCredits(){
    		if(won){
    			System.out.println("\n\n\nCongratulations, O' Big Master!!!");
    			Csleep();
    			System.out.println("\nI wish you a still nice day! :)");
    		}
    		if(lost){
    			System.out.println("\n\n\nToo bad you couldn't make it...");
    			sleep();
    			System.out.println("\nhope you'll do it better next time! :)");
    			Csleep();
    			System.out.println("\nI wish you a still nice day! :)");
    		}
    		if(tie){
    			System.out.println("\n\n\nlooks like none of you died...");
    			sleep();
    			System.out.println("\nI wish some of you had died... :(");
    			sleep();
    			System.out.println("\nWell.. Better luck next time!");
    			Csleep();
    			System.out.println("\nI wish you a still nice day! :)");
    		}
    	}
    	
    	public static void Qsleep(){
    		try{
    			Thread.sleep(500);
    		}catch(Exception e){}
    	}
    	
    	public static void sleep(){
    		try{
    			Thread.sleep(1000);
    		}catch(Exception ex){}
    	}
    	
    	public static void Lsleep(){
    		try{
    			Thread.sleep(3000);
    		}catch(Exception exc){}
    	}
    	
    	public static void Csleep(){
    		try{
    			Thread.sleep(2250);
    		}catch(Exception exce){}
    	}
    }

  2. #2
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: My first game! :D

    Does anyone know how to make an AI?
    Last edited by HelloWorld1234; 06-16-2012 at 12:26 AM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: My first game! :D

    If you properly comment your program, there is a tool in the JDK (javadoc???) that will create API doc files for you.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: My first game! :D

    I meant how to make an AI for the computer in the game, is that something else than just lots of lines with code?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: My first game! :D

    I think that AI in a program would be done with lines of code.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: My first game! :D

    okay

  7. #7
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: My first game! :D

    Quote Originally Posted by Norm View Post
    If you properly comment your program, there is a tool in the JDK (javadoc???) that will create API doc files for you.
    really? Could you give me a useful link where I can read more about this?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: My first game! :D

    Try google.
    Or go to the doc for the JDK and look under tools. On my PC its at: file:///E:/Java/docs/technotes/tools/index.html
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: My first game! :D

    AI is just a fancy way of saying clever coding. There are a number of AI methods out there each with varying degrees of difficulty. Which one you implement is up to you. Topics inclue things like Neural Networks, Genetic Algorithms, Finite State Machines (FSMs). Simply getting the computer to do something in responce to a action could be regarded by some as AI.

    For your game I would suggest looking into FSMs. A basic FSM is not too difficult to implement and can produce some interesting results if based on probability instead of fixe values.

    Regards.

  10. #10
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: My first game! :D

    Can someone explain to me why all the methods and all the variables(almost all of them) are static? I didn't understand it when I made the game, but eclipse said I needed it.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: My first game! :D

    Quote Originally Posted by HelloWorld1234 View Post
    Can someone explain to me why all the methods and all the variables(almost all of them) are static? I didn't understand it when I made the game, but eclipse said I needed it.
    No, Eclipse didn't say you needed it. It said instead, "cannot make a static reference to the non-static field (or method) ....". You've fixed it the wrong way. The solution is to make none of the fields and methods static but instead to call them in a non-static way -- to create an object and to call your methods off of the object.

  12. #12
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: My first game! :D

    it won't let me call the WhoStarts() method and the EndingCredits() method if they aren't static, and if those two methods are static everything must be static... :/

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: My first game! :D

    Quote Originally Posted by HelloWorld1234 View Post
    it won't let me call the WhoStarts() method and the EndingCredits() method if they aren't static, and if those two methods are static everything must be static... :/
    Again, you're mis-interpreting the error message. It won't let you call a non-static method in a static context. So again, create a FirstGame object and call the method off of the object, not in a static way.

  14. #14
    Join Date
    Jun 2012
    Posts
    52
    Rep Power
    0

    Default Re: My first game! :D

    oh, now I see.
    Thx man that worked :)
    Fubarable likes this.

Similar Threads

  1. Replies: 1
    Last Post: 06-02-2011, 10:29 AM
  2. first game help
    By relith in forum New To Java
    Replies: 2
    Last Post: 12-02-2010, 06:04 AM
  3. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  4. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 09:06 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •