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.
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
The split function defaults to ScanLines.
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) *ScannerNewScanner 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.
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.
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.
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
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)"
(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.
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.
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
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
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.
then os is very helpful. os most powerful.
Comments
Post a Comment