Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 5.69 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "math/rand"
  7.     "os"
  8.     "strconv"
  9. )
  10.  
  11. func main() {
  12.     reader := bufio.NewReader(os.Stdin)
  13.  
  14.     players := [...]string{"X", "O"}
  15.     moveNumber := 0
  16.     grid := [...]string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}
  17.  
  18.     showGrid := func() {
  19.         fmt.Printf("%s %s %s\n", grid[0], grid[1], grid[2])
  20.         fmt.Printf("%s %s %s\n", grid[3], grid[4], grid[5])
  21.         fmt.Printf("%s %s %s\n", grid[6], grid[7], grid[8])
  22.     }
  23.  
  24.     showMenu := func() {
  25.         fmt.Println("Tic Tac Toe")
  26.         fmt.Println("1. Versus")
  27.         fmt.Println("2. Computer Versus")
  28.         fmt.Println("3. Exit")
  29.     }
  30.  
  31.     gameState := func() string {
  32.         if grid[0] == grid[1] && grid[2] == grid[0] || grid[3] == grid[4] && grid[5] == grid[3] ||
  33.             grid[6] == grid[7] && grid[8] == grid[6] || grid[0] == grid[3] && grid[6] == grid[0] ||
  34.             grid[1] == grid[4] && grid[7] == grid[1] || grid[2] == grid[5] && grid[8] == grid[2] ||
  35.             grid[0] == grid[4] && grid[8] == grid[0] || grid[2] == grid[4] && grid[6] == grid[2] {
  36.             return "win"
  37.         } else if moveNumber == 9 {
  38.             return "draw"
  39.         }
  40.         return "still playing"
  41.     }
  42.  
  43.     versus := func() {
  44.         for {
  45.             showGrid()
  46.  
  47.             currentPlayer := players[moveNumber%2]
  48.  
  49.             line, _, _ := reader.ReadLine()
  50.             choice, err := strconv.ParseInt(string(line), 10, 64)
  51.             wrongRange := choice < 1 || choice > 9
  52.  
  53.             for ; err != nil || wrongRange; {
  54.                 if wrongRange {
  55.                     fmt.Println("Wrong input")
  56.                 } else {
  57.                     fmt.Println("Not an integer")
  58.                 }
  59.  
  60.                 line, _, _ = reader.ReadLine()
  61.                 choice, err = strconv.ParseInt(string(line), 10, 64)
  62.                 wrongRange = choice < 0 || choice > 3
  63.             }
  64.  
  65.             grid[choice-1] = currentPlayer
  66.  
  67.             moveNumber++
  68.             fmt.Println()
  69.  
  70.             switch gameState() {
  71.             case "win":
  72.                 fmt.Println(currentPlayer + " won")
  73.                 return
  74.             case "draw":
  75.                 fmt.Println("It's a draw")
  76.                 return
  77.             }
  78.         }
  79.     }
  80.  
  81.     computer := func() {
  82.         for {
  83.             showGrid()
  84.  
  85.             currentPlayer := players[moveNumber%2]
  86.             if currentPlayer == "X" {
  87.                 line, _, _ := reader.ReadLine()
  88.                 choice, err := strconv.ParseInt(string(line), 10, 64)
  89.                 wrongRange := choice < 1 || choice > 9
  90.  
  91.                 for ; err != nil || wrongRange; {
  92.                     if wrongRange {
  93.                         fmt.Println("Wrong input")
  94.                     } else {
  95.                         fmt.Println("Not an integer")
  96.                     }
  97.  
  98.                     line, _, _ = reader.ReadLine()
  99.                     choice, err = strconv.ParseInt(string(line), 10, 64)
  100.                     wrongRange = choice < 0 || choice > 3
  101.                 }
  102.  
  103.                 grid[choice-1] = "X"
  104.             } else {
  105.                 var choice int
  106.                 if grid[0] == grid[1] && grid[2] != "X" && grid[2] != "O" {
  107.                     choice = 2
  108.                 } else if grid[1] == grid[2] && grid[0] != "X" && grid[0] != "O" {
  109.                     choice = 0
  110.                 } else if grid[0] == grid[2] && grid[1] != "X" && grid[1] != "O" {
  111.                     choice = 1
  112.  
  113.                 } else if grid[3] == grid[4] && grid[5] != "X" && grid[5] != "O" {
  114.                     choice = 5
  115.                 } else if grid[4] == grid[5] && grid[3] != "X" && grid[3] != "O" {
  116.                     choice = 3
  117.                 } else if grid[3] == grid[5] && grid[4] != "X" && grid[4] != "O" {
  118.                     choice = 4
  119.  
  120.                 } else if grid[6] == grid[7] && grid[8] != "X" && grid[8] != "O" {
  121.                     choice = 8
  122.                 } else if grid[7] == grid[8] && grid[6] != "X" && grid[6] != "O" {
  123.                     choice = 6
  124.                 } else if grid[6] == grid[8] && grid[7] != "X" && grid[7] != "O" {
  125.                     choice = 7
  126.  
  127.                 } else if grid[0] == grid[3] && grid[6] != "X" && grid[6] != "O" {
  128.                     choice = 6
  129.                 } else if grid[3] == grid[6] && grid[0] != "X" && grid[0] != "O" {
  130.                     choice = 0
  131.                 } else if grid[0] == grid[6] && grid[3] != "X" && grid[3] != "O" {
  132.                     choice = 3
  133.  
  134.                 } else if grid[1] == grid[4] && grid[7] != "X" && grid[7] != "O" {
  135.                     choice = 7
  136.                 } else if grid[4] == grid[7] && grid[1] != "X" && grid[1] != "O" {
  137.                     choice = 1
  138.                 } else if grid[1] == grid[7] && grid[4] != "X" && grid[4] != "O" {
  139.                     choice = 4
  140.  
  141.                 } else if grid[2] == grid[5] && grid[8] != "X" && grid[8] != "O" {
  142.                     choice = 8
  143.                 } else if grid[5] == grid[8] && grid[2] != "X" && grid[2] != "O" {
  144.                     choice = 2
  145.                 } else if grid[2] == grid[8] && grid[5] != "X" && grid[5] != "O" {
  146.                     choice = 5
  147.  
  148.                 } else if grid[0] == grid[4] && grid[8] != "X" && grid[8] != "O" {
  149.                     choice = 8
  150.                 } else if grid[4] == grid[8] && grid[0] != "X" && grid[0] != "O" {
  151.                     choice = 0
  152.                 } else if grid[0] == grid[8] && grid[4] != "X" && grid[4] != "O" {
  153.                     choice = 4
  154.  
  155.                 } else if grid[2] == grid[4] && grid[6] != "X" && grid[6] != "O" {
  156.                     choice = 6
  157.                 } else if grid[4] == grid[6] && grid[2] != "X" && grid[2] != "O" {
  158.                     choice = 2
  159.                 } else if grid[2] == grid[6] && grid[4] != "X" && grid[4] != "O" {
  160.                     choice = 4
  161.  
  162.                 } else {
  163.                     choice = rand.Intn(9)
  164.                     for ; grid[choice] == "O" || grid[choice] == "X"; {
  165.                         choice = rand.Intn(9)
  166.                     }
  167.                 }
  168.  
  169.                 grid[choice] = "O"
  170.             }
  171.  
  172.             moveNumber++
  173.             fmt.Println()
  174.  
  175.             switch gameState() {
  176.             case "win":
  177.                 showGrid()
  178.                 fmt.Println(currentPlayer + " won")
  179.                 return
  180.             case "draw":
  181.                 showGrid()
  182.                 fmt.Println("It's a draw")
  183.                 return
  184.             }
  185.         }
  186.  
  187.         showGrid()
  188.         fmt.Println(players[(moveNumber-1)%2] + " won")
  189.         fmt.Println()
  190.     }
  191.  
  192.     for {
  193.         showMenu()
  194.  
  195.         line, _, _ := reader.ReadLine()
  196.         choice, err := strconv.ParseInt(string(line), 10, 64)
  197.         wrongRange := choice < 0 || choice > 3
  198.  
  199.         for ; err != nil || wrongRange; {
  200.             if wrongRange {
  201.                 fmt.Println("Wrong input")
  202.             } else {
  203.                 fmt.Println("Not an integer")
  204.             }
  205.  
  206.             line, _, _ = reader.ReadLine()
  207.             choice, err = strconv.ParseInt(string(line), 10, 64)
  208.             wrongRange = choice < 0 || choice > 3
  209.         }
  210.  
  211.         fmt.Println()
  212.  
  213.         switch choice {
  214.         case 1:
  215.             versus()
  216.         case 2:
  217.             computer()
  218.         case 3:
  219.             return
  220.         }
  221.  
  222.         fmt.Println()
  223.  
  224.         moveNumber = 0
  225.         grid = [...]string{"1", "2", "3", "4", "5", "6", "7", "8", "9"}
  226.     }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement