Results 1 to 9 of 9
  1. #1
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Board game - player's names aren't removed from squares

    In a board game application, I tried the following code so that when a player's name is on two squares, it goes away from the previous square:
    Java Code:
            for (int e = 0; e < squares.length; e++) { //squares is an array containing all the labels that players 
    //play on  
                tmp = squares[e].getText(); //this is supposed to be the text the squares originally had without 
    //any player's name 
      
            for (int i=0; i < 6; i++) { //i=6 because the die has 6 numbers. If a square 1-6 before another 
    //square has the player's name it should be removed from it. 
                if (squares[e].getText().equals(plyr1+tmp)) {
                        squares[e-i].getText().replaceAll(tmp+plyr1,tmp);                 
                }
                }
                    
            }
    The problem is that the players' names aren't removed from previous squares once they go on a square 1-6 places ahead. How should I solve it? Thank you.

    Edit - I also don't know how to switch between players' turns. I have their names listed on the board. I want to, first of all, highlight a player's name when it's their turn, and, secondly, dehighlight it when their turn is over. I also want to change their score (each square on the board has a number that increases/decreases their points. How should I change the player's score depending on the number on the square?). When it's not a player's turn, their name on the board should stay stationary and the name of the player whose turn it is should move. For this, I need to start with all the players' names on the first label, "Start," and move their names according to whose turn it is. I do not know how to do this. Can anyone explain how to do any of this?
    Last edited by Anne; 08-20-2015 at 05:56 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Board game - player's names aren't removed from squares

    It looks like your inner loop compares the same thing each time. And once you fix that there is a potential problem here. i increments faster than e, which means e-i could be < 0 which arrays don't like.

    In the future, please wrap you code in code tags. BB Code List - Java Programming Forum - Learn Java Programming
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Board game - player's names aren't removed from squares

    How should I fix that problem, and what causes it? Why does i increment faster than e? "i" is supposed to refer to any square players play on. If two squares, one i and the other i minus any number from 1-6, have a player's name - plyr1 - on them, then from the square that comes before plyr1 should be removed. I don't understand why this is not working as it's supposed to.

    Also, there are more than 6 squares, so since e refers to the squares, e-i shouldn't be less than 0. I don't know why the inner loop compares the same thing each time either. How should I solve that problem and make the loops & if statement work properly?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Board game - player's names aren't removed from squares

    Quote Originally Posted by Anne View Post
    How should I fix that problem, and what causes it? Why does i increment faster than e?
    Let's examine the inner loop in isolation.

    let say e = 0
    Java Code:
    tmp = squares[e].getText();
    
    for (int i=0; i < 6; i++) { //i=6 because the die has 6 numbers. If a square 1-6 before another 
                                   //square has the player's name it should be removed from it. 
        if (squares[e].getText().equals(plyr1+tmp)) {
                 squares[e-i].getText().replaceAll(tmp+plyr1,tmp);              
        }
    }
    tmp is not changing inside the loop. Nor is plyr1. And e is still 0 for all six iterations. i increments faster than e because i increments 6 times for every one time of e. You can verify it yourself by doing the following:

    Java Code:
    for (int e = 0; e < 6; e++) {
       for (int i = 0; i < 6; i++) {
          System.out.println("e = " + e + ", i = " + i);
       }
    }
    The best way to solve problems like this is with pencil and paper. Draw your 6 squares on paper and record the values of e and i. Then play computer and go thru each iteration and see what you would do. Then you should try and code the exact algorithm you used.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Board game - player's names aren't removed from squares

    Okay, I put system. Out. Println in the loop and found that. But how should I fix that problem?
    Other than that, I need to know how to keep track of players' turns and give them points depending on the number on the square they landed on. I also need to switch between turns and highlight the name of the player whose turn it is.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    34

    Default Re: Board game - player's names aren't removed from squares

    how should I fix that problem?
    Did you do what Jim suggested at the end of post#4?
    You need to work out the logic for the code before writing it.
    Can you post here the list of steps the program needs to take
    so that when a player's name is on two squares, it goes away from the previous square?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Anne is offline Member
    Join Date
    Aug 2015
    Posts
    18
    Rep Power
    0

    Default Re: Board game - player's names aren't removed from squares

    Yes, I did that and found that i increments faster, but don't know why. I think that the first step is:

    1.) Add a listener to keep track of how many times the "Roll" button, which rolls a die, is pressed. This determines whose turn is.

    But that step isn't really what I want to ask - what I need to know is how to highlight labels with player names based on whose turn it is. I also want to know how to ensure that a player's name isn't on multiple labels at once; once it randomly moves to a label 1-6 places ahead, the name should go away from the previous label. For this, maybe the steps are to add another array and use a for loop like the one I made in post 1. Here's what I think the code's structure to switch between turns is, but how should I switch between turns?

    Java Code:
      dicerolled++; //listener for "Roll" button 
            
       if (dicerolled%4 == 1) {
                //highlight player 1's name. Keep their name wherever it rolled. 
            }
            else if (dicerolled%4 == 2) {
                //same as previous comment 
            }
            else if (dicerolled%4 == 3) {
                
            }
            else {
                
            }
    I am also making a memory game for the board game. I don't know how to add a timer to it. For this, I can add a listener to detect when "Start Timer" is pressed but how should I start the timer? Any help will be appreciated.
    Last edited by Anne; 08-21-2015 at 03:47 AM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    34

    Default Re: Board game - player's names aren't removed from squares

    how to highlight labels with player names based on whose turn it is.
    What would highlighting of a label look like? Would changing the background color do it?

    how to ensure that a player's name isn't on multiple labels at once; once it randomly moves to a label 1-6 places ahead, the name should go away from the previous label.
    What is the logic for doing that? Is that the problem you were asking about earlier?
    Make a list of the steps you need to take to solve that problem. Don't try to write any code until you have the logic.

    how should I switch between turns
    How many players are there to switch between? Have a method that returns the next player based on who the last player was. For example with three players, if the last player was #3 the next will be #1
    Last edited by Norm; 08-21-2015 at 04:21 AM.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Join Date
    Aug 2015
    Location
    Northern California
    Posts
    31
    Rep Power
    0

    Default Re: Board game - player's names aren't removed from squares

    I believe this is cross posted to stackoverflow.com.

    An,

    Create a field for Player ID I would use an int here but I would not allow a player ID of 0. If the int has a value of 0 then a new game is started. So assign the default first player to the int.

    Java Code:
    if(intPlayerID==0){System.out.println("Starting new game..."); intPlayerID=1;}

Similar Threads

  1. Board method for Tic Tac Toe game
    By ecc.soccer in forum Forum Lobby
    Replies: 2
    Last Post: 11-26-2013, 08:23 PM
  2. Java board game
    By sumys in forum New To Java
    Replies: 10
    Last Post: 03-18-2012, 09:23 PM
  3. Help with JAVA board game
    By corky1501 in forum New To Java
    Replies: 5
    Last Post: 02-09-2011, 03:18 AM
  4. need help making a game board
    By Don k in forum New To Java
    Replies: 2
    Last Post: 04-30-2010, 12:09 AM
  5. game board design
    By tomitu in forum New To Java
    Replies: 12
    Last Post: 02-27-2010, 05:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •