Tic Tac Toe Game

Below are the steps to code Tic tac Toe game

1- Create a file tictactoe.sh and write below code inside.

#!/bin/bash
# Author : LEOxian
# License: GPLv2
# Description: Tic Tac Toe Game
# Date : 23-March-2013

trap 'echo -e "\033[00;30m \n\nThank you for playing Tic Tac Toe\n"; clear; exit 127' SIGINT

# Colour codes
r='\033[01;31m' # Colour r
g='\033[01;32m' # Colour g
b='\033[00;30m' # Colour b
bb='\033[01;30m' # Colour b + bold
dc='\033[00;30m' # Default Colour

message(){
  if [ "$1" == "win" ]
    then
    echo -e "${g}Congratulation $current_player you Won!${b}\n"
    exit
  fi
  if [ "$1" == "draw" ]
    then
    echo -e "${g}\nIt's draw between both players. Try again!${b}\n"
    exit
  fi
 }

# Empty user's input game_board
game_board=( "x" "y" "y" "y" "x" "y" "x" "y" "y")
game_board=( "x" "x" "x" "" "" "" "" "" "")
game_board=( "" "" "" "" "" "" "" "" "")

# Algorithm
winning_rules(){
  matrix=(012 036 048 147 246 258 345 678)
  for i in ${matrix[@]}
    do
      block1=${game_board[${i:0:1}]}
      block2=${game_board[${i:1:1}]}
      block3=${game_board[${i:2:1}]}
      if [ "$1" ==  "$block1" ]
        then
        if [ "$1" ==  "$block2" ]
          then
          if [ "$1" == "$block3" ]
            then
            message win
          fi
        fi
      fi
    done
 }


draw_rules(){
  for i_block in {0..8}
    do
      if [ ! -z ${game_board[$i_block]} ]
        then
          ((no_of_blocks_filled++))
          if [ "9"  -eq "$no_of_blocks_filled" ]
            then
              message draw
          fi
      fi
    done
    unset no_of_blocks_filled
 }

live_board(){
  clear
  for (( i=0;i<=8;i+=3))
    do
      ((loop_index++))
      tput cup $[4 + $loop_index] 12
      echo -e "${bb}${game_board[$i]:-$i} | ${game_board[$[$i+1]]:-$[$i+1]} | ${game_board[$[$i+2]]:-$[$i+2]}"
      ((loop_index++))
      if [ "$i" -lt "6" ]
        then
          tput cup $[4 + $loop_index] 10
          echo -e "-------------"
      fi
    done
  unset loop_index
  echo -e ${b}
}

blocks(){
  if [ "$value_outof_range" == "yes" ]
    then
      echo -e -n "${r}"
      read -e -p "$current_player - type only number between 0 to 8 : " block
      echo -e -n "${dc}"
      unset value_outof_range
  elif [ "$block_is_empty" == "no" ]
    then
      echo -e -n "${g}"
      read -e -p "$current_player - Block is not empty, choose different : " block
      echo -e -n "${dc}"
  else
      echo -e -n "$current_player - "
      echo -e -n "${dc}"
      read -e -p "Enter block no of your choice : " block
  fi
  case "$block" in
    [0-8])  if [ -z ${game_board[$block]} ];
              then
                block_is_empty=yes
              else
                block_is_empty=no
                blocks
            fi;;
    *) value_outof_range=yes
       blocks;;
  esac
  echo -e -n "${b}"

  if [ "$block_is_empty" == "yes" ]
    then
      game_board[$block]="$user_input"
  fi
}




players(){
  if [ -z $player1 ]; then
    echo -e "${r}"
    read -e -p "Enter First Player Name: " player1
  fi
  if [ -z $player2 ]; then
    echo -n -e "${g}"
    read -e -p "Enter Second Player Name: " player2
  fi
  player1=${player1:- Player1}
  player2=${player2:- Player2}
  echo -e "${dc}"
}

# Main Program
main(){
  live_board
  echo -e "${dc}Welcome to Tic Tac toe Game"
  echo -e "Rule - To play type block number between 0 to 8 when asked"
  read -n 1 -p "Continue to play - Press Y : " y

  if  [ "$y" == "y" ]  ||  [ "$y" == "Y" ]
    then
      players
    else
      clear
      echo -e "\nHope to see you again!${dc}"
      exit
  fi
  live_board
  while :
    do
      user_turn=`expr ${main_index:- 0} % 2`
      if  [ "$user_turn" == "0" ]
        then
          main_index=1
          echo -e -n "${r}"
          current_player=$player1
          user_input="${r}X${bb}"
          blocks
      elif  [ "$user_turn" == "1" ]
        then
          unset main_index
          echo -e -n "${g}"
          current_player=$player2
          user_input="${g}O${bb}"
          blocks
      fi
      live_board
      winning_rules $user_input
      draw_rules
  done
}
main

2- Save the code in file.

3- Set executable permission – “chmod   a+x   tictactoe.sh.

4- Run script like  ./tictactoe.sh  which will draw pyramid on terminal.

Note: Run script on terminal with white background.

Output-

Leave a Reply

Your email address will not be published. Required fields are marked *