2016-08-14 4 views
-1

this is the error description im getting, i cant really understand what is the problem이 프로그램에서는 기사의 움직임에 따라 체스 판에있는 두 개의 사각형 사이의 최단 경로를 계산하려고하는데, 그 길이로, 나는 조용하게 일하는 법을 안다. 나는 "공포감 : 범위를 벗어난 색인"이라는 오류를 만난다. 아무도 나를 도울 수 없다. .. !!공황 : 골란의 범위를 벗어난 색인의 굴착

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을 (문자열)"함수에 0 또는 1 문자의 문자열을 전달하는하시기 바랍니다. 그래서 당황하고 있습니다.

이것은 아마도 chessin.txt에 적절한 정보가 없기 때문에 발생했을 수 있습니다. 당신이 그것을 쪼개었을 때 strs := strings.Split(scanner.Text(), ",")는 당신이 기대하는 것을 발견하지 못합니다