Click here to Skip to main content
15,557,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Bill, Tommy and Daisy are playing basket ball. Bill's father is capturing the score. Each player gets 2 chances. prompt the user to input the player's name who scored the basket. If a player is not scoring in his/her chance, enter a space.

Quote:
Scoring Condition:
Bill's father records the basket scorer's name. Each basket gives the player 1 point.

If any player is scoring continuous basket , then they will get one bonus point.

For example, if Bill baskets 1st and 2nd consecutively, he gets a bonus point (which means, he gets 3 points)


I am a beginner in c# . My code fails in major cases in the calculation of scores? which is the right approach and for certain inputs I get value cannot be null.


Sample Input 1:                       

Player Name : Daisy
Player Name: Daisy
Player Name : Bill
Player Name : Bill
Player Name : Tommy
<space>

Sample output  :

Bill Score : 3
Tommy Score : 1
Daisy Score : 3

Sample Input 2:

Player Name : Tommy
Player Name : Bill
Player Name : Bill
Player Name : Daisy
<space>
Player Name : Tommy

Sample output  :
Bill Score : 3
Tommy Score : 2
Daisy Score : 1


What I have tried:

using System.IO;
using System;
using System.Collections;
using System.Linq;


class Program
{
    static void Main()
    {
        Dictionary<string,int> players = new Dictionary<string,int>();
        players.Add("Bill",0);
        players.Add("Tommy",0);
        players.Add("Daisy",0);
        int n=6;
        string[] arr = new string[6];
        for(int i=0;i<n;i++)
        {
            Console.WriteLine("Player Name : ");
            arr[i]=Console.ReadLine();
        }
        //to check consecutive chances
        for(int i=0;i<=n-2;i++)
        {
            if(arr[i]==arr[i+1])
            {
                players[arr[i]]=3;
            }
        }
        //to check missed chances
        for(int i=1;i<n;i++)
        {
            if(arr[i]==" ")
            {
                players[arr[i-1]]=1;
            }
        }
        
        //not consecutive chances taken and one chance missed
        string remainingPlayer = players.FirstOfDefault(x=> x.Value == 0).Key;
        players[remainingPlayer]=2;
        
        //printing results
        for(int count=0;count<players.Count;count++)
        {
            var element = players.ElementAt(count);
            var key = element.Key;
            var value = element.Value;
            Console.WriteLine("{0} Score : {1} ",key,value);
        }
    }
}
Posted
Updated 7-May-21 4:53am

1 solution

Your code can be simplified to the following:
C#
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;


class Program
{
    static void Main()
    {
        Dictionary<string,int> players = new Dictionary<string,int>();
        players.Add("Bill",0);
        players.Add("Tommy",0);
        players.Add("Daisy",0);
        string previous = "";
        for (int n = 0; n < 6; n++)
        {
            Console.Write("Player Name : ");
            string player = Console.ReadLine();
            if (!string.IsNullOrEmpty(player) && player != " ")
            {
                players[player]++;
                if (player.Equals(previous))
                    players[player]++;
                previous = player;
            }
        }
        foreach (KeyValuePair<string, int> kvp in players)
        {
            Console.WriteLine($"Player: {kvp.Key} scored {kvp.Value} baskets");
        }
    }
}

You have not explained which test cases fail, or where the reported error occurs, so this may still be incomplete.
 
Share this answer
 
Comments
Aniket May2021 26-May-21 3:32am    
Its Working 100%.Thank You.
Richard MacCutchan 26-May-21 3:40am    
Are you the same person as priyaa_2021?
Aniket May2021 26-May-21 5:06am    
No Sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900