Reputation: 1538
here is the code that im have problems getting to work.
public void fight (Graphics canvas, int fightcount,int curloc)
{
if(curloc==10)
{
fightcount=fightcount-1;
}
else if (curloc==10 && fightcount==0)
{
JOptionPane.showMessageDialog(null, "You Win!");
}
}
here is the rest of the code
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.Applet;
import java.applet.*;
import java.awt.image.*;
import java.util.*;
import java.net.*;
public class AlcatrazEscape extends JApplet implements KeyListener, FocusListener
{
int strength=0, health=100, curloc=1,randomnumber=0,fightcount=10;
boolean focus = false;
boolean left=false, right=false, up=false, down=false, space=false, enter=false;
boolean start=false,train=false,showers=false,CBC=false,tunnel=false,SecRoom1=false,SecRoom2=false,courtyard=false,fight=false,free=false,GP=false,SolCon=false;
boolean key1=false, key2=false;
public void init()
{
addKeyListener(this);
addFocusListener(this);
play(getCodeBase(), "SpyHunter.mid");
}
public void paint(Graphics canvas)
{
clearScreen(canvas);
canvas.setColor(Color.BLACK); // Must do this so words will
// display on the screen
if (!focus)
{
canvas.setColor(Color.WHITE);
canvas.drawString("Welcome to Alcatraz Breakout! The objective of the game is to escape Alcatraz.",10,10);
canvas.drawString("Click the screen to begin the game.",10,30);
return;
}
else
{
makeMove(canvas);
dispLoc(canvas);
stats(canvas);
randomnumber=randomnum();
ran(randomnumber,curloc,canvas);
health(canvas, health);
fight(canvas, curloc, fightcount);
System.out.println(fightcount);
left=right=up=down=space=enter=false;
}
}
public void room1(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in your cell.", 10, 10);
canvas.drawString("Left: To the Training Room", 10, 30);
canvas.drawString("Right: To the Showers", 10, 50);
}
public void room2(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in the Training Room.", 10, 10);
canvas.drawString("Right: To your Cell", 10, 30);
}
public void room3(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in the Showers.", 10, 10);
canvas.drawString("Left: To your Cell", 10, 30);
canvas.drawString("Up: To Cell Block C", 10, 50);
}
public void room4(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in Cell Block C.", 10, 10);
canvas.drawString("Down: To the Showers", 10, 30);
canvas.drawString("Up: To Security Room 1", 10, 50);
}
public void room5(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in the first security room.", 10, 10);
canvas.drawString("Left: To the Guard Post", 10, 30);
canvas.drawString("Down: To Cell Block C", 10, 50);
}
public void room6(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in the Guard Post.", 10, 10);
canvas.drawString("Right: To Security Room 1", 10, 30);
canvas.drawString("Left: To The Courtyard", 10, 50);
canvas.drawString("Up: To Solitary Confinement", 10, 70);
}
public void room7(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in the Courtyard.", 10, 10);
canvas.drawString("Up: To the Guard Post", 10, 30);
canvas.drawString("Down: To Security Room 2", 10, 50);
}
public void SolCon(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in Solitary Confinement.", 10, 10);
canvas.drawString("Down: To the Guard Post", 10, 30);
}
public void room8(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in the second security room.", 10, 10);
canvas.drawString("Right: Leave", 10, 30);
canvas.drawString("Up: To The Couryard", 10, 50);
}
public void room9(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You walk by a blown up tank... weird.", 10, 10);
canvas.drawString("Right: Freedom!", 10, 30);
}
public void room10(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are FREE!!.", 10, 10);
}
public void room11(Graphics canvas)
{
canvas.setColor(Color.WHITE);
canvas.drawString("You are in SOLITARY CONFINEMENT: HOME OF THE FREAKS!!.", 10, 10);
canvas.drawString("Down: To the Guard Post.", 10, 30);
}
public void guardFight1(Graphics canvas)
{
int GuardHP=75,GATK=10;
}
public void guardFight2(Graphics canvas)
{
int GuardHP=150,GATK=25;
}
public void makeMove(Graphics canvas)
{
if (curloc==1 && left)
{
curloc=2;
}
else if (curloc==1 && right)
{
curloc=3;
}
else if (curloc==2 && right)
{
curloc=1;
}
else if (curloc==3 && left)
{
curloc=1;
}
else if (curloc==3 && up)
{
curloc=4;
}
else if (curloc==4 && down)
{
curloc=3;
}
else if (curloc==4 && up)
{
curloc=5;
}
else if (curloc==5 && down)
{
curloc=4;
}
else if (curloc==5 && left)
{
curloc=6;
}
else if (curloc==6 && right)
{
curloc=5;
}
else if (curloc==6 && left)
{
curloc=7;
}
else if (curloc==6 && up)
{
curloc=11;
}
else if (curloc==7 && up)
{
curloc=6;
}
else if (curloc==7 && down)
{
curloc=8;
}
else if (curloc==8 && up)
{
curloc=7;
}
else if (curloc==8 && right)
{
curloc=9;
}
else if (curloc==9 && right)
{
curloc=10;
}
else if (curloc==10)
{
//freedom!
}
else if (curloc==11 && down)
{
curloc=6;
}
}
public void ran(int randomnumber,int curloc ,Graphics canvas)
{
if (curloc==2 && randomnumber==1)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("You have gained 10 health", 10, 100);
health=health+10;
}
if (curloc==2 && randomnumber==2)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==2 && randomnumber==3)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==2 && randomnumber==4)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==2 && randomnumber==5)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("You have gained 10 strength", 10, 100);
strength=strength+10;
}
if (curloc==2 && randomnumber==6)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==2 && randomnumber==7)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==2 && randomnumber==8)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==2 && randomnumber==9)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==2 && randomnumber==10)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("You trip and fall and break your ankle", 10, 100);
canvas.drawString("You lose 5 health",10,120);
health=health-5;
}
//showers
if (curloc==3 && randomnumber==1)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==3 && randomnumber==2)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None",10,100);
}
if (curloc==3 && randomnumber==3)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("You find an Energy Drink", 10, 100);
canvas.drawString("You gain 10 strength",10,120);
strength=strength+10;
}
if (curloc==3 && randomnumber==4)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==3 && randomnumber==5)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("None", 10, 100);
}
if (curloc==3 && randomnumber==6)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("You trip and fall and break your ankle", 10, 100);
canvas.drawString("You lose 5 health",10,120);
health=health-5;
}
if (curloc==3 && randomnumber==7)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("You find a First Aid Kit", 10, 100);
canvas.drawString("You gain 25 health",10,120);
health=health+25;
}
if (curloc==3 && randomnumber==8)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("You trip and fall and break your ankle", 10, 100);
canvas.drawString("You lose 5 health",10,120);
health=health-5;
}
if (curloc==3 && randomnumber==9)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("none", 10, 100);
}
if (curloc==3 && randomnumber==10)
{
canvas.drawString("Random Events: ",10,80);
canvas.drawString("You slip and fall and break your nose", 10, 100);
canvas.drawString("You lose 5 health",10,120);
health=health-5;
}
if (curloc==10 && randomnumber==1)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He dodges and you lose 5 health", 10, 100);
health=health-5;
}
if (curloc==10 && randomnumber==2)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He dodges and you lose 5 health", 10, 100);
health=health-5;
}
if (curloc==10 && randomnumber==3)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He dodges and you lose 0 health", 10, 100);
}
if (curloc==10 && randomnumber==4)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He attacks and you lose 5 health", 10, 100);
health=health-5;
}
if (curloc==10 && randomnumber==5)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He attacks and you lose 0 health", 10, 100);
}
if (curloc==10 && randomnumber==6)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He attacks and you lose 5 health", 10, 100);
health=health-5;
}
if (curloc==10 && randomnumber==7)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He attacks and you lose 0 health", 10, 100);
}
if (curloc==10 && randomnumber==8)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He attacks and you lose 5 health", 10, 100);
health=health-5;
}
if (curloc==10 && randomnumber==9)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He attacks and you lose 0 health", 10, 100);
}
if (curloc==10 && randomnumber==10)
{
canvas.drawString("You lunge at the warden. ",10,80);
canvas.drawString("He attacks and you lose 5 health", 10, 100);
health=health-5;
}
}
public void fight (Graphics canvas, int fightcount,int curloc)
{
if(curloc==10)
{
fightcount=fightcount-1;
}
else if (curloc==10 && fightcount==0)
{
JOptionPane.showMessageDialog(null, "You Win!");
}
}
{
}
public void dispLoc(Graphics canvas)
{
if (curloc==1)
{
room1(canvas);
//cell
}
if (curloc==2)
{
room2(canvas);
//training
}
if (curloc==3)
{
room3(canvas);
//showers
}
if (curloc==4)
{
room4(canvas);
//Cell Block C
}
if (curloc==5)
{
room5(canvas);
//Security Room1
}
if (curloc==6)
{
room6(canvas);
//Guard Post
}
if (curloc==7)
{
room7(canvas);
//Courtyard
}
if (curloc==8)
{
room8(canvas);
//Security Room 2
}
if (curloc==9)
{
room9(canvas);
//Tank
}
if (curloc==10)
{
room10(canvas);
//Freedom
}
if (curloc==11)
{
room11(canvas);
//Solitary Confinement
}
}
public void stats(Graphics canvas)
{
canvas.drawString("Health: " + health, 750, 10);
canvas.drawString("Strength: " + strength, 750, 30);
}
public void health(Graphics canvas, int health)
{
if (health==0)
{
JOptionPane.showMessageDialog(null,"You Died Game over!");
System.exit(0);
}
}
public void delayGame(double numSecs)
{
double delayTime = 1000 * numSecs;
long startDelay = System.currentTimeMillis();
long endDelay = 0;
while (endDelay - startDelay < delayTime)
{
endDelay = System.currentTimeMillis();
}
}
public int randomnum()
{
int n1=0;
n1=(int) (Math.random()*10+1);
return n1;
}
public void clearScreen(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(0,0,5000,5000);
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT)
{
left = true;
repaint();
}
if (key == KeyEvent.VK_DOWN)
{
down = true;
repaint();
}
if (key == KeyEvent.VK_RIGHT)
{
right = true;
repaint();
}
if (key == KeyEvent.VK_UP)
{
up = true;
repaint();
}
if (key == KeyEvent.VK_SPACE)
{
space = true;
repaint();
}
if (key == KeyEvent.VK_ENTER)
{
enter = true;
repaint();
}
}
public void focusGained(FocusEvent evt) {focus=true; repaint();}
public void focusLost(FocusEvent evt) {focus=false;}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e) {}
public void restart(Graphics g){}
}
Upvotes: 0
Views: 146
Reputation: 114847
The fight()
method only changes the local copy of fightcount
. If you pass 10, then it will change to 9 but not outside the fight()
method.
If you want to change it globally, declare it static
and don't pass it to fight()
(doesn't harm but confuses)
Some explanation:
int
is a java primitive and primitives are passed by value. So actually you send the value 10 to the method and not a reference to the outer fightcount
variable. Inside the method, the incoming value 10 is assigned to a new local variable (fightcount
) and the value of that variable is decremented.
Quick fix (as suggested above):
replace
int strength=0, health=100, curloc=1,randomnumber=0,fightcount=10;
with
static int fightcount = 10;
int strength=0, health=100, curloc=1,randomnumber=0;
and the method signature
public void fight (Graphics canvas, int fightcount,int curloc)
with
public void fight (Graphics canvas, int curloc)
Upvotes: 2
Reputation: 4758
OK, i assume you're talking about the fightcount variable in the fight method? If so then it's not changing because you're decrementing the passed int, which will have no effect once that method has completed.
int fightcount;
public void fight (Graphics canvas, int curloc)
{
if(curloc==10)
{
fightcount=fightcount-1;
}
else if (curloc==10 && fightcount==0)
{
JOptionPane.showMessageDialog(null, "You Win!");
}
}
I'm not suggesting you actually do this as this is terrible code and the above is just an example, but you need to update a fightcount field that lives outside the method. i.e. is not a method param...
Upvotes: 3
Reputation: 81734
In that "problem" block of code, if the second "if" is true, the first one would always be true as well, right? So the first one would always run, and the second one is blocked by the "else" and never runs. You could put the "else" condition first, or better, do something like
if(curloc==10)
{
if (fightcount==0) {
JOptionPane.showMessageDialog(null, "You Win!");
} else {
fightcount=fightcount-1;
}
}
Upvotes: 4