Go Notebook
  • Introduction
  • Syntax Helpers
    • Append
    • String
    • Slice
  • Idioms
    • Custom JSON Marshaling
    • Functional Options
    • Type Embedding
    • Laws of Reflection
  • Design Patterns
    • Builder
    • Factory Method
    • Object Pool
    • Singleton
    • Observer
    • Strategy
  • Hello World
    • Getting Started With Go
    • Go Packages
    • Hello World
  • Tic Tac Toe
    • Go Interfaces
    • Go Error Handling
    • Tic Tac Toe
  • HTTP Server
    • HTTP Handler
    • Build a Calculator
    • HTTP Unit Test
  • Concurrency
    • Goroutines
    • Go Concurrency Part 1
    • Go Concurrency Part 2
  • WebSocket Server
    • WebSocket Handler
    • Build a Chatroom
  • User Authentication
    • Go Module
    • Popular Web Libraries
    • React Tools
    • Build an Authentication Server
  • Numerical Computing
    • Gonum
    • Neural Networks
Powered by GitBook
On this page
  • Setup
  • Object Oriented Programming in Go
  • Quick Note
  • Private vs Public
  • Project Tic Tac Toe
  • Source
  1. Tic Tac Toe

Tic Tac Toe

Let's build a terminal based Tic Tac Toe game in Go!

Setup

Create a folder named tictactoe in your go-academy directory. Then create a main.go inside it. We will put the game logic into a package named ttt. For now, just make an empty ttt folder inside tictactoe.

WORKSPACE/
    src/
        go-academy/
            tictactoe/
                main.go
                ttt/
                    ...

Object Oriented Programming in Go

Let's define what are entities that we need for a terminal based Tic Tac Toe game.

Game: This is a struct that keeps track of the turns, the players, and the board information.

Player: This is an interface that has the following methods.

  • GetMove(*board) (int, int, error)

  • Mark() string

  • Name() string

HumanPlayer and ComputerPlayer

board: This is an 3 by 3 array that keeps track of X and O marks, using _ to render empty space.

Quick Note

Private vs Public

Functions or variables with upper-cased name are exported from a package. If the functions or variables have lower-cased name, then program outside of the package cannot have reference to them. I chose to make board a private struct because user need not to care about the implementation and usage of game board.

Project Tic Tac Toe

Source

PreviousGo Error HandlingNextHTTP Server

Last updated 5 years ago

GitHub
https://www.youtube.com/watch?v=EgpXNBqmhP8&feature=youtu.bewww.youtube.com
https://www.youtube.com/watch?v=CqsfJw4HJyA&feature=youtu.bewww.youtube.com