# Problem with using a boolean to skip turn(coin game)

• (2 Pages)
• 1
• 2

## 28 Replies - 1386 Views - Last Post: 14 May 2013 - 03:10 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=321072&amp;s=dce728be472368a4475525c3b6140a9c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jawhnay

Reputation: 0
• Posts: 13
• Joined: 14-May 13

# Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 12:50 PM

I'm having trouble using a boolean to make the program skip a player in a coin game if he meets the certain criteria which is going over 16 points or tossing 0 heads. The players go in order starting with Tom, Hank, Hannah, and Tina. If Hank tosses 0 heads then the order after Hank's turn should be Hannah, Tina, Tom, Hannah, Tina, Tom, and Hank. Then the order is back to normal if Hank does not toss 0 heads again. Can anyone help me make the boolean work that is within the
```nextPlayer()
```
method. It is
```players[player_index].loseTurn(false)
```

```public int nextPlayer(Player[] players)
{
//if player has extra turn then set the state to normal and give the player an extra turn.
if(players[player_index].getState() == State.EXTRA_TURN)
{
players[player_index].setState(State.NORMAL);
}

else if(players[player_index].getState() == State.LOSE_TURN)
{
players[player_index].loseTurn(false);
player_index = (player_index + 1) % n_players;
}
//move on to next player.
else
{
player_index = (player_index + 1) % n_players;
}

/*while(players[player_index].getState() != State.NORMAL)
{
players[player_index].setState(State.NORMAL);
player_index = (player_index + 1) % n_players;
}*/
return player_index;
}
public void computeState(Player player, int m_heads)
{

int player_points = player.getPoints();

//criteria for the different states. lose turn, extra turn, normal.
if(player_points == p_points)
game_over = true;
else if(player_points > p_points)
{
player.setPoints(player_points / 2);
player.setState(State.LOSE_TURN);
}
{
player.setState(State.LOSE_TURN);
}
{
player.setState(State.EXTRA_TURN);
}
else if(m_heads == 3 && player_points > p_points)
{
player.setState(State.NORMAL);
}
else
player.setState(State.NORMAL);

}
```

Is This A Good Question/Topic? 0

## Replies To: Problem with using a boolean to skip turn(coin game)

• D.I.C Regular

Reputation: 40
• Posts: 434
• Joined: 08-January 12

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 01:05 PM

I am confused how would you lose a turn if you are calling a function that moves to the next player. This makes me think the player already had a turn?

Also is this inside of a loop that we can not see?

### #3 jawhnay

Reputation: 0
• Posts: 13
• Joined: 14-May 13

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 01:11 PM

Yeah, I'm I added the fuction to move onto the next player because I was hoping that would in some way make the boolean work. I tried doing it without the line after the boolean, but it ended up making the player get another turn when he is suppose to lose one.

Below is the code for the loop that I have.

```public static void main(String[] args)
{
try
{
int m_coins = 3;
int n_players = 4;
int p_points = 16;
String [] names = {"Hank", "Tina", "Hannah", "Tom"};
Player [] players = new Player[n_players];

for(int index = 0; index < players.length; index++)
players[index] = new Player(names[index]);

Coins coins = new Coins();
Game game = new Game();
int player_index;
do
{
player_index = game.nextPlayer(players);
System.out.printf("It is %s's turn\n", players[player_index].getName());
System.out.printf("%s has %d points\n", players[player_index].getName(),
players[player_index].getPoints());

coins.tossCoins();

int old_points = players[player_index].getPoints();
int new_points = old_points + n_heads;
players[player_index].setPoints(new_points);
System.out.printf("%s has %d points\n", players[player_index].getName(),players[player_index].getPoints());
}
while(!game.gameOver());
System.out.printf("%s wins!\n", players[player_index].getName());
}
catch(Exception ex)
{
}
}
```

• D.I.C Regular

Reputation: 40
• Posts: 434
• Joined: 08-January 12

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 01:11 PM

Wouldn't it be more like this

```if(players[player_index + 1].getState() == State.LOSE_TURN)
{
// The next player loses the turn not this player.
players[player_index + 1].loseTurn(false);
//Move to the player after the next one
player_index = (player_index + 2) % n_players;
}

```

This post has been edited by Toadill: 14 May 2013 - 01:14 PM

### #5 jawhnay

Reputation: 0
• Posts: 13
• Joined: 14-May 13

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 01:15 PM

I want that player to lose his NEXT turn though. I don't want to next player in line to lose their turn. I tried using that code and it ended up messing up my program and stopping it completely if a player tosses 0 heads.

• D.I.C Regular

Reputation: 40
• Posts: 434
• Joined: 08-January 12

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 01:23 PM

```public static void main(String[] args)
{
try
{
int m_coins = 3;
int n_players = 4;
int p_points = 16;
String [] names = {"Hank", "Tina", "Hannah", "Tom"};
Player [] players = new Player[n_players];

for(int index = 0; index < players.length; index++)
players[index] = new Player(names[index]);

Coins coins = new Coins();
Game game = new Game();
int player_index;
do
{
//Calls next game.nextPlayer and returns the index number of the player

player_index = game.nextPlayer(players);
//Prints the players name

System.out.printf("It is %s's turn\n", players[player_index].getName());
//Prints the players points

System.out.printf("%s has %d points\n", players[player_index].getName(),

players[player_index].getPoints());

coins.tossCoins();

int old_points = players[player_index].getPoints();
int new_points = old_points + n_heads;
players[player_index].setPoints(new_points);
System.out.printf("%s has %d points\n", players[player_index].getName(),players[player_index].getPoints());
}
while(!game.gameOver());
System.out.printf("%s wins!\n", players[player_index].getName());
}
catch(Exception ex)
{
}
}

```

```public int nextPlayer(Player[] players)
{
//What is the starting value of player_index?
//SET STATE.EXTRA_TURN
if(players[player_index].getState() == State.EXTRA_TURN)
{
//Use the CURRENT PLAYER and set it to normal 		 players[player_index].setState(State.NORMAL);
}
//Is it possible to have an extra turn then lose a turn
// as well if so they should both be if statement rather
// then else if
//State equals LOSE_TURN for the current player
else if(players[player_index].getState() == State.LOSE_TURN)
{
players[player_index].loseTurn(false);
player_index = (player_index + 1) % n_players;
}
//move on to next player.
else
{
player_index = (player_index + 1) % n_players;
}

/*while(players[player_index].getState() != State.NORMAL)
{
players[player_index].setState(State.NORMAL);
player_index = (player_index + 1) % n_players;
}*/
return player_index;
}

```

This post has been edited by Toadill: 14 May 2013 - 01:43 PM

### #7 jawhnay

Reputation: 0
• Posts: 13
• Joined: 14-May 13

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 01:34 PM

What did you edit?

• D.I.C Regular

Reputation: 40
• Posts: 434
• Joined: 08-January 12

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 01:48 PM

okay this should be right but why are you using the modulus symbol
I guess you use this incase a state is not set.
```//move on to next player.
else
{
player_index = (player_index + 1) % n_players;
}

```

Why not just make it

```else
{
player_index = (player_index + 1);
}

```

Same here

```else if(players[player_index].getState() == State.LOSE_TURN)
{
players[player_index].loseTurn(false);
player_index = (player_index + 1) % n_players;
}

```

```else if(players[player_index].getState() == State.LOSE_TURN)
{
players[player_index].loseTurn(false);
player_index = (player_index + 1);
}

```

This post has been edited by Toadill: 14 May 2013 - 01:49 PM

### #9 jawhnay

Reputation: 0
• Posts: 13
• Joined: 14-May 13

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 01:50 PM

This is how my professor suggested that we do the player_index. I can't recall why, but I don't think he had a specific reason.

• D.I.C Regular

Reputation: 40
• Posts: 434
• Joined: 08-January 12

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 01:56 PM

Well it is pointless the way it looks to use a modulus operator there. Although it looks like it gives you the same answer.

If you are afraid of messing it up copy the whole project to another area then open that and change the code to see if it works.

I think I may see the problem you are not setting a bool value anywhere. In order to skip the player.
Just create another player method to get the bool value.

``` players[player_index].loseTurn(false);

```

```  public void computeState(Player player, int m_heads)
{

int player_points = player.getPoints();

//criteria for the different states. lose turn, extra turn, normal.
if(player_points == p_points)
game_over = true;
else if(player_points > p_points)
{
player.setPoints(player_points / 2);
player.setState(State.LOSE_TURN);
player.loseTurn(true);
}
{
player.setState(State.LOSE_TURN);
}
{
player.setState(State.EXTRA_TURN);
}
else if(m_heads == 3 && player_points > p_points)
{
player.setState(State.NORMAL);
}
else
player.setState(State.NORMAL);

}

```

