Monday, October 12, 2009

Programming Challenge 1 - Mow the Lawn

A friend of mine has just switched from developing in COBALT, to C#. As a fun way to help him learn C# and to argue over better methods of programming, we decided to make little programming challenges that were designed to be short, simple and fun. This contest is open to anyone, just feel free to e-mail me your zipped project solution and I'll post it and comment on it.

So here goes the first challenge:
Write a winform ASCII lawn mowing application with these properties:
  • It should start with a 10x10 grid of capital "X"'s.
  • The lawn mower should be represented by an "@" symbol, and start in the upper left hand corner.
  • The user should be able to use the arrow keys to move the lawnmower around the lawn.
  • The first time the lawn mower passes over an X, it gets converted to a lower case "v".
  • When the lawn mower passes over a "v", it gets converted to a ".".
  • When the entire lawn has been reduced to "."'s a message should be displayed to the user.

Good luck and happy mowing!

* Legal Notice * These Programming Challenges shall not be reproduced in any form without my express written consent.


1 comment:

Brandon said...

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

namespace WindowsFormsApplication1
{
public partial class MowerForm : Form
{
private char[,] grassArray = new char[10, 10];
private char underMower;
private int MowerX=0, MowerY=0, numcut=0;

public MowerForm()
{
InitializeComponent();
}

public void PrintGrass()
{
String strDisplay = string.Empty;

for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
strDisplay += grassArray[i,j].ToString();
}
strDisplay += Environment.NewLine;
}
lblDisplay.Text = strDisplay;
}

private void MowerForm_Load(object sender, EventArgs e)
{
//fill grass array for first time
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
grassArray[i, j] = 'X';

}
}
//put mower at 0,0
grassArray[MowerY, MowerX] = '@';
underMower = 'v';
PrintGrass();
}

private void MowerForm_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Down:
if (MowerY < 9)
{
grassArray[MowerY, MowerX] = underMower;
MowerY++;
checkCutStatus();
}
break;
case Keys.Right:
if (MowerX < 9)
{
grassArray[MowerY, MowerX] = underMower;
MowerX++;
checkCutStatus();
}
break;
case Keys.Up:
if (MowerY > 0)
{
grassArray[MowerY, MowerX] = underMower;
MowerY--;
checkCutStatus();
}
break;
case Keys.Left:
if (MowerX > 0)
{
grassArray[MowerY, MowerX] = underMower;
MowerX--;
checkCutStatus();
}
break;
default:
break;
}
PrintGrass();

if (numcut == 100)
MessageBox.Show("The lawn is mowed!! Daryl's mom is lathered up and waiting to congratulate you inside!");

}
private void checkCutStatus()
{
switch (grassArray[MowerY, MowerX])
{
case 'X':
underMower = 'v';
grassArray[MowerY, MowerX] = '@';
break;
case 'v':
underMower = '.';
grassArray[MowerY, MowerX] = '@';
numcut += 1;
break;
case '.':
underMower = '.';
grassArray[MowerY, MowerX] = '@';
break;
}
}

}
}