Task

Completing the GHW Games challenge - Code a Command Line Game with Copilot, and Use GitHub Copilot to complete any of this week's challenges

It requires creating a command line game using a language I've never coded in before.

I'll be using Golang as its been long since I wanted to code with it.

I'll mostly be doing Hey Copilot do this, Hey Copilot do that but before all that a quick google search would help downloading Go, and setting it up on my code editor (VSCode).

What it does

Its a command line based hangman game. Player gets a certain number of incorrect attempts and within those he'll have to guess the word, letter by letter, chosen by the computer.

How we built it

I built it by giving prompts to Copilot, no not "Make a hangman game using Go" because it would just make it too easy.

Starting with getting a basic starter code for Go.

Prompt: write a basic starter code

package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}

Now generating 20 words (here I'm generating 20 luxury car names).

Prompt: create a global wordlist with 20 car names in lower case only

var wordList = []string{
    "rollsroyce",
    "bentley",
    "lamborghini",
    "ferrari",
    "porsche",
    "koenigsegg",
    "mercedes",
    "bmw",
    "audi",
    "jaguar",
    "lexus",
    "maserati",
    "bugatti",
    "mclaren",
    "pagani",
    "tesla",
    "cadillac",
    "lincoln",
    "infiniti",
    "acura",
}

Now defining three variables such as:

  • chosenWord: the string that will be chosen to guess,_
  • guessWord: array of characters initially underscore ''. Each underscore will be replaced by a letter once player correctly guesses it.
  • attempts: the number of attempts user will have._

Prompt: define 3 global variables, chosenWord string, guessWord array of characters, attempts int

var chosenWord string
var guessWord []rune
var attempts int

Get rid of the unnecessary print statement from within the main function.

Choose a word randomly from the wordList.

Prompt: Assign a random word from wordList to chosenWord

package main

import (
    "fmt"
    "math/rand"
    "time"
)
// Rest of the code
func main() {
    rand.Seed(time.Now().UnixNano())
    chosenWord = wordList[rand.Intn(len(wordList))]
}

Fill the guessWord with same number of underscores as the length of chosenWord.

Prompt: fill guessWord with underscores, and the array size should be same as length of chosenWord

    guessWord = make([]rune, len(chosenWord))
    for i := range guessWord {
        guessWord[i] = '_'
    }

Keep the number of attempts as 1.5 times the length of chosenWord

Prompt :attempts = length of chosenWord * 1.5 as integer

    attempts = int(float64(len(chosenWord)) * 1.5)

Iterate until attempts is greater than 0, decrementing it by 1, print guessWord and attempts, and take a character input from player.

Prompt: for loop until attempts > 0. inside it print guessWord, attempts, and take character input

and make necessary print statement changes.

import (
    "bufio"
    "fmt"
    "os"
    "strings"
    "math/rand"
    "time"
)

func main() {
    // Rest of the code
    reader := bufio.NewReader(os.Stdin)
    for attempts > 0 {
        fmt.Printf("Guess the word: %s\n", string(guessWord))
        fmt.Printf("Attempts remaining: %d\n", attempts)
        fmt.Print("Enter a letter: ")
        input, _ := reader.ReadString('\n')
        input = strings.TrimSpace(input)

        attempts--
    }
}

Do not let player enter multiple letters at once

Prompt: if length of input > 1, ask the user to enter only one letter and continue

        if len(string(input)) > 1 {
            fmt.Println("Please enter only one letter.")
            continue
        }

Check if the player input letter is in the chosenWord, and if it is replace underscore with that letter at required positions in guessWord.

Prompt: check if input letter is in chosenWord. if it is then replace each _ in guessWord with the letter where the letter is in chosenWord

        found := false
        for i, c := range chosenWord {
            if rune(input[0]) == c {
                guessWord[i] = c
                found = true
            }
        }

If letter is not present in chosenWord we'll decrement attempts

        if !found {
            attempts--
        }

Keep checking is guessWord as a string is equals to chosenWord.

        if string(guessWord) == chosenWord {
            fmt.Printf("Congratulations! You've guessed the word: %s\n", chosenWord)
            return
        }

Display a message if player runs out of attempts

    fmt.Printf("Sorry, you've run out of attempts. The word was: %s\n", chosenWord)

Full Code:

package main

import (
    "bufio"
    "fmt"
    "math/rand"
    "os"
    "strings"
    "time"
)

var wordList = []string{
    "rollsroyce",
    "bentley",
    "lamborghini",
    "ferrari",
    "porsche",
    "koenigsegg",
    "mercedes",
    "bmw",
    "audi",
    "jaguar",
    "lexus",
    "maserati",
    "bugatti",
    "mclaren",
    "pagani",
    "tesla",
    "cadillac",
    "lincoln",
    "infiniti",
    "acura",
}
var chosenWord string
var guessWord []rune
var attempts int

func main() {
    rand.Seed(time.Now().UnixNano())
    chosenWord = wordList[rand.Intn(len(wordList))]

    guessWord = make([]rune, len(chosenWord))
    for i := range guessWord {
        guessWord[i] = '_'
    }

    attempts = int(float64(len(chosenWord)) * 1.5)

    reader := bufio.NewReader(os.Stdin)
    for attempts > 0 {
        fmt.Printf("Guess the word: %s\n", string(guessWord))
        fmt.Printf("Attempts remaining: %d\n", attempts)
        fmt.Print("Enter a letter: ")
        input, _ := reader.ReadString('\n')
        input = strings.TrimSpace(input)

        if len(input) != 1 {
            fmt.Println("Please enter a single letter.")
            continue
        }

        found := false
        for i, c := range chosenWord {
            if rune(input[0]) == c {
                guessWord[i] = c
                found = true
            }
        }

        if !found {
            attempts--
        }

        if string(guessWord) == chosenWord {
            fmt.Printf("Congratulations! You've guessed the word: %s\n", chosenWord)
            return
        }
    }

    fmt.Printf("Sorry, you've run out of attempts. The word was: %s\n", chosenWord)
}

Challenges we ran into

  • Coding in a completely new language.
  • Fixing minute errors by Copilot such as that of handling character input in the for loop.

Accomplishments that we're proud of

Coding in a completely new language.

What we learned

The basics of Go.

Built With

Share this project:

Updates