This post has been edited by Toadill: 14 May 2013 - 02:14 PM

### #11 CasiOo

• D.I.C Lover

Reputation: 1577
• Posts: 3,551
• Joined: 05-April 11

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 02:00 PM

Sooooooo maybe using recursion would be a pretty solution
```public int nextPlayer(Player[] players) {
currentPlayer = (currentPlayer + 1) % n_players;
Player player = players[currentPlayer];

if (player.getState() == State.NORMAL || player.getState() == State.EXTRATURN) {
player.setState(State.NORMAL);
return currentPlayer;
}

player.setState(State.NORMAL);
return nextPlayer(players);
}

```

### #12 jawhnay

Reputation: 0
• Posts: 13
• Joined: 14-May 13

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 02:15 PM

Toadill, on 14 May 2013 - 01:56 PM, said:

Well it is pointless the way it looks to use a modulus operator there. Although it looks like it gives you the same answer.

If you are afraid of messing it up copy the whole project to another area then open that and change the code to see if it works.

I think I may see the problem you are not setting a bool value anywhere. In order to skip the player.
Just create another player method to get the bool value.

``` players[player_index].loseTurn(false);

```

```  public void computeState(Player player, int m_heads)
{

int player_points = player.getPoints();

//criteria for the different states. lose turn, extra turn, normal.
if(player_points == p_points)
game_over = true;
else if(player_points > p_points)
{
player.setPoints(player_points / 2);
player.setState(State.LOSE_TURN);
player.loseTurn(true);
}
{
player.setState(State.LOSE_TURN);
}
{
player.setState(State.EXTRA_TURN);
}
else if(m_heads == 3 && player_points > p_points)
{
player.setState(State.NORMAL);
}
else
player.setState(State.NORMAL);

}

```

I created the boolean method in the Player class. Below is the player class.

```public class Player
{
private String name;
private int state;
private int points;
private boolean lose_turn;

public Player()
{
state = State.NORMAL;
points = 0;
name = "no name";
}

public Player(String new_name) throws Exception
{
state = State.NORMAL;
points = 0;
setName(new_name);
}
//accessors
public int getState()
{
return state;
}
public int getPoints()
{
return points;
}
public String getName()
{
return name;
}
//mutators
public void setState(int new_state)
{
state = new_state;
}
public void setPoints(int new_points)
{
points = new_points;
}
public void setName(String new_name) throws Exception
{
if(new_name.length() == 0)
throw new Exception("setName error - empty name");
name = new_name;
}

//predicate
public boolean loseTurn(boolean lose)
{
lose_turn = lose;
return lose_turn;
}
}
```

• D.I.C Regular

Reputation: 40
• Posts: 434
• Joined: 08-January 12

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 02:15 PM

```  public void computeState(Player player, int m_heads)
{

int player_points = player.getPoints();

//criteria for the different states. lose turn, extra turn, normal.
if(player_points == p_points)
game_over = true;
else if(player_points > p_points)
{
player.setPoints(player_points / 2);
player.setState(State.LOSE_TURN);
player.loseTurn(true);
}
{
player.setState(State.LOSE_TURN);
}
{
player.setState(State.EXTRA_TURN);
}
else if(m_heads == 3 && player_points > p_points)
{
player.setState(State.NORMAL);
}
else
player.setState(State.NORMAL);

}

```

### #14 jawhnay

Reputation: 0
• Posts: 13
• Joined: 14-May 13

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 02:16 PM

CasiOo, on 14 May 2013 - 02:00 PM, said:

Sooooooo maybe using recursion would be a pretty solution />
```public int nextPlayer(Player[] players) {
currentPlayer = (currentPlayer + 1) % n_players;
Player player = players[currentPlayer];

if (player.getState() == State.NORMAL || player.getState() == State.EXTRATURN) {
player.setState(State.NORMAL);
return currentPlayer;
}

player.setState(State.NORMAL);
return nextPlayer(players);
}

```

I am more interested in getting this program done than I am in making it look pretty

### #15 jawhnay

Reputation: 0
• Posts: 13
• Joined: 14-May 13

## Re: Problem with using a boolean to skip turn(coin game)

Posted 14 May 2013 - 02:23 PM

```player.loseTurn(true);