package main
import "fmt"
/*
函数说明:
Erebus_shows_world 打印棋盘
Erebus_plays_game 用户下棋、电脑下棋
Erebus_checks_system 检测是否满足结束条件
Erebus 英文名
main 引出Erebus
*/
var a = [3][3]byte{
{'_', '_', '_'},
{'_', '_', '_'},
{'_', '_', '_'},
}
func main() {
Erebus()
}
func Erebus() {
fmt.Printf("电脑先手请输入1,玩家先手请输入2\n")
var ijn int
fmt.Scanf("%d", &ijn)
if ijn == 1 {
a[1][1] = 'X'
Erebus_shows_world()
}
Erebus_plays_game()
}
func Erebus_shows_world() {
var i1, ji int
for i1 = 0; i1 < 3; i1++ {
for ji = 0; ji < 3; ji++ {
fmt.Printf("%c ", a[i1][ji])
}
fmt.Printf("\n")
}
}
func Erebus_plays_game() {
var x, y, h, i, j int
var k = true
for true {
fmt.Printf("输入你的棋子坐标:\n")
fmt.Scanf("%d %d", &x, &y)
fmt.Scanf("%d %d", &x, &y)
if x > 3 || x < 1 || y > 3 || y < 1 {
fmt.Printf("坐标有误,请重输\n")
continue
}
if a[x - 1][y - 1] == '_' {
a[x - 1][y - 1] = 'O'
Erebus_checks_system()
}
else {
fmt.Printf("已有子,请重输!\n")
continue
}
for k, i = true, 0; k && i < 3; i++ {
for j = 0; k && j < 3; j++ {
for h = 0; k && h < i; h++ {
if a[i][j] == a[h][j] && a[i][j] == 'X' && a[3 - i - h][j] == '_' {
a[3 - i - h][j] = 'X'
k = false
}
}
}
}
for i = 0; k && i < 3; i++ {
for j = 0; k && j < 3; j++ {
for h = 0; k && h < j; h++ {
if a[i][j] == a[i][h] && a[i][j] == 'X' && a[i][3 - j - h] == '_' {
a[i][3 - j - h] = 'X'
k = false
}
}
}
}
for i = 0; k && i < 3; i++ {
for j = 0; k && j < i; j++ {
if a[i][i] == a[j][j] && a[i][i] == 'X' && a[3 - i - j][3 - i - j] == '_' {
a[3 - i - j][3 - i - j] = 'X'
k = false
}
}
}
for i = 0; k && i < 3; i++ {
for j = 0; k && j < i; j++ {
if a[2 - i][i] == a[2 - j][j] && a[2 - i][i] == 'X' && a[i + j - 1][3 - i - j] == '_' {
a[i + j - 1][3 - i - j] = 'X'
k = false
}
}
}
for i = 0; k && i < 3; i++ {
for j = 0; k && j < 3; j++ {
for h = 0; k && h < i; h++ {
if a[i][j] == a[h][j] && a[i][j] == 'O' && a[3 - i - h][j] == '_' {
a[3 - i - h][j] = 'X'
k = false
}
}
}
}
for i = 0; k && i < 3; i++ {
for j = 0; k && j < 3; j++ {
for h = 0; k && h < j; h++ {
if a[i][j] == a[i][h] && a[i][j] == 'O' && a[i][3 - j - h] == '_' {
a[i][3 - j - h] = 'X'
k = false
}
}
}
}
for i = 0; k && i < 3; i++ {
for j = 0; k && j < i; j++ {
if a[i][i] == a[j][j] && a[i][i] == 'O' && a[3 - i - j][3 - i - j] == '_' {
a[3 - i - j][3 - i - j] = 'X'
k = false
}
}
}
for i = 0; k && i < 3; i++ {
for j = 0; k && j < i; j++ {
if a[2 - i][i] == a[2 - j][j] && a[2 - i][i] == 'O' && a[i + j - 1][3 - i - j] == '_' {
a[i + j - 1][3 - i - j] = 'X'
k = false
}
}
}
if k && a[1][1] == '_' {
a[1][1] = 'X'
k = false
}
if k && a[0][2] == '_' {
a[0][2] = 'X'
k = false
}
for i = 0; k && i < 3; i++ {
for j = 0; k && j < 3; j++ {
if a[i][j] == '_' {
a[i][j] = 'X'
k = false
}
}
}
Erebus_checks_system()
Erebus_shows_world()
}
}
func Erebus_checks_system() {
var i2, j2 int
var count = 0
if a[0][0] == a[0][1] && a[0][1] == a[0][2] && a[0][2] == 'X' || a[1][0] == a[1][1] && a[1][1] == a[1][2] && a[1][2] == 'X' || a[2][0] == a[2][1] && a[2][1] == a[2][2] && a[2][2] == 'X' || a[0][0] == a[1][1] && a[1][1] == a[2][2] && a[2][2] == 'X' || a[2][0] == a[1][1] && a[1][1] == a[0][2] && a[0][2] == 'X' || a[0][0] == a[1][0] && a[1][0] == a[2][0] && a[2][0] == 'X' || a[0][1] == a[1][1] && a[1][1] == a[2][1] && a[2][1] == 'X' || a[0][2] == a[1][2] && a[1][2] == a[2][2] && a[2][2] == 'X' {
Erebus_shows_world()
fmt.Printf("LOST!\n")
}
else {
if a[0][0] == a[0][1] && a[0][1] == a[0][2] && a[0][2] == 'O' || a[1][0] == a[1][1] && a[1][1] == a[1][2] && a[1][2] == 'O' || a[2][0] == a[2][1] && a[2][1] == a[2][2] && a[2][2] == 'O' || a[0][0] == a[1][1] && a[1][1] == a[2][2] && a[2][2] == 'O' || a[2][0] == a[1][1] && a[1][1] == a[0][2] && a[0][2] == 'O' || a[0][0] == a[1][0] && a[1][0] == a[2][0] && a[2][0] == 'O' || a[0][1] == a[1][1] && a[1][1] == a[2][1] && a[2][1] == 'O' || a[0][2] == a[1][2] && a[1][2] == a[2][2] && a[2][2] == 'O' {
fmt.Printf("WIN!\n")
Erebus_shows_world()
}
else {
for i2 = 0; i2 < 3; i2++ {
for j2 = 0; j2 < 3; j2++ {
if a[i2][j2] == '_' {
count++
}
}
}
if count == 0 {
fmt.Printf("DRAW!\n")
Erebus_shows_world()
}
count = 0
}
}
}
转载于:https://www.cnblogs.com/lightac/p/10534739.html