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 3 of 3

Thread: help with board game assignment

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with board game assignment

    I was given this assignment:
    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.

    and given this driver:
    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();
      }
    }

    This is the code that I wrote to go with the driver:
    import java.util.*;
     
    public class Opoly{
     
      private int width;
      private  int spinner;
      private int o;
      int[] opoly;
      private int currentPos;
      private int moves;
      private int reward;
      private int round;
     
     public Opoly(int o){
       this.o = o;
       opoly = new int[o];
       currentPos = 0;
       reward = 12;
     }
     
     public void spin(){
     spinner = (int)(Math.random()*6);//generates a random integer between 1 and 5 and stores it in spinner
     }
     
      public void move(){
        currentPos = currentPos+spinner;
      }
     
      public int spinAndMove(){
        round=0;
        while(currentPos < opoly.length-1){
        spin();
        move();
        round++;
        if(currentPos % 5==0){
          reward=reward*2;
        }
        if(currentPos==opoly.length-1){
          currentPos=currentPos;
          break;
        }
        if(currentPos > opoly.length-1){
          currentPos=currentPos;
          round++;
        }
        if(currentPos==opoly.length-2){
          currentPos=0;
          reward=reward/5;
      }
        if(currentPos % 5==0)
          reward=reward*2;
      }
        return round;
      }
      public void isGameOver(){
        if(moves==opoly.length-1)
          System.out.println("game over");
      }
     
      public void drawBoard(){
        while(opoly.length<currentPos){
          System.out.print('*');
          if(opoly.length>currentPos){
            System.out.print('*'+reward);
          }
          else
            System.out.print('O');
        }
        }
     
      public void displayReport(){
        isGameOver();
        System.out.println("Total rounds: "+round);
        System.out.println("Total reward: "+reward);
      }
     
       public void playGame(){
         while(currentPos !=opoly.length-1){
           spinAndMove();
           drawBoard();
         if(currentPos==opoly.length-1){
           drawBoard();
         displayReport();
         break;
         }}
       }

    This only prints out the end result of the game, but doesn't print out the drawBoard method. Any help is appreciated!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: help with board game assignment

    I guess you're in the same class as this guy: http://www.javaprogrammingforums.com...ile-loops.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: help with board game assignment

    At least on my screen, you didn't post the code that you need help with. (On iDevice, so it may just be the scroing) Either way, have you tried going through the code on pencil and paper? Write down all your variable (I actually have gotten quite fond of putting each variable on a flashcard then stacking the variables in the same class next to each other; however that can get to be a lot of flashcards if you have a lot of variables) , cross out/erase the variable and put the new value if it changes, and see if your code still works? Then figure out the problem method/loop, and then ask for help if you still can't figure it out. You don't need to show what each variable is unless that variable is a problem. EG you have a py triple method, that is initiated for some reason. Instead of showing how the variables are initialized, write the precondition, and what the postcondition/return value should be.

    /*Preconditon: A and B are two positive doubles
     *Returns the hypotenuse using the py theorem. 
    */
    public double getHypot()
    {
      return Math.sqrt(A*A + B*B);
    }
    Last edited by Tjstretch; October 19th, 2011 at 01:08 PM.

Similar Threads

  1. Programming USB Relay Board
    By Infinity8 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 23rd, 2013, 03:38 AM
  2. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  3. Chess Board GUI full of buttons
    By falkowa42 in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2011, 07:07 AM
  4. Help with score board!
    By vlan in forum Java Applets
    Replies: 0
    Last Post: June 2nd, 2010, 04:34 AM
  5. DOS board game
    By SageNTitled in forum Java Theory & Questions
    Replies: 5
    Last Post: March 4th, 2010, 03:53 PM