预期:char'H'在由默认“X”组成的5x5数组中移动。
结果1:网格在添加“H”和“P”时展开,而不是替换匹配的索引“X”。
结果2:char'H'不动。
到目前为止,这是代码:
class Mainclass
{
public static void Main(string[] args)
{
Console.WriteLine("The Board Game");
Console.WriteLine();
Console.WriteLine(">>Press any key to begin<<");
Console.ReadKey();
Console.Clear();
Console.WriteLine("The Board Game");
Console.WriteLine();
CreateGrid();
Console.WriteLine();
Console.WriteLine("H = your hero");
Console.WriteLine();
Console.WriteLine("X = floor tiles");
Console.WriteLine();
Console.WriteLine("P = enemy pawn");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press the arrows in your keyboard to move in that direction");
}
public static void CreateGrid()
{
int width = 5;
int height = 5;
char[,] grid = new int[width, height];
grid[2, 2] = 'H';
grid[4, 4] = 'P';
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Console.Write(grid[x, y] + " ");
}
Console.WriteLine();
}
if (ConsoleKeydown(ConsoleKey.up)){
get hero.index[,];
newHeroPosition = Hero.index +1, +0;
char hero.index = H;
char newHeroPosition = x;
}
/*I want the tile to be identified and swap the char value
of it with the one above it on the grid */
}
}
}
答案 0 :(得分:0)
这样的事情可以帮助你
public static void move(ref int[][] grid, ref int heroX, ref int heroY, int upDown, int leftRight) {
if ((heroX + upDown >= 0) && (heroX + upDown < grid.Length) &&
(heroY + leftRight >= 0) && (heroY + leftRight < grid[heroX + upDown].Length)) {
int aux = grid[heroX][heroY];
grid[heroX][heroY] = grid[heroX += upDown][heroY += leftRight];
grid[heroX][heroY] = aux; //heroX and heroY were changed in the previous operation
}
}
只需传递网格,英雄的当前x,y位置,如果你想让他向下移动,则为+1,如果你想让他向上移动,则为向上移动,对于leftRight,也是如此。
答案 1 :(得分:-2)
好。代码很有趣。可能做得太多了。遗憾。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The Board Game");
Console.WriteLine();
Console.WriteLine(">>Press any key to begin<<");
Console.ReadKey();
Console.Clear();
Console.WriteLine("The Board Game");
Console.WriteLine();
CreateGrid();
}
static void CreateGrid()
{
int heroX = 2;
int heroY = 2;
int width = 5;
int height = 5;
char[,] grid = new char[width, height];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
grid[j, i] = 'x';
}
}
grid[heroX, heroY] = 'H';
grid[4, 4] = 'P';
Console.WriteLine();
Console.WriteLine("H = your hero");
Console.WriteLine();
Console.WriteLine("X = floor tiles");
Console.WriteLine();
Console.WriteLine("P = enemy pawn");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Press the arrows in your keyboard to move in that direction");
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Console.Write(grid[x, y] + " ");
}
Console.WriteLine();
}
while (true)
{
var key = Console.ReadKey();
char temp;
switch (key.Key)
{
case ConsoleKey.UpArrow:
if (heroX == 0)
break;
temp = grid[heroX - 1, heroY];
grid[heroX - 1, heroY] = 'H';
grid[heroX, heroY] = temp;
heroX--;
Console.Clear();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Console.Write(grid[x, y] + " ");
}
Console.WriteLine();
}
break;
case ConsoleKey.DownArrow:
if (heroX == height - 1)
break;
temp = grid[heroX + 1, heroY];
grid[heroX + 1, heroY] = 'H';
grid[heroX, heroY] = temp;
heroX++;
Console.Clear();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Console.Write(grid[x, y] + " ");
}
Console.WriteLine();
}
break;
case ConsoleKey.LeftArrow:
if (heroY == 0)
break;
temp = grid[heroX, heroY - 1];
grid[heroX, heroY - 1] = 'H';
grid[heroX, heroY] = temp;
heroY--;
Console.Clear();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Console.Write(grid[x, y] + " ");
}
Console.WriteLine();
}
break;
case ConsoleKey.RightArrow:
if (heroY == width - 1)
break;
temp = grid[heroX, heroY + 1];
grid[heroX, heroY + 1] = 'H';
grid[heroX, heroY] = temp;
heroY++;
Console.Clear();
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
Console.Write(grid[x, y] + " ");
}
Console.WriteLine();
}
break;
default:
break;
}
}
}
}
}