Skip to main content

Do you support us?

Business address
0x1D24D8f27ea73ff604C7685246bdC6ae55bddaEF

Build my own Universe

We create our own universe called programmer stimulation Where we play rules of physics , mathematics and language . Two days before i was thinking about how can i build tic toc in this language. So i starting working on it. There're so many concepts which i learn from this artwork.

package main
import (
"fmt"
"os"
"bufio"
"strconv"
"math/rand"
)
const (
cols = 3
rows
)
func main(){
arr := [rows][cols]string{}
playerState := playerSelectionMode()
computerState := computerMode(playerState)
var e int =0
var p int = 0
var q int = 0
var mark int =0
var status bool = true
var isOver bool = false
fmt.Printf("Player Mark:%s \t KIzo Mark:%s\n",playerState, computerState)
board := drawBoard(arr)
for !isOver {
if playerState == "x" || playerState == "X" && computerState == "o" || computerState == "O" || playerState == "o" || playerState == "O" && computerState == "x" || computerState == "X"{
fmt.Println("\nPlayer Turn:",mark)
e = moveExecute()
p,q = cood2D(e)
if board[p][q] == "" {
board[p][q] = playerState
screen(board)
}else if board[p][q] == "x" || board[p][q] == "X" || board[p][q] == "o" || board[p][q] == "O" {
fmt.Println("Condition execute this place contain x or o")
e = moveExecute()
fmt.Println("\n Player re-enter your position:",mark)
p,q = cood2D(e)
if board[p][q] != "" {
board[p][q] = playerState
screen(board)
}
}
mark = computerMove()
fmt.Println("\nKizo thinking:",mark)
p,q = cood2D(mark)
if board [p][q] == ""{
board[p][q] = computerState
screen(board)
}else if board[p][q] == "x" || board[p][q] == "X" || board[p][q] == "o" || board[p][q] == "O" {
fmt.Println("Condition execute kizo re-decide his move")
mark = computerMove()
fmt.Println("\nKizo thinking:",mark)
p,q = cood2D(e)
if board[p][q] == "" {
board[p][q] = computerState
screen(board)
}
}
}
status = gameOver(board)
fmt.Println(status)
stop := stopRoutine(status)
if stop != false{
isOver = true
releaseBy()
os.Exit(0)
}else if status != false{
isOver = false
}
}
}
func playerSelectionMode()string{
fmt.Printf("Press x or o\n")
fmt.Printf("Press yes : quit , no : cancel\n")
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
text := input.Text()
if text == "yes"{
break
}
if text == "x" || text =="X"{
fmt.Printf("****You SELECT Player1**** KIzo ready to beat you\n")
}else if text == "o" || text == "O"{
fmt.Printf("****You SELECT Player2**** KIzo ready to beat you\n")
}
return text
}
return "nothing"
}
func computerMode(action string)string{
if action == "x" || action =="X"{
return "o"
}else if action == "o" || action == "O"{
return "x"
}
return "nothing"
}
func drawBoard(arr [cols][rows]string) *[cols][rows]string{
for i := 0; i < cols; i++{
for j := 0; j < rows; j++{
arr[i][j] = ""
}
}
return &arr
}
func moveExecute()int{
fmt.Printf("Keys Range [0-8]\n")
move := bufio.NewScanner(os.Stdin)
for move.Scan(){
key := move.Text()
i,err := strconv.Atoi(key)
if err == nil && i >= 0 && i< 9{
return i
}
}
return -1
}
func cood2D(point int) (int , int){
var p int = -1
var q int = -1
switch point {
case 0:
p,q = 0,0
break
case 1:
p,q = 0,1
break
case 2:
p,q = 0,2
break
case 3:
p,q = 1,0
break
case 4:
p,q = 1,1
break
case 5:
p,q = 1,2
break
case 6:
p,q = 2,0
break
case 7:
p,q = 2,1
break
case 8:
p,q = 2,2
break
default:
fmt.Println("invalid key")
}
return p,q
}
func screen(board *[cols][rows]string){
for i := 0; i < cols; i++{
for j := 0; j < rows; j++{
fmt.Printf("%s",board[i][j])
}
fmt.Println()
}
}
func computerMove()int{
return rand.Intn(8)+rand.Intn(1)
}
func gameOver(board *[cols][rows]string)bool{
var stats bool =false
if board[0][0] == "x" && board[0][1] == "x" && board[0][2] == "x"{
stats =true
} else if board[0][0] == "X" && board[0][1]== "X" && board[0][2]== "X"{
stats =true
} else if board[0][0] == "x" && board[1][0]== "x" && board[2][0]== "x"{
stats =true
} else if board[0][0] == "X" && board[1][0]== "X" && board[2][0]== "X"{
stats =true
} else if board[0][0] == "x" && board[1][1]== "x" && board[2][2]== "x"{
stats =true
} else if board[0][0] == "X" && board[1][1]== "X" && board[2][2]== "X"{
stats =true
} else if board[0][2] == "X" && board[1][2]== "X" && board[2][2]== "X"{
stats =true
} else if board[0][2] == "x" && board[1][2]== "x" && board[2][2]== "x"{
stats =true
} else if board[0][2] == "x" && board[1][1]== "x" && board[2][0]== "x"{
stats =true
} else if board[0][2] == "X" && board[1][1]== "X" && board[2][0]== "X"{
stats =true
} else if board[2][0] == "x" && board[2][1]== "x" && board[2][2]== "x"{
stats =true
} else if board[2][0] == "X" && board[2][1]== "X" && board[2][2]== "X"{
stats =true
} else if board[1][0] == "X" && board[1][1]== "X" && board[1][2]== "X"{
stats =true
} else if board[1][0] == "x" && board[1][1]== "x" && board[1][2]== "X"{
stats =true
} else if board[0][0] == "o" && board[0][1] == "o" && board[0][2] == "o"{
stats =false
} else if board[0][0] == "O" && board[0][1]== "O" && board[0][2]== "O"{
stats =false
} else if board[0][0] == "o" && board[1][0]== "o" && board[2][0]== "o"{
stats =false
} else if board[0][0] == "O" && board[1][0]== "O" && board[2][0]== "O"{
stats =false
} else if board[0][0] == "o" && board[1][1]== "o" && board[2][2]== "o"{
stats =false
} else if board[0][0] == "O" && board[1][1]== "O" && board[2][2]== "O"{
stats =false
} else if board[0][2] == "O" && board[1][2]== "O" && board[2][2]== "O"{
stats =false
} else if board[0][2] == "o" && board[1][2]== "o" && board[2][2]== "o"{
stats =false
} else if board[0][2] == "o" && board[1][1]== "o" && board[2][0]== "o"{
stats =false
} else if board[0][2] == "O" && board[1][1]== "O" && board[2][0]== "O"{
stats =false
} else if board[2][0] == "o" && board[2][1]== "o" && board[2][2]== "o"{
stats =false
} else if board[2][0] == "O" && board[2][1]== "O" && board[2][2]== "O"{
stats =false
} else if board[1][0] == "O" && board[1][1]== "O" && board[1][2]== "O"{
stats =false
} else if board[1][0] == "o" && board[1][1]== "o" && board[1][2]== "o"{
stats =false
}
return stats
}
func stopRoutine(s bool) bool{
if s == true {
fmt.Println("\t ******Player Ko****")
return s
}
return false
}
func releaseBy(){
fmt.Printf("Ali Hassan")
fmt.Println("\t Alideveloper95@gmail.com")
}

Multidimensional Array:
         In my previous article, I had show you how to play with arrays. 

arr := [rows][cols]string{}

You can create array of any size and any dimension
just like this. Rows and Columns together represent
dimension of array. Initially empty array.

Input
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
text := input.Text()
               

func NewScanner1.1

func NewScanner(r io.Reader) *Scanner
NewScanner returns a new Scanner to read from r. 
The split function defaults to ScanLines. 
                

func (*Scanner) Scan1.1

func (s *Scanner) Scan() bool
Scan advances the Scanner to the next token, which will then be
available through the Bytes or Text method. It returns false when
the scan stops, either by reaching the end of the input or an error.
After Scan returns false, the Err method will return any error that
occurred during scanning, except that if it was io.EOF, Err will
return nil. Scan panics if the split function returns too many empty
tokens without advancing the input. This is a common error mode
for scanners.
                

func (*Scanner) Text1.1

func (s *Scanner) Text() string
Text returns the most recent token generated by a call to Scan
 as a newly allocated string holding its bytes.
Return reference:

func drawBoard(arr [cols][rows]string) *[cols][rows]string{}
if you return pointer which is safe operation for array
then you simply return like this "&arr". In golang this
is called pass by reference.

Function Return type:
Suppose you can return data then you need to add
return type after ")".In golang programmer return so
many variables at same time
     func cood2D(point int) (int , int){}
Because i return two return types, so that i used "
(return-type, return-type)"

if you look at code you obverse whole code take only 
one parameter.In upcoming articles , we discuss in very
detail that why function only take one parameter . How
we pass multiple parameters this technique 
called variadic function.

Strconv() :
      i,err := strconv.Atoi(key)
strconv return two variables one is value called "i" and other is
error function that return error called function panic which is
similar to exceptions. In Java you can handle exception, similar
case in golang you should care about panic. Don't worry this
easy just like catch mouse. Before use strconv you can import
this package.

Rand:
In Java rand function initialize with arguments, but in golang
you simply import math/rand and use it
rand.Intn(8)+rand.Intn(1)

Scope:
In go, Programmer care about format, scope , lifetime and
variables.If your code are not in proper format or your variable
out of scope then code will n't execute.If you use variables
outside the loop or conditions then declare your
variable, otherwise you can use short name declaration.

OS:
If you can open, read file , use console or system level operation
then os is very helpful. os most powerful.




Comments

Popular posts from this blog

Rosicrucian cipher

In 1513 Cornelius Agrippa introduce early form Rosicrucian Cipher in his books Occult of Philosophy. Geometric and simple substitution cipher are well known ciphers      Pigpen , Freemason, Napoleon and tic tac toe ciphers in which alphabets or symbols arrange in grids.  This is an anagram, "How to reconstruct a data " lets encode the message.       Pigpen cipher , Rosicrucian and Tic tac toe Cipher                      Thank you have a good day.    

Without this Agent , mission doesn't accomplish?

If you play military games such as Delta force , IGI Project , commandos etc . You familiar about in your team. Each member of team are unique in their skills. Some are snipers , short gun , hackers, communication officer. Now I will introduce golang special agent which are quite helpful in most of time.                 Variadic Function  will show in a moment! Variadic Function:             Variadic  Functions means a function that arguments varing. In variadic function ... called ellipsis.   package main import ( "fmt" ) func main() { values := []int{1,2,3} fmt.Println("Value[0]",seriesAdd(values[0])) fmt.Println("Total", seriesAdd(values...)) } func seriesAdd(x ...int)int{ var count int = 0 for _, v := range x{ count+= v } return count } Conditions :     Before passing to the variadic function array must be slice.               ...

Life is all about decisions and repeat your routine

Life is all about Decisions and repeat our routine is an excellent idea which i show you within a moment. Everyday we take so many decisions based on environment conditions, circumstances. Each decision based on cause and effect philosophy. Programmer are wizards who create own world with data and structures. Without data or structure our world is nothing. Conditions: Conditions help us to take decision based under certain circumstances, such as if a number divisible by 2 at least two times then it's said to be double even. package main import ( "fmt" ) func main() { const divisible =2 var even int = 4 if even%divisible == 0 { n := even/divisible n /= divisible if n ==1 { fmt.Println("Doubly Even")  } }   } If you're a developer then you you know this code quite different from other languages syntax. In most languages, you see like this "if (condition)". Yeah it's different but this language compilati...