2016-08-14 76 views
-1

this is the error description im getting, i cant really understand what is the problem在這個程序中,我試圖根據騎士的移動計算棋盤上任意兩個方格之間的最短路徑,我查了幾個網站,他們說了一些關於查找在長度,我ddint安靜明白如何處理。我遇到一個錯誤,說「恐慌:索引超出範圍」任何人都可以幫助我.. ..請恐慌:索引超出範圍誤差在Golang

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) 
} 

} 
+3

沒有人可以幫助你一樣好。開始使用調試器,它會告訴你線路,問題在哪裏; o) – lofcek

+2

你甚至不需要調試器,輸出將顯示導致恐慌的確切的線路和堆棧跟蹤。 – JimB

+0

@JimB我也發佈了錯誤,我不明白代碼有什麼問題 – Riya

回答

1

您正在向您的「toNumber(s字符串)」函數傳遞一個0或1個字符串。這就是恐慌的原因。

這可能是因爲您沒有在chessin.txt中的正確信息。當你分裂它在strs := strings.Split(scanner.Text(), ",")沒有找到你所期待的