【问题标题】:C# - 2d matrix - is guess left/right/up/down of pointC# - 2d 矩阵 - 猜测点的左/右/上/下
【发布时间】:2019-03-24 20:06:59
【问题描述】:

使用实现并放置在表单上的二维按钮数组。

Button[,] tmpButton = new Button[x, y];
private void DrawGrid()
{
     int ButtonWidth = 48;
     int ButtonHeight = 48;
     int start_x = 88;
     int start_y = 200;

     for (int i = 0; i < x; i++)
     {
         for (int j = 0; j < y; j++)
         {
             tmpButton[i, j] = new Button();
             tmpButton[i, j].Top = start_x + (i * ButtonHeight);
             tmpButton[i, j].Left = start_y + (j * ButtonWidth);
             tmpButton[i, j].Width = ButtonWidth;
             tmpButton[i, j].Height = ButtonHeight;
             tmpButton[i, j].Click += new EventHandler(BTN_Grid_Click);
             this.Controls.Add(tmpButton[i, j]);
         }
     }
}

如果在网格内设置了一个随机位置 (x,y) true 以猜测(通过单击周围的网格按钮)它的位置。如果位置是!true,它应该返回left right up down 指向我们之前设置的随机位置。如果true 位置在左上角,它也应该返回最接近的位置。

实现这样的事情的最佳方式是什么?

这已经接近了,但我看不到的东西有点偏离了......

public String GetDirection()
{
    int xd = Guess.X - Clue.X;
    int yd = Guess.Y - Clue.Y;

    if(Math.Abs(xd) <= Math.Abs(yd))
        return (xd <= 0) ? "Left" : "Right";
    else
        return (yd <= 0) ? "Down" : "Up";
}

这是正在发生的事情的直观表示......

【问题讨论】:

  • 你试过什么?'
  • 你有没有自己尝试过一些方法并且遇到过问题?
  • 你有两个坐标。得到它们的区别,xd=x1-x2,yd=y1-y2。根据绝对值决定使用哪个坐标,然后根据符号决定实际方向。
  • @dumbdumbbadatthis 不分配。以前我使用的是一个简单的解决方案,如果guessCount % 2 == 0 根据 x 值放置在左侧或右侧,else 根据 y 值向上或向下放置。但是如果guessCount 是偶数,这个解决方案有缺点,它会放置一个向上或向下箭头,即使它在同一行。试图考虑一种使用距离公式(x1,y1)^2 + (x2,y2)^2 的方法,但不确定在这种情况下会有什么帮助。
  • @Dialectus 使用哪个坐标将基于最低绝对值,如果是负数 (left/down) 或如果是正数 (right/up) ...我的逻辑在这里正确吗?

标签: c# winforms linq matrix multidimensional-array


【解决方案1】:

好的,所以你大部分都是对的,但是你犯了一个简单的错误。当您创建按钮时,您将从左到右然后从上到下进行渲染,这会将高度创建为 x 位置,然后将宽度创建为 y 位置。因此,当您将猜测与正确的位置相关联时,您会忘记这样做并假设 x 是宽度。

这是我解决此问题的实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UpDownLeftRight {

    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form1_Load);
        }

        public int[] winner = new int[2];
        public Button[,] tmpButton = new Button[10, 10];
        private void Form1_Load(object sender, EventArgs e) {
            Random r = new Random();
            int ButtonWidth = 48;
            int ButtonHeight = 48;
            int start_x = 0;
            int start_y = 0;
            winner = new int[] { r.Next(0, 10), r.Next(0, 10) };
            this.Size = new Size((ButtonWidth * 11), (ButtonHeight * 11));
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    tmpButton[i, j] = new Button();
                    tmpButton[i, j].Left = start_x + (i * ButtonWidth);
                    tmpButton[i, j].Top = start_y + (j * ButtonHeight);
                    tmpButton[i, j].Width = ButtonWidth;
                    tmpButton[i, j].Height = ButtonHeight;
                    tmpButton[i, j].Font = new Font(FontFamily.GenericMonospace, 17);
                    tmpButton[i, j].Click += new EventHandler(BTN_Grid_Click);
                    this.Controls.Add(tmpButton[i, j]);
                }
            }
        }

        public void BTN_Grid_Click(object o, EventArgs e) {
            int[] guess = new int[2];
            for (int i = 0; i != tmpButton.GetLength(1); i++) {
                for (int j = 0; j != tmpButton.GetLength(0); j++) {
                    if (tmpButton[i, j] == o) {
                        guess = new int[] { i, j };
                    }
                }
            }
            int xDist = guess[0] - winner[0];
            int yDist = guess[1] - winner[1];
            string possible = "˂˃˄˅";
            if (xDist > 0) { possible = possible.Replace("˃", ""); }
            if (xDist < 0) { possible = possible.Replace("˂", ""); }
            if (xDist == 0) { possible = possible.Replace("˂", ""); possible = possible.Replace("˃", ""); }
            if (yDist > 0) { possible = possible.Replace("˅", ""); }
            if (yDist < 0) { possible = possible.Replace("˄", ""); }
            if (yDist == 0) { possible = possible.Replace("˄", ""); possible = possible.Replace("˅", ""); }
            ((Button)o).Text = possible;
        }
    }
}

【讨论】:

  • 我想要完成的是只显示一个箭头,而不是像你在这里看到的 2 个。显示的方向应以最接近线索的方向计算。
  • 如果两者相等,哪个优先?
  • 你知道吗...我实际上可以修改图像以指向角落并在它们相等时使用它们。我从来没有想过让箭头指向对角线是多么容易。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 2019-09-17
  • 2012-05-28
  • 2021-05-12
  • 2011-04-18
  • 1970-01-01
相关资源
最近更新 更多