public static void move_to_direction() { MyPoint new_point; MyPoint old_point; Snake_Element Main_element = SnakeElements.Where(x => x.Is_Main).ToList()[0]; new_point = MyFunctions.Clone_MyPoint(Main_element.Point); old_point = MyFunctions.Clone_MyPoint(Main_element.Point); if (Is_Bot_Mode) { if (Right_Path.Count > 0) { new_point.x = Right_Path[0].x; new_point.y = Right_Path[0].y; Right_Path.Remove(Right_Path[0]); } else { throw new Exception("error expected right path element but not found!"); } } else { switch (LocalData.Curr_Direction) { case DirectionType.Down: if (new_point.y < y_Length - 1) { new_point.y += 1; } else { if (Are_Borders_Open) { new_point.y = 0; } else { Game_Over(); return; } } break; case DirectionType.Up: if (new_point.y >= 1) { new_point.y -= 1; } else { if (Are_Borders_Open) { new_point.y = y_Length - 1; } else { Game_Over(); return; } } break; case DirectionType.Right: if (new_point.x < x_Length - 1) { new_point.x += 1; } else { if (Are_Borders_Open) { new_point.x = 0; } else { Game_Over(); return; } } break; case DirectionType.Left: if (new_point.x >= 1) { new_point.x -= 1; } else { if (Are_Borders_Open) { new_point.x = x_Length - 1; } else { Game_Over(); return; } } break; default: break; } } switch (Board.Matrix[new_point.x, new_point.y].MyValue) { case MyValueType.free: Board.Matrix[SnakeElements[0].Point.x, SnakeElements[0].Point.y].MyValue = MyValueType.free; Main_element.Is_Main = false; SnakeElements.RemoveAt(0); SnakeElements.Add(new Snake_Element { Point = new_point, Is_Main = true }); Board.Matrix[new_point.x, new_point.y].MyValue = MyValueType.snake; break; case MyValueType.snake: Game_Over(); break; case MyValueType.target: if (Is_Enabled_Audio) { SoundEffect.take_target.play(); } Board.Matrix[SnakeElements[0].Point.x, SnakeElements[0].Point.y].MyValue = MyValueType.free; Main_element.Is_Main = false; Board.Matrix[old_point.x, old_point.y].MyValue = MyValueType.snake; Board.Matrix[new_point.x, new_point.y].MyValue = MyValueType.snake; SnakeElements.Add(new Snake_Element { Point = new_point, Is_Main = true }); Board.Matrix[new_point.x, new_point.y].MyValue = MyValueType.snake; paint_Random_Target(); paint_Score(); if (Is_Bot_Mode) { Find_Right_Path(); } break; case MyValueType.wall: Game_Over(); break; default: break; } }
public static void Find_Right_Path() { MyPathNode[,] grid = new MyPathNode[x_Length, y_Length]; bool isWall = false; for (var index_x = 0; index_x < x_Length; index_x++) { for (var index_y = 0; index_y < y_Length; index_y++) { isWall = false; switch (Board.Matrix[index_x, index_y].MyValue) { case MyValueType.snake: isWall = true; break; case MyValueType.wall: isWall = true; break; default: break; } grid[index_x, index_y] = new MyPathNode() { IsWall = isWall, X = index_x, Y = index_y, }; } } Snake_Element Main_element = SnakeElements.Where(x => x.Is_Main).ToList()[0]; MySolver <MyPathNode, Object> aStar = new MySolver <MyPathNode, object>(grid); LinkedList <MyPathNode> path = aStar.Search(new Point(Main_element.Point.x, Main_element.Point.y), new Point(target_position.x, target_position.y), null); if (path == null) { Game_Over(); } else { if (path.Count == 0) { Game_Over(); } } foreach (var item in path) { Right_Path.Add(new MyPoint(item.X, item.Y)); } Right_Path.RemoveAt(0); }