Go语言实现支持两个人一起对玩五子棋游戏程序

代码示例评论阅读6分46秒

由于Go语言不直接支持图形用户界面(GUI),为了简化起见,我们将提供一个基于控制台的五子棋游戏实现。这个实现将使用简单的文本输入和输出来模拟游戏过程。

五子棋游戏文章源自Golang编程指南-https://www.va26.com/work/380.html

以下是五子棋程序的简化版实现:文章源自Golang编程指南-https://www.va26.com/work/380.html

package main  
  
import (  
 "bufio"  
 "fmt"  
 "os"  
 "strings"  
)  
  
const (  
 Empty = iota  
 Black  
 White  
)  
  
const BoardSize = 15  
  
var board [BoardSize][BoardSize]int  
var currentPlayer = Black  
  
func main() {  
 reader := bufio.NewReader(os.Stdin)  
 initBoard()  
  
 for {  
 printBoard()  
 fmt.Printf("Player %s's turn. Enter your move (row col): ", getPlayerSymbol(currentPlayer))  
 input, _ := reader.ReadString('\n')  
 input = strings.TrimSpace(input)  
 move := strings.Split(input, " ")  
  
 row, _ := strconv.Atoi(move[0])  
 col, _ := strconv.Atoi(move[1])  
  
 if placeStone(row, col, currentPlayer) {  
 if checkWin(row, col, currentPlayer) {  
 printBoard()  
 fmt.Printf("Player %s wins!\n", getPlayerSymbol(currentPlayer))  
 break  
 }  
 currentPlayer = 3 - currentPlayer // switch player  
 } else {  
 fmt.Println("Invalid move. Try again.")  
 }  
 }  
}  
  
func initBoard() {  
 for i := 0; i < BoardSize; i++ {  
 for j := 0; j < BoardSize; j++ {  
 board[i][j] = Empty  
 }  
 }  
}  
  
func placeStone(row, col, player int) bool {  
 if row >= 0 && row < BoardSize && col >= 0 && col < BoardSize && board[row][col] == Empty {  
 board[row][col] = player  
 return true  
 }  
 return false  
}  
  
func checkWin(row, col, player int) bool {  
 directions := [][]int{{1, 0}, {0, 1}, {1, 1}, {1, -1}} // Horizontal, Vertical, Diagonal \ and /  
  
 for _, dir := range directions {  
 dx, dy := dir[0], dir[1]  
 count := 1  
  
 // Check forward direction  
 for x, y := row+dx, col+dy; isValid(x, y) && board[x][y] == player; x, y = x+dx, y+dy {  
 count++  
 if count == 5 {  
 return true  
 }  
 }  
  
 // Check backward direction (needed for horizontal and vertical checks)  
 if dx == 0 || dy == 0 {  
 count = 1 // Reset count for backward check  
 for x, y := row-dx, col-dy; isValid(x, y) && board[x][y] == player; x, y = x-dx, y-dy {  
 count++  
 if count == 5 {  
 return true  
 }  
 }  
 }  
 }  
 return false  
}  
  
func isValid(row, col int) bool {  
 return row >= 0 && row < BoardSize && col >= 0 && col < BoardSize  
}  
  
func printBoard() {  
 for i := 0; i < BoardSize; i++ {  
 for j := 0; j < BoardSize; j++ {  
 fmt.Print(getPlayerSymbol(board[i][j]), " ")  
 }  
 fmt.Println()  
 }  
}  
  
func getPlayerSymbol(player int) string {  
 switch player {  
 case Black:  
 return "●" // Black stone  
 case White:  
 return "○" // White stone  
 default:  
 return "+" // Empty space  
 }  
}

如何测试和验证:文章源自Golang编程指南-https://www.va26.com/work/380.html

1、将上述代码保存为 gomoku.go。文章源自Golang编程指南-https://www.va26.com/work/380.html

2、在命令行中,使用 go run gomoku.go 命令运行程序。文章源自Golang编程指南-https://www.va26.com/work/380.html

3、按照程序提示,两个玩家可以交替输入他们的走棋位置(行和列),例如:“7 7”。如果走棋位置有效且该位置为空,则棋子将被放置,并轮到另一位玩家。如果某位玩家在横、竖或斜线上连成五子,则该玩家获胜,游戏结束。文章源自Golang编程指南-https://www.va26.com/work/380.html 文章源自Golang编程指南-https://www.va26.com/work/380.html

 
  • 本文由golang编程指南作者原创,请勿恶意转载!
  • 转载请务必保留本文链接:https://www.va26.com/work/380.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证