Saturday, 14 June 2014

Java Program To Print the Golf Scoreboard : Any number of Players

Filled under:

Java Program To Print the Golf Scoreboard : Any number of Players

Golf  is one of the niche game . In this program ,  we  print  the scoreboard for all the golfers taking part in the tournament , and also printing out the  winner of the tournament with the least number of strokes to complete all the 18  holes  in 4 days competition .

It is one of the coolest program , as we just need to submit the number of players and player scores in the text , thats it .

About Golf : Golf has 18 holes with each hole has certain minimum number of strokes to play for known as PAR score . Player has to complete all holes each day and this is a 4 day competition .

For this program we are assuming the following stats :\

GolfScore.txt

The first line contains the total number of players in the competition

Rest lines contain the score of each player corresponding to that hole .

As there are 18 holes in the game , so GolfScore.txt  contains total 19 lines .

Course.txt

It contains the PAR score of each hole

One line represents the PAR value of one hole .

It contains total of 18 lines as there are 18 holes in the game of golf .



Demo :


golf java program source code




































Please find the code below :






import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class Golf{
    static int b=1;
    
    public static void main (String args[])
    {
        String course[]= new String[100];
        int i=0,j=0,k=1,N=0;
        String holescores[]=new String[100];
        String[] tokens=new String[100];
        String subtoken[] =new String[100];
        String playerscore[] =new String[100];
        
        try
        {
            Scanner scanner = new Scanner(new File("GolfScore.txt")).useDelimiter(" ");
            while (scanner.hasNext()) {
                holescores[i] = scanner.nextLine();
                Scanner scanner1 = new Scanner(holescores[i]).useDelimiter(" ");
                while (scanner1.hasNext()) {
                    playerscore[j] = scanner1.nextLine();
                    j++;
                }  i++;
            }
            String strLine;
            N=Integer.parseInt(playerscore[0]);
            
            
            Integer player[][]=new Integer[N+1][19];
            
            int total[]=new int[N+1];
            while ((strLine = playerscore[k]) != null)   {
                tokens = strLine.split(" "); // it will split the words in the line
                for(int m=0; m<tokens.length;m++){
                    subtoken[m]=tokens[m];  // we create an array of strings to prevent  from exception
                }
                calculateTotalPlayerScore(subtoken,N,player,total);
                k++;
            }
        }
        catch (NumberFormatException e) {
            System.out.println("Cause: May be you entered the number of golfers in wrong format or You have entered the characters in the text file instead of number");
            e.printStackTrace();
            System.exit(0);
            
        }
        catch(FileNotFoundException e1){
            System.out.println("File not found ,please place the text file ");
        e1.printStackTrace();}
        
        catch(Exception e2)
        {
            System.out.println("Not enough lines in the file ");
            e2.printStackTrace();
        }
    }
    public static void calculateTotalPlayerScore(String [] s,int N,Integer[][]player,int []total )
    {
        player[1][0]=0;
        for(int a=1;a<=N;a++)
        {
            player[a][b]=Integer.parseInt(s[a-1]);
            if(b>1)
            total[a]=player[a][b-1]+total[a];
        }
        b++;
        
        if(b==19)
        {
            int par=calculateCourseParScore();
            createTable();
            for(int i=1;i<=N;i++)
            {
                // System.out.println(" Golfer "+i+" total score " + total[i]);
                finalScoreBoard(par,total[i],i);
            }
            int k=winningScore(total);
            congratulations(k,total);
        }
    }
    
    public static int calculateCourseParScore()
    {
        int i=1,j=1;
        String hole[]=new String[19];
        int parTotal[]=new int[100];
        try
        {
            Scanner scanner = new Scanner(new File("course.txt")).useDelimiter(" ");
            while (scanner.hasNext()) {
                hole[i] = scanner.nextLine();
                Scanner scanner1 = new Scanner(hole[i]).useDelimiter(" ");
                while (scanner1.hasNext()) {
                    parTotal[j] = Integer.parseInt(scanner1.nextLine());
                    parTotal[j]=parTotal[j]+parTotal[j-1];
                    j++;
                }  i++;
                
            }
            
            
            }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        return parTotal[18];
    }
    
    
    public static void createTable()
    {
        System.out.println("-----------------------------------------------------------------------------------------------------------");
        System.out.println("");
        System.out.println("   SCOREBOARD");
        System.out.println("");
        System.out.println("-----------------------------------------------------------------------------------------------------------");
        
    }
    public static void finalScoreBoard(int par,int totalindividualscore,int i)
    {   int c=totalindividualscore-par;
        if(c<0)
        System.out.println("Golfer "+i+" scores   " + (-c) +"  under par ");
        else if (c>0)
        System.out.println("Golfer "+i+" scores   " + c +"  over par ");
        else
        System.out.println("Golfer "+i+" scores   " + par +" i.e. course par ");
        System.out.println("-----------------------------------------------------------------------------------------------------------");
        System.out.println("");
    }
    
    public static int winningScore(int total[])
    {int min=total[1];
        int winnergolfer=1;
        for (int i = 1; i < total.length; i++) {
            if (min > total[i])
            {
                min = total[i];
                winnergolfer=i;
            }
            
        }
        return winnergolfer;
    }
    
    public static void congratulations(int i,int [] total)
    {
        System.out.println("");
        System.out.println("");
        System.out.println("");
        System.out.println("***********CONGRATULATIONS*****************");
        System.out.println("********************************************");
        System.out.println("** WINNER IS GOLFER "+i+" with score "+total[i]+" **");
        System.out.println("********************************************");
    }
    
}


GolfScore.txt

7
2 3 4 3 2 4 2
4 3 2 3 3 3 1
3 4 2 4 5 2 1
4 4 4 4 4 4 5
4 3 2 1 3 4 3
3 2 3 4 5 2 4
4 3 2 3 4 4 3
4 3 5 4 3 3 2
2 3 4 5 3 1 3
6 4 3 4 3 2 3
4 3 5 4 5 3 2
3 6 3 3 3 4 4
4 3 4 5 3 2 1
3 4 5 6 4 2 3
4 3 5 3 3 2 7
3 3 3 3 3 3 5
4 4 4 4 4 2 2
2 2 2 2 2 2 2


Course.txt


2
3
4
3
2
4
3
3
2
4
5
3
4
4
3
4
5
4

0 comments: