Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
231 changes: 231 additions & 0 deletions Go/tictactoe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package main

import (
"strings"
"bufio"
"fmt"
"os"
)

func mapper(letter string) int{
if strings.TrimRight(letter, "\n") == "1" { return 0 }
if strings.TrimRight(letter, "\n") == "2" { return 1 }
if strings.TrimRight(letter, "\n") == "3" { return 2 }
if strings.TrimRight(letter, "\n") == "4" { return 3 }
if strings.TrimRight(letter, "\n") == "5" { return 4 }
if strings.TrimRight(letter, "\n") == "6" { return 5 }
if strings.TrimRight(letter, "\n") == "7" { return 6 }
if strings.TrimRight(letter, "\n") == "8" { return 7 }
if strings.TrimRight(letter, "\n") == "9" { return 8 }
return 99
}

func bprinter (board [9]string) {
fmt.Println("The board now looks like this:")
fmt.Println("-------")
fmt.Println("|" + board[0] + "|" + board[1] + "|" + board[2] + "|")
fmt.Println("|" + board[3] + "|" + board[4] + "|" + board[5] + "|")
fmt.Println("|" + board[6] + "|" + board[7] + "|" + board[8] + "|")
fmt.Println("-------")
fmt.Println("")
}

func legendprinter () {
fmt.Println("Here is the legend for you to make a mark:")
fmt.Println("_______")
fmt.Println("|1|2|3|")
fmt.Println("|4|5|6|")
fmt.Println("|7|8|9|")
fmt.Println("-------")
}


func step1(board [9]string, player string) [9]string {
boardpos := requestor(player)
newboard := marker(board, player, boardpos)
tflag := islistsame(board, newboard)
if (tflag) {
fmt.Println("No change was made. The same player must make a valid selection. Player " + player + " please try again.")
newboard = step1(board, player)
}
return newboard
}


func boardwinchecker(board [9]string) string {
lastmessage := "nowinner"
if board[0] == board[4] { // check the diagonal from left top to right bottom corner
if board[4] == board[8] {
if board[4] != " " {
lastmessage = "Player " + board[0] + " wins!"
}
}
}
if board[2] == board[4] { // check the diagonal from right top to left bottom corner
if board[4] == board[6] {
if board[6] != " " {
lastmessage = "Player " + board[2] + " wins!"
}
}
}
if board[0] == board[3] { // check the left column
if board[3] == board[6] {
if board[6] != " " {
lastmessage = "Player " + board[0] + " wins!"
}
}
}
if board[0] == board[1] { // check the top row
if board[1] == board[2] {
if board[0] != " " {
lastmessage = "Player " + board[0] + " wins!"
}
}
}
if board[2] == board[5] { // check right column
if board[5] == board[8] {
if board[2] != " " {
lastmessage = "Player " + board[2] + " wins!"
}
}
}
if board[6] == board[7] { // check bottom row
if board[7] == board[8] {
if board[8] != " " {
lastmessage = "Player " + board[6] + " wins!"
}
}
}
if board[1] == board[4] { // check middle column
if board[4] == board[7] {
if board[1] != " " {
lastmessage = "Player " + board[1] + " wins!"
}
}
}
if board[3] == board[4] { // check middle row
if board[4] == board[5] {
if board[3] != " " {
lastmessage = "Player " + board[3] + " wins!"
}
}
}
return lastmessage
}

func marker(board [9]string, player string, position string) [9]string {
x := mapper(position)
if (board[x] != " ") {
fmt.Println("That square has already been marked. Please try again.")
} else {
board[x] = player
}
return board
}

func requestor(player string) string {

fmt.Println("Player", player, "it is your turn.")
fmt.Println("Please enter a letter for the square you want to mark (1 through 9):")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
if strings.TrimRight(text, "\n") != "1" {
if strings.TrimRight(text, "\n") != "2" {
if strings.TrimRight(text, "\n") != "3" {
if strings.TrimRight(text, "\n") != "4" {
if strings.TrimRight(text, "\n") != "5" {
if strings.TrimRight(text, "\n") != "6" {
if strings.TrimRight(text, "\n") != "7" {
if strings.TrimRight(text, "\n") != "8" {
if strings.TrimRight(text, "\n") != "9" {
fmt.Println("*** WARNING ***")
fmt.Println("That was an invalid entry. Please try again player " + player)
fmt.Println(" ")
text = requestor(player)
}
}
}
}
}
}
}
}
}
return text
}

func islistsame(board1 [9]string, board2 [9]string) bool {
var j = true
for i := 0; i < 9; i++ {
if board1[i] == board2[i] {
} else {
j = false
}
}
return j
}

func main() {

var tttb [9]string
tttb[0] = " "
tttb[1] = " "
tttb[2] = " "
tttb[3] = " "
tttb[4] = " "
tttb[5] = " "
tttb[6] = " "
tttb[7] = " "
tttb[8] = " "

legendprinter()

a := step1(tttb, "X")
bprinter(a)
b := step1(a, "O")
bprinter(b)
c := step1(b, "X")
bprinter(c)
d := step1(c, "O")
bprinter(d)
e := step1(d, "X")
bprinter(e)
legendprinter()
be := boardwinchecker(e)
if be == "nowinner" {
f := step1(e, "O")
bprinter(f)
bf := boardwinchecker(f)
if bf == "nowinner" {
bprinter(f)
g := step1(f, "X")
bg := boardwinchecker(g)
if bg == "nowinner" {
bprinter(g)
h := step1(g, "O")
bh := boardwinchecker(h)
if bh == "nowinner" {
bprinter(h)
i := step1(h, "X")
bi := boardwinchecker(i)
if bi == "nowinner" {
legendprinter()
lastmessage := "CAT's GAME. (It was a draw aka a tie game.)"
bprinter(i)
fmt.Println(lastmessage)
} else {
bprinter(i)
fmt.Println(bi) }
} else {
bprinter(h)
fmt.Println(bh) }
} else {
bprinter(g)
fmt.Println(bg) }
} else {
bprinter(f)
fmt.Println(bf) }
} else {
bprinter(e)
fmt.Println(be) }
}