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

Thread: Simple java board game exercise

  1. #1
    Junior Member
    Join Date
    Jan 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Simple java board game exercise

    Hello i have recently started to learn java from book "Introductory Java" by David Parsons and i got stuck on exercises trying to go back all way and can't find how to do the exercise but even tho i would like to see how it should look like so maybe it will stuck in my mind if someone can help with that would really appreciate
    Exercises:
    1. Create a class called 'counter' to represent the type of counter used to play board games. It should have attributes to represent its colour and the number of the square it is currently on. It should have methods to set and return its colour. It should also have a method to move to a given position on the board using an integer parameter and another method to return its current position. The counter should begin on square zero(i.e., off the board) and its default colour should be white.
    2. Create a class called 'BoardGame'. This class should create two counters of different colours. For each counter, use a dice object to move it with four throws of the dice. Display the various positions of the two counters as they are moved.
    3. Many games insist that the player throw a six before being allowed to start. Use an 'if' statement to write a start BoardGame class that checks if the counters have thrown a six on the dice. To avoid too much tedious code, give each counter only two throws, and display whether or not either has thrown a six(and is therefore able to start). If a counter throws a six on its first go, move the counter on its second throw.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Simple java board game exercise

    Do you have any specific java programming questions?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member azirafaelsish's Avatar
    Join Date
    Jan 2024
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple java board game exercise

    Hmmm I love simple games, but now I'm too busy to play for a long time to get some wins

  4. #4
    Junior Member tiamatzzo's Avatar
    Join Date
    Jun 2023
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simple java board game exercise

    I'd suggest focusing on creating a Counter class. Define attributes for color and position, and methods to set and return the color, move the counter to a given position, and return its current position. Remember, the counter starts on square zero by default with a white color.
    In the BoardGame class, you'll create two counters of different colors and use a dice object to simulate movement. Implement the logic for throwing a six before starting the game, and display the positions of the counters as they move.
    And hey, if you need a breather from coding, why not take a break and enjoy a game of Spades? It's a classic card game that's perfect for unwinding and having some fun.
    Last edited by tiamatzzo; March 4th, 2024 at 10:09 AM.

  5. #5
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Simple java board game exercise

    Sure, here's a possible solution for the exercises:

    ```java
    import java.util.Random;

    class Counter {
    private String color;
    private int position;

    public Counter() {
    this.color = "white";
    this.position = 0;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public String getColor() {
    return color;
    }

    public void moveToPosition(int newPosition) {
    this.position = newPosition;
    }

    public int getCurrentPosition() {
    return position;
    }
    }

    class BoardGame {
    private Counter counter1;
    private Counter counter2;
    private Random dice;

    public BoardGame() {
    counter1 = new Counter();
    counter2 = new Counter();
    dice = new Random();
    }

    public void playGame() {
    int throw1 = dice.nextInt(6) + 1; // Roll the dice for counter 1
    int throw2 = dice.nextInt(6) + 1; // Roll the dice for counter 2

    System.out.println("Counter 1 rolled: " + throw1);
    System.out.println("Counter 2 rolled: " + throw2);

    if (throw1 == 6) {
    counter1.moveToPosition(1);
    System.out.println("Counter 1 moved to position 1");
    } else {
    System.out.println("Counter 1 did not roll a 6.");
    }

    if (throw2 == 6) {
    counter2.moveToPosition(1);
    System.out.println("Counter 2 moved to position 1");
    } else {
    System.out.println("Counter 2 did not roll a 6.");
    }
    }
    }

    public class Main {
    public static void main(String[] args) {
    BoardGame game = new BoardGame();
    game.playGame();
    }
    }
    ```

    This code creates two classes: `Counter` and `BoardGame`, as per the exercise requirements. The `Counter` class represents a game piece with color and position attributes, along with methods to set and return its color, move to a given position, and return its current position. The `BoardGame` class creates two counters of different colors and uses a random number generator to simulate dice throws for each counter. It checks if either counter throws a 6, and if so, it moves the counter accordingly. Finally, it displays the results of the dice throws and the movement of the counters.

    Finally, it displays the results of the dice throws and the movement of the counters. If you ever find yourself needing additional help with Java assignment or homework, there are various online resources available like like programminghomeworkhelp.com to provide guidance and support.

Similar Threads

  1. Problem in my 2-Dimensional Array Board Game
    By rodneybunkley in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 2nd, 2024, 07:02 AM
  2. Help with assignment about a board game (enum and array question)
    By Kranti1992 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 12th, 2012, 07:47 AM
  3. Java TV Board Game/Grid + Moving objects
    By Massaslayer in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 08:03 AM
  4. help with board game assignment
    By pjay in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 19th, 2011, 12:58 PM
  5. DOS board game
    By SageNTitled in forum Java Theory & Questions
    Replies: 5
    Last Post: March 4th, 2010, 03:53 PM