java - Coin flipping game. How do I make it so the player can get another turn after hitting a certain number of heads? -


i'm making program has 4 players taking turns tossing n coins. first player earn 16 points wins. player earns points each time tosses coins. number of points earns equals number of heads tosses. if tosses no heads loses next turn. if flips 3 heads earns turn , tosses coins again. if tosses less 3 heads next player’s turn. player must earn 16 points win. if player has 14 points , tosses 2 heads wins if tosses n heads , goes on 16 points loses half of points , loses turn too. must have 16 points win.

my problem lies within game class. can't seem player go again if tosses 3 heads in row award him turn.

coin class

import java.util.random;  public class coins {    //constants    private static final int heads = 0;    private static final int tails = 1;      //constants    private final int n_coins;     //instance variables    private int n_heads;    private int n_tails;    private random randomizer;     //constructors    public coins()    {       n_tails = 0;       n_heads = 0;       n_coins = 3;       randomizer = new random();    }     public coins(int new_n_coins) throws exception    {        if(new_n_coins < 1)          throw new exception("coins constructor number of coins less 1");        n_coins = new_n_coins;        n_tails = 0;        n_heads = 0;        randomizer = new random();    }    //custom method    public void tosscoins()    {       n_tails = 0;       n_heads = 0;       for(int toss_counter = 1; toss_counter <= n_coins; toss_counter++)       {        int coin_face = randomizer.nextint(tails + 1);        if(coin_face == heads)         n_heads++;       else         n_tails++;        }    }    //accessors    public int getncoins()    {        return n_coins;    }     public int getnheads()    {        return n_heads;    }    public int getntails()    {        return n_tails;    }  }    

player class

public class player     {       private string name;       private int state;       private int points;         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;       }     } 

state class

public class state     {         public static final int normal = 0;         public static final int extra_turn = 1;         public static final int lose_turn = 2;     } 

game class

import java.util.random; public class game     {       private random randomizer;       private final int n_players;       private final int m_coins;       private final int p_points;       private int player_index;       private boolean game_over;        public game()       {         n_players = 4;         m_coins = 3;         p_points = 16;         game_over = false;         randomizer = new random();         player_index = randomizer.nextint(n_players);       }        public game(int new_m_coins, int new_n_players, int new_p_points)       {           n_players = new_n_players;           m_coins = new_m_coins;           p_points = new_p_points;           game_over = false;           randomizer = new random();           player_index = randomizer.nextint(n_players);       }        public int getplayerindex()       {           return player_index;       }       //write mutators         public boolean gameover()       {            return game_over;        }        public int nextplayer(player[] players)       {           player_index = (player_index + 1) % n_players;            if(players[player_index].getstate() == state.extra_turn)           {              players[player_index].setstate(state.normal);           }           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 oldpoints, int newpoints)       {              int player_points = player.getpoints();              if(player_points == p_points)                 game_over = true;             else if(player_points > p_points)             {                 player.setpoints(player_points / 2);                 player.setstate(state.lose_turn);             }             else if(player_points == oldpoints + m_heads)             {                 player.setstate(state.extra_turn);             }             else                 player.setstate(state.normal);        }     } 

testcoingame

public class testcoingame     {       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;                     {               player_index = game.nextplayer(players);               system.out.printf("it %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 n_heads = coins.getnheads();               system.out.printf("%s tossed %d heads\n",               players[player_index].getname(), n_heads);                int old_points = players[player_index].getpoints();               int new_points = old_points + n_heads;               players[player_index].setpoints(new_points);               game.computestate(players[player_index], n_heads, old_points, 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)          {          }       }     } 

edited

just remove first line player_index = (player_index + 1) % n_players; in game.nextplayer()


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -