我的GOOGLE编程题答案(C#版)

昨天本来想用C写的,结果写着写着死机了,真是老天爷不让我用C写了,今天用C#又写了一个带界面版的,把命令格式稍做了点改变,如{"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"}改成了FORWARD 19, LEFT, FORWARD 19, LEFT,FORWARD 19,LEFT,FORWARD 19

不过效率肯定不敢和C比了,存属娱乐!

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GoogleCode
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.RichTextBox richTextBox1;
  private int down = 1;
  private int right = 2;
  private int up = 3;
  private int left = 4;
  private int direction = 1;
  private string[,] canvas = new System.String[20,20];
   private System.Drawing.Point p = new Point();

  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();
   this.Inicial();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.richTextBox1 = new System.Windows.Forms.RichTextBox();
   this.SuspendLayout();
   //
   // textBox1
   //
   this.textBox1.Location = new System.Drawing.Point(96, 16);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(384, 21);
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "";
   this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(32, 16);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(56, 23);
   this.label1.TabIndex = 1;
   this.label1.Text = "Command";
   //
   // richTextBox1
   //
   this.richTextBox1.Location = new System.Drawing.Point(24, 48);
   this.richTextBox1.Name = "richTextBox1";
   this.richTextBox1.Size = new System.Drawing.Size(456, 272);
   this.richTextBox1.TabIndex = 2;
   this.richTextBox1.Text = "";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(504, 334);
   this.Controls.Add(this.richTextBox1);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.textBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
  private void Inicial()
  {
   for(int i = 0 ;i<20 ; i++)
   {
    for(int n = 0 ; n<20 ; n++)
    {
     this.canvas[i,n] = ".";
    }
   }
   p.X = 0;
   p.Y = 0;
   this.direction = this.down;
  }

  private void Form1_Load(object sender, System.EventArgs e)
  {
  
  }
  private void LEFT()
  {
   if(this.direction == this.down)
    this.direction++;
   else if(this.direction == this.right)
    this.direction++;
   else if(this.direction == this.up)
    this.direction++;
   else if(this.direction == this.left)
    this.direction = this.down;
  }
  private void Forward(int distance)
  {
   this.canvas[p.Y,p.X] = "X";
   if(this.direction == this.down)
   {
    if(p.Y + distance < 20 )
    {
     for( int i=0;i<distance;i++)
     {
      p.Y++;
      this.canvas[p.Y,p.X] = "X";
     }
    }
    else
    {
     MessageBox.Show("Commands are foul!");
     return ;
    }
   }
   else if(this.direction == this.right)
   {
    if(p.X + distance < 20)
    {
     for( int i=0;i<distance;i++)
     {
      p.X++;
      this.canvas[p.Y,p.X] = "X";
     }
    }
    else
    {
     MessageBox.Show("Commands are foul!");
     return ;
    }
   }
   else if(this.direction == this.up)
   {
    if(p.Y - distance >=0)
    {
     for(int i = 0;i<distance;i++)
     {
      p.Y--;
      this.canvas[p.Y,p.X] = "X";
     }
    }
    else
    {
     MessageBox.Show("Commands are foul!");
     return;
    }
   }
   else
   {
    if(p.X - distance >= 0)
    {
     for(int i = 0;i<distance;i++)
     {
      p.X--;
      this.canvas[p.Y,p.X] = "X";
     }
    }
    else
    {
     MessageBox.Show("Commands are foul!");
     return;
    }
   }
  

  }


  private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
   if(e.KeyCode == System.Windows.Forms.Keys.Enter)
   {
    this.richTextBox1.Text = "";
    string commands = this.textBox1.Text.Trim();
    string[] command = commands.Split(new char[] {','});
    foreach(string s in command)
    {
     if(s.Trim().Substring(0 , 4).CompareTo("LEFT") == 0)
     {
      this.LEFT();
     }
     else if(s.Trim().Substring(0 , 7).CompareTo("FORWARD") == 0)
     {
      int distance = 0;
      try
      {
       distance = Convert.ToInt32(s.Trim().Substring(8 , s.Trim().Length - 8).Trim());
       this.Forward(distance);
      }
      catch
      {
       MessageBox.Show("Commands are wrong!");
       return ;
      }
     }
    }
   

    for(int n = 0; n<20;n++)
    {
     this.richTextBox1.Text += "/"" ;
     for(int i = 0;i<20;i++)
     {
      this.richTextBox1.Text += this.canvas[n,i].ToString() ;
     }
     this.richTextBox1.Text += "/",/n";
    }
    
    this.Inicial();
   }
   else if(e.KeyCode == System.Windows.Forms.Keys.Escape)
   {
    this.richTextBox1.Text = "";
    this.textBox1.Text = "";
    this.Inicial();
   }

  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值