Let's make console Bulls and Cows (Numberbaseball) game with golang

SourceCode

This post is made from velog

https://github.com/jjmin321/numberbaseball

Bulls and Cows (Numberbaseball)

  • The numerical version of the game is usually played with 4 digits, but can also be played with 3 or any other number of digits.

  • On a sheet of paper, the players each write a 4-digit secret number. The digits must be all different. Then, in turn, the players try to guess their opponent’s number who gives the number of matches. If the matching digits are in their right positions, they are “bulls”, if in different positions, they are “cows”.

  • Example:
    Secret number: 4271
    Opponent’s try: 1234
    Answer: 1 bull and 2 cows. (The bull is “2”, the cows are “4” and “1”.)
    The first one to reveal the other’s secret number in the least number of guesses wins the game.

  • The game may also be played by two teams of players, with the team members discussing their strategy before selecting a move.

Numberbaseball

Need to mypackage

github.com/jjmin321/mypackage/color

github.com/jjmin321/mypackage/random

What I learned and other thing, etc…

- Got used to use struct
- learned how to make own package
- Create package wass so fun and could use more function simply.
- Making the console game wass very exciting and interesting.
- Next time, I challenge making the console pokemon game with golang.
- Could you tell me if you have any advice and feedback?

Game rule

1. Enter your name first.

2. Choose play or quit(y/n)
 - y : start the game
 - n : quit the game
 - other key : error -> quit the game
 
3. Play the game
 - If your answer is correct, you saw end message and won the game.
 - If your answer is 77, you can quit the game.
  • numberbaseball_ver1

    • Made numberbaseballgame with golang.
  • numberbaseball_ver2

    • Modified console interface, helps user’s easily play.
    • Added function of play numberbaseball
  • numberbaseball_ver3(now)

    • Console can now receive without enter space bar
    • Many bug fixed.
    • Banned first digit 0

Code

insertname.png

Program makes random number without overlapping numbers.

  • Algorithm
opponent := pitcher{[3]int{random.Randomint(10), random.Randomint(10), random.Randomint(10)}}
 for {
  if opponent.numbers[0] == opponent.numbers[1] || opponent.numbers[0] == opponent.numbers[2] || opponent.numbers[1] == opponent.numbers[2] || opponent.numbers[0] == 0 {
   opponent = pitcher{[3]int{random.Randomint(10), random.Randomint(10), random.Randomint(10)}}
  } else if opponent.numbers[0] != opponent.numbers[1] && opponent.numbers[0] != opponent.numbers[2] && opponent.numbers[1] != opponent.numbers[2] {
   break
  }
 }
 play(me, opponent)

Play or Quit

playorquit.png

If you enter the key n

  • The game will terminate.

pressn.png

if answer == "n" {
  complete(a, b)
    }

If you enter the other key

  • The game will terminate.

pressother.png

 else {
  color.Colorprint("please insert y/n !!!!!!!!!!!!", "red+underline")
  os.Exit(0)
    }

If you enter the key y

  • The game will start.

pressy.png

 else if answer == "y" {
  color.Colorprint(`OK! Let's start!!`, "green+underline")
  color.Colorprint("┌──────────────────────────────────┐", "red")
  color.Colorprint("│     You could enter like 123     │", "blue")
  color.Colorprint("|     for example) 456 or 723      |", "green")
  color.Colorprint("|   Don't enter number like 1 2 3  |", "magenta")
  color.Colorprint("└──────────────────────────────────┘", "red")
  color.Colorprint(`When you wanna quit the game, enter the key "77" and enter the enter key`, "magenta+underline")
  for {
   var num int
   fmt.Println("Now your try : ", a.try)
   fmt.Print("enter your number : ")
   fmt.Scanln(&num)
   one := num / 100
   two := num/10 - one*10
   three := num % 10
   //fmt.Println(num, " ", one, " ", two, " ", three)

   if one == two { //If user enter number like 1 2 3 , program couldn't get it. so goto RETRY
    fmt.Println("ERROR! DON'T enter NUMBER LIKE 1 2 3 !!!!")
    color.Colorprint("┌──────────────────────────────────┐", "red")
    color.Colorprint("│     You could enter like 123     │", "blue")
    color.Colorprint("|     for example) 456 or 723      |", "green")
    color.Colorprint("|   Don't enter number like 1 2 3  |", "magenta")
    color.Colorprint("└──────────────────────────────────┘", "red")
    continue
   }

   compare := func() {
    a.try++
    if num == 77 {
     fmt.Println("\nThe pitcher's ball is\a", b)
     color.Colorprint("┌──────────────────────┐", "red")
     color.Colorprint("│      10/26/2019      │", "yellow")
     color.Colorprint("│  NumberBaseball Game │", "green")
     color.Colorprint("│  Made by jejeongmin  │", "cyan")
     color.Colorprint("│    Make with golang  │", "blue")
     color.Colorprint("│      ToyProject      │", "magenta")
     color.Colorprint("└──────────────────────┘", "red")
     fmt.Println()
     os.Exit(0)
    }
    if one == b.numbers[0] {
     a.strike++
    } else if one == b.numbers[1] {
     a.ball++
    } else if one == b.numbers[2] {
     a.ball++
    }
    if two == b.numbers[0] {
     a.ball++
    } else if two == b.numbers[1] {
     a.strike++
    } else if two == b.numbers[2] {
     a.ball++
    }
    if three == b.numbers[0] {
     a.ball++
    } else if three == b.numbers[1] {
     a.ball++
    } else if three == b.numbers[2] {
     a.strike++
    }
    color.Colorprint("your count is .........", "cyan")
    fmt.Printf("%s's strike: %d\t %s's ball: %d\t %s's try: %d\n", a.name, a.strike, a.name, a.ball, a.name, a.try)
    fmt.Println()
   }
   compare()
   if a.strike == 3 {
    break
   }
   a.strike, a.ball = 0, 0
  }
  complete(a, b)
 }

When the game was started.

gamestart.png

If you enter the key 77 while play the game

  • The game will terminate

enter77.png

if num == 77 {
 fmt.Println("\nThe pitcher's ball is\a", b)
color.Colorprint("┌──────────────────────┐", "red")
color.Colorprint("│      10/26/2019      │", "yellow")
color.Colorprint("│  NumberBaseball Game │", "green")
color.Colorprint("│  Made by jejeongmin  │", "cyan")
color.Colorprint("│    Make with golang  │", "blue")
color.Colorprint("│      ToyProject      │", "magenta")
  color.Colorprint("└──────────────────────┘", "red")     
 fmt.Println()
  os.Exit(0)
}

If you enter your number exactly, Compare function would be start.

  • The game could be resume.

exactly.png

compare := func() {
 a.try++
 if num == 77 {
  fmt.Println("\nThe pitcher's ball is\a", b)       color.Colorprint("┌──────────────────────┐", "red")
color.Colorprint("│      10/26/2019      │", "yellow")
color.Colorprint("│  NumberBaseball Game │", "green")
color.Colorprint("│  Made by jejeongmin  │", "cyan")
color.Colorprint("│    Make with golang  │", "blue")
color.Colorprint("│      ToyProject      │", "magenta")      color.Colorprint("└──────────────────────┘", "red")
  fmt.Println()
  os.Exit(0)
    }
  
    if one == b.numbers[0] {
     a.strike++
    } else if one == b.numbers[1] {
     a.ball++
    } else if one == b.numbers[2] {
     a.ball++
    }
    if two == b.numbers[0] {
     a.ball++
    } else if two == b.numbers[1] {
     a.strike++
    } else if two == b.numbers[2] {
     a.ball++
    }
    if three == b.numbers[0] {
     a.ball++
    } else if three == b.numbers[1] {
     a.ball++
    } else if three == b.numbers[2] {
     a.strike++
    }
    color.Colorprint("your count is .........", "cyan")
    fmt.Printf("%s's strike: %d\t %s's ball: %d\t %s's try: %d\n", a.name, a.strike, a.name, a.ball, a.name, a.try)
    fmt.Println()
   }

If the answer is correct, the program goes to complete(type function)

  • complete(pitcher’s number, hitter’s number)
if a.strike == 3 {
    break
            }
            complete(a, b)

Complete

  • If you won the game

correct.png

func complete(a hitter, b pitcher) {
 if a.strike == 3 {
  color.Colorprint("┌──────────────────────────────────────────────────┐", "red")
  color.Colorprint("│      WOW!! you're win!! you're so awesome!!!!!   │", "cyan")
  color.Colorprint("└──────────────────────────────────────────────────┘", "red")
  fmt.Println("The pitcher's ball is\a", b)
  color.Colorprint("┌──────────────────────┐\n", "red")
  color.Colorprint("│      10/26/2019      │\n", "yellow")
  color.Colorprint("│  NumberBaseball Game │\n", "green")
  color.Colorprint("│  Made by jejeongmin  │\n", "cyan")
  color.Colorprint("│    Make with golang  │\n", "blue")
  color.Colorprint("│      ToyProject      │\n", "magenta")
  color.Colorprint("└──────────────────────┘\n", "red")
  fmt.Println()

    } 
    os.Exit(0)
}

This is being created on a Monday, 28 October 2019

댓글

이 블로그의 인기 게시물

2019 My first hackathon in Daegu software highschool

DUCAMI education volunteer club - Micro:bit education activity, operate a booth