banner
biuaxia

biuaxia

"万物皆有裂痕,那是光进来的地方。"
github
bilibili
tg_channel

Golang輸出中國象棋

標題:Golang 輸出中國象棋
日期:2022-05-18 17:18:00
toc:false
index_img:https://b3logfile.com/file/2022/05/image-40bc4e8c.png
類別:

  • Go
    標籤:
  • Go
  • go
  • Golang
  • golang
  • 輸出

package main

import (
	"fmt"
)

var (
	boardInfo = [9]rune{
		'車', '馬', '相', '士', '帥', '士', '相', '馬', '車',
	}
)

func main() {
	var board [10][9]rune

	board[2][1] = '炮'
	board[2][7] = '炮'
	board[7][1] = '炮'
	board[7][7] = '炮'

	for i := 0; i < len(board[1]); i++ {
		board[4][i] = '卒'
	}
	for i := 0; i < len(board[1]); i++ {
		board[5][i] = '兵'
	}

	for i := 0; i < len(boardInfo); i++ {
		board[0][i] = boardInfo[i]
	}
	for column := range board[len(board)-1] {
		board[len(board)-1][column] = boardInfo[column]
		if 4 == column {
			board[len(board)-1][column] = '將'
		}
	}

	for i := range board {
		for j := range board[i] {
			if board[i][j] == 0 {
				board[i][j] = '·'
			}
		}
	}

	for i := 0; i < len(board); i++ {
		for j := 0; j < len(board[i]); j++ {
			fmt.Printf("%c\t", board[i][j])
		}
		fmt.Println()
	}
}

執行結果:

車      馬      相      士      帥      士      相      馬      車
·       ·       ·       ·       ·       ·       ·       ·       ·
·       炮      ·       ·       ·       ·       ·       炮      ·
·       ·       ·       ·       ·       ·       ·       ·       ·
卒      卒      卒      卒      卒      卒      卒      卒      卒
兵      兵      兵      兵      兵      兵      兵      兵      兵
·       ·       ·       ·       ·       ·       ·       ·       ·
·       炮      ·       ·       ·       ·       ·       炮      ·
·       ·       ·       ·       ·       ·       ·       ·       ·
車      馬      相      士      將      士      相      馬      車
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。