Skip to content

Instantly share code, notes, and snippets.

@LaPingvino
Created December 10, 2021 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LaPingvino/874b7127406575313c570cdf277720b5 to your computer and use it in GitHub Desktop.
Save LaPingvino/874b7127406575313c570cdf277720b5 to your computer and use it in GitHub Desktop.
Github copilot test with Tic Tac Toe in Go
// Tic Tac Toe in Go
package main
import (
"fmt"
)
type XO int
const (
None XO = iota
X
O
Draw
)
func (xo XO) String() string {
if xo == X {
return "X"
}
return "O"
}
type Board [9]XO
func (b Board) Check() XO {
lines := [][3]int{
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6},
}
// Check for X, then check for O
// If X wins, return X
// If O wins, return O
// If draw, return Draw
// Otherwise, return None
for _, line := range lines {
if b[line[0]] == X && b[line[1]] == X && b[line[2]] == X {
return X
}
if b[line[0]] == O && b[line[1]] == O && b[line[2]] == O {
return O
}
}
for _, xo := range b {
if xo == None {
return None
}
}
return Draw
}
// Get X or O from board position as string, if None get the position number instead converted to string
func (b Board) Get(pos int) string {
if b[pos] == None {
return fmt.Sprintf("%d", pos)
}
return b[pos].String()
}
// Board string representation
func (b Board) String() string {
return fmt.Sprintf("%s|%s|%s\n%s|%s|%s\n%s|%s|%s", b.Get(0), b.Get(1), b.Get(2), b.Get(3), b.Get(4), b.Get(5), b.Get(6), b.Get(7), b.Get(8))
}
// Game loop as main function
func main() {
var board Board
var turn XO
turn = X
for {
fmt.Println(board)
fmt.Printf("%s's turn\n", turn)
var pos int
fmt.Scanf("%d", &pos)
board[pos] = turn
if turn == X {
turn = O
} else {
turn = X
}
if board.Check() != None {
fmt.Println(board)
if board.Check() == Draw {
fmt.Println("Draw")
} else {
fmt.Println(board.Check(), "has won!")
}
break
}
}
}
@LaPingvino
Copy link
Author

To make it clear, I didn't let Copilot write it all, when I knew how to do it Copilot wrote the functions for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment