Hi all,
Total newbee...New to java and new to the forum. Going through the stanford cs106a itunes U (fantastic) to one day reach their iphone programming course. In an assignment I wish to do a "cascading if" as the professor has shown in the slide attached, but it will not work for me. Eclipse wants me to remove the second and third "else" and then if I do that is says the method has to return an object i.e. the null statement is missing. Can someone point out the error(s)? (the relevant section is in red font section close to the end):
Thanks a lot
Jesper
ps. the program is work in progress, so there are still missing sections and draft examples from other methods pasted in it
Here's the code:
/*
* File: Breakout.java
* -------------------
* Name:
* Section Leader:
*
* This file will eventually implement the game of Breakout.
*/
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Breakout extends GraphicsProgram {
/** Width and height of application window in pixels */
public static final int APPLICATION_WIDTH = 400;
public static final int APPLICATION_HEIGHT = 600;
/** Dimensions of game board (usually the same) */
private static final int WIDTH = APPLICATION_WIDTH;
private static final int HEIGHT = APPLICATION_HEIGHT;
/** Dimensions of the paddle */
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
/** Offset of the paddle up from the bottom */
private static final int PADDLE_Y_OFFSET = 30;
/** Number of bricks per row */
private static final int NBRICKS_PER_ROW = 10;
/** Number of rows of bricks */
private static final int NBRICK_ROWS = 10;
/** Separation between bricks */
private static final int BRICK_SEP = 4;
/** Width of a brick */
private static final int BRICK_WIDTH =
(WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW;
/** Height of a brick */
private static final int BRICK_HEIGHT = 8;
/** Radius of the ball in pixels */
private static final int BALL_RADIUS = 10;
/** Offset of the top brick row from the top */
private static final int BRICK_Y_OFFSET = 70;
/** Number of turns */
private static final int NTURNS = 3;
/** Regulate Game Difficulty */
private static final int DELAY = 10;
/** Runs the Breakout program. */
public void run() {
// Run game for the number of turns defined by NTURNS
points=0; //initialize score
for(int i=1; i < NTURNS; i++){
setUpGame(); // Setup fresh board, paddle and ball
// Set random direction for ball when starting new game
vx = rgen.nextDouble(1.0, 3.0);
if (rgen.nextBoolean(0.5)) vx=-vx;
// println("Size of Canvas" + getWidth() + " , "+ getHeight());
// Run game until a gameover condition occurs
while (!gameOver()) {
moveBall();
checkForWallCollision();
checkForGameCollision();
pause(DELAY);
}
}
// Number of turns exhausted game ends
GLabel gameover = new GLabel("Game over",0,0);
double GAMEOVERWIDTH = gameover.getWidth();
add(gameover,WIDTH/2-GAMEOVERWIDTH/2,HEIGHT/2);
}
private void setUpGame(){
setUpCanvas();
setUpBricks();
setUpPaddle();
SetUpBall();
addMouseListeners();
startGame();
}
private void setUpCanvas(){
getGCanvas().setSize(WIDTH, HEIGHT);;
}
private void setUpBricks(){
double StartPosX = BRICK_SEP;
double StartPosY = BRICK_Y_OFFSET;
int linecount=0;
for (int i = 0;i<NBRICK_ROWS;i++){
for (int j = 0;j<NBRICKS_PER_ROW;j++){
if(linecount<2){
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.RED);
Brick.setFilled(true);
add (Brick);
}
else if(linecount<4){
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.ORANGE);
Brick.setFilled(true);
add (Brick);
}
else if(linecount<6){
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.YELLOW);
Brick.setFilled(true);
add (Brick);
}
else if(linecount<8){
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.GREEN);
Brick.setFilled(true);
add (Brick);
}
else {
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.CYAN);
Brick.setFilled(true);
add (Brick);
}
}
linecount++;
}
}
private void setUpPaddle(){
paddle = new GRect(100,HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT,PADDLE_WIDTH,PADDLE_HEIGHT);
paddle.setFillColor(Color.BLACK);
paddle.setFilled(true);
add (paddle,100,HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT);
}
private void SetUpBall(){
ball = new GOval(WIDTH/2-BALL_RADIUS,HEIGHT/2-BALL_RADIUS,BALL_RADIUS*2,BALL_RADIUS*2);
ball.setFillColor(Color.RED);
ball.setFilled(true);
add (ball);
}
private void startGame(){
// Ask user to start game
GLabel gamestart = new GLabel("Click to Start",0,0);
gamestart.setFont("Helvetica-24");
double gamestartwidth = gamestart.getWidth();
println("width" + gamestart.getWidth() + " ,X= "+ (WIDTH/2-gamestartwidth/2));
add(gamestart,(WIDTH/2)-(gamestartwidth/2),HEIGHT/2*0.8);
waitForClick();
remove(gamestart);
}
public void mouseMoved(MouseEvent e)
{
double mouseXPos = e.getX();
if (mouseXPos < (WIDTH- PADDLE_WIDTH))
paddle.setLocation(mouseXPos,HEIGHT - (PADDLE_Y_OFFSET + PADDLE_HEIGHT));
}
private void moveBall() {
ball.move(vx, vy);
}
private void checkForWallCollision() {
// determine if ball has dropped beyond the boundries
//Did ball hit the bottom?
if (ball.getY() > (HEIGHT - BALL_RADIUS*2)) {
vy = -vy;
double diff = (ball.getY() - (HEIGHT - BALL_RADIUS*2));
ball.move(vx, -2 * diff);
}
//Did ball hit the top?
else if(ball.getY() < 0) {
vy = -vy;
// assume bounce will move ball an amount above the
// floor equal to the amount it would have dropped
// below the floor.
double diff = (ball.getY());
ball.move(vx, -2 * diff);
}
//Did ball hit the left wall?
else if(ball.getX() < 0) {
// change ball's Y velocity to now bounce upwards
vx = -vx;
// assume bounce will move ball an amount above the
// floor equal to the amount it would have dropped
// below the floor.
double diff = (ball.getX());
ball.move(-2 * diff,vy);
}
//Did ball hit the right wall?
else if(ball.getX() > WIDTH-BALL_RADIUS*2) {
// change ball's Y velocity to now bounce upwards
vx = -vx;
// assume bounce will move ball an amount above the
// floor equal to the amount it would have dropped
// below the floor.
double diff = ball.getX() - (WIDTH - BALL_RADIUS*2);
ball.move(-2 * diff,vy);
// else proceed with next move
}
}
private void checkForGameCollision(){
getCollidingObject();
if(collider == paddle){
vy = -vy;
double diff = (ball.getY() - (HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT));
ball.move(vx, -2 * diff);
}
else {
vy = -vy;
double diff = (ball.getY() - (HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT));
ball.move(vx, -2 * diff);
points++;
remove(collider);
}
}
private GObject getCollidingObject(){
if(getElementAt(ball.getX(),ball.getY())!=null){
GObject collider = getElementAt(ball.getX(),ball.getY()); }
else if(getElementAt((ball.getX()+BALL_RADIUS*2),ball.g etY())!=null);{
GObject collider = getElementAt((ball.getX()+BALL_RADIUS*2),ball.getY ());}
else if(getElementAt((ball.getX()+BALL_RADIUS*2),ball.g etY())!=null);{
GObject collider = getElementAt((ball.getX()+BALL_RADIUS*2),ball.getY ());
else if(getElementAt((ball.getX()+BALL_RADIUS*2),ball.g etY())!=null);{
GObject collider = getElementAt((ball.getX()+BALL_RADIUS*2),ball.getY ());}
else null;
}
/** determines if game is over -- true if either
* the UFO is destroyed or if the UFO lands */
private boolean gameOver() {
return false;
/* return (ufo == null) ||
(ufo.getY() >= getHeight() - UFO_HEIGHT);*/
}
private GRect paddle;
private GOval ball;
private GObject collider;
private double vx = 5;
private double vy = 3; // velocity of Ball
private RandomGenerator rgen = RandomGenerator.getInstance();
private int points;
}

