Monthly Archives: July 2016

Arranging scores in a Game Board.

Problem Statement: Let us say a scenario where player’s scores are displayed in a Game Board. As and when the player scores, it should be inserted in the proper position in the Game Board.

Assumptions:

  1. We assume maximum number of scorecards to be inserted in the Game Board to be fifty.
  2. The player names must be unique in a Game Board.

Solution: A simple approach is:

  1. When we get a scorecard, we iterate through the existing Game Board as insert it in the proper position.
  2. When we have inserted it in the proper position, all the next scorecards has to be shifted right and score card length will be incremented by 1.
  3. If we want to remove a score card from the Game Board, we search it by name and remove it. Also, we shift the array left by one position.

Code:

 

public class GameBoard {
 ScoreCard[] scorecards;
 static int scorecardlength;

public GameBoard() {
 this.scorecards = new ScoreCard[50] ;
 scorecardlength = 0;
 }

public GameBoard(ScoreCard[] scorecards) 
 {
 this.scorecards = scorecards ;
 }

public int insertScoreCard(ScoreCard myscorecard)
 {
 int i = 0;
 try {
 do
 {
 
 if(i == scorecardlength)
 {
 scorecards[i] = myscorecard;
 scorecardlength++;
 break;
 }
 
 if(scorecards[i].playerName.equals(myscorecard.playerName))
 {
 System.out.println("Player "+myscorecard.playerName+" already mentioned in GameBoard. This will be skipped. ");
 break;
 }
 
 if(scorecardlength > 0)
 {
 if(myscorecard.score > scorecards[i].score)
 {
 scorecardlength++;
 ScoreCard temp = scorecards[i];
 scorecards[i] = myscorecard;

int iterator = i+1;
 while(iterator <= scorecardlength)
 {
 ScoreCard tempnext = scorecards[iterator];
 scorecards[iterator] = temp;
 temp = tempnext;
 iterator++;
 }
 break;
 } 
 }
 else
 {
 scorecards[i] = myscorecard;
 scorecardlength++;
 break;
 }

i++;
 }
 while( i <= scorecardlength);
 } catch (Exception e) {
 e.printStackTrace();
 }
 return scorecardlength;

}
 
 
 public void removeScoreCard(String playerName)
 {
 for(int i=0; i <scorecardlength ; i++)
 {
 if(scorecards[i].playerName.equals(playerName))
 {
 int iterator = i;
 while(iterator < scorecardlength-1 )
 {
 scorecards[iterator] = scorecards[iterator+1];
 iterator++;
 }
 scorecardlength--; 
 break;
 }
 }
 }
 
 public int getScoreCards()
 {
 return scorecardlength;
 }
 public void displayGameBoard()
 {
 for(int i=0;i<scorecardlength;i++)
 {
 if(scorecardlength >0 )
 {
 System.out.println("Player "+scorecards[i].playerName+" scored "+scorecards[i].score+" score. You are on "+(i+1)+" position.");
 }
 else
 {
 System.out.println("No scorecards in the Game Board.");
 }
 }
 }
 
 
 public static void main(String[] args) {
 GameBoard gameboard = new GameBoard();
 
 
 gameboard.insertScoreCard(new ScoreCard("player1", 10));
 gameboard.insertScoreCard(new ScoreCard("player2", 20));
 gameboard.insertScoreCard(new ScoreCard("player3", 5));
 gameboard.insertScoreCard(new ScoreCard("player4", 10));
 gameboard.insertScoreCard(new ScoreCard("player5", 10));
 gameboard.insertScoreCard(new ScoreCard("player6", 10));
 gameboard.insertScoreCard(new ScoreCard("player7", 10));
 gameboard.insertScoreCard(new ScoreCard("player8", 10));
 gameboard.insertScoreCard(new ScoreCard("player1", 10));
 gameboard.insertScoreCard(new ScoreCard("player2", 20));
 gameboard.insertScoreCard(new ScoreCard("player3", 5));
 gameboard.insertScoreCard(new ScoreCard("player4", 10));
 gameboard.insertScoreCard(new ScoreCard("player5", 10));
 gameboard.insertScoreCard(new ScoreCard("player6", 10));
 gameboard.insertScoreCard(new ScoreCard("player7", 10));
 gameboard.insertScoreCard(new ScoreCard("player8", 10));
 
 gameboard.displayGameBoard();
 }
}

class ScoreCard
{
 String playerName ; 
 int score;
 public ScoreCard() {

}

ScoreCard(String playerName, int score)
 {
 this.playerName = playerName ;
 this.score = score ;
 }

}

Keep Safe and Happy Coding ­čÖé