本文整理汇总了C#中System.Collections.Queue.Last方法的典型用法代码示例。如果您正苦于以下问题:C# Queue.Last方法的具体用法?C# Queue.Last怎么用?C# Queue.Last使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Queue
的用法示例。
在下文中一共展示了Queue.Last方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
#region InitializeConsoleWindowSize
Console.SetWindowSize(Window_Width, Window_Height);
Console.BufferHeight = Console.WindowHeight;
Console.BufferWidth = Console.WindowWidth;
#endregion
#region InitializeGameElements
int initialRowOfSnake = Window_Height / 2;
int initialColoumOfSnake = Window_Width / 2;
Direction currentDirection = initialDirection;
int curentSnakeSpeed = initialSnakeSpeed;
Queue<Position> snake = new Queue<Position>();
for (int i = 0; i < initialSnakeLength; i++)
{
snake.Enqueue(new Position(initialColoumOfSnake + i, initialRowOfSnake));
}
Position snakeFood = GenerateFood();
DrawSingleElement(snakeFood, foodSymbol);
DrawBorderElement();
Position snakeHead;
Position snakeTail;
Position headingOfSnake;
Position newSnakeHead;
#endregion
#region Directions
Position[] directions = new Position[]
{
new Position(-1, -1), //NorthWest
new Position(0, -1), // North
new Position(1, -1), //NorthtEast
new Position(-1, 0), //West
new Position(1, 0), //East
new Position(-1, 1), //SouthWest
new Position(0, 1), //South
new Position(1, 1) //SouthEast
};
#endregion
while (true)
{
if (Console.KeyAvailable)
{
currentDirection = ReadKeyFromKeyboard(currentDirection);
DrawSingleElement(new Position(1 + curentLevel.Length, 1), spaceSymbol);
}
snakeHead = snake.Last();
headingOfSnake = directions[(int)currentDirection];
newSnakeHead = new Position(
snakeHead.Column + headingOfSnake.Column,
snakeHead.Row + headingOfSnake.Row
);
#region GameOverLogic
if (newSnakeHead.Column < 1 ||
newSnakeHead.Row < 1 ||
newSnakeHead.Column >= Console.WindowWidth - 1 ||
newSnakeHead.Row >= Console.WindowHeight - 1 ||
snake.Contains(newSnakeHead))
{
Console.Clear();
DrawSingleElement(new Position(initialColoumOfSnake - 3, initialRowOfSnake - 3),
"Game Over!");
DrawSingleElement(new Position(initialColoumOfSnake - 5, initialRowOfSnake - 1),
String.Format("Level reached: {0}", gameLevel));
DrawSingleElement(new Position(initialColoumOfSnake - 7, initialRowOfSnake),
"(Press Esc to exit)");
ConsoleKeyInfo exitKey = Console.ReadKey();
while (exitKey.Key != ConsoleKey.Escape)
{
exitKey = Console.ReadKey();
}
return;
}
#endregion
#region SnakeGrowingLogic
if (newSnakeHead == snakeFood)
//.........这里部分代码省略.........
示例2: Main
static void Main(string[] args)
{
byte right = 0;
byte left = 1;
byte down = 2;
byte up = 3;
//Environment.TickCount tova 6te broi vremeto ot kakto e pusnata programata
//DateTime.Now
int lastFoodTime = 0;
int foodDisappearTime = 8000; // 8 sec delay
lastFoodTime = Environment.TickCount;
Position[] directions = new Position[]{
new Position(0,1), //right
new Position(0,-1),//left
new Position(1,0), // down
new Position(-1,0), // up
};
int sleepTime = 100;
int direction = right; // 0 teku6tat posoka // saotvetno kat elementi na masiva positoin
Random randomNumberGenerator = new Random();
Console.BufferHeight = Console.WindowHeight; // consolata da nqma scrollbar i buffera da bude golqm kolkoto consolata
Position food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), // slaga qbalkata na random mesto
randomNumberGenerator.Next(0, Console.WindowWidth)); // tuka 6te ni se generira slu4aino 4islo koeto 6te opredelq kade e qbalkata
Queue<Position> snakeElements = new Queue<Position>(); // tova 6te e opa6kata TUKA SE PRAVQT ZMIQTA
Position position1 = new Position();
for (int i = 0; i <= 5; i++)
{
snakeElements.Enqueue(new Position(0, i));
}
foreach (Position position in snakeElements) // obxojda vsi4kite elementi i postavqme kursora tam kadeto se namira zmiqta
{
Console.SetCursorPosition(position.col, position.row); // tazi poziciq priema left top kolona
Console.WriteLine('*');
}
while (true)
{
if (Console.KeyAvailable) // proverqva dali potrebitelq e natisnal nqkakvo kop4e
{
ConsoleKeyInfo userInput = Console.ReadKey(); // pro4ita ne6to tam
if (userInput.Key == ConsoleKey.LeftArrow)
{
if (direction != right) // ako posokata ne e nadqsno 4ak togava da se mesti nalqvo
{
direction = left;
}
}
if (userInput.Key == ConsoleKey.RightArrow)
{
if (direction != left)
{
direction = right;
}
}
if (userInput.Key == ConsoleKey.UpArrow)
{
if (direction != down)
{
direction = up;
}
}
if (userInput.Key == ConsoleKey.DownArrow)
{
if (direction != up) // ako posokata ne e nagore 4ak togava da se mestim nadolo
{
direction = down;
}
}
}
Position snakeHead = snakeElements.Last(); // tozi method vru6ta posledniq element ot opa6kata
Position nextDirection = directions[direction]; // s tozi red vzimame na kade da se dviji zmiqta kato directions e masiva a direction 0,1,2,3
//novata poziciq na zmiqta
Position snakeNewHead = new Position(snakeHead.row + nextDirection.row, snakeHead.col + nextDirection.col);// purvo e X posle e Y kato za primer vzimame stariq direction na row i go
//Position newHead = snakeElements.Last();// tova ni vru6ta glavata na na6ta zmiq i q zapisva v promenlivata new head
snakeElements.Enqueue(snakeNewHead); // tozi kod slaga nova glava vseki put na novata poziciq
// tova proverqva dali glavata na zmiqta ne e izlezla ot ekrana bukvalno
if (snakeNewHead.row < 0 ||
snakeNewHead.col < 0 ||
snakeNewHead.row >= Console.WindowHeight ||
snakeNewHead.col >= Console.WindowWidth) // row = red
{
Console.SetCursorPosition(0, 0);
Console.WriteLine();
Console.WriteLine("Game over");
Console.WriteLine("Your points are: {0}", (snakeElements.Count - 6) * 100); // tova vru6ta broq elementi na zmiqta
//.........这里部分代码省略.........
示例3: Main
static void Main()
{
byte right = 0;
byte left = 1;
byte down = 2;
byte up = 3;
Position[] directions = new Position[]
{
new Position(0, 1), //right
new Position(0, -1), //left
new Position(1, 0), //down
new Position(-1, 0) //up
};
int sleepTime = 100;
int direction = right;
Random randomNumbersGenerator = new Random();
Console.BufferHeight = Console.WindowHeight;
Position food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
randomNumbersGenerator.Next(0, Console.WindowWidth));
Queue<Position> snakeElements = new Queue<Position>();
/*
Position position1 = new Position(0,0);
position1.X = 0;
position1.Y = 0; */
for (int i = 0; i < 7; i++)
{
snakeElements.Enqueue(new Position(0, i));
}
foreach (Position position in snakeElements)
{
Console.SetCursorPosition(position.col, position.row);
Console.Write("*");
}
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo userInput = Console.ReadKey();
if (userInput.Key == ConsoleKey.LeftArrow)
{
direction = left;
}
if (userInput.Key == ConsoleKey.RightArrow)
{
direction = right;
}
if (userInput.Key == ConsoleKey.UpArrow)
{
direction = up;
}
if (userInput.Key == ConsoleKey.DownArrow)
{
direction = down;
}
}
Position snakeHead = snakeElements.Last();
Position nextDirection = directions[direction];
Position snakeNewHead = new Position(snakeHead.row + nextDirection.row,
snakeHead.col + nextDirection.col);
if (snakeNewHead.row < 0 ||
snakeNewHead.col < 0 ||
snakeNewHead.row >= Console.WindowHeight ||
snakeNewHead.col >= Console.WindowWidth)
{
Console.SetCursorPosition(0, 0);
Console.WriteLine("Game over!");
}
snakeElements.Enqueue(snakeNewHead);
if ((snakeNewHead.col == food.col) && (snakeNewHead.row == food.row))
{
//feeding
food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
randomNumbersGenerator.Next(0, Console.WindowWidth));
}
else
{
//moving...
snakeElements.Dequeue();
}
Console.Clear();
foreach (Position position in snakeElements)
{
Console.SetCursorPosition(position.col, position.row);
Console.Write("*");
}
Console.SetCursorPosition(food.row, food.col);
Console.Write("@");
Thread.Sleep(sleepTime);
//.........这里部分代码省略.........
示例4: Main
static void Main()
{
byte right = 0, left = 1, down = 2, up = 3;
int lastFoodTime = 0;
int FoodDissapearTime = 8000;
double NegativePoints = 0;
Position[] directions = new Position[]
{
new Position(0, 1), // right
new Position(0, -1), // left
new Position(1, 0), // down
new Position(-1, 0), // up
};
double sleeptime = 100;
int direction = 0;
Random randomNumbersGenerator = new Random();
Console.BufferHeight = Console.WindowHeight;
Position food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
randomNumbersGenerator.Next(0, Console.WindowWidth));
List<Position> Obstacles = new List<Position>()
{
new Position(12,12),
new Position(13,12),
new Position(14,12),
new Position(15,12),
new Position(16,12),
};
foreach (Position obstacle in Obstacles)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.SetCursorPosition(obstacle.col, obstacle.row);
Console.Write("|");
}
Queue<Position> snakeElements = new Queue<Position>();
for (int i = 0; i <= 5; i++)
{
snakeElements.Enqueue(new Position(0, i));
}
foreach (Position position in snakeElements)
{
Console.SetCursorPosition(position.col, position.row);
Console.ForegroundColor = ConsoleColor.White;
Console.Write("*");
}
do
{
food = new Position(randomNumbersGenerator.Next(0, Console.WindowHeight),
randomNumbersGenerator.Next(0, Console.WindowWidth));
lastFoodTime = Environment.TickCount;
Console.SetCursorPosition(food.col, food.row);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("@");
}
while (snakeElements.Contains(food) || Obstacles.Contains(food));
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo userInput = Console.ReadKey();
while (Console.KeyAvailable)
{
userInput = Console.ReadKey(true);
}
if (userInput.Key == ConsoleKey.LeftArrow && direction != right)
{
direction = left;
}
if (userInput.Key == ConsoleKey.RightArrow && direction != left)
{
direction = right;
}
if (userInput.Key == ConsoleKey.UpArrow && direction != down)
{
direction = up;
}
if (userInput.Key == ConsoleKey.DownArrow && direction != up)
{
direction = down;
}
}
Position snakeHead = snakeElements.Last();
Position nextDirection = directions[direction];
Position snakeNewHead = new Position(snakeHead.row + nextDirection.row,
snakeHead.col + nextDirection.col);
if (snakeNewHead.col < 0) snakeNewHead.col = Console.WindowWidth - 1;
if (snakeNewHead.row < 0) snakeNewHead.row = Console.WindowHeight - 1;
if (snakeNewHead.col >= Console.WindowWidth) snakeNewHead.col = 0;
if (snakeNewHead.row >= Console.WindowHeight) snakeNewHead.row = 0;
if (snakeElements.Contains(snakeNewHead) || Obstacles.Contains(snakeNewHead))
{
//.........这里部分代码省略.........
示例5: Main
static void Main(string[] args)
{
Position[] directions = new Position[]
{
new Position (0, 1),//right
new Position (0, -1), // left
new Position (1, 0), // down
new Position (-1, 0),// top
};
int direction = 0; //0
Queue<Position> snakeElement = new Queue<Position>();
for (int i = 0; i <= 5; i++)
{
snakeElement.Enqueue(new Position(0, i));
}
foreach (Position position in snakeElement)
{
Console.SetCursorPosition(position.Col, position.Row);
Console.Write("*");
}
while (true)
{
ConsoleKeyInfo userinput = Console.ReadKey();
if (userinput.Key == ConsoleKey.LeftArrow)
{
direction = 1;
}
if (userinput.Key == ConsoleKey.RightArrow)
{
direction = 0;
}
if (userinput.Key == ConsoleKey.UpArrow)
{
direction = 3;
}
if (userinput.Key == ConsoleKey.DownArrow)
{
direction = 2;
}
snakeElement.Dequeue();
Position snakeHead = snakeElement.Last();
snakeElement.Dequeue();
Position nextdirection = directions[direction];
Position smakenewhead = new Position(snakeHead.Row + nextdirection.Row, snakeHead.Col + nextdirection.Col);
snakeElement.Enqueue(smakenewhead);
foreach (Position position in snakeElement)
{
Console.SetCursorPosition(position.Col, position.Row);
Console.Write("*");
}
}
}
示例6: Main
static void Main(string[] args)
{
byte right = 0;
byte left = 1;
byte down = 2;
byte up = 3;
Position[] directions = new Position[]
{
new Position(0, 1), //right
new Position(0, -1), //left
new Position(1, 0), // down
new Position(-1, 0), //up
};
int sleepTime = 100;
int direction = right; //0
Random rand = new Random();
Console.BufferHeight = Console.WindowHeight;
Console.BufferWidth = Console.WindowWidth;
Position food = new Position(rand.Next(0,Console.WindowHeight),
rand.Next(0, Console.WindowWidth));
Console.SetCursorPosition(food.col, food.row);
Console.Write("$");
Queue<Position> snakeElements = new Queue<Position>();
for (int i=0; i<=5; i++)
{
snakeElements.Enqueue(new Position(0, i));
}
foreach (Position position in snakeElements)
{
Console.SetCursorPosition(position.col, position.row);
Console.WriteLine("*");
}
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo userInput = Console.ReadKey();
if (userInput.Key == ConsoleKey.LeftArrow)
{
if (direction != right)
direction = left;
}
if (userInput.Key == ConsoleKey.RightArrow)
{
if (direction !=left)
direction = right;
}
if (userInput.Key == ConsoleKey.UpArrow)
{
if (direction!=down)
direction = up;
}
if (userInput.Key == ConsoleKey.DownArrow)
{
if (direction!=up)
direction = down;
}
}
Position snakeHead = snakeElements.Last();
Position nextDirection = directions[direction];
Position snakeNewHead = new Position(snakeHead.row + nextDirection.row,
snakeHead.col + nextDirection.col);
if (snakeNewHead.row < 0 || snakeNewHead.col < 0 ||
snakeNewHead.row >= Console.WindowHeight || snakeNewHead.col >= Console.WindowWidth ||
snakeElements.Contains(snakeNewHead))
{
Console.SetCursorPosition(0, 0);
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Game Over!");
Console.WriteLine("Your points are {0}", (snakeElements.Count-6) * 100);
Console.ReadKey();
return;
}
snakeElements.Enqueue(snakeNewHead);
if (snakeNewHead.col == food.col && snakeNewHead.row == food.row)
{
food = new Position(rand.Next(0, Console.WindowHeight),
rand.Next(0, Console.WindowWidth));
sleepTime--;
}
else
{
snakeElements.Dequeue();
}
Console.Clear();
foreach (Position position in snakeElements)
{
Console.SetCursorPosition(position.col, position.row);
Console.WriteLine("*");
}
//.........这里部分代码省略.........
示例7: Main
static void Main(string[] args)
{
String[] adresses = System.IO.File.ReadAllLines("ip.txt"); // read ip's from file
String[] ip = new String[adresses.Length];
String[] ports = new String[adresses.Length];
Threads = new Queue<Thread>();
// Проходим по адресам
for (int i = 0; i < adresses.Length - 1; i++)
{
String[] result = adresses[i].Split(new Char[] { ':' });
ip[i] = result[0].ToString();
ports[i] = result[1].ToString();
}
int q = 0;
while (!res)
{
if (q < adresses.Length - 1)
{
string currip = ip[q], currrport = ports[q];
// Create but do not start it.
Thread current = new Thread(new ThreadStart(delegate()
{
try
{
var g = new GoogleScholarDataGetter("https://scholar.google.com.ua/citations?user=b18TCTYAAAAJ&hl=uk", ref currip, ref currrport);
// Вызываем парсер
g.executeParser();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return;
}));
current.Name = "Worker " + q.ToString();
current.IsBackground = true;
current.Priority = ThreadPriority.Highest;
Threads.Enqueue(current);
Threads.Last().Start();
q++;
}
}
}
示例8: Main
static void Main(string[] args)
{
byte right = 0; //massive index
byte left = 1;
byte down = 2;
byte up = 3;
Position[] directions = new Position[]
{
new Position(0, 1), // right
new Position(0, -1), // left
new Position(1, 0), // down
new Position(-1, 0) // up
};
int sleepTime = 100;
int direction = right;
Random randomNumberGenerator = new Random();
Console.BufferHeight = Console.WindowHeight;
Position food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight), //food
randomNumberGenerator.Next(0, Console.WindowWidth));
Queue<Position> snakeElements = new Queue<Position>();
for (int i = 0; i <= 5; i++)
{
snakeElements.Enqueue(new Position(0, i));
}
foreach (Position position in snakeElements)
{
Console.SetCursorPosition(position.cow, position.row);
Console.Write("*");
}
while (true)
{
Position snakeHead = snakeElements.Last();
Position nextDirection = directions[direction];
Position snakeNewHead = new Position(snakeHead.row + nextDirection.row,
snakeHead.cow + nextDirection.cow);
if (snakeNewHead.row < 0 || snakeNewHead.cow < 0 || snakeNewHead.row >= Console.WindowHeight
|| snakeNewHead.cow >= Console.WindowWidth || snakeElements.Contains(snakeNewHead))
{
Console.SetCursorPosition(0, 0);
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.Beep();
Console.Beep();
Console.Beep();
Console.WriteLine("Game Over !!!");
Console.WriteLine("Your points are: {0}", snakeElements.Count - 6);
return;
}
if (snakeNewHead.cow == food.cow && snakeNewHead.row == food.row)
{
// feeding the snake
food = new Position(randomNumberGenerator.Next(0, Console.WindowHeight),
randomNumberGenerator.Next(0, Console.WindowWidth));
Console.Beep();
sleepTime--; // speed up
}
else
{
// moving...
snakeElements.Dequeue();
}
if (Console.KeyAvailable)
{
ConsoleKeyInfo userInput = Console.ReadKey();
if (userInput.Key == ConsoleKey.LeftArrow)
{
if (direction != right) direction = left;
}
if (userInput.Key == ConsoleKey.RightArrow)
{
if (direction != left) direction = right;
}
if (userInput.Key == ConsoleKey.UpArrow)
{
if (direction != down) direction = up;
}
if (userInput.Key == ConsoleKey.DownArrow)
{
if(direction != up) direction = down;
}
}
snakeElements.Enqueue(snakeNewHead);
Console.Clear();
foreach (Position position in snakeElements)
{
Console.SetCursorPosition(position.cow, position.row);
Console.Write("*");
}
Console.SetCursorPosition(food.cow, food.row);
Console.Write("@");
//.........这里部分代码省略.........