Ejemplo n.º 1
0
// PutAt will put a give piece at the given position and return removed pieces and the response code.
func (b *Board) PutAt(c cell.Cell, x int, y int, h *history.History) (vector.Vector, PutResponse) {
	if b.At(x, y) != cell.SPACE {
		return nil, OCCUPIED
	}

	b.putAt(c, x, y)
	takenOffs := vector.Vector{}
	rc := c.Reverse()
	if b.shouldTakeOff(x-1, y, rc) {
		b.takeOff(x-1, y, rc, &takenOffs)
	}
	if b.shouldTakeOff(x+1, y, rc) {
		b.takeOff(x+1, y, rc, &takenOffs)
	}
	if b.shouldTakeOff(x, y-1, rc) {
		b.takeOff(x, y-1, rc, &takenOffs)
	}
	if b.shouldTakeOff(x, y+1, rc) {
		b.takeOff(x, y+1, rc, &takenOffs)
	}

	if b.shouldTakeOff(x, y, c) {
		b.TakeAt(x, y)
		return nil, FORBIDDEN
	}

	if len(takenOffs) == 1 && h.IsKo(c, x, y) {
		b.putAt(cell.SPACE, x, y)
		taken := takenOffs.Last().(point.Point)
		b.putAt(c.Reverse(), taken.X(), taken.Y())
		return nil, KO
	}

	return takenOffs, OK
}
Ejemplo n.º 2
0
func (lex *Lexer) ParseExpression() *vector.Vector {
	tree := new(vector.Vector)
	for lex.current != "" {
		if lex.current == "," {
			break
		} else if lex.current == "(" {
			lex.Consume()
			args := lex.ParseArguments()
			if lex.current == ")" {
				if tree.Len() == 0 {
					tree.Push(NewMessage("", new(vector.Vector)))
				}

				if tree.Last().(IMessage).GetArguments().Len() > 0 {
					println("*** Arguments are empty")
					tree.Push(NewMessage("", new(vector.Vector)))
				}

				tree.Last().(IMessage).SetArguments(args)
			} else {
				println("Syntax Error: ')' expected")
			}
			println(len(*args))
		} else if lex.current == ")" {
			break
		} else {
			println("*** (ParseExpression) / fallback (line: " + strconv.Itoa(lex.line) + ") -- lex.current = " + lex.current + "; lex.next = " + lex.next)
			tree.Push(NewMessage(lex.current, new(vector.Vector)))
			lex.Consume()
		}
	}

	return tree
}
Ejemplo n.º 3
0
func ConvertGridToString(grid *vector.Vector) string {
	var output, format string

	for _, lineInterface := range *grid {
		line := lineInterface.(*vector.IntVector)
		for j, number := range *line {
			format = "%d"
			if j+1 != line.Len() {
				format += " "
			}

			output += fmt.Sprintf(format, number)
		}

		if lineInterface != grid.Last() {
			output += "\n"
		}
	}

	return output
}
Ejemplo n.º 4
0
Archivo: main.go Proyecto: nunb/GoForth
func parse_forth(dat string, DataStack *vector.Vector) {
	L := DataStack.Len()

	switch strings.TrimSpace(string(dat)) {
	case "":
	case "<cr>":
		return
	case "t":
		//check the DataStack size using the popped value
		//	if it passes, then the program continues
		minimum := int(DataStack.Pop().(float32))
		if DataStack.Len() < minimum {
			log.Println("DataStack has not enough minerals (values)")
		}
	case ".":
		log.Println(DataStack.Pop())
	case "0SP":
		DataStack.Cut(0, L)
	case ".S":
		log.Println(DataStack)
	case "2/":
		DataStack.Push(DataStack.Pop().(float32) / 2)
	case "2*":
		DataStack.Push(DataStack.Pop().(float32) * 2)
	case "2-":
		DataStack.Push(DataStack.Pop().(float32) - 2)
	case "2+":
		DataStack.Push(DataStack.Pop().(float32) + 2)
	case "1-":
		DataStack.Push(DataStack.Pop().(float32) - 1)
	case "1+":
		DataStack.Push(DataStack.Pop().(float32) + 1)
	case "DUP":
		DataStack.Push(DataStack.Last())
	case "?DUP":
		if DataStack.Last().(float32) != 0 {
			DataStack.Push(DataStack.Last().(float32))
		}
	case "PICK":
		number := int(DataStack.Pop().(float32))

		if number < L {
			DataStack.Push(DataStack.At(L - 1 - number).(float32))
		} else {
			log.Fatal("picking out of stack not allowed. Stack Length: " + string(L) + ". Selecting: " + string(number) + ".")
			return
		}
	case "TUCK":
		DataStack.Insert(L-2, int(DataStack.Last().(float32)))
	case "NIP":
		DataStack.Delete(L - 2)
	case "2DROP":
		DataStack.Pop()
		DataStack.Pop()
	case "2DUP":
		DataStack.Push(DataStack.At(L - 2))
		DataStack.Push(DataStack.At(DataStack.Len() - 2))
	case "DROP":
		DataStack.Pop()
	case "OVER":
		DataStack.Push(DataStack.At(L - 2))
	case "SWAP":
		l := DataStack.Len()
		DataStack.Swap(l-2, l-1)
	case "*":
		num1 := DataStack.Pop().(float32)
		num2 := DataStack.Pop().(float32)
		DataStack.Push(num1 * num2)
	case "+":
		num1 := DataStack.Pop().(float32)
		num2 := DataStack.Pop().(float32)
		DataStack.Push(num1 + num2)
	case "-":
		num1 := DataStack.Pop().(float32)
		num2 := DataStack.Pop().(float32)
		DataStack.Push(num2 - num1)
	case "/":
		num1 := DataStack.Pop().(float32)
		num2 := DataStack.Pop().(float32)
		DataStack.Push(num2 / num1)
	case "-ROT":
		DataStack.Swap(L-1, L-2)
		DataStack.Swap(L-2, L-3)
	case "ROT":
		DataStack.Swap(L-3, L-2)
		DataStack.Swap(L-2, L-1)
	case "2OVER":
		DataStack.Push(DataStack.At(L - 4))
		DataStack.Push(DataStack.At(DataStack.Len() - 4))
	case "2SWAP":
		DataStack.Swap(L-4, L-2)
		DataStack.Swap(L-3, L-1)
	case "EMIT":
		log.Println(string([]byte{uint8(DataStack.Last().(float32))}))
	default:
		val, ok := strconv.Atof32(dat)

		if ok == nil {
			DataStack.Push(val)
		} else {
			log.Println(ok)
			log.Fatalln("error, unknown token \"" + dat + "\"")
		}
	}
}
Ejemplo n.º 5
0
func (m *Map) Overlay(n *Map, mergeFaces func(interface{}, interface{}) (interface{}, os.Error)) (*Map, os.Error) {
	o := NewMap()
	CopiedEdges := new(vector.Vector)
	m.Edges.Do(func(l interface{}) {
		e := l.(*Edge)
		if e.start.Point.Less(e.end.Point) {
			f, g := NewEdgePair(NewVertex(e.start.Copy()), NewVertex(e.end.Copy()))
			f.face = e.face
			g.face = e.twin.face
			f.Generation = e.Generation
			g.Generation = e.twin.Generation
			f.fromMap,g.fromMap = m,m
			CopiedEdges.Push(f)
			CopiedEdges.Push(g)
		}
	})
	n.Edges.Do(func(l interface{}) {
		e := l.(*Edge)
		if e.start.Point.Less(e.end.Point) {
			f, g := NewEdgePair(NewVertex(e.start.Copy()), NewVertex(e.end.Copy()))
			f.face = e.face
			g.face = e.twin.face
			f.Generation = e.Generation
			g.Generation = e.twin.Generation
			f.fromMap,g.fromMap = n,n
			CopiedEdges.Push(f)
			CopiedEdges.Push(g)
		}
	})

	Q := new(vector.Vector)
	T := new(sweepStatus)
	T.segments = new(vector.Vector)

	CopiedEdges.Do(func(l interface{}) {
		e, _ := l.(*Edge)
		if e.start.Point.Less(e.end.Point) {
			evt := new(sweepEvent)
			evt.point = e.start.Point
			evt.coincidentEdge = e
			Q.Push(evt)
			evt = new(sweepEvent)
			evt.point = e.end.Point
			evt.coincidentEdge = nil
			Q.Push(evt)
		}
	})

	heap.Init(Q)

	for Q.Len() > 0 {
		evt, _ := heap.Pop(Q).(*sweepEvent)
		//fmt.Fprintf(os.Stderr,"event: %v\n",evt.point)
		L := new(vector.Vector)
		Lswp := new(sweepStatus)
		Lswp.segments = L
		Lswp.sweepLocation = evt.point
		if evt.coincidentEdge != nil {
			L.Push(evt.coincidentEdge)
		}
		for Q.Len() > 0 && evt.point.Equal(Q.At(0).(*sweepEvent).point) {
			evt, _ := heap.Pop(Q).(*sweepEvent)
			if evt.coincidentEdge != nil {
				L.Push(evt.coincidentEdge)
			}
		}
		sort.Sort(Lswp)
		for i := 0; i < L.Len()-1; {
			if L.At(i).(*Edge).Equal(L.At(i + 1).(*Edge)) {
				L.At(i).(*Edge).newFace = L.At(i + 1).(*Edge).face
				L.At(i).(*Edge).twin.newFace = L.At(i + 1).(*Edge).twin.face
				L.At(i).(*Edge).fromMap = nil //no longer from a unique map
				L.At(i).(*Edge).twin.fromMap = nil //no longer from a unique map
				L.Delete(i + 1)
			} else {
				i++
			}
		}
		//fmt.Fprintf(os.Stderr,"L: %v\n",L)
		R := new(vector.Vector)
		for i := 0; i < T.segments.Len(); {
			e := T.segments.At(i).(*Edge)
			if e.end.Point.Equal(evt.point) {
				R.Push(e)
				T.segments.Delete(i)
			} else if e.Line().On(evt.point) {
				return nil, os.NewError("intersection not at an endpoint")
			} else {
				i++
			}
		}

		// Fill in handle event. You won't need the whole damn thing because
		// Most of the time you just abort with non-terminal intersection
		T.sweepLocation = evt.point
		sort.Sort(T)
		//fmt.Fprintf(os.Stderr,"status: %v\n",T.segments)
		if L.Len() == 0 && R.Len() == 0 {
			return nil, os.NewError("event point with no edges terminal at it " + evt.point.String() + fmt.Sprintf("current status: %v", T.segments))
		} else if L.Len() == 0 {
			above := sort.Search(T.Len(), func(i int) bool {
				return T.segments.At(i).(*Edge).Line().Below(evt.point)
			})
			//fmt.Fprintf(os.Stderr,"Testing status point, no new edge. above: %v, Len: %v\n",above,T.Len())
			if 0 < above && above < T.Len() {
				sa := T.segments.At(above).(*Edge)
				sb := T.segments.At(above - 1).(*Edge)

				cross, _, _ := sb.Line().IntersectAsFloats(sa.Line())
				if cross && !sa.Coterminal(sb) {
					return nil, os.NewError("intersection not at an endpoint")
				}
			}
		} else {
			aboveL := sort.Search(T.Len(), func(i int) bool {
				return L.Last().(*Edge).Line().LessAt(T.segments.At(i).(*Edge).Line(), evt.point)
			})
			belowL := aboveL - 1
			//fmt.Fprintf(os.Stderr,"Testing status point, new edges. above: %v, Len: %v\n",aboveL,T.Len())
			if 0 <= belowL && belowL < T.Len() {
				sa := L.At(0).(*Edge)
				sb := T.segments.At(belowL).(*Edge)

				cross, _, _ := sa.Line().IntersectAsFloats(sb.Line())
				if cross && !sa.Coterminal(sb) {
					return nil, os.NewError("intersection not at an endpoint")
				}
			}
			if aboveL < T.Len() {
				sa := T.segments.At(aboveL).(*Edge)
				sb := L.Last().(*Edge)

				cross, _, _ := sa.Line().IntersectAsFloats(sb.Line())
				if cross && !sa.Coterminal(sb) {
					return nil, os.NewError("intersection not at an endpoint")
				}
			}
		}

		// This is the barrier between preparing the new vertex (below) and determining if the new vertex is good 

		// Setting up edges
		nv := NewVertex(evt.point.Copy())
		R.Do(func(r interface{}) {
			nv.OutgoingEdges.Push(r.(*Edge).twin)
			r.(*Edge).end = nv
			r.(*Edge).twin.start = nv
			o.Edges.Push(r)
			o.Edges.Push(r.(*Edge).twin)
		})
		L.Do(func(l interface{}) {
			l.(*Edge).start = nv
			l.(*Edge).twin.end = nv
			nv.OutgoingEdges.Push(l)
		})
		sort.Sort(nv.OutgoingEdges)

		for i := 0; i < nv.OutgoingEdges.Len(); i++ {
			e := nv.OutgoingEdges.At(i).(*Edge)
			f := nv.OutgoingEdges.At((i + 1) % nv.OutgoingEdges.Len()).(*Edge)
			e.prev = f.twin
			f.twin.next = e
		}

		// Setting up nv's inFace.
		// A new vertex only has relevant inFace information when it comes exclusively from one map, we first determine that
		fromOneMap := true
		fromMap := nv.OutgoingEdges.At(0).(*Edge).fromMap
		nv.OutgoingEdges.Do(func(e interface{}) {
			fromOneMap = fromOneMap && e.(*Edge).fromMap == fromMap && e.(*Edge).fromMap != nil
		})
		// we're from one map, got to find the elements in T from the other map above and below us
		if fromOneMap {
			//fmt.Fprintf(os.Stderr,"from one map, finding face of other\n")
			TfromOtherMap := new(sweepStatus)
			TfromOtherMap.sweepLocation = evt.point
			TfromOtherMap.segments = new(vector.Vector)
			T.segments.Do(func(e interface{}) {
				if e.(*Edge).fromMap != fromMap {
					TfromOtherMap.segments.Push(e)
				}
			})
			sort.Sort(TfromOtherMap)
			above := sort.Search(TfromOtherMap.Len(), func(i int) bool {
				return TfromOtherMap.segments.At(i).(*Edge).Line().Below(evt.point)
			})
			if 0 < above && above < TfromOtherMap.Len() {
				//fmt.Fprintf(os.Stderr,"Testing status point, looking for vertex in face. above: %v, Len: %v\n",above,TfromOtherMap.Len())
				sb := TfromOtherMap.segments.At(above - 1).(*Edge)
				if sb.fromMap == nil {
					if sb.face.fromMap == fromMap {
						nv.inFace = sb.newFace
					} else { 
						nv.inFace = sb.face
					}
				} else {
					nv.inFace = sb.face
				}
			} else if TfromOtherMap.Len() == 0 {
				nv.inFace = nil // completely outside other map, not our problem
			} else if above == 0 {
				sa := TfromOtherMap.segments.At(above).(*Edge)
				if sa.twin.fromMap == nil {
					if sa.twin.face.fromMap == fromMap {
						nv.inFace = sa.twin.newFace
					} else { 
						nv.inFace = sa.twin.face
					}
				} else {
					nv.inFace = sa.twin.face
				}
			} else if above == TfromOtherMap.Len() {
				sb := TfromOtherMap.segments.At(above - 1).(*Edge)
				nv.inFace = sb.face
				if sb.fromMap == nil {
					if sb.face.fromMap == fromMap {
						nv.inFace = sb.newFace
					} else { 
						nv.inFace = sb.face
					}
				} else {
					nv.inFace = sb.face
				}
			} 
		} else {
			nv.inFace = nil //we're not from one map, this vertex happens at the overlay, edges have complete face information.
		}

		o.Verticies.Push(nv)

		// Vertex done, add any new edges to the sweep line.

		L.Do(func(l interface{}) {
			T.segments.Push(l)
		})
	}

	var leFuck string
	for i := 0; i < o.Edges.Len(); i++ {
		e, _ := o.Edges.At(i).(*Edge)
		if e.visited {
			continue
		}
		//fmt.Fprintf(os.Stderr,"found a face containing: %v,",e.start)

		F := new(Face)
		F.boundary = e
		F.fromMap = o
		e.visited = true
		oldFaces := make(map[*Face]int)
		//fmt.Fprintf(os.Stderr,"%v: ",e.start)
		if e.face != nil {
			//fmt.Fprintf(os.Stderr,"f %v ",e.face.Value)
			oldFaces[e.face]++
		}
		if e.newFace != nil {
			//fmt.Fprintf(os.Stderr,"nf %v ",e.newFace.Value)
			oldFaces[e.newFace]++
		}
		if e.start.inFace != nil {
			//fmt.Fprintf(os.Stderr,"if %v ",e.start.inFace.Value)
			oldFaces[e.start.inFace]++
		}
		if e.face == nil && e.newFace == nil {
			panic("the edge without a face\n")
		}
		e.face = F

		for f := e.Next(); f != e; f = f.Next() {
			//fmt.Fprintf(os.Stderr,"%v: ",f.start)
			f.visited = true
			if f.face != nil {
				//fmt.Fprintf(os.Stderr,"f %v ",f.face.Value)
				oldFaces[f.face]++
			}
			if f.newFace != nil {
				//fmt.Fprintf(os.Stderr,"nf %v ",f.newFace.Value)
				oldFaces[f.newFace]++
			}
			if f.start.inFace != nil {
				//fmt.Fprintf(os.Stderr,"if %v ",f.start.inFace.Value)
				oldFaces[f.start.inFace]++
			}
			//fmt.Fprintf(os.Stderr,",")
			f.face = F
		}
		//os.Stderr.WriteString("\n")

		//fmt.Fprintf(os.Stderr,"%v old faces\n",len(oldFaces))
		if len(oldFaces) > 2 {
			leFuck += fmt.Sprintf("%v faces overlapping a new face, input must have been malformed, maps m: %p n: %p\n", len(oldFaces), m, n)
			for f, _ := range oldFaces {
				leFuck = leFuck + fmt.Sprintf("face %p from: %p containing: %v\n", f, f.fromMap, f.Value)
			}
			//os.Stderr.WriteString(leFuck)
		} else if len(oldFaces) == 0 {
			panic(fmt.Sprintf("No old faces. e: %v, e.face: %+v, maps: m: %p n: %p o: %p\n", e, e.face, m, n, o))
		}

		var mFace, nFace *Face
		for f, _ := range oldFaces {
			if f.fromMap == m {
				mFace = f
			} else {
				nFace = f
			}
		}
		if mFace != nil && nFace != nil {
			v, ok := mergeFaces(mFace.Value, nFace.Value)
			if ok != nil {
				return nil, ok
			}
			if mFace.Type != "" {
				F.Type = mFace.Type
			} else if nFace.Type != "" {
				F.Type = nFace.Type
			}
			F.Value = v
		} else if mFace != nil {
			F.Value = mFace.Value
			F.Type = mFace.Type
		} else if nFace != nil {
			F.Value = nFace.Value
			F.Type = nFace.Type
		} else {
			panic(fmt.Sprintf("face didn't come from an mFace or an nFace, pointers m: %v n: %v o: %v face: %v", m, n, o, e.face))
		}

		o.Faces.Push(F)
	}

	if leFuck != "" {
		return o, os.NewError(leFuck)
	}

	o.Init()
	return o, nil
}