2 Replies - 1296 Views - Last Post: 31 March 2011 - 02:01 AM Rate Topic: -----

#1 frustrated25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-February 11

Simplistic 1D reindeer game code not working

Posted 24 February 2011 - 06:11 AM

hi guys. i am taking a 1 week compulsory java course and haven't used java before. well, turns out we have a compulsory assignment due in 1 and a half weeks. i have come up with as much code as i possibly can but for some reason it does not work. Am getting some compiler errors and the .status, .reset are not working. Please help me sort this out.

The code is as below:
CMReindeerMidlet.java


package carolinm.metropolia.fi;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author carolinm
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

public class CMReindeerMidlet extends MIDlet implements CommandListener{
    private static final int RealSPD = 2;
    Reindeer sherry;
    private Command exit, move, eat, restart;
    private Displayable d;
    private Form rForm;
    public StringItem distans,ammo,wolfSays;
    public Gauge speedG;
    private Alert startGameA;
    private Alert wonGameA;
    private Alert reindeerStarvedA;
    private Alert wolfCatchA;
    private Display display;
    public Alert myAlert;
    private Reindeer myR;
    private Wolf myW;



    public CMReindeerMidlet(){ //constructor

        sherry = new Reindeer(100,this);
        rForm = new Form("Reindeer Game");


        distans=new StringItem("Distance", "100");
        //dist.setLayout(StringItem.LAYOUT_2);
        ammo = new StringItem("Ammo","100");
        //dist.setLayout(StringItem.LAYOUT_2);
        speedG = new Gauge("Speed", true,10,RealSPD);
        myAlert = new Alert("Alert Heading","Press move to Start", null, AlertType.CONFIRMATION);
        myAlert.setTimeout(Alert.FOREVER);
        wolfSays =new StringItem("", "");
        //implementing command to midlet
        exit = new Command("Eat",Command.OK,1);
        move = new Command("Move",Command.EXIT,1);
        eat = new Command("Exit",Command.OK,1);
        restart = new Command("Restart", Command.OK,1);

        //add commands to form
        rForm.addCommand(move);
        rForm.addCommand(exit);
        rForm.addCommand(eat);
        //appending to the form
        rForm.append(distans);
        rForm.append(ammo);
        rForm.append(speedG);
        rForm.append(wolfSays);


       startGameA = new Alert("Reindeer game", "Press move to start", null, AlertType.CONFIRMATION);
        startGameA.setTimeout(Alert.FOREVER);

        wonGameA = new Alert("YOU WON!!!", "YOU WON!!. Press restart to start a new game!",
                null, AlertType.CONFIRMATION);
        wonGameA.setTimeout(Alert.FOREVER);

        wolfCatchA = new Alert("GAME OVER!", "Wolf caught you!!! Press restart to start a new game!",
                null, AlertType.CONFIRMATION);
        wolfCatchA.setTimeout(Alert.FOREVER);

        reindeerStarvedA = new Alert("GAME OVER!", "Reindeer starved!!! Press restart to start a new game!",
                null, AlertType.CONFIRMATION);
        reindeerStarvedA.setTimeout(Alert.FOREVER);
        
        myR = new Reindeer(RealSPD, this);
        myW = new Wolf ();


    }
    public void startApp() {
    display = Display.getDisplay(this);
    display.setCurrent(rForm);
    myR.status();
    myW.status();
    display.setCurrent(myAlert);
    rForm.setCommandListener(this);
    }// end main method
    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
public void show() {
        display.setCurrent(rForm);
        display.setCurrentItem(speedG);
        System.out.println("START THE SHOW");
    }

    public void showGameWonAlert() {
        display.setCurrent(wonGameA);
        System.out.println("You won!!!!!");
    }

    public void showReindeerStarvedAlert() {
        display.setCurrent(reindeerStarvedA);
        System.out.println("Game over");
    }

    public void showWolfCatchAlert() {
        display.setCurrent(wolfCatchA);
        System.out.println("Game over");
    }

