public bool Go(Direction headdirection, ref Food food) // 'true' if alive { if (Math.Abs(headdirection - HeadDirection) == 2) { return(true); } HeadDirection = headdirection; WormElement elem = Elements[0]; int x = elem.X, y = elem.Y; if ((int)headdirection % 2 == 0) { y += (int)headdirection - 1; if (y < 0) { y = Program.Conf.FieldHeight - 1; } else if (y >= Program.Conf.FieldHeight) { y = 0; } } else { x -= (int)headdirection - 2; if (x < 0) { x = Program.Conf.FieldWidth - 1; } else if (x >= Program.Conf.FieldWidth) { x = 0; } } foreach (WormElement e in Elements) { if ((e.X == x) && (e.Y == y)) { return(false); } } bool bfood = (x == food.Element.X) && (y == food.Element.Y); Elements.Insert(0, new WormElement((byte)x, (byte)y, bfood)); DeletedElement = Elements[Elements.Count - 1]; Elements.Remove(DeletedElement); if (DeletedElement.Food) { Elements.Add(new WormElement(DeletedElement.X, DeletedElement.Y, false)); DeletedElement = null; } return(true); }
public Worm(byte length, byte headX, byte headY) { if (length >= Program.Conf.FieldWidth || headX-length < 0 || headY >= Program.Conf.FieldHeight) throw new ApplicationException("Error: worm does not fit in the field."); Elements = new List<WormElement>(); for(byte i = 0; i<length; i++) { WormElement elem = new WormElement((byte)(headX - i), headY, false); Elements.Add(elem); } HeadDirection = Direction.Right; DeletedElement = null; }
public Worm(byte length, byte headX, byte headY) { if (length >= Program.Conf.FieldWidth || headX - length < 0 || headY >= Program.Conf.FieldHeight) { throw new ApplicationException("Error: worm does not fit in the field."); } Elements = new List <WormElement>(); for (byte i = 0; i < length; i++) { WormElement elem = new WormElement((byte)(headX - i), headY, false); Elements.Add(elem); } HeadDirection = Direction.Right; DeletedElement = null; }
static void PrintAction(WormElement newhead, WormElement oldend) { Console.SetCursorPosition(newhead.X, newhead.Y); Console.WriteLine(Conf.WormSymbol); if (oldend==null) return; Console.SetCursorPosition(oldend.X, oldend.Y); Console.WriteLine(" "); }