duanna3634 2016-08-14 16:49
浏览 47
已采纳

恐慌:Golang中的索引超出范围错误

this is the error description im getting, i cant really understand what is the problemIn this program, I'm trying to calculate the shortest path between any two squares on chess board according to the moves of the knight, I checked few websites they say something about finding out the length, I ddint quiet understand how to work with.I'm encountering a error saying "panic:index out of range" Can anybody help me out..!! please

package main

import (
    "bufio"
    "fmt"
    "os"
    "sort"
    "strings"
)

var heightcurrstack int
var currentSource string
var currentDest string
var currentDestn int
var PosMoves [8]int

func main() {

    file, err := os.Open("chessin.txt")
    if err != nil {
        fmt.Println(err)
    }
    defer file.Close()
    scanner := bufio.NewScanner(file)
    for scanner.Scan() {
        strs := strings.Split(scanner.Text(), ",")

        currentSource = strs[0]
        currentDest = strs[1]
        IsValid(currentSource)
      toNumber(currentSource)
        IsValid(currentDest)
        currentDestn = toNumber(currentDest)
    }

}
func IsValid(b string) bool {

    if b[0] <= 'H' && b[0] >= 'A' && b[1] <= '8' && b[1] >= '1' {
        return true
    }
    return false
}

func toNumber(s string) int {
    var Number int

    if len(s) != 2 {
        fmt.Println("Invalid Input", s, ".")
    }
    Number = int(s[0]-'A')*8 + int(s[1]-'0')
    return Number
}

func ToString(n int) string {
    n--
    return string((n/8)+65) + string((n%8)+49)

}

func PossibleMoves(n int) [8]int {

    a := ToString(n)
    isval := IsValid(a)

    if isval == true {
        if IsValid(string(a[0]+1) + string(a[1]+2)) {
            PosMoves[0] = toNumber(string(a[0]+1) + string(a[1]+2))
        }
        if IsValid(string(a[0]+1) + string(a[1]-2)) {
            PosMoves[1] = toNumber(string(a[0]+1) + string(a[1]-2))
        }
        if IsValid(string(a[0]-1) + string(a[1]+2)) {
            PosMoves[2] = toNumber(string(a[0]-1) + string(a[1]+2))
        }
        if IsValid(string(a[0]-1) + string(a[1]-2)) {
            PosMoves[3] = toNumber(string(a[0]-1) + string(a[1]-2))
        }
        if IsValid(string(a[0]+2) + string(a[1]+1)) {
            PosMoves[4] = toNumber(string(a[0]+2) + string(a[1]+1))
        }
        if IsValid(string(a[0]+2) + string(a[1]-1)) {
            PosMoves[5] = toNumber(string(a[0]+2) + string(a[1]-1))
        }
        if IsValid(string(a[0]-2) + string(a[1]+1)) {
            PosMoves[6] = toNumber(string(a[0]-2) + string(a[1]+1))
        }
        if IsValid(string(a[0]-2) + string(a[1]-1)) {
            PosMoves[7] = toNumber(string(a[0]-2) + string(a[1]-1))
        }
    }

    sort.Sort(sort.Reverse(sort.IntSlice(PosMoves[0:8])))
    return PosMoves
}
var visithistory [64]bool

func IsvisitedNode(currentSource int) bool {


    visithistory[currentSource] = true

    if visithistory[currentSource] == true {

        return false

    }
    return true


}

type stack []int

func (s stack) Push(currentSource int) stack {
    return append(s, currentSource)
}

func (s stack) Pop() ([]int) {
    heightcurrstack := len(s)

    return s[0:heightcurrstack]
}

func dfstraversal(currentSource int) {

    var currentchildren [8]int
    copy(PosMoves[:], currentchildren[:])
    s := make(stack, 0)

   if IsvisitedNode(currentSource) == true {

        var j int = 0
        for j < len(currentchildren) {
            currentchildren[j+1] = currentSource
        }
 } else  if IsvisitedNode(currentSource) == false { //condition 1:previously not visited
        if heightcurrstack > 6 { //condition 2: if the number of moves are more than 6
            tracesuccessfulpath()
        }

        if currentSource == currentDestn { //condition 3 : if the destination  posititon is found
            tracesuccessfulpath()
        }
        s = s.Push(currentSource)

        s = s.Push(currentchildren[0])

        currentchildren[0] = currentSource

        if currentSource == currentDestn {
            tracesuccessfulpath()
        }

    }

    PossibleMoves(currentSource)
    dfstraversal(currentSource)

}

func tracesuccessfulpath() {


    s := make(stack, 0)
     s.Pop()


var Path []string 

for _,x := range s {
    Path := append(Path, ToString(x))

 fmt.Println(Path)
}

}
  • 写回答

1条回答 默认 最新

  • douhui5953 2016-08-14 18:43
    关注

    You are passing a 0 or 1 char string to your "toNumber(s string)" function. That's why is panicking.

    This is probably caused because you don't have the proper information in chessin.txt. When you split it in strs := strings.Split(scanner.Text(), ",") is not finding what you are expecting

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?