Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Whats Wrong with this code?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Whats Wrong with this code?

    Hey im having a bit of confusion with this particular programming assignment,
    "Programming Assignment: Opoly

    For this assignment, your job is to create a kind of lobotomized version of a board game (such as Monopoly) called Opoly.

    Opoly works this way: The board is a straight linear track of variable length (you determine the length when you start up the game). There is only one playing piece, which begins the game just off the board, at position 0. Thus, if the board length is 20, then the board positions start at position 1 and end at position 20. To finish the game, the piece must land exactly on the last cell of the board (e.g., cell 20 in the example above).

    The object of the game is to acquire reward. The reward amount is initialized to 10. If your board piece lands on a board cell that is divisible evenly by 4, your reward doubles. However, if your piece lands one cell shy of the final board cell, your reward is reduced to 1/4 of its current value (via integer division), and your piece must go back to the start position - position 0.

    In Opoly the game piece advances via a spinner - a device that takes on the values 1-2-3-4-5 at random, with each advance value equally likely.

    Two additional rules:

    1) if a spin would move the piece beyond the end of the board, the piece should not advance at all (thus, if the piece is at location 18 of a 20 cell board, and if the spinner spins a 5, this is counted as a move but the piece remains at location 18.)

    2) if the next to last board location is divisible by 4, and if the piece lands on this location, the reward is reduced to 1/4 of its current value only - the reward is NOT also doubled. Example: the board size is 25, and the piece is at location 22, with reward 30. Suppose the spinner spins a 2. This puts the piece on the next to the last location on the board. This resets the reward to 10, and places the piece at location 0. Even though location 24 is divisible by 4, no doubling happens.

    Here is the driver class for the game:
    import java.util.*;
     
    public class OpolyDriver{
     
    public static void main(String[] args){
    System.out.println("Enter an int - the size of the board");
    Scanner s = new Scanner(System.in);
    int boardSize = s.nextInt();
    System.out.println("Board Size: " + boardSize);
    Opoly g = new Opoly(boardSize);
    g.playGame();
    }
    }

    Here is a sample run:

    > java OpolyDriver
    Enter an int - the size of the board
    Board Size: 20
    *O****************** 10 // top line says: current position is 2, current reward is 10
    ***O**************** 20
    *******O************ 40
    ************O******* 40
    ***************O**** 80
    ****************O*** 80
    *******************O 160
    game over
    rounds of play 7
    final reward 160

    A requirement: your Opoly class must include the following methods, in addition to the Opoly constructor and principal method playGame(). These are:

    * spin - generates a integer value from 1 to 5 at random
    * move - advances the piece
    * spinAndMove - spins the spinner and then advances the piece according to the rules of the game (uses spin, move methods)
    * isGameOver - checks if game termination condition has been met
    * drawBoard - draws the board using *'s and an O to mark the current board position. Feel free to improve on the display we've shown above.
    * displayReport - reports the end of the game, and gives the number of rounds of play, and the final reward




    How to proceed:

    * First, decide on the attributes for the Opoly class. At any instant, what is the "state" of the board? What do you need to keep track of to give the final report? The answers to these questions will tell you what the attributes to Opoly can be.

    * Second, write the Opoly constructor.

    * Third, try to write the playGame method using the methods outlined above. A good way to proceed is to write a "dummy" drawBoard method first - instead of having it draw anything, merely have it print out the current position. Once you're satisfied with the general running of the game in this format, then work on the drawing part.

    Paste in your Opoly class in the box below:

    NOTE: you may NOT use any import statements in your class. Use the Math class methods to generate your random numbers. Do NOT import the java.util library"

    Now i think i have most of it done, however when i run the program it doesnt print anything but the board size and it seems that its still running but running forever, so the problem is in the while loop, (i think) but im not exactly sure.

    my code in the class looks like such,

      public class Opoly{
      private int boardSize;
      int[] gameArray;
      private int currentPos;
      private int spins;
      private int moves;
      private int reward;
      private int round;
     
      public Opoly(int boardSize){
        this.boardSize = boardSize;
        gameArray = new int[boardSize];
        currentPos = 0;
        reward = 10;
      }
     
      public void spin(){
        spins = (int)(Math.random()*6);
      }
     
      public void move(){
        moves = currentPos + spins;
     
      }
     
      public int spinAndMove(){//here
        round = 0;
        while(currentPos < gameArray.length-1){
          spin();
          move();
          round++;
          if ((currentPos%4) == 0){
            reward = reward * 2;
          }
          if (currentPos == (gameArray.length - 2)){
            reward = reward / 3;
            currentPos = 0;
          }
          if ((currentPos == (gameArray.length - 2)) && ((currentPos%4) == 0)){
            reward = reward / 3;
            currentPos = 0;
          }
     
          if(currentPos > gameArray.length-1){
            currentPos = currentPos;
            round++;
          }
          if(currentPos==gameArray.length-1){
            break;
          }
        }
        return round; 
      }
      public void isGameOver(){
        if(currentPos == gameArray.length-1)
          System.out.println("game over");
      }
     
      public void drawBoard(){
        if(gameArray[currentPos] == 0){
         System.out.print("O");
        }
        else
         System.out.print("*");
      }
     
      public void displayReport(){
        isGameOver();
        System.out.println("rounds of play: " + round);
        System.out.println("final reward " + reward);
     
      }
     
      public void playGame(){//or here
        while(gameArray[currentPos] != gameArray.length-1){
          spinAndMove();
          drawBoard();
        if(gameArray[currentPos] == gameArray.length-1){
          drawBoard();
          displayReport();
          break;
        }
        } 
      }
     
     
     
      }


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Whats Wrong with this code?

    Something doesn't look right with your move method, also take a look at your currentPos variable, put a println() in the spinAndMove while loop.

    you should find an issue there.

    Hope this helps.

    General debugging for programs that hang in loops:
    System.out.println("Is your friend, it likes to hide in loops and print out loop condition variables ");

Similar Threads

  1. Whats wrong with my code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 4th, 2011, 05:34 PM
  2. Collision detection - whats wrong with my code?
    By Javaz in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 22nd, 2011, 08:20 PM
  3. Whats wrong with my code?
    By mlan in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2010, 01:42 PM
  4. i'm new to java. whats is wrong in this code?
    By igorek83 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2009, 08:38 PM
  5. [SOLVED] whats wrong with my IDE
    By chronoz13 in forum Java IDEs
    Replies: 2
    Last Post: August 27th, 2009, 06:34 AM