add: new tictactoe game

add: testing to tictactoe

rename: tictactoe

add: tictactoe steps

refactor: tictactoe const names

refactor: tictactoe loop

add: tictactoe bad switch example (fallthrough)

refactor: tictactoe loop skin

remove: tictactoe changable skin

refactor: tictactoe all

remove: tictactoe unnecessary base dir

add: tictactoe slices

add: tictactoe slices

remove: tictactoe fallthrough

rename: tictactoe slices 10 -> 09

update: loops skin tictactoe

add: tictactoe randomization

add: tictactoe infinite loop and labeled break

refactor: tictactoe rand and infinite loop

add: tictactoe buggy winning algo

add: tictactoe more tests

rename: tictactoe wrongPlay to wrongMove

add: tictactoe even more tests

fix: tictactoe

rename: tictactoe waitForInput to wait

add: tictactoe os.args gameSpeed

remove: tictactoe unnecessary files

rename: tictactoe game messages

refactor: tictactoe main loop

add: types and arrays
This commit is contained in:
Inanc Gumus
2019-07-27 18:16:17 +03:00
parent e191567e1f
commit eb8f9987a8
97 changed files with 3457 additions and 3 deletions

View File

@@ -0,0 +1,149 @@
package main
import (
"fmt"
"math/rand"
"strings"
"time"
rainbow "github.com/guineveresaenger/golang-rainbow"
)
const (
maxTurn = 9
// skin options :-)
empty = " "
player1 = " X "
player2 = " O "
header = "---+---+---"
footer = "---+---+---"
separator = "|"
banner = `
~~~~~~~~~~~~~~~
TIC~TAC~TOE
~~~~~~~~~~~~~~~`
)
// -------------------------------------------------
// INITIALIZE THE GAME
// -------------------------------------------------
var (
turn int
won bool
board = [][]string{
{empty, empty, empty},
{empty, empty, empty},
{empty, empty, empty},
}
player = player1
)
func main() {
rand.Seed(time.Now().UnixNano())
rainbow.Rainbow(banner, strings.Count(banner, "\n"))
for {
// -------------------------------------------------
// PRINT THE BOARD AND THE PROMPT
// -------------------------------------------------
fmt.Printf("\n %s\n", header)
for _, line := range board {
fmt.Printf(" %s\n", strings.Join(line, separator))
fmt.Printf(" %s\n", footer)
}
// -------------------------------------------------
// IS THERE A WINNER? OR IS IT A TIE?
// -------------------------------------------------
for i := 1; i <= 2; i++ {
m := player2
if i == 1 {
m = player1
}
b, mmm := board, strings.Repeat(m, 3)
/* horizontals */
hor := strings.Join(b[0], "") == mmm ||
strings.Join(b[1], "") == mmm ||
strings.Join(b[2], "") == mmm
/* verticals */
ver := b[0][0]+b[1][0]+b[2][0] == mmm ||
b[0][1]+b[1][1]+b[2][1] == mmm ||
b[0][2]+b[1][2]+b[2][2] == mmm
/* diagonals */
diag := b[0][0]+b[1][1]+b[2][2] == mmm ||
b[0][2]+b[1][1]+b[2][0] == mmm
won = hor || ver || diag
if won {
player = m
break
}
}
if won {
player = strings.TrimSpace(player)
fmt.Printf("\n>>> WINNER: %s\n", player)
break
} else if turn == maxTurn {
fmt.Printf("\n>>> TIE!\n")
break
}
// -------------------------------------------------
// PLAY
// -------------------------------------------------
pos := rand.Intn(9) + 1
fmt.Printf("\nPlayer %s plays to %d\n", player, pos)
// -------------------------------------------------
// IS IT A VALID MOVE?
// -------------------------------------------------
var row int
switch {
case pos <= 3:
row = 0
case pos <= 6:
row = 1
case pos <= 9:
row = 2
default:
fmt.Println(">>>", "wrong position!")
continue
}
col := pos - row*3 - 1
if board[row][col] != empty {
fmt.Println(">>>", "already played!")
continue
}
// -------------------------------------------------
// MARK THE MOVE AND INCREMENT THE TURN
// -------------------------------------------------
// put a mark on the board
board[row][col] = player
// switch to the next player
if player == player1 {
player = player2
} else {
player = player1
}
turn++
// time.Sleep(time.Millisecond * 100)
}
}