How to build a game with Go
Today, I have spent my whole day to build this game . Honestly , I used to think I would be Game developer first but then I realized that I am not such a person who wants to take someone peace of life with useless stuff . After I had experienced building Game has a huge logical impact on our mind and to boost it as much as possible .
Let's dive into code
First we need to start with something
package main
import "fmt"
func start(condition string) {
fmt.Println("############################################")
fmt.Println("################ " + condition + " ##############")
fmt.Println("############################################")
}
We don't use any third party package ( except cores ) therefore we should store our dashboard. I used " map " to create dashboard in cli
var board = [][]string{
{"_", "_", "_"},
{"_", "_", "_"},
{"_", "_", "_"},
}
User can pick out X or O . I used os , fmt packages to do this
package main
import (
"fmt"
"os"
"strings"
)
func pickRole() string {
var role string
fmt.Fprintln(os.Stderr, "Choose X or O")
fmt.Scan(&role)
role = strings.Trim(role, "")
if role == "O" || role == "X" {
return role
} else {
return ""
}
}
We also need cli cleaner but we don't know what kind of machine is used to run our program as a result I made this for two ( linux and windows ) . os/exec helps us to command and runtime.GOOS return machine's name like linux , windows etc.
package main
import (
"os"
"os/exec"
"runtime"
)
var clear map[string]func()
func init() {
clear = make(map[string]func())
clear["linux"] = func() {
cmd := exec.Command("clear")
cmd.Stdout = os.Stdout
cmd.Run()
}
clear["windows"] = func() {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
}
}
func callCleaner() {
value, ok := clear[runtime.GOOS]
if ok {
value()
} else {
panic("Your platform is not supported")
}
}
To keep track of board , we have to show new board and to beautify our program I removed [ ] .
package main
import (
"fmt"
"strings"
)
func showTheBoard(arr [][]string) {
callCleaner()
fmt.Println()
for i := range arr {
fmt.Println(strings.Join(arr[i], " "))
}
}
Ok, Almost done . after choosing role let's move . Actually , I admit it's not the best solution but it works ( Don't touch it )
package main
func Move(board [][]string, n int, xOrO string) [][]string {
if n <= 3 {
board[0][n-1] = xOrO
}
if n > 3 && n <= 6 {
board[1][n-(3+1)] = xOrO
}
if n > 6 && n <= 9 {
board[2][n-(6+1)] = xOrO
}
if n > 9 {
return board
}
return board
}
Now users can move . in this game there are only 9 step to move .Time to find a winner . if you have a idea , I'm open mind and feel free to comment
package main
import (
"strings"
)
func findWinner(board [][]string) string {
str := ""
for i := range board {
str = strings.Join(board[i], "")
switch str {
case "OOO":
return "Winner is O"
case "XXX":
return "Winner is X"
}
}
arr := make([]string, 5)
c, y := 0, 0
for x := 0; x < 3; x++ {
makeStr := ""
for i := range board {
makeStr += board[i][y]
}
arr[c] = makeStr
c++
y++
}
ver, unver := board[0][0]+board[1][1]+board[2][2], board[0][2]+board[1][1]+board[2][0]
arr[3] = ver
arr[4] = unver
return winner(arr)
}
func winner(arr []string) string {
for i := range arr {
if arr[i] == "OOO" {
return "Winner is O"
}
if arr[i] == "XXX" {
return "Winner is X"
}
}
return ""
}
There should be restart function to restart our game
package main
import (
"fmt"
"os"
)
func restartGame() {
var restart int
fmt.Println("To restart , Enter 1 else 0")
fmt.Scan(&restart)
if restart == 0 {
os.Exit(0)
}
}
Time to merge everything we have ( sorry for this there will be worse code , it's not recommended to look for people who take to heart )
package main
import (
"fmt"
"time"
)
func main() {
callCleaner()
Restart:
var board = [][]string{
{"_", "_", "_"},
{"_", "_", "_"},
{"_", "_", "_"},
}
start("Game Started")
role := pickRole()
if role != "" {
showTheBoard(board)
maxMove := 9
for maxMove > 0 {
var inp int
getInput:
fmt.Scan(&inp)
validInput := isValidInput(inp)
if !validInput {
fmt.Println("Wrong input")
time.Sleep(1 * time.Second)
goto Restart
}
isFree := isFree(inp, board)
if isFree {
board = Move(board, inp, role)
DoWeHaveWinner := findWinner(board)
if DoWeHaveWinner != "" {
fmt.Println(DoWeHaveWinner)
restartGame()
goto Restart
}
showTheBoard(board)
maxMove--
} else {
fmt.Println("It's not empty")
goto getInput
}
if role == "X" {
role = "O"
} else {
role = "X"
}
}
fmt.Println("Draw")
} else {
fmt.Println("Wrong input")
time.Sleep(1 * time.Second)
goto Restart
}
}
Result
Thanks for reading .