Interview Question in LINQ


 

Interview Question :: Whats wrong with this cSharp code? cSharp expert help needed

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;
using System.IO;

namespace pipeworksSlidingPuzzle {
public partial class mainWindow : Form {
int rows, cols, blank, r, c;
String path = "F:\\IT312\\pipeworksSlidingPuzzle\\pipe...
int index = 0, count = 0;
Button b1;
bool secondClick = false;
bool shuffling;
int tileWidth, tileHeight;

public mainWindow()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{
AutoSize = true;

String[] files = Directory.GetFiles(path, "*.jpg");

for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (r == rows - 1 && c == cols - 1) {
break;
}
PictureBox pb = new PictureBox();
pb.Size = new Size(100, 100);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
pb.Margin = new Padding(0);
pb.BorderStyle = BorderStyle.FixedSingle;
pb.Location = new Point(c * 100, r * 100);
pb.Image = Image.FromFile(files[index]);
pb.Tag = index;
count++;
if (count==2)
{
index++;
count = 0;
}
Controls.Add(pb);

Button b = new Button();
b.Location = pb.Location;
b.Size = pb.Size;
b.Tag = pb;
Controls.Add(b);
b.BringToFront();
b.Click += new EventHandler(ButtonClick);
}
}
blank = rows * cols - 1;
}
private void Shuffle() {
// shuffle...
shuffling = true;
Random rnd = new Random();
for (int i = 0; i < 1000; i++) {
int x = rnd.Next(rows * cols - 1) + 1;
pb_Click(Controls[x], null);
}
shuffling = false;
Refresh();
}
void pb_Click(object sender, EventArgs e) {
int brow = blank / cols;
int bcol = blank % rows;

PictureBox pb = (PictureBox) sender;
object o = pb.Tag;
int tileIndex = (int) o;

int trow = tileIndex / cols;
int tcol = tileIndex % rows;

// down...
if (tcol == bcol && trow + 1 == brow) {
if (shuffling) {
pb.Top += tileHeight;
} else {
for (int i = 0; i < tileHeight; i++) {
pb.Top++;
}
}
blank = tileIndex;
tileIndex += cols;
pb.Tag = tileIndex;
if (isSolved()) {
MessageBox.Show("nalpas!!!");
}
return;
}
// up...
if (tcol == bcol && trow - 1 == brow) {
if (shuffling) {
pb.Top -= tileHeight;
} else {
for (int i = 0; i < tileHeight; i++) {
pb.Top--;
}
}
blank = tileIndex;
tileIndex -= cols;
pb.Tag = tileIndex;
if (isSolved()) {
MessageBox.Show("nalpas!!!");
}
return;
}

// left...
if (tcol - 1 == bcol && trow == brow) {
if (shuffling) {
pb.Left -= tileWidth;
} else {
for (int i = 0; i < tileHeight; i++) {
pb.Left--;
}
}
blank = tileIndex;
tileIndex--;
pb.Tag = tileIndex;
if (isSolved()) {
MessageBox.Show("nalpas!!!");
}
return;
}

// right...
if (tcol + 1 == bcol && trow == brow) {
if (shuffling) {
pb.Left += tileWidth;
} else {
for (int i = 0; i < tileHeight; i++) {
pb.Left++;
}
}
blank = tileIndex;
tileIndex++;
pb.Tag = tileIndex;
if (isSolved()
by ksk