maddada

c#code

May 24th, 2016
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         enum inst
  15.         {
  16.             move,         // integer value = 0
  17.             turnleft,     // 1
  18.             pickbeeper,   // 2
  19.             putbeeper,    // 3
  20.             turnoff,      // 5
  21.             x
  22.         };
  23.  
  24.  
  25.         //global variables
  26.         Button[,] b;
  27.         int[,] map;
  28.  
  29.         int[] mapText;
  30.         int[,] mapTextC;
  31.         int mapSize;
  32.  
  33.         String[] programText;
  34.  
  35.         inst command;
  36.         int karel_off;
  37.         int bag = 0;
  38.         int posX = 1;
  39.         int posY = 1;
  40.         int direction = 4; //1 north 2 west 3 south 4 east
  41.  
  42.         //3 types of elements.
  43.         //0 empty
  44.         //1 boundary
  45.         //2 beeper
  46.         //3 karel
  47.         //4 karel + beeper      
  48.  
  49.         public Bitmap empty = Properties.Resources.empty;
  50.         public Bitmap beeper = Properties.Resources.beeper;
  51.         public Bitmap boundary = Properties.Resources.boundary;
  52.         public Bitmap kareln = Properties.Resources.karel_north;
  53.         public Bitmap karels = Properties.Resources.karel_south;
  54.         public Bitmap karele = Properties.Resources.karel_east;
  55.         public Bitmap karelw = Properties.Resources.karel_west;
  56.  
  57.         public Form1() // This is MAIN for our program
  58.         {
  59.             InitializeComponent();
  60.         }
  61.         private void Form1_Load(object sender, EventArgs e)
  62.         {
  63.             map = new int[8, 8]; //we will ignore 0th row and column..            
  64.             mapText = new int[36];
  65.             mapTextC = new int[8, 8];
  66.             command = inst.turnleft;
  67.             karel_off = 0;
  68.  
  69.             messagesListBox.Items.Add("Karel's starting position is: 1,1 Facing East");
  70.  
  71.             createbuttonarray();
  72.  
  73.             loadMapText();
  74.  
  75.             loadProgramText();
  76.  
  77.             Display();
  78.         }
  79.  
  80.         //Functions and their usage:
  81.         // void loadMapText() this function gets the map text.
  82.         // convertMapText() this function converts the map text into 2d so it can be applied on the map.
  83.         // void applyMap() this function sets the pictures of all the locations based on the map text array.
  84.         // Bitmap img(int x, int y)  this function returns a picture object to be set based on the location it's given.
  85.         // void loadProgramText() - this function loads the instructions written by the user to be executed
  86.         // void createbuttonarray() - this function creates and initializes an array that's used to interact with the buttons used as a display grid.
  87.         // void Run() - this function contains the main parsing code that is used to perform the actions
  88.         // void Display() - this function changes the images shown after each action so the view is updated.
  89.  
  90.         // in mapTextC[]
  91.         //0 = empty,1 = beeper,2 = boundary
  92.  
  93.         void loadMapText() //this function gets the map text.
  94.         {
  95.             String[] temp = new String[35];
  96.             String content = String.Empty;
  97.  
  98.             try
  99.             {
  100.                 System.IO.StreamReader myFile = new System.IO.StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + "/karel_map.txt");
  101.                 content = myFile.ReadToEnd();
  102.                 myFile.Close();
  103.             }
  104.             catch { }
  105.  
  106.             temp = content.Split(';');
  107.  
  108.             int tempint;
  109.             for (int i = 0; i < 35; i++)
  110.             {
  111.                 temp[i].Trim();
  112.                 if (i != 0 && i != 37)
  113.                     temp[i].Remove(0, 3);
  114.                 tempint = Convert.ToInt32(temp[i]);
  115.                 mapText[i] = tempint;
  116.             }
  117.  
  118.             convertMapText();
  119.             applyMap();
  120.  
  121.         }
  122.  
  123.         void convertMapText()
  124.         {
  125.             int z = 0;
  126.  
  127.             mapTextC[0, 0] = 0;
  128.  
  129.             for (int i = 1; i < 7; i++)
  130.             {
  131.                 for (int j = 1; j < 7; j++)
  132.                 {
  133.                     mapTextC[j, i] = mapText[z];
  134.                     z++;
  135.  
  136.                     if (z == 35) break;
  137.                 }
  138.             }
  139.         }
  140.  
  141.         void applyMap()
  142.         {
  143.             for (int i = 1; i < 7; i++)
  144.             {
  145.                 for (int j = 1; j < 7; j++)
  146.                 {
  147.                     b[i, j].BackgroundImage = img(i, j);
  148.                 }
  149.             }
  150.         }
  151.  
  152.         Bitmap img(int x, int y) //set the image for each x and y
  153.         {
  154.             switch (mapTextC[x, y])
  155.             {
  156.                 case 0:
  157.                     return empty;
  158.                 case 1:
  159.                     return beeper;
  160.                 case 2:
  161.                     return boundary;
  162.             }
  163.  
  164.             return empty;
  165.         }
  166.  
  167.         public void loadProgramText() //this function gets the program text.
  168.         {
  169.             programText = new String[35];
  170.  
  171.             String content = String.Empty;
  172.  
  173.             try
  174.             {
  175.                 System.IO.StreamReader myFile = new System.IO.StreamReader(System.AppDomain.CurrentDomain.BaseDirectory + "/karel_program.txt");
  176.                 content = myFile.ReadToEnd();
  177.                 myFile.Close();
  178.             }
  179.             catch { }
  180.  
  181.             programText = content.Split(';');
  182.  
  183.             for (int i = 0; i < programText.Length; i++)
  184.             {
  185.                 programText[i].Trim();
  186.  
  187.                 programListBox.Items.Add(programText[i] + ";");
  188.  
  189.             }
  190.         }
  191.  
  192.         void createbuttonarray() //makes button array to be used in other functions
  193.         {
  194.             b = new Button[7, 7];
  195.  
  196.             b[1, 1] = b11;
  197.             b[2, 1] = b21;
  198.             b[3, 1] = b31;
  199.             b[4, 1] = b41;
  200.             b[5, 1] = b51;
  201.             b[6, 1] = b61;
  202.  
  203.             b[1, 2] = b12;
  204.             b[2, 2] = b22;
  205.             b[3, 2] = b32;
  206.             b[4, 2] = b42;
  207.             b[5, 2] = b52;
  208.             b[6, 2] = b62;
  209.  
  210.             b[1, 3] = b13;
  211.             b[2, 3] = b23;
  212.             b[3, 3] = b33;
  213.             b[4, 3] = b43;
  214.             b[5, 3] = b53;
  215.             b[6, 3] = b63;
  216.  
  217.             b[1, 4] = b14;
  218.             b[2, 4] = b24;
  219.             b[3, 4] = b34;
  220.             b[4, 4] = b44;
  221.             b[5, 4] = b54;
  222.             b[6, 4] = b64;
  223.  
  224.             b[1, 5] = b15;
  225.             b[2, 5] = b25;
  226.             b[3, 5] = b35;
  227.             b[4, 5] = b45;
  228.             b[5, 5] = b55;
  229.             b[6, 5] = b65;
  230.  
  231.             b[1, 6] = b16;
  232.             b[2, 6] = b26;
  233.             b[3, 6] = b36;
  234.             b[4, 6] = b46;
  235.             b[5, 6] = b56;
  236.             b[6, 6] = b66;
  237.         }
  238.  
  239.         void Display()
  240.         {
  241.             applyMap();
  242.             DisplayKarel();
  243.             label11.Text = "" + bag;
  244.         }
  245.         void DisplayKarel()
  246.         {
  247.             switch (direction)
  248.             {
  249.                 case 1:
  250.                     {
  251.                         b[posX, posY].BackgroundImage = kareln;
  252.                         label12.Text = "North";
  253.                         break;
  254.                     }
  255.                 case 2:
  256.                     {
  257.                         b[posX, posY].BackgroundImage = karelw;
  258.                         label12.Text = "West";
  259.                         break;
  260.                     }
  261.                 case 3:
  262.                     {
  263.                         b[posX, posY].BackgroundImage = karels;
  264.                         label12.Text = "South";
  265.                         break;
  266.                     }
  267.                 case 4:
  268.                     {
  269.                         b[posX, posY].BackgroundImage = karele;
  270.                         label12.Text = "East";
  271.                         break;
  272.                     }
  273.             }
  274.         }
  275.  
  276.  
  277.         /// ALL THE KAREL COMMANDS:
  278.         int i = 0;
  279.         void Run()
  280.         {
  281.             if (karel_off == 1)
  282.             {
  283.                 messagesListBox.Items.Add("Karel is turned off!");
  284.                 try { messagesListBox.SelectedIndex++; }
  285.                 catch (Exception e) { }
  286.             }
  287.  
  288.             if (i >= programText.Length) return;
  289.  
  290.  
  291.  
  292.             try
  293.             {
  294.                 programText[i].Trim();
  295.                 programText[i] = System.Text.RegularExpressions.Regex.Replace(programText[i], @"\t|\n|\r", "");
  296.             }
  297.             catch (Exception e) { }
  298.             //0 = empty,1 = beeper,2 = boundary
  299.  
  300.             command = inst.x;
  301.  
  302.             if (programText[i] == "move")
  303.             {
  304.                 command = inst.move; label16.Text = "move";
  305.             }
  306.             else if (programText[i] == "turnleft")
  307.             {
  308.                 command = inst.turnleft; label16.Text = "turnleft";
  309.             }
  310.             else if (programText[i] == "pickbeeper")
  311.             {
  312.                 command = inst.pickbeeper; label16.Text = "pickbeeper";
  313.             }
  314.             else if (programText[i] == "putbeeper")
  315.             {
  316.                 command = inst.putbeeper; label16.Text = "putbeeper";
  317.             }
  318.             else if (programText[i] == "turnoff")
  319.             {
  320.                 command = inst.turnoff; label16.Text = "turnoff";
  321.             }
  322.             else { label16.Text = ""; }
  323.  
  324.  
  325.  
  326.             switch (command)
  327.             {
  328.                 case inst.move: //move
  329.                     {
  330.                         switch (direction)
  331.                         {
  332.                             case 1: //north
  333.                                 {
  334.                                     if (mapTextC[posX, posY + 1] == 2)
  335.                                     {
  336.                                         messagesListBox.Items.Add("Movement was blocked by an obstacle!");
  337.                                     }
  338.                                     else
  339.                                     {
  340.                                         posY++;
  341.                                     }
  342.                                     break;
  343.                                 }
  344.                             case 2: //west
  345.                                 {
  346.                                     if (mapTextC[posX - 1, posY] == 2)
  347.                                     {
  348.                                         messagesListBox.Items.Add("Movement was blocked by an obstacle!");
  349.                                     }
  350.                                     else
  351.                                     {
  352.                                         posX--;
  353.                                     }
  354.                                     break;
  355.                                 }
  356.                             case 3: //south
  357.                                 {
  358.                                     if (mapTextC[posX, posY - 1] == 2)
  359.                                     {
  360.                                         messagesListBox.Items.Add("Movement was blocked by an obstacle!");
  361.                                     }
  362.                                     else
  363.                                     {
  364.                                         posY--;
  365.                                     }
  366.                                     break;
  367.                                 }
  368.                             case 4: //east
  369.                                 {
  370.                                     if (mapTextC[posX + 1, posY] == 2)
  371.                                     {
  372.                                         messagesListBox.Items.Add("Movement was blocked by an obstacle!");
  373.                                     }
  374.                                     else
  375.                                     {
  376.                                         posX++;
  377.                                     }
  378.                                     break;
  379.                                 }
  380.                         }
  381.                         if (posX > 6 || posX < 1 || posY > 6 || posY < 1)
  382.                         {
  383.                             messagesListBox.Items.Add("Error: Karel is outside the world, Turning off.");
  384.                             karel_off = 1;
  385.                             return;
  386.                         }
  387.                         else
  388.                         {
  389.                             messagesListBox.Items.Add("Current position of Karel: (" + posX + "," + posY + "), direction is: " + label12.Text);
  390.                         }
  391.                         break;
  392.                     }
  393.                 case inst.turnleft:
  394.                     {
  395.                         if (direction == 4)
  396.                         {
  397.                             direction = 1;
  398.                         }
  399.                         else
  400.                         {
  401.                             direction++;
  402.                         }
  403.                         break;
  404.                     }
  405.                 case inst.pickbeeper:
  406.                     {
  407.                         if (mapTextC[posX, posY] == 1)
  408.                         {
  409.                             mapTextC[posX, posY] = 0;
  410.                             b[posX, posY].BackgroundImage = empty;
  411.                             bag++;
  412.                             messagesListBox.Items.Add("Karel picked up a beeper!");
  413.                         }
  414.                         else
  415.                         {
  416.                             messagesListBox.Items.Add("There are no beeper in this cell");
  417.                         }
  418.                         break;
  419.                     }
  420.                 case inst.putbeeper:
  421.                     {
  422.                         if (bag == 0)
  423.                         {
  424.                             messagesListBox.Items.Add("Error: No beepers in the bag.");                            
  425.                         }
  426.                         else
  427.                         {
  428.                             if (mapTextC[posX, posY] == 0)
  429.                             {
  430.                                 mapTextC[posX, posY] = 1;
  431.                                 b[posX, posY].BackgroundImage = beeper;
  432.                                 bag--;
  433.                                 messagesListBox.Items.Add("Karel dropped a beeper!");
  434.                             }
  435.                             else
  436.                             {
  437.                                 if (mapTextC[posX, posY] == 1)
  438.                                 {
  439.                                     messagesListBox.Items.Add("Cell already contains a beeper.");
  440.                                 }
  441.                             }
  442.                         }
  443.                         break;
  444.                     }
  445.                 case inst.turnoff: //turnoff
  446.                     {
  447.                         messagesListBox.Items.Add("Turning Karel off.");
  448.                         karel_off = 1;
  449.                         break;
  450.                     }
  451.  
  452.             }
  453.  
  454.             Display();
  455.             try { programListBox.SelectedIndex = i; } //make list item selected be at the end.
  456.             catch (Exception e) { }
  457.             try { messagesListBox.SelectedIndex++; }
  458.             catch (Exception e) { }
  459.  
  460.             i++;
  461.         }
  462.  
  463.         private void Restart()
  464.         {   //unused .. no need for it.
  465.             i = 0;
  466.             bag = 0;
  467.             direction = 4;
  468.             posX = 1;
  469.             posY = 1;
  470.             loadMapText();
  471.             loadProgramText();
  472.             //clearimages();
  473.             Display();
  474.             DisplayKarel();
  475.         }
  476.  
  477.  
  478.         private void RunButton_Click(object sender, EventArgs e)
  479.         {
  480.             Run();
  481.         }
  482.  
  483.     }
  484. }
Advertisement
Add Comment
Please, Sign In to add comment