Introduction

In this tutorial, we will create a simple digital scoreboard for a Ping Pong game using the C programming language. A digital scoreboard allows players to keep track of their scores in real-time during the game. We will develop a program that initializes the scores for both players, allows the users to update the scores, and displays the final results at the end of the game.

Objective

The main objective of this program is to implement a digital scoreboard for a ping pong game. The user will be able to update the scores of two players and see the current score displayed on the screen. The program will also handle inputs for score updates and display the winner when the game ends.

Code

#include 

// Function to display the scoreboard
void display_scoreboard(int player1_score, int player2_score) {
    printf("\n---- Ping Pong Scoreboard ----\n");
    printf("Player 1: %d\n", player1_score);
    printf("Player 2: %d\n", player2_score);
    printf("-----------------------------\n");
}

int main() {
    int player1_score = 0, player2_score = 0;
    int choice;

    // Game loop
    while(1) {
        display_scoreboard(player1_score, player2_score);
        
        // Prompt for player action
        printf("Enter 1 to add a point to Player 1\n");
        printf("Enter 2 to add a point to Player 2\n");
        printf("Enter 0 to end the game\n");
        printf("Your choice: ");
        scanf("%d", &choice);

        // Update the score based on the input
        if (choice == 1) {
            player1_score++;
        } else if (choice == 2) {
            player2_score++;
        } else if (choice == 0) {
            break;
        } else {
            printf("Invalid choice. Please try again.\n");
        }
    }

    // Display the final scores and declare the winner
    display_scoreboard(player1_score, player2_score);
    if (player1_score > player2_score) {
        printf("\nPlayer 1 wins!\n");
    } else if (player2_score > player1_score) {
        printf("\nPlayer 2 wins!\n");
    } else {
        printf("\nThe game is a draw.\n");
    }

    return 0;
}

Program Explanation

The program begins by initializing the scores of both players to zero. A game loop is created where the user is prompted to either add a point to Player 1 or Player 2. If the user enters ‘1’, the score of Player 1 is incremented; if ‘2’, Player 2’s score is incremented. If ‘0’ is entered, the game loop ends, and the final scores are displayed.

The display_scoreboard function is responsible for showing the current score of both players on the screen. After the game loop ends, the program will display the winner based on the final scores.

How to Run the Program

  1. Open your preferred C compiler or IDE (such as Code::Blocks, DevC++, or GCC).
  2. Create a new C file and paste the code provided above into the file.
  3. Save the file with a .c extension (e.g., ping_pong_scoreboard.c).
  4. Compile and run the program in your IDE or use the following command if you are using GCC:
    gcc ping_pong_scoreboard.c -o ping_pong_scoreboard
  5. Execute the compiled program by running ./ping_pong_scoreboard.
  6. Follow the on-screen prompts to play the game and update the scores.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)