c# - C# - 2d 矩阵 - 猜测点的左/右/上/下
问题描述
解决方案
好吧,所以你大部分都是对的,但是你犯了一个简单的错误。当您创建按钮时,您将从左到右然后从上到下进行渲染,这会将高度创建为 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;
}
}
}
推荐阅读
- firefox - Cloudflare Steam 视频在 Firefox 中不起作用
- javascript - 如何在现有的 html 文件和网站中包含 Angular 7 应用程序?
- javascript - 数据表功能不只显示表中的数据,而是在撇号中显示
- amazon-web-services - 如何使用 AWS Codebuild 运行并行构建?
- reactjs - Redux reducer 中的访问 URL
- c - 如何使用 MPI 发送和接收二叉树?
- hl7-fhir - FHIR:如何检查暴露的 JSON 是否符合正确的 FHIR 标准
- python - 无法找到元素 - 多个元素似乎具有相似且最少的信息,我无法正确找到
- javascript - 从数组中选择一个随机元素,重复直到满足条件
- spring - 使用生产和消费设置 bindingMode 是否多余?