   Confusion with columns and rows in C# Windows App "Robot" Simulation
  I was wondering if someone could review my code.  I get so confused with columns and rows and which is which in a 2D array versus drawing it to the Window.I have a 4 x 4 "world" with ONE robot.This robot does not know how to move in any direction but NORTH.When I update the world the robot is moving, but not how I expected:HERE IS MY CURRENT SOURCE CODE:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace TestAI{public class World{private const int _MIN_ROW = 0;private const int _MAX_ROW = 1;private const int _MIN_COL = 0;private const int _MAX_COL = 1;public Graphics world_graphics;public Image img;private Robot[,] robo = new Robot[_MAX_COL + 1,_MAX_ROW + 1];public World(){}public void CreateRobots(){for(int row=0;row<=_MAX_ROW;row++){for(int col=0;col<=_MAX_COL;col++){robo[col,row] = new Robot();robo[col,row]._row = row;robo[col,row]._col = col;try{robo[col,row].RobotStatus = "none";}catch(Exception ee){//System.Diagnostics.Debug.WriteLine("Status problem:  " + ee.ToString());}robo[col,row]._name = "";}}robo[1,1]._name = "Bob";robo[1,1].RobotStatus = "robo";}public void DisplayWorld(Graphics gr){for(int row=0;row<=_MAX_ROW;row++){for(int col=0;col<=_MAX_COL;col++){gr.DrawImage(robo[col,row]._status_icon,row*32,col*32);    //System.Diagnostics.Debug.WriteLine("Row,Col  " + robo[row,col].Row.ToString() + "," + robo[row,col].Col.ToString() + ":  " + robo[row,col].RobotStatus); }}}public void MoveRobot(int old_row, int old_col, int new_row, int new_col){robo[new_col,new_row].SufferDeathFrom(robo[old_col, old_row]);}public string WhoIsAt(int temp_row, int temp_col){if(temp_row < _MIN_ROW){return "blocked";}if(temp_row > _MAX_ROW){return "blocked";}if(temp_col < _MIN_COL){return "blocked";}if(temp_col > _MAX_COL){return "blocked";}return robo[temp_row,temp_col].RobotStatus;}public void UpdateWorld(){for(int row=0;row<=_MAX_ROW;row++){for(int col=0;col<=_MAX_COL;col++){robo[col,row].LookAround(this);robo[col,row].Move(this);}}}}}
  Ok thanks for the information!Sorry. I was too quick.Illco[Edited by - Illco on December 7, 2005 3:54:39 PM];  Normally, the order is (row,col) as opposed to (col,row).Also, in your WhoIsAt method, you have (row,col), but everywhere else, you have (col,row).;Quote:Original post by IllcoOk thanks for the information!IllcoSorry...I was not quite done editing my post.I meant to ask for help debugging.   ;Quote:Original post by Dave HuntNormally, the order is (row,col) as opposed to (col,row).Also, in your WhoIsAt method, you have (row,col), but everywhere else, you have (col,row).Hmmmm...let me double-check.So my for loops would beOuter Row   Inner ColumnStart at Row 0 then loop through colums 0 and 1then go to Row 1 and loop through columns 0 and 1 again?In my mind....the Robot should move as the last thing before the loops exit.;Quote:Original post by Tom KnowltonSo my for loops would beOuter Row   Inner ColumnStart at Row 0 then loop through colums 0 and 1then go to Row 1 and loop through columns 0 and 1 again?Exactly.;Quote:Original post by Dave HuntQuote:Original post by Tom KnowltonSo my for loops would beOuter Row   Inner ColumnStart at Row 0 then loop through colums 0 and 1then go to Row 1 and loop through columns 0 and 1 again?Exactly.I must still be missing something.Here is my updated source...I have put everything BACK to   row, col    did I miss something still?  :ROBOT CLASS:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace TestAI{public class Robot{//CONSTANTSprivate const string _IMAGE_DIR = "images";private const string _DIR_DELIMETER = "\\";private const string _FULL_IMAGE_DIR = _IMAGE_DIR + _DIR_DELIMETER;private const string _ROBO_BMP = "robo.bmp";private const string _NONE_BMP = "none.bmp";private const string _FULL_ROBO_IMAGE_PATH = _FULL_IMAGE_DIR + _ROBO_BMP;private const string _FULL_NONE_IMAGE_PATH = _FULL_IMAGE_DIR + _NONE_BMP;private const int _MIN_HEALTH = 0;private const string _ROBO = "robo";private const string _NONE = "none";private const string _BLOCKED = "blocked";private const int sight_range = 1;private const int move_range = 1;//CONSTANTSpublic Image _status_icon;public string _name = "unknown";public int _health = 100;public string _status = _NONE;public int _row;public int _col;private string N;private string NE;private string E;private string SE;private string S;private string SW;private string W;private string NW;public void LookAround(World temp_world){N = temp_world.WhoIsAt(_row - 1, _col);NE = temp_world.WhoIsAt(_row - 1, _col + 1);E = temp_world.WhoIsAt(_row, _col + 1);SE = temp_world.WhoIsAt(_row + 1, _col + 1);S = temp_world.WhoIsAt(_row + 1, _col);SW = temp_world.WhoIsAt(_row + 1, _col - 1);W = temp_world.WhoIsAt(_row, _col - 1);NW = temp_world.WhoIsAt(_row - 1,_col - 1);}public void SufferDeathFrom(Robot temp_robot){this._row = temp_robot._row;this._col = temp_robot._col;this.RobotStatus = temp_robot.RobotStatus;this._name = temp_robot._name;temp_robot.Die();}public void Die(){this._name = _NONE;this._status = _NONE;}public void Move(World temp_world){if(N != _BLOCKED){temp_world.MoveRobot(_row,_col,_row-1,_col);}}public string RobotStatus{get{ return _status;}set{_status = value;if(this._status == _ROBO){this._status_icon = Image.FromFile(_FULL_ROBO_IMAGE_PATH);}if(this._status == _NONE){this._status_icon = Image.FromFile(_FULL_NONE_IMAGE_PATH);}}}public Robot(){}}}WORLD CLASS:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace TestAI{public class World{private const int _MIN_ROW = 0;private const int _MAX_ROW = 1;private const int _MIN_COL = 0;private const int _MAX_COL = 1;public Graphics world_graphics;public Image img;private Robot[,] robo = new Robot[_MAX_ROW + 1,_MAX_COL + 1];public World(){}public void CreateRobots(){for(int row=0;row<=_MAX_ROW;row++){for(int col=0;col<=_MAX_COL;col++){robo[row,col] = new Robot();robo[row,col]._row = row;robo[row,col]._col = col;try{robo[row,col].RobotStatus = "none";}catch(Exception ee){//System.Diagnostics.Debug.WriteLine("Status problem:  " + ee.ToString());}robo[row,col]._name = "";}}robo[1,1]._name = "Bob";robo[1,1].RobotStatus = "robo";}public void DisplayWorld(Graphics gr){for(int row=0;row<=_MAX_ROW;row++){for(int col=0;col<=_MAX_COL;col++){gr.DrawImage(robo[row,col]._status_icon,row*32,col*32);    //System.Diagnostics.Debug.WriteLine("Row,Col  " + robo[row,col].Row.ToString() + "," + robo[row,col].Col.ToString() + ":  " + robo[row,col].RobotStatus); }}}public void MoveRobot(int old_row, int old_col, int new_row, int new_col){robo[new_row,new_col].SufferDeathFrom(robo[old_row, old_col]);}public string WhoIsAt(int temp_row, int temp_col){if(temp_row < _MIN_ROW){return "blocked";}if(temp_row > _MAX_ROW){return "blocked";}if(temp_col < _MIN_COL){return "blocked";}if(temp_col > _MAX_COL){return "blocked";}return robo[temp_row,temp_col].RobotStatus;}public void UpdateWorld(){for(int row=0;row<=_MAX_ROW;row++){for(int col=0;col<=_MAX_COL;col++){robo[row,col].LookAround(this);robo[row,col].Move(this);}}}}}MAIN FORM:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace TestAI{/// <summary>/// Summary description for Form1./// </summary>public class Form1 : System.Windows.Forms.Form{private System.Windows.Forms.TextBox textBoxWorld;private System.Windows.Forms.PictureBox pictureBoxWorld;private World _new_world = new World();private System.Windows.Forms.Button buttonUpdateWorld;/// <summary>/// Required designer variable./// </summary>private System.ComponentModel.Container components = null;public Form1(){//// Required for Windows Form Designer support//InitializeComponent();//// TODO: Add any constructor code after InitializeComponent call//}/// <summary>/// Clean up any resources being used./// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>private void InitializeComponent(){this.textBoxWorld = new System.Windows.Forms.TextBox();this.pictureBoxWorld = new System.Windows.Forms.PictureBox();this.buttonUpdateWorld = new System.Windows.Forms.Button();this.SuspendLayout();// // textBoxWorld// this.textBoxWorld.Location = new System.Drawing.Point(16, 32);this.textBoxWorld.Multiline = true;this.textBoxWorld.Name = "textBoxWorld";this.textBoxWorld.Size = new System.Drawing.Size(384, 400);this.textBoxWorld.TabIndex = 0;this.textBoxWorld.Text = "";// // pictureBoxWorld// this.pictureBoxWorld.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;this.pictureBoxWorld.Location = new System.Drawing.Point(472, 40);this.pictureBoxWorld.Name = "pictureBoxWorld";this.pictureBoxWorld.Size = new System.Drawing.Size(408, 392);this.pictureBoxWorld.TabIndex = 1;this.pictureBoxWorld.TabStop = false;this.pictureBoxWorld.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxWorld_Paint);// // buttonUpdateWorld// this.buttonUpdateWorld.Location = new System.Drawing.Point(600, 448);this.buttonUpdateWorld.Name = "buttonUpdateWorld";this.buttonUpdateWorld.Size = new System.Drawing.Size(144, 32);this.buttonUpdateWorld.TabIndex = 3;this.buttonUpdateWorld.Text = "Update World";this.buttonUpdateWorld.Click += new System.EventHandler(this.buttonUpdateWorld_Click);// // Form1// this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);this.ClientSize = new System.Drawing.Size(960, 606);this.Controls.Add(this.buttonUpdateWorld);this.Controls.Add(this.pictureBoxWorld);this.Controls.Add(this.textBoxWorld);this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);this.ResumeLayout(false);}#endregion/// <summary>/// The main entry point for the application./// </summary>[STAThread]static void Main() {Application.Run(new Form1());}private void Form1_Load(object sender, System.EventArgs e){_new_world.CreateRobots();}private void pictureBoxWorld_Paint(object sender, System.Windows.Forms.PaintEventArgs e){try{_new_world.DisplayWorld(e.Graphics);}catch(Exception ee){//System.Diagnostics.Debug.WriteLine(ee.ToString());}}private void buttonUpdateWorld_Click(object sender, System.EventArgs e){_new_world.UpdateWorld();this.pictureBoxWorld.Refresh();}}};  That code looks fine, but it may be something missed in your World class code.;Quote:Original post by Dave HuntThat code looks fine, but it may be something missed in your World class code.Uuuugh...I am still lost.I guess I will figure it out eventually.;  Looks like I posted before you were finished editing. On the surface, that code looks okay.I would recommend adding a ton of debug outputs at each point where you update row and col and where you update the robots members. That should give you a clue to where it's going wrong.