[ ![avatar](/assets/img/avatar.png) ](/)

[ Miigon's blog ](/)

My ideas, thoughts and experiences

  * [ __ HOME  ](/)
  * [ __ CATEGORIES  ](/categories/)
  * [ __ TAGS  ](/tags/)
  * [ __ ARCHIVES  ](/archives/)
  * [ __ ABOUT  ](/about/)

__ [ __ ](https://github.com/Miigon) [ __ ](https://twitter.com/) [ __
](javascript:location.href = 'mailto:' +
\['miigon.pg','gmail.com'\].join\('@'\)) [ __ ](/feed.xml)

[ Home ](/) Golang Project: Tic Tac Toe  __

Post

__ __ Cancel

#  Golang Project: Tic Tac Toe

Posted _Jan 29, 2021_ Updated _Jan 29, 2021_

By _[ Miigon ](https://github.com/Miigon) _

_16 min_ read

In this article, I’ll go through my process of writing a simple Tic-Tac-Toe
game in Golang. Error handling, closures, iota as well as other golang
features are used to create the game.

> Before we start:
>
> After some more research to the language, It seems that Go is really not
> _that_ different from order C-family programming languages. So I decided
> that it would not be worthwhile to document every single detail of Golang in
> my blog posts, since there’s a lot of resources readily available on the
> internet which go much deeper into the Go language features than my blog
> will ever be able to do. So it doesn’t make much sense for me to write about
> everything, a quick Google search will usually work better.
>
> As a result, this article series will now be focused on my experiences of
> making experimental and/or actual project with Golang. The goal is to show
> Golang’s features and neuances.

[ Full code on Github ](https://github.com/Miigon/full-codes-from-
blog/blob/master/tic-tac-toe.go)

#  Define the board and game state

The first thing we want to do is define the board on which the game will be
played on.

The game Tic-Tac-Toe has a 3*3 board. Each of the squares can be one of three
states: it can be a ` X ` or a ` O ` , or it can be just empty. We define
these states with type alias and const definition, which is Go’s equivalance
of ` enum ` :

__ __

`

    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    

|

    
    
    // what kind of piece is on a certain square
    type squareState int
    
    const (
    	none   = iota
    	cross  = iota
    	circle = iota
    )
    
      
  
---|---  
`

` iota ` represents successive integer constants 0,1,2,….  
It resets to 0 whenever the keyword ` const ` appears in the source code and
increments after each const specification.  
In our case, ` none ` would be 0, ` cross ` would be 1 and ` circle ` would be
2.

Noted that all the ` iota ` (except for the first one) can be omitted and
still have the same effect.

With ` suqareState ` we can represent the state of one square, now we can use
a 3*3 array to represent the whole board.

__ __

`

    
    
    1
    

|

    
    
    var board [3][3]squareState
      
  
---|---  
`

For each turn, we also need to know whose turn it is. So we introduce another
variable to represent that:

__ __

`

    
    
    1
    2
    3
    

|

    
    
    type player int
    
    var turnPlayer player
      
  
---|---  
`

We don’t have to define constants again, the constants defined for the
squareState before can be used.

> This is when we run into the discussion about whether Golang’s decision to
> not include C/C++ style enum keyword had made the language difficult to use
> in some cases.
>
> Some have argued that the lack of compile-time type checking makes the code
> more prone to mistakes. ` const ` names being in the same package scope also
> can cause some confusion. Since it’s not always immediately obvious which
> enum a const name belongs to when you see one.

The last thing for us to do is wrap them (the board & whose turn is it) up as
the current game state struct:

__ __

`

    
    
    1
    2
    3
    4
    5
    

|

    
    
    // current state of the game
    type gameState struct {
    	board [3][3]squareState
    	turnPlayer player
    }
      
  
---|---  
`

Storing them