Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Form1 : Form
- {
- enum inst
- {
- move, // integer value = 0
- turnleft, // 1
- pickbeeper, // 2
- putbeeper, // 3
- turnoff, // 5
- x
- };
- //global variables
- Button[,] b;
- int[,] map;
- int[] mapText;
- int[,] mapTextC;
- int mapSize;
- String[] programText;
- inst command;
- int karel_off;
- int bag = 0;
- int posX = 1;
- int posY = 1;
- int direction = 4; //1 north 2 west 3 south 4 east
- //3 types of elements.
- //0 empty
- //1 boundary
- //2 beeper
- //3 karel
- //4 karel + beeper
- public Bitmap empty = Properties.Resources.empty;
- public Bitmap beeper = Properties.Resources.beeper;
- public Bitmap boundary = Properties.Resources.boundary;
- public Bitmap kareln = Properties.Resources.karel_north;
- public Bitmap karels = Properties.Resources.karel_south;
- public Bitmap karele = Properties.Resources.karel_east;
- public Bitmap karelw = Properties.Resources.karel_west;
- public Form1() // This is MAIN for our program
- {
- InitializeComponent();
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- map = new int[8, 8]; //we will ignore 0th row and column..
- mapText = new int[36];
- mapTextC = new int[8, 8];
- command = inst.turnleft;
- karel_off = 0;
- messagesListBox.Items.Add("Karel's starting position is: 1,1 Facing East");
- createbuttonarray();
- loadMapText();
- loadProgramText();
- Display();
- }
- //Functions and their usage:
- // void loadMapText() this function gets the map text.
- // convertMapText() this function converts the map text into 2d so it can be applied on the map.
- // void applyMap() this function sets the pictures of all the locations based on the map text array.
- // Bitmap img(int x, int y) this function returns a picture object to be set based on the location it's given.
- // void loadProgramText() - this function loads the instructions written by the user to be executed
- // void createbuttonarray() - this function creates and initializes an array that's used to interact with the buttons used as a display grid.
- // void Run() - this function contains the main parsing code that is used to perform the actions
- // void Display() - this function changes the images shown after each action so the view is updated.
- // in mapTextC[]
- //0 = empty,1 = beeper,2 = boundary
- void loadMapText() //this function gets the map text.
- {
- String[] temp = new String[35];
- String content = String.Empty;
- try
- {
- System.IO.StreamReader myFile = new System.IO.StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + "/karel_map.txt");
- content = myFile.ReadToEnd();
- myFile.Close();
- }
- catch { }
- temp = content.Split(';');
- int tempint;
- for (int i = 0; i < 35; i++)
- {
- temp[i].Trim();
- if (i != 0 && i != 37)
- temp[i].Remove(0, 3);
- tempint = Convert.ToInt32(temp[i]);
- mapText[i] = tempint;
- }
- convertMapText();
- applyMap();
- }
- void convertMapText()
- {
- int z = 0;
- mapTextC[0, 0] = 0;
- for (int i = 1; i < 7; i++)
- {
- for (int j = 1; j < 7; j++)
- {
- mapTextC[j, i] = mapText[z];
- z++;
- if (z == 35) break;
- }
- }
- }
- void applyMap()
- {
- for (int i = 1; i < 7; i++)
- {
- for (int j = 1; j < 7; j++)
- {
- b[i, j].BackgroundImage = img(i, j);
- }
- }
- }
- Bitmap img(int x, int y) //set the image for each x and y
- {
- switch (mapTextC[x, y])
- {
- case 0:
- return empty;
- case 1:
- return beeper;
- case 2:
- return boundary;
- }
- return empty;
- }
- public void loadProgramText() //this function gets the program text.
- {
- programText = new String[35];
- String content = String.Empty;
- try
- {
- System.IO.StreamReader myFile = new System.IO.StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + "/karel_program.txt");
- content = myFile.ReadToEnd();
- myFile.Close();
- }
- catch { }
- programText = content.Split(';');
- for (int i = 0; i < programText.Length; i++)
- {
- programText[i].Trim();
- programListBox.Items.Add(programText[i] + ";");
- }
- }
- void createbuttonarray() //makes button array to be used in other functions
- {
- b = new Button[7, 7];
- b[1, 1] = b11;
- b[2, 1] = b21;
- b[3, 1] = b31;
- b[4, 1] = b41;
- b[5, 1] = b51;
- b[6, 1] = b61;
- b[1, 2] = b12;
- b[2, 2] = b22;
- b[3, 2] = b32;
- b[4, 2] = b42;
- b[5, 2] = b52;
- b[6, 2] = b62;
- b[1, 3] = b13;
- b[2, 3] = b23;
- b[3, 3] = b33;
- b[4, 3] = b43;
- b[5, 3] = b53;
- b[6, 3] = b63;
- b[1, 4] = b14;
- b[2, 4] = b24;
- b[3, 4] = b34;
- b[4, 4] = b44;
- b[5, 4] = b54;
- b[6, 4] = b64;
- b[1, 5] = b15;
- b[2, 5] = b25;
- b[3, 5] = b35;
- b[4, 5] = b45;
- b[5, 5] = b55;
- b[6, 5] = b65;
- b[1, 6] = b16;
- b[2, 6] = b26;
- b[3, 6] = b36;
- b[4, 6] = b46;
- b[5, 6] = b56;
- b[6, 6] = b66;
- }
- void Display()
- {
- applyMap();
- DisplayKarel();
- label11.Text = "" + bag;
- }
- void DisplayKarel()
- {
- switch (direction)
- {
- case 1:
- {
- b[posX, posY].BackgroundImage = kareln;
- label12.Text = "North";
- break;
- }
- case 2:
- {
- b[posX, posY].BackgroundImage = karelw;
- label12.Text = "West";
- break;
- }
- case 3:
- {
- b[posX, posY].BackgroundImage = karels;
- label12.Text = "South";
- break;
- }
- case 4:
- {
- b[posX, posY].BackgroundImage = karele;
- label12.Text = "East";
- break;
- }
- }
- }
- /// ALL THE KAREL COMMANDS:
- int i = 0;
- void Run()
- {
- if (karel_off == 1)
- {
- messagesListBox.Items.Add("Karel is turned off!");
- try { messagesListBox.SelectedIndex++; }
- catch (Exception e) { }
- }
- if (i >= programText.Length) return;
- try
- {
- programText[i].Trim();
- programText[i] = System.Text.RegularExpressions.Regex.Replace(programText[i], @"\t|\n|\r", "");
- }
- catch (Exception e) { }
- //0 = empty,1 = beeper,2 = boundary
- command = inst.x;
- if (programText[i] == "move")
- {
- command = inst.move; label16.Text = "move";
- }
- else if (programText[i] == "turnleft")
- {
- command = inst.turnleft; label16.Text = "turnleft";
- }
- else if (programText[i] == "pickbeeper")
- {
- command = inst.pickbeeper; label16.Text = "pickbeeper";
- }
- else if (programText[i] == "putbeeper")
- {
- command = inst.putbeeper; label16.Text = "putbeeper";
- }
- else if (programText[i] == "turnoff")
- {
- command = inst.turnoff; label16.Text = "turnoff";
- }
- else { label16.Text = ""; }
- switch (command)
- {
- case inst.move: //move
- {
- switch (direction)
- {
- case 1: //north
- {
- if (mapTextC[posX, posY + 1] == 2)
- {
- messagesListBox.Items.Add("Movement was blocked by an obstacle!");
- }
- else
- {
- posY++;
- }
- break;
- }
- case 2: //west
- {
- if (mapTextC[posX - 1, posY] == 2)
- {
- messagesListBox.Items.Add("Movement was blocked by an obstacle!");
- }
- else
- {
- posX--;
- }
- break;
- }
- case 3: //south
- {
- if (mapTextC[posX, posY - 1] == 2)
- {
- messagesListBox.Items.Add("Movement was blocked by an obstacle!");
- }
- else
- {
- posY--;
- }
- break;
- }
- case 4: //east
- {
- if (mapTextC[posX + 1, posY] == 2)
- {
- messagesListBox.Items.Add("Movement was blocked by an obstacle!");
- }
- else
- {
- posX++;
- }
- break;
- }
- }
- if (posX > 6 || posX < 1 || posY > 6 || posY < 1)
- {
- messagesListBox.Items.Add("Error: Karel is outside the world, Turning off.");
- karel_off = 1;
- return;
- }
- else
- {
- messagesListBox.Items.Add("Current position of Karel: (" + posX + "," + posY + "), direction is: " + label12.Text);
- }
- break;
- }
- case inst.turnleft:
- {
- if (direction == 4)
- {
- direction = 1;
- }
- else
- {
- direction++;
- }
- break;
- }
- case inst.pickbeeper:
- {
- if (mapTextC[posX, posY] == 1)
- {
- mapTextC[posX, posY] = 0;
- b[posX, posY].BackgroundImage = empty;
- bag++;
- messagesListBox.Items.Add("Karel picked up a beeper!");
- }
- else
- {
- messagesListBox.Items.Add("There are no beeper in this cell");
- }
- break;
- }
- case inst.putbeeper:
- {
- if (bag == 0)
- {
- messagesListBox.Items.Add("Error: No beepers in the bag.");
- }
- else
- {
- if (mapTextC[posX, posY] == 0)
- {
- mapTextC[posX, posY] = 1;
- b[posX, posY].BackgroundImage = beeper;
- bag--;
- messagesListBox.Items.Add("Karel dropped a beeper!");
- }
- else
- {
- if (mapTextC[posX, posY] == 1)
- {
- messagesListBox.Items.Add("Cell already contains a beeper.");
- }
- }
- }
- break;
- }
- case inst.turnoff: //turnoff
- {
- messagesListBox.Items.Add("Turning Karel off.");
- karel_off = 1;
- break;
- }
- }
- Display();
- try { programListBox.SelectedIndex = i; } //make list item selected be at the end.
- catch (Exception e) { }
- try { messagesListBox.SelectedIndex++; }
- catch (Exception e) { }
- i++;
- }
- private void Restart()
- { //unused .. no need for it.
- i = 0;
- bag = 0;
- direction = 4;
- posX = 1;
- posY = 1;
- loadMapText();
- loadProgramText();
- //clearimages();
- Display();
- DisplayKarel();
- }
- private void RunButton_Click(object sender, EventArgs e)
- {
- Run();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment