Skip to content

Instantly share code, notes, and snippets.

@nunq
Last active March 16, 2019 14:39
Show Gist options
  • Save nunq/dc4eda26f028d3ac6493daa5b269a60a to your computer and use it in GitHub Desktop.
Save nunq/dc4eda26f028d3ac6493daa5b269a60a to your computer and use it in GitHub Desktop.
simple tictactoe in go using prng
package main
import (
"fmt"
"math/rand"
"os"
"time"
)
//player=0 ; computer=1
var tiles[9]string;
var lastMoveWasBy int;
func main() {
fmt.Println("Welcome to TicTacToe!\nThis is the playing field:");
//add padding to make "ascii art" work
for i := 0; i < 9; i++ {
tiles[i] = " ";
}
printField(true);
intro();
}
func intro() {
if (getRandomInt(1)==0) {
fmt.Println("You start!");
askForPlayerMove();
} else {
fmt.Println("The computer starts");
computerPlayMove();
}
}
func getRandomInt(lim int) int {
rndm := rand.New(rand.NewSource(time.Now().UnixNano()));
return rndm.Intn(lim);
}
func computerPlayMove() {
setTile(1, getRandomInt(8));
}
func askForPlayerMove() {
fmt.Println("\nSo what's your move?");
var userInput int;
fmt.Scanf("%d", &userInput);
setTile(0, userInput);
}
func checkTile(tilenum int) bool {
if (!(tilenum>=0&&tilenum<=8)) {
fmt.Println("\nThat field doesn't exist");
askForPlayerMove();
}
//check if tile is empty
if (tiles[tilenum]!=" ") {
return false;
} else {
return true;
}
}
func checkForWinEtc() {
//checking for wins, losses, etc. in a terrible, hardcoded way
if ( (tiles[3]=="O"&&tiles[4]=="O"&&tiles[5]=="O") || (tiles[6]=="O"&&tiles[7]=="O"&&tiles[8]=="O") || (tiles[0]=="O"&&tiles[3]=="O"&&tiles[6]=="O") || (tiles[1]=="O"&&tiles[4]=="O"&&tiles[7]=="O") || (tiles[2]=="O"&&tiles[5]=="O"&&tiles[8]=="O") || (tiles[0]=="O"&&tiles[4]=="O"&&tiles[8]=="O") || (tiles[2]=="O"&&tiles[4]=="O"&&tiles[6]=="O") ) {
endgame(0);
}
if ( (tiles[3]=="X"&&tiles[4]=="X"&&tiles[5]=="X") || (tiles[6]=="X"&&tiles[7]=="X"&&tiles[8]=="X") || (tiles[0]=="X"&&tiles[3]=="X"&&tiles[6]=="X") || (tiles[1]=="X"&&tiles[4]=="X"&&tiles[7]=="X") || (tiles[2]=="X"&&tiles[5]=="X"&&tiles[8]=="X") || (tiles[0]=="X"&&tiles[4]=="X"&&tiles[8]=="X") || (tiles[2]=="X"&&tiles[4]=="X"&&tiles[6]=="X") ) {
endgame(1);
}
if ( tiles[0]!=" "&&tiles[1]!=" "&&tiles[2]!=" "&&tiles[3]!=" "&&tiles[4]!=" "&&tiles[5]!=" "&&tiles[6]!=" "&&tiles[7]!=" "&&tiles[8]!=" " ) {
endgame(2);
}
}
func setTile(player int, tilenum int) {
if (checkTile(tilenum)!=true) {
if (player==0) {
fmt.Println("\nTile is not free");
askForPlayerMove();
} else {
computerPlayMove();
}
}
if (player==0) {
tiles[tilenum] = "O";
} else {
tiles[tilenum] = "X";
}
lastMoveWasBy = player;
prepNextMove(player);
}
func prepNextMove(player int) {
checkForWinEtc();
if (player==1) {
fmt.Println("\nComputer's response\n");
}
printField(false);
if (lastMoveWasBy==0) {
computerPlayMove();
} else {
askForPlayerMove();
}
}
func printField(intro bool) {
if (intro) {
fmt.Printf("_____________\n| %d | %d | %d |\n|———|———|———|\n| %d | %d | %d |\n|———|———|———|\n| %d | %d | %d |\n¯¯¯¯¯¯¯¯¯¯¯¯¯\n", 0, 1, 2, 3, 4, 5, 6, 7, 8);
} else {
fmt.Printf("_____________\n| %s | %s | %s |\n|———|———|———|\n| %s | %s | %s |\n|———|———|———|\n| %s | %s | %s |\n¯¯¯¯¯¯¯¯¯¯¯¯¯\n", tiles[0], tiles[1], tiles[2], tiles[3], tiles[4], tiles[5], tiles[6], tiles[7], tiles[8]);
}
}
func endgame(winner int) {
printField(false);
if (winner==0) {
fmt.Println("You won :)");
} else if (winner==1) {
fmt.Println("You lost :(");
} else {
fmt.Println("Stalemate :/");
}
os.Exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment