Created
July 6, 2016 14:52
-
-
Save maurostorch/571797123ff4ade44c8c202fdaf7e1e0 to your computer and use it in GitHub Desktop.
Learning Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func check(tab [3][3]int) int { | |
if tab[0][0] == tab[1][0] && tab[0][0] == tab[2][0] && tab[0][0] > 0{ | |
return tab[0][0] | |
} | |
if tab[0][0] == tab[0][1] && tab[0][0] == tab[0][2] && tab[0][0] > 0 { | |
return tab[0][0] | |
} | |
if tab[0][0] == tab[1][1] && tab[0][0] == tab[2][2] && tab[0][0] > 0 { | |
return tab[0][0] | |
} | |
if tab[2][0] == tab[1][1] && tab[2][0] == tab[0][2] && tab[2][0] > 0 { | |
return tab[2][0] | |
} | |
if tab[1][0] == tab[1][1] && tab[1][0] == tab[1][2] && tab[1][0] > 0 { | |
return tab[1][0] | |
} | |
if tab[2][0] == tab[2][1] && tab[2][0] == tab[2][2] && tab[2][0] > 0 { | |
return tab[2][0] | |
} | |
if tab[0][1] == tab[1][1] && tab[0][1] == tab[2][1] && tab[0][1] > 0 { | |
return tab[0][1] | |
} | |
if tab[0][2] == tab[1][2] && tab[0][2] == tab[2][2] && tab[0][2] > 0 { | |
return tab[0][2] | |
} | |
return 0 | |
} | |
func main() { | |
var tab [3][3]int | |
for i:=0;i<3;i++ { | |
for j:=0;j<3;j++ { | |
tab[i][j]=0 | |
} | |
} | |
fmt.Println("Player 1 name:") | |
var player1 string | |
fmt.Scanf("%s",&player1) | |
fmt.Println("Player 2 name:") | |
var player2 string | |
fmt.Scanf("%s",&player2) | |
side:=true | |
for { | |
var player int; | |
if side { | |
player = 1 | |
fmt.Println("Player1") | |
side = false | |
} else { | |
player = 2 | |
fmt.Println("Player2") | |
side = true | |
} | |
fmt.Println("Position (1-3) (1-3)") | |
var x,y int | |
fmt.Scanf("%d %d",&x,&y) | |
if tab[x-1][y-1] > 0 { | |
side=!side | |
fmt.Println("Position not valid:",x,y) | |
continue | |
} else { | |
tab[x-1][y-1] = player | |
win:=check(tab) | |
if win > 0 { | |
fmt.Println("Winner is:", win) | |
break | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment