School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,152 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,690 people online right now. Registration is fast and FREE... Join Now!




A better way?

 

A better way?, How can I code this more efficiently?

randomdane

6 Oct, 2009 - 08:55 PM
Post #1

New D.I.C Head
*

Joined: 4 Oct, 2009
Posts: 8

Hi, for my university work this week(we don't do your homework i hear you say..please read on) I had to create a program in MaSH that draws a checker board. I chose to draw one 8 squares by 8 squares. I have got the program working by using this code


CODE


/*
** file:    checkers.mash
** purpose: Draws a red and black checker board.
**          
*/

import graphics;

void main () {
   setFrameSize(200, 200);
   setFrameVisible(true);
}

void paintWindow() {
   setColor(255, 0, 0);
   fillRect(2, 2, 20, 20);
   setColor(0, 0, 0);
   fillRect(22, 2, 20, 20);
   setColor(255, 0, 0);
   fillRect(42, 2, 20, 20);
   setColor(0, 0, 0);
   fillRect(62, 2, 20, 20);
   setColor(255, 0, 0);
   fillRect(82, 2, 20, 20);
   setColor(0, 0, 0);
   fillRect(102, 2, 20, 20);
   setColor(255, 0, 0);
   fillRect(122, 2, 20, 20);
   setColor(0, 0, 0);
   fillRect(142, 2, 20, 20);
   //***********************************this is the end of the first row
   setColor(0, 0, 0);
   fillRect(2, 22, 20, 20);
   setColor(255, 0, 0);
   fillRect(22, 22, 20, 20);
   setColor(0, 0, 0);
   fillRect(42, 22, 20, 20);
   setColor(255, 0, 0);
   fillRect(62, 22, 20, 20);
   setColor(0, 0, 0);
   fillRect(82, 22, 20, 20);
   setColor(255, 0, 0);
   fillRect(102, 22, 20, 20);
   setColor(0, 0, 0);
   fillRect(122, 22, 20, 20);
   setColor(255, 0, 0);
   fillRect(142,22,20,20);
   //**********************************this is the end of the second row
   setColor(255, 0, 0);
   fillRect(2, 42, 20, 20);
   setColor(0, 0, 0);
   fillRect(22, 42, 20, 20);
   setColor(255, 0, 0);
   fillRect(42, 42, 20, 20);
   setColor(0, 0, 0);
   fillRect(62, 42, 20, 20);
   setColor(255, 0, 0);
   fillRect(82, 42, 20, 20);
   setColor(0, 0, 0);
   fillRect(102, 42, 20, 20);
   setColor(255, 0, 0);
   fillRect(122, 42, 20, 20);
   setColor(0, 0, 0);
   fillRect(142, 42, 20, 20);
   //*********************************this is the end of the third row
   setColor(0, 0, 0);
   fillRect(2, 62, 20, 20);
   setColor(255, 0, 0);
   fillRect(22, 62, 20, 20);
   setColor(0, 0, 0);
   fillRect(42, 62, 20, 20);
   setColor(255, 0, 0);
   fillRect(62, 62, 20, 20);
   setColor(0, 0, 0);
   fillRect(82, 62, 20, 20);
   setColor(255, 0, 0);
   fillRect(102,62, 20, 20);
   setColor(0, 0, 0);
   fillRect(122, 62, 20, 20);
   setColor(255, 0, 0);
   fillRect(142,62,20,20);
   //********************************this is the end of the 4th row
      setColor(255, 0, 0);
   fillRect(2, 82, 20, 20);
   setColor(0, 0, 0);
   fillRect(22, 82, 20, 20);
   setColor(255, 0, 0);
   fillRect(42, 82, 20, 20);
   setColor(0, 0, 0);
   fillRect(62, 82, 20, 20);
   setColor(255, 0, 0);
   fillRect(82, 82, 20, 20);
   setColor(0, 0, 0);
   fillRect(102, 82, 20, 20);
   setColor(255, 0, 0);
   fillRect(122, 82, 20, 20);
   setColor(0, 0, 0);
   fillRect(142, 82, 20, 20);
   //***********************************this is the end of the 5th row
   setColor(0, 0, 0);
   fillRect(2, 102, 20, 20);
   setColor(255, 0, 0);
   fillRect(22, 102, 20, 20);
   setColor(0, 0, 0);
   fillRect(42, 102, 20, 20);
   setColor(255, 0, 0);
   fillRect(62, 102, 20, 20);
   setColor(0, 0, 0);
   fillRect(82, 102, 20, 20);
   setColor(255, 0, 0);
   fillRect(102, 102, 20, 20);
   setColor(0, 0, 0);
   fillRect(122, 102, 20, 20);
   setColor(255, 0, 0);
   fillRect(142,102,20,20);
   //**********************************this is the end of the 6th row
   setColor(255, 0, 0);
   fillRect(2, 122, 20, 20);
   setColor(0, 0, 0);
   fillRect(22, 122, 20, 20);
   setColor(255, 0, 0);
   fillRect(42, 122, 20, 20);
   setColor(0, 0, 0);
   fillRect(62, 122, 20, 20);
   setColor(255, 0, 0);
   fillRect(82, 122, 20, 20);
   setColor(0, 0, 0);
   fillRect(102, 122, 20, 20);
   setColor(255, 0, 0);
   fillRect(122, 122, 20, 20);
   setColor(0, 0, 0);
   fillRect(142, 122, 20, 20);
   //*********************************this is the end of the 7th row
   setColor(0, 0, 0);
   fillRect(2, 142, 20, 20);
   setColor(255, 0, 0);
   fillRect(22, 142, 20, 20);
   setColor(0, 0, 0);
   fillRect(42, 142, 20, 20);
   setColor(255, 0, 0);
   fillRect(62, 142, 20, 20);
   setColor(0, 0, 0);
   fillRect(82, 142, 20, 20);
   setColor(255, 0, 0);
   fillRect(102,142, 20, 20);
   setColor(0, 0, 0);
   fillRect(122, 142, 20, 20);
   setColor(255, 0, 0);
   fillRect(142,142,20,20);
   //********************************this is the end of the 8th row
}



This program works, and draws the checker board. It is 150 lines of code though..

I know I need to create a formula or loop(or both) to read where i draw the first 2 squares ( 1 red, 1 black) then add the dimension (20) to the "x" axis for the first row then they "y" and "x" axis for the next rows.

I have no idea how to do this, I mean I know what I need to do but I have no idea how to code it.

Any feedback or help would be appreciated.

Thanks

Dane






User is offlineProfile CardPM
+Quote Post


AdamSpeight2008

RE: A Better Way?

6 Oct, 2009 - 11:40 PM
Post #2

The Bandido Coder
Group Icon

Joined: 29 May, 2008
Posts: 2,734



Thanked: 160 times
Dream Kudos: 3925
Expert In: vb.net, LINQ

My Contributions
I would do something like
CODE

Dim ox=10
Dim oy=10
dim s=10
Dim c
Dim colrs(1)={Red,Black}
For  x = 0 to 7
For y = 0 to 7
c=(x+y) mod 2
DrawFilledRectangle(clors(c),s*x+ox,s*y+oy,s,s)
Next y
Next x

It certainly not 150 line, more like 11 lines.
Ever that's big, when compared to 3 lines (of vb.net code)
CODE

Public Sub DrawCheckerBoard(ByRef g As Graphics,  ByVal ox As Single, ByVal oy As Single, ByVal s As Single, ByVal w As Single, ByVal ParamArray Colrs() As Brush)
For i As Integer=0 To (w*w)-1
  g.FillRectangle(Colrs(((i\w) + ( i Mod w)) Mod Colrs.Count), (i Mod w) * s + ox, (i \ w) * s + oy,  s,s)
Next i
End Sub

User is offlineProfile CardPM
+Quote Post

baavgai

RE: A Better Way?

7 Oct, 2009 - 05:04 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,351



Thanked: 412 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
First, what the hell is "MaSH"? Seriously; no one on the internet besides you seems to have heard of it.

For your code, I don't know if you can do loops or even variables. I'm hoping for variables.

CODE

import graphics;

void main () {
   setFrameSize(200, 200);
   setFrameVisible(true);
}


void paintRow(int x, int y) {
    fillRect(x, y, 20, 20);
    x = x + 40; fillRect(x, y, 20, 20);
    x = x + 40; fillRect(x, y, 20, 20);
    x = x + 40; fillRect(x, y, 20, 20);
}

void paintWindow() {
    int x = 2, y = 2;
    
    // fill all one color
    setColor(0, 0, 0);
    fillRect(x, y, 160, 160);
    
    setColor(255, 0, 0);
    
    paintRow(x, y);
    y = y + 20; paintRow(x+20, y);
    y = y + 20; paintRow(x, y);
    y = y + 20; paintRow(x+20, y);
    y = y + 20; paintRow(x, y);
    y = y + 20; paintRow(x+20, y);
    y = y + 20; paintRow(x, y);
    y = y + 20; paintRow(x+20, y);
}



This post has been edited by baavgai: 7 Oct, 2009 - 05:05 AM
User is online!Profile CardPM
+Quote Post

randomdane

RE: A Better Way?

7 Oct, 2009 - 10:27 PM
Post #4

New D.I.C Head
*

Joined: 4 Oct, 2009
Posts: 8

Hi, thanks for you replies.

Baavgai, MaSH is a language written by my lecturer Dr Andrew Rock. He described it to me as an easy way to learn the basics of Java without all of the extra stuff that makes it hard to learn for beginners.

I would post a link but that is against the rules. I'm sure a popular search engine would find my lecturer's homepage.

Your code made a lot of sense to me Baavgai, Adam i can sort of follow your code but I'm unfamiliar with that language and I'm also new so it is hard for me, but thanks.

Baavgai, we have variables and loops, the loops we have are

CODE

  do() while()   {
  }
  if()   {
  }else   {
  }
  for()   {
  }
  while()  {
  }


I cut and pasted your code, ran it and it worked brilliantly.

Another person in my class showed me their code which looks like this

CODE

    /*checkers.mash
** draw a checker board
*/


import graphics;  

   int n = 0;
   int m = 0;
   int column = 0;
  
void main () {
   setFrameSize(250, 250);
   setFrameVisible(true);
}

void blackRow () {
   for (int i = 0; i < 5; i = i + 1) {
      fillRect(n, column, 20, 20);
      n = n + 40;
   }
}

void redRow () {
   for (int k = 0; k < 5; k = k + 1) {
      fillRect(m, column, 20, 20);
      m = m + 40;
   }
}

void paintWindow() {
   n = 10;
   m = 30;
   column = 10;
   setColor(255, 0, 0);
   fillRect(10, 10, 200, 200);
   setColor(0, 0, 0);
   for (int j = 0; j < 5;j = j + 1) {
      blackRow ();
      column = column + 20;
      redRow ();
      column = column + 20;
      n = 10;
      m = 30;
   }
}


This works as well.

Thanks again
Dane.

This post has been edited by randomdane: 7 Oct, 2009 - 11:32 PM
User is offlineProfile CardPM
+Quote Post

baavgai

RE: A Better Way?

8 Oct, 2009 - 05:39 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,351



Thanked: 412 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Found him: http://www.cit.gu.edu.au/~arock/

So, rather than teaching something useful like Java, he's teaching his own pet project language? Charming.

Global are bad, something that Doc Rock's toy obfuscates. A better approach wouldn't use them. For instance, blackRow and redRow seem identical, but use globals rather than parameters.

CODE

import graphics;  

void main () {
    setFrameSize(250, 250);
    setFrameVisible(true);
}

void paintRow(int row, int col) {
    for (int i=0; i<5; i++) {
        fillRect(row, col, 20, 20);
        row += 40;
    }
}

void paintWindow() {
    setColor(255, 0, 0);
    fillRect(10, 10, 200, 200);
    
    setColor(0, 0, 0);
    for (int col=0; col<5; col++) {
        int column = 10 + (col*40);
        paintRow(row, column);
        paintRow(row+20, column+20);
    }
}


This could be cleaned up a little more, but given the "language" I shan't risk it.


This post has been edited by baavgai: 8 Oct, 2009 - 05:39 AM
User is online!Profile CardPM
+Quote Post

randomdane

RE: A Better Way?

8 Oct, 2009 - 01:39 PM
Post #6

New D.I.C Head
*

Joined: 4 Oct, 2009
Posts: 8

I think in the next couple of courses we get into Java or something more? He said this makes it easier to learn Java, but I have no idea as I dont' really know either??

I can follow this code very well, but there is no way I would have thought of something like that!! Well not yet anyway.


Thanks for all of your help, I have learned a lot from just following your code and seeing many different ways to write the same thing...Its very interesting, even if on the scheme of things it is simple code,(for you I'm guessing) not me.


Dane








User is offlineProfile CardPM
+Quote Post

Raynes

RE: A Better Way?

10 Oct, 2009 - 03:56 AM
Post #7

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
It's really not hard enough to learn Java itself to justify creating your own (dumbed down) language to help people learning. Especially since it's hard to get help for a language that basically doesn't exist.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:39PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month