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: Opoly game

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    Mass
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Opoly game

    Opoly Game Rules:
    Reward starts at 20
    1. Board cells divisible evenly by 5 double the reward
    2. If the piece lands on the second to last cell of the board, reward is reduced to 1/4 of its current value.
    3. If a spin would move the piece beyond the end of the board, the piece should not advance at all and its current reward remains unchanged unless rule 6 is met.
    4. If the piece lands on the second to last cell of the board, the reward is only reduced to 1/4 and no other points are given, even if rule 1 is met.
    5. If the piece ever lands on board position boardSize/2, 25 points are added to the reward.
    6. Every tenth move, the reward is reduced by 10.

    The Opoly class must make use of the following methods:
    spin
    move
    spinAndMove
    isGameOver
    drawBoard
    displayReport

    OpolyDriver class:

    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();
      }
    }

    My Code: Opoly class

    public class Opoly{
     
      int reward = 20;
      int boardSize;
      int pos = 0;
      int round = 0;
     
      int position;
      boolean moveCheck;
      int a = 0;
     
      // Creates an opoly method to take in boardSize
      public Opoly(int boardSize){
        this.boardSize = boardSize;
      }
     
      // Generates a random number between 1 and 5
      public void spin(){
          position = pos + (int)(1+ Math.random()*5);
      }
     
      // Based on the value of "Position" in the spin() method, move() determines if and where
      // that value is on the board, then gives pos a value based on the rules of the game.
      public void move(){
          if(position <= boardSize){
            pos = position;
            moveCheck = true;  //moveCheck keeps track of if pos has moved and able to get points
          }
          if(position == (boardSize - 1)){
            pos = 0;
            System.out.println("hit second to last position");
            moveCheck = true;
          }
          else{moveCheck = false;}
     
          //Gives "a" a value depending on whether moveCheck is true or false.
          if(moveCheck == true){a = a+2;}
          else{a = a+1;}
      }
     
      // Calls on the spin() and move() methods to play a round
      public void spinAndMove(){
        spin();
        move();
        round = round + 1;
      }
     
      // Based on where pos is on the board, this method calculates the reward score
      public void rewardCalc(){
        if((pos%5 == 0) && (pos != 0) && (a%2 == 0) && (pos != boardSize - 1) && (pos != (boardSize/2))){
          reward = (reward * 2);
          System.out.println("DOUBLE!");
        }
        if(pos == boardSize-1){
          reward = (reward/4);
        }
        if(pos == (boardSize/2)){
          reward = (reward + 25);
        }
        if(round%10 == 0){
          reward = reward - 10;
        }
      }
     
      // Draws each line of the board as the game is played
      public void drawBoard(){
        for(int j = 0; j<= boardSize; j++){
          if(j == pos){System.out.print("o");}
          else{System.out.print("*");}
      }
            System.out.println(" " + reward);
      }
     
      // Determines if the game is over
        public boolean isGameOver(){
        if(pos == boardSize){return true;}
        else{return false;}
        }
     
    // Main method which calls on the other methods to play a whole game of Opoly
      public void playGame(){
        do {
          drawBoard();
          spinAndMove();
          rewardCalc();
        } while((pos != boardSize) && (isGameOver() == false));
     
        if(isGameOver() == true){
          for(int j = 0; j<1; j++){
            drawBoard();
            displayReport();
          }
        }
      }
     
      // Prints the final reward and rounds of play to the console
      public void displayReport(){
        System.out.println("");
        System.out.println("Rounds of play: " + round);
        System.out.println("Final Reward: " + reward);
    }
      }

    I'm having trouble having the board displayed correctly for both the first and last round. As the drawBoard() method is right now, it is adding a cell to each line of the board. the o should count as a cell and take the place of the * at whatever position it is at that round.
    I tried changing
    for(int j = 0; j<= boardSize; j++)
    to
    for(int j = 0; j<= boardSize-1; j++)
    But then it isn't showing the last line of the game, and the value of the second to last cell is messed up.

    In addition to that problem, if anyone has any advice of cleaning the code up a little bit.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Opoly game

    the o should count as a cell and take the place of the * at whatever position
    Which lines of code contain the logic for this? Locate them and test them to verify they perform as expected.


    for(int j = 0; j<= boardSize; j++)
    for(int j = 0; j<= boardSize-1; j++)
    Do you understand what you are changing? Do you understand what boardSize is as in it's actual value? Print it out if you are curious, and just for giggles print out j right beside it. {System.out.println("j=" + j + " / " + boardSize);} or something to the like.

Similar Threads

  1. Game Maker Language and Game Maker and Zelda Classic thread
    By Fira in forum Other Programming Languages
    Replies: 3
    Last Post: April 17th, 2012, 08:59 AM
  2. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 08:21 PM
  3. Game
    By aneeeeen in forum Paid Java Projects
    Replies: 2
    Last Post: April 16th, 2011, 08:32 AM
  4. Game !
    By Ahmed.Ibrahem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 07:01 PM
  5. Game 3x3
    By Koren3 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 07:43 PM