• GO语言井字棋


    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
    ")
    		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("
    ")
    		}
    }
    
    func Erebus_plays_game() {
    	var x, y, h, i, j int
    		var k = true
    		for true {
    			fmt.Printf("输入你的棋子坐标:
    ")
    				fmt.Scanf("%d %d", &x, &y)
    				fmt.Scanf("%d %d", &x, &y)
    				if x > 3 || x < 1 || y > 3 || y < 1 {
    					fmt.Printf("坐标有误,请重输
    ")
    						continue
    				}
    			if a[x - 1][y - 1] == '_' {
    				a[x - 1][y - 1] = 'O'
    					Erebus_checks_system()
    			}
    			else {
    				fmt.Printf("已有子,请重输!
    ")
    					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!
    ")
    		}
    		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!
    ")
    					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!
    ")
    						Erebus_shows_world()
    				}
    				count = 0
    			}
    		}
    }
    
    作者:LightAc
    出处:https://www.cnblogs.com/lightac/
    联系:
    Email: dzz@stu.ouc.edu.cn
    QQ: 1171613053
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    LeetCode【125. 验证回文串】
    LeetCode【122. 买卖股票的最佳时机 II】
    LeetCode【121. 买卖股票的最佳时机】
    LeetCode【119. 杨辉三角 II】
    LeetCode【118. 杨辉三角】
    LeetCode【112. 路径总和】
    PAT1024
    PAT1020
    PAT1018
    PAT1017
  • 原文地址:https://www.cnblogs.com/lightac/p/10534739.html
Copyright © 2020-2023  润新知