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
- First, ensure you have Go installed on your system. If not, download and install it from here.
- Copy the code into a text editor and save the file with a “.go” extension, for example,
pingpong.go
. - Open your terminal or command prompt and navigate to the folder where you saved the file.
- Run the program using the command
go run pingpong.go
. - 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.