Golang

 

Introduction

In this tutorial, we will learn how to create a digital scoreboard for a Ping Pong game using the Go programming language. This scoreboard will keep track of the players’ scores and display them during the game. It’s a simple and fun project that will help you practice working with variables, loops, and basic input-output in Go.

Objective

The objective of this project is to develop a digital Ping Pong scoreboard where two players can keep track of their individual scores. The game will continue until one of the players reaches a predefined score, and the program will declare the winner. This simple project demonstrates how to manage and display scores in a game setting using Go.

Code

package main

import (
    "fmt"
)

func main() {
    var player1Score, player2Score int
    var player1Name, player2Name string
    var targetScore int

    fmt.Println("Enter Player 1 name:")
    fmt.Scanln(&player1Name)

    fmt.Println("Enter Player 2 name:")
    fmt.Scanln(&player2Name)

    fmt.Println("Enter target score:")
    fmt.Scanln(&targetScore)

    for player1Score < targetScore && player2Score < targetScore { var winner string fmt.Println("Who won this round? (Enter '1' for Player 1 or '2' for Player 2):") fmt.Scanln(&winner) if winner == "1" { player1Score++ } else if winner == "2" { player2Score++ } else { fmt.Println("Invalid input, please enter '1' or '2'.") continue } fmt.Printf("%s's Score: %d\n", player1Name, player1Score) fmt.Printf("%s's Score: %d\n", player2Name, player2Score) if player1Score >= targetScore {
            fmt.Printf("%s wins the game with a score of %d!\n", player1Name, player1Score)
            break
        } else if player2Score >= targetScore {
            fmt.Printf("%s wins the game with a score of %d!\n", player2Name, player2Score)
            break
        }
    }
}

Explanation of the Program

The Ping Pong scoreboard program is written in the Go programming language. Let’s break down the key components of the code:

  • Variables: The program uses variables to store the names and scores of both players. It also stores the target score to end the game.
  • Input: It prompts the user to input the names of both players and the target score to decide when the game will end.
  • Game Loop: The game loop continues running as long as neither player has reached the target score. Inside the loop, the user is asked to input which player won each round (Player 1 or Player 2).
  • Score Update: Based on the input, the program updates the scores and prints the updated scores after each round.
  • Winner Declaration: Once one of the players reaches the target score, the program prints the winner’s name and the final score.

How to Run the Program

  1. First, ensure you have Go installed on your system. If not, download and install it from here.
  2. Copy the code into a text editor and save the file with a “.go” extension, for example, pingpong.go.
  3. Open your terminal or command prompt and navigate to the folder where you saved the file.
  4. Run the program using the command go run pingpong.go.
  5. Follow the on-screen prompts to input player names and the target score. The program will then display the scores after each round and declare the winner when the game ends.
© 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 :)