    public void reset(){

                if (myR.eatEnabled) {
                    rForm.removeCommand(eat);
                    myR.eatEnabled = false;
                }
                rForm.removeCommand(move);
                rForm.addCommand(restart);
    }

    public void restart() {

        myR.reset();
        myW.reset();
        myR.status();
        myW.status();
        rForm.addCommand(move);
        rForm.removeCommand(restart);
    }

    public void commandAction(Command c, Displayable d) {

        if (c == exit) {
            destroyApp(true);
            notifyDestroyed();
        }

        if (c == eat) {
            //reindeer eats
            if (myR.eatEnabled) {
                myR.eat();
                myR.eatEnabled = false;
                rForm.removeCommand(eat);
                myW.move();
                myW.status();
                myR.status();
            }
        }

          if (myR.eatEnabled) {
                rForm.addCommand(eat);
            } else {
                rForm.removeCommand(eat);
            }

            myW.status();
            myR.status();
    }

        if (c == restart) {
            this.restart();
            rForm.addCommand(move);

    }
       if (c== move){
           System.out.println("WooHoo! Enjoy the ride!!!");
           myR.Move();
           myW.move();
       }
    }






Reindeer class:



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package carolinm.metropolia.fi;

/**
 *
 * @author carolinm
 */

public class Reindeer extends Animal{

    Reindeer sherry;
    //public StringItem first,second;
    private int locX,remDis,remAmmo;
    public boolean eatEnabled;
    private int ammo;
    int[] though;
    private int speed;
    private CMReindeerMidlet myReindeer;
    static final int DISTANCE=100;
    static final int AMMO=100;
    static final int PORTION=15;
    private CMReindeerMidlet myCM;

    public Reindeer(int speed,CMReindeerMidlet CM){
       
       ammo = 100;
       this.ammo = remAmmo;
       this.remDis = DISTANCE;
       this.remAmmo = AMMO;
       this.speed = speed;
       this.myCM = CM;
       

    }

    public void Eat(){
        ammo = ammo + PORTION;
    }

    public void Move(){
        speed = myReindeer.speedG.getValue();
        locX = locX + speed;
        remAmmo = AMMO - ammo;

        ammo = ammo -(19/4 * speed + 30/4);
        myReindeer.ammo.setText(Integer.toString(remAmmo));
        myReindeer.distans.setText(Integer.toString(DISTANCE - locX));

        remDis = DISTANCE - locX;
    }
    public int getAmmo(){
           return ammo;
       }
    public void setAmmo(int a){
       if(a < this.ammo){
            this.ammo = a;
        }else{
           System.out.println ("Too bad! Reindeer DEAD!");
        }
    }

    public void Die(){

    }

    public void reset () {
        ammo = AMMO;
        remDis = DISTANCE;
        locX = 0;
    }

    public void Status(){
if (remDis<=0) {
           myCM.showGameWonAlert();
           myCM.reset();
       } else if (ammo>0) {
           myCM.ammo.setText(Integer.toString(ammo));
           myCM.distans.setText(Integer.toString(remDis));
           myCM.show();
           System.out.println("****ammo>0, status*****");
       } else if (ammo == -100) {
           myCM.showWolfCatchAlert();
           myCM.reset();
       } else {
           myCM.showReindeerStarvedAlert();
           myCM.reset();
}
    }
}





thanks guys!

Please :code:

This post has been edited by pbl: 24 February 2011 - 08:05 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Simplistic 1D reindeer game code not working

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Simplistic 1D reindeer game code not working

Posted 24 February 2011 - 08:07 PM

Post your errors... hoping the line numbers will match the ones your correctly posted code has now
Was This Post Helpful? 1
  • +
  • -

#3 frustrated25  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-February 11

Re: Simplistic 1D reindeer game code not working

Posted 31 March 2011 - 02:01 AM

View Postpbl, on 24 February 2011 - 08:07 PM, said:

Post your errors... hoping the line numbers will match the ones your correctly posted code has now



hei....i got it working and have not seen this mail till now....sorry bout that and thanks for helping out
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1