Results 1 to 14 of 14
  1. #1
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Angry While-loop help please!

    Okay, so here's what I've been assigned to do:

    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 12. If your board piece lands on a board cell that is divisible evenly by 5, your reward doubles. However, if your piece lands one cell shy of the final board cell, your reward is reduced to 1/5 of its current value (via integer division), and your piece must go back to the start position - position 0. Note, though, that if the position of the last cell is divisible evenly by 5, then reward is doubled just before the game ends.

    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.) If a piece doesn't move at all, its current reward amount should remain unchanged, even if the piece sits at a location that is divisible evenly by 5.

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

    Here is the driver class for the game:

    Java Code:
    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****************** 12 // current position is 2, current reward is 12
    ****O*************** 24 // current position is 5, reward is 24
    *******O************ 24
    **************O***** 48
    ********************O 96 // board size is 20, so reward doubles before game ends
    game over
    rounds of play 5
    final reward 96

    A requirement: your Opoly class must include and make use of 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.

    * My board rendering is particularly klunky. If you like, you may devise a better way to draw the board. Of course make sure that you get the numbers right.

    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.
    [/B]

    I've come up with what I believe is very close to being correct:

    Java Code:
    public class Opoly{
      private int boardSize;
      int[] gameArray;
      private int position;
      private int spins;
      private int moves;
      private int reward;
      private int round;
      
      public Opoly(int boardSize){
        this.boardSize = boardSize;
        gameArray = new int[boardSize];
        position = 0;
        reward = 12;
      }
      public void spin(){
        spins = 1+(int)(Math.random()*5);
      }
      public void move(){
        moves = position + spins;
      }
      public int spinAndMove(){
        round=0;
        while(position<gameArray.length-1){
          spin();
          move();
          round=round+1;
          if ((position%5)==0){
            reward=reward*2;
          }
          if (position==(gameArray.length-2)){
            reward=reward/5;
            position=0;
          }
          if ((position==(gameArray.length-2))&&((position%5)==0)){
            reward=reward/5;
            position=0;
          }
          if(position>gameArray.length-1){
            position=position;
            round=round+1;
          }
          if(position==gameArray.length-1){
            break;
          }
        }
        return round; 
      }
      public void isGameOver(){
        if(position == gameArray.length-1)
          System.out.println("game over");
      }
      public void drawBoard(){
        if(gameArray[position]==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(){
        while(gameArray[position]!=gameArray.length-1){
          spinAndMove();
          drawBoard();
        if(gameArray[position]==gameArray.length-1){
          drawBoard();
          displayReport();
          break;
        }
        } 
      }
    }
    Right now, the program will ONLY print the board size. Where am I going wrong as far as making it print the board, "game over", rounds of play, etc.? I'm assuming I screwed up one of the while-loops.

    Thank you in advanced!
    Last edited by JosAH; 10-12-2011 at 07:53 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: While-loop help please!

    move() is supposed to advance the piece, ie update position. You don't do that, so the position doesn't advance except for the cases when it is made zero.

    Also what is moves? Unless I have missed something you don't use this for any purpose. If you aren't using it you can remove it.

    You could clean up the number of instance variables also by getting rid of spins: have spin() return the random int, and move() take what spin() returns as an argument.

  3. #3
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: While-loop help please!

    Alright, there were some issues with your loops and the way you tried to draw the board was not correct. I changed them around and got it to work for you. As for the reward I did not edit this, and it seems to be off. It's getting late where I am so I will leave that part to you. ;)

    Java Code:
    public class Opoly{
    	  private int boardSize;
    	  int[] gameArray;
    	  private int position;
    	  private int playerPosition;
    	  private int spins;
    	  private int moves;
    	  private int reward;
    	  private int round = 0;
    	   
    	  public Opoly(int boardSize){
    	    this.boardSize = boardSize;
    	    this.gameArray = new int[boardSize];
    	    this.position = 0;
    	    this.playerPosition = 0;
    	    this.reward = 12;
    	  }
    	  
    	  public void spin(){
    	    this.spins = (int)(Math.random()*5+1);
    	  }
    	  
    	  public void move(){
    	    this.moves = playerPosition + spins;
    	  }
    	  
    	  public void spinAndMove(){
    	    spin();
    	    move();
    	      if ((playerPosition%5)==0){ // divisible by 5
    	        reward = reward*2;
    	      }
    	      if ((playerPosition%5)!=0){ // not divisible by 5
    		        reward = reward/5;
    		      }
    	      if(moves > gameArray.length){ // what happens if greater than 20
    		    	playerPosition = playerPosition;
    		    }
    	      if(moves < gameArray.length){ // what happens if less than 20
    	    	  playerPosition += spins;
    	      }
    	      round++;	    
    	  }
    	  
    	  public void isGameOver(){
    	      System.out.println("game over");
    	  }
    	  
    	  public void drawBoard(){
    		  for(int i =1; i < gameArray.length+1; i++){
    			  if(playerPosition==i){
    				     System.out.print("O");
    			  }else{
    				     System.out.print("*");
    			}
    		}
    		  System.out.print("Reward: " +reward + 
    				  ", Current Spin: " + spins +
    				  ", Position: " + playerPosition +
    				  ", Round: " + round);
    		  System.out.println("");
    
    	  } 
    	  
    	  public void displayReport(){
    		isGameOver();
    	    System.out.println("rounds of play: "+round);
    	    System.out.println("final reward "+reward);  
    	  }  
    	  
    	  public void playGame(){
    		  do{
    			  spinAndMove();
    	    	  drawBoard();
    		  }while(moves != gameArray.length);
    		  
    		  if(moves == gameArray.length){
    		      displayReport();
    		    }
    	  }
    	  
    	}
    I'm thinking this is what you were wanting.

  4. #4
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: While-loop help please!

    Okay, I took both of your suggestions, and tweaked the program.


    Java Code:
    public class Opoly{
      private int boardSize;
      int[] gameArray;
      private int position;
      private int spins;
      private int moves;
      private int reward;
      private int round=0;
      
      public Opoly(int boardSize){
        this.boardSize = boardSize;
        this.gameArray = new int[boardSize];
        this.position = 0;
        this.reward = 12;
      }
      public void spin(){
        this.spins = (int)(Math.random()*5+1);
      }
      public void move(){
        this.moves = position + spins;
      }
      public void spinAndMove(){
        spin();
        move();
          if ((position%5)==0){
            reward=reward*2;
          }
          if ((position%5)!=0){
            reward=reward/5;
          }
          if(moves > gameArray.length){
            position = position;
          }
          if(moves < gameArray.length){
            position += spins;
          }
          round++;     
      }
      public void isGameOver(){
        System.out.println("game over");
      }
      public void drawBoard(){
        for(int k=1; k<gameArray.length+1; k++){
          if(position==k){
            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(){
        while(moves!=gameArray.length){
          spinAndMove();
          drawBoard();
        }
        if(moves==gameArray.length){
          displayReport();
        } 
      }
    }

    Right now, the output when the board game is 20 is:

    Board Size: 20
    ***O***********************O********************** *O*********************O************************O* ******************O*game over
    rounds of play: 6
    final reward 0
    So there's still an issue when I created the board. I will figure out the reward stuff after I get the board figured out.

    This is what it SHOULD look like:

    Enter an int - the size of the board
    Board Size: 20
    *O****************** 12
    ****O*************** 24
    *******O************ 24
    **************O***** 48
    ********************O 96
    game over
    rounds of play 5
    final reward 96
    Last edited by ayelleeeecks; 10-12-2011 at 10:19 PM.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: While-loop help please!

    It looks like you need to print some newlines when you draw the board. "System.out.println()" will print a newline. You have to decide when to do this in the for loop based on the value of k. Remember you have a % operator.

  6. #6
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: While-loop help please!

    Quote Originally Posted by pbrockway2 View Post
    It looks like you need to print some newlines when you draw the board. "System.out.println()" will print a newline. You have to decide when to do this in the for loop based on the value of k. Remember you have a % operator.
    Like he said...you must not have copied down my code very well, because towards the end of drawBoard I have
    Java Code:
    System.out.println("");

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: While-loop help please!

    I've just read more closely ... I had thought the board was 2 dimensional. Yes, you just need a println() once you have printed the array: it doesn't depend on the value of k as I said.

  8. #8
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: While-loop help please!

    Quote Originally Posted by kraigballa View Post
    Like he said...you must not have copied down my code very well, because towards the end of drawBoard I have
    Java Code:
    System.out.println("");
    Wow, I feel like a moron. I totally missed that part of your code. Sorry, and thanks! The only thing I need to figure out now is the rewards part. I just tried to fix it and I got stuck in an infinite loop, and I can't figure out what I'm doing wrong. Here's spinAndMove:

    Java Code:
      public void spinAndMove(){
        spin();
        move();
          if ((position%5)==0){
            reward=reward*2;
          }
          else if (position==gameArray.length-1){
            reward=reward/5;
          }
          else if(moves > gameArray.length){
            position = position;
          }
          else if(moves < gameArray.length){
            position += spins;
          }
          round++;     
      }
    Do I need another else or something? Blah, thanks for all the help, guys!

  9. #9
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: While-loop help please!

    Any thoughts as to why the loop is infinite? I'm trying to think about it logically, but I just can't figure it out. I tried writing out the conditions on paper before coding, too.

  10. #10
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: While-loop help please!

    I thought I already solved this one? Your drawBoard() method was incorrectly made. With the code I posted in post #3 this is the output I get. Of course with my own little tweaks to show you what the spin was, your players current position, and what round it is. Like I said earlier the reward part is not working correctly yet. This may not function the exact same way you want. but at least everything is working and you can edit it from there.

    Java Code:
    Enter an int - the size of the board
    20
    Board Size: 20
    O*******************Reward: 24, Current Spin: 1, Position: 1, Round: 1
    *****O**************Reward: 4, Current Spin: 5, Position: 6, Round: 2
    *******O************Reward: 0, Current Spin: 2, Position: 8, Round: 3
    ********O***********Reward: 0, Current Spin: 1, Position: 9, Round: 4
    **********O*********Reward: 0, Current Spin: 2, Position: 11, Round: 5
    ***********O********Reward: 0, Current Spin: 1, Position: 12, Round: 6
    ***************O****Reward: 0, Current Spin: 4, Position: 16, Round: 7
    ******************O*Reward: 0, Current Spin: 3, Position: 19, Round: 8
    ******************O*Reward: 0, Current Spin: 1, Position: 19, Round: 9
    game over
    rounds of play: 9
    final reward 0
    Last edited by kraigballa; 10-17-2011 at 11:10 PM.

  11. #11
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: While-loop help please!

    I already fixed the drawBoard() method. My spinAndMove() method is the part that is messed up right now, because that is where I include the reward stuff. I can't figure out how to get the rewards working. I end up in an infinite loop every time.

  12. #12
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: While-loop help please!

    You shouldn't need a loop for that part. Because if you loop there you need to remember that you have a while loop in the playGame() method. Post what edits you made in order to get the rewards to work, because I can't determine what is wrong with your loop without seeing it first.

  13. #13
    ayelleeeecks is offline Member
    Join Date
    Sep 2011
    Posts
    16
    Rep Power
    0

    Default Re: While-loop help please!

    EDIT: I think I've got it! Thank you for all of your patience and help, but I think I got it! I'm just running in a few times over and over to make sure it works everytime!
    Java Code:
    public class Opoly{
      private int boardSize;
      private int position;
      private int reward;
      private int spins;
      private int moves;
      private int round;
      
      public Opoly(int boardSize){
        this.boardSize = boardSize;
        this.round=0;
        this.position = 0;
        this.reward=12;
      }
    
      public void spin(){//generates random integer for spinner
        this.spins=(1+(int)(Math.random()*5));
      }
      public void move(){//advances the piece
        if((position+spins)>boardSize){
          round++;
        }
        if((position+spins)<=boardSize){
          position+=spins;
          if(position==(boardSize-1)){
            reward=reward/5;
            position=0;
          }
          else if(position%5==0){
            reward=reward*2;
          }
          round++;
        }
      }
      public void spinAndMove(){//spins spinner and advances piece according to rules of game
        spin();
        move();
      }
     
      private boolean isGameOver(){//checks if condition for ending game is met
        boolean end=false;  
        if(this.position==this.boardSize){
          end=true;
          return end;
        }
        else return end;
      }
      public void drawBoard(){//prints board with *'s and O's to show current position
        for(int k=1; k<=boardSize; k++){
          if(position==k){
            System.out.print("O");
          }
          else{
            System.out.print("*");
          }
        }
        System.out.print(" " +reward);
        System.out.println("");
      }
      public void displayReport(){//reports game over, rounds, and final reward
        System.out.println("rounds of play: "+round);
        System.out.println("final reward: "+reward);  
      }  
      public void playGame(){//initiates game play
        while(position<boardSize){
          spinAndMove();
          drawBoard();
        } 
        if(position>boardSize){
          round++;
        }
        if(position==boardSize){
          System.out.println("game over");
          displayReport();
        } 
      }
    }
    Last edited by ayelleeeecks; 10-20-2011 at 04:38 PM.

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  2. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  3. [Q] Loop issue (while loop)
    By iriscience in forum New To Java
    Replies: 9
    Last Post: 01-31-2011, 04:21 PM

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
  •