Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 131,526 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,008 people online right now. Registration is fast and FREE... Join Now!




Rowwise user input in Mastermind game

 
Reply to this topicStart new topic

Rowwise user input in Mastermind game

Zeddicus
post 9 Oct, 2008 - 01:19 AM
Post #1


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


I'm working on a mastermind game and have come to a point where I wonder how to implement the part of the game where the user only gets to fill in the colors for one row at a time.

The solution I have for the moment is posted below and you will instantly realize why it is not acceptable. blink.gif currentButton comes as sender. "b1", "b2" etc points to the row which was assigned to the name property at the creation of the buttons.

CODE


if (currentButton.Name.StartsWith("b0")
            {
              currentButton.BackColor = panel2.BackColor;
            }

if (currentButton.Name.StartsWith("b1") && currentButtonSet[0].BackColor != Color.Black && currentButtonSet[1].BackColor != Color.Black && currentButtonSet[2].BackColor != Color.Black &&  currentButtonSet[3].BackColor != Color.Black)
            {
                currentButton.BackColor = panel2.BackColor;
            }

            if (currentButton.Name.StartsWith("b2") && currentButtonSet[4].BackColor != Color.Black && currentButtonSet[5].BackColor != Color.Black && currentButtonSet[6].BackColor != Color.Black &&  currentButtonSet[7].BackColor != Color.Black)
            {
                currentButton.BackColor = panel2.BackColor;
            }

            if (currentButton.Name.StartsWith("b3") && currentButtonSet[8].BackColor != Color.Black && currentButtonSet[9].BackColor != Color.Black && currentButtonSet[10].BackColor != Color.Black &&  currentButtonSet[11].BackColor != Color.Black)
            {
                currentButton.BackColor = panel2.BackColor;
            }

            if (currentButton.Name.StartsWith("b4") && currentButtonSet[12].BackColor != Color.Black && currentButtonSet[13].BackColor != Color.Black && currentButtonSet[14].BackColor != Color.Black &&  currentButtonSet[15].BackColor != Color.Black)
            {
                currentButton.BackColor = panel2.BackColor;
            }

            if (currentButton.Name.StartsWith("b5") && currentButtonSet[16].BackColor != Color.Black && currentButtonSet[17].BackColor != Color.Black && currentButtonSet[18].BackColor != Color.Black &&  currentButtonSet[19].BackColor != Color.Black)
            {
                currentButton.BackColor = panel2.BackColor;
            }

            if (currentButton.Name.StartsWith("b6") && currentButtonSet[20].BackColor != Color.Black && currentButtonSet[21].BackColor != Color.Black && currentButtonSet[22].BackColor != Color.Black &&  currentButtonSet[23].BackColor != Color.Black)
            {
                currentButton.BackColor = panel2.BackColor;
            }

            if (currentButton.Name.StartsWith("b7") && currentButtonSet[24].BackColor != Color.Black && currentButtonSet[25].BackColor != Color.Black && currentButtonSet[26]BackColor != Color.Black &&  currentButtonSet[27].BackColor != Color.Black)
            {
                currentButton.BackColor = panel2.BackColor;
            }



Any suggestions on how I could implement the "row for row" part of the game in a more code effiecient way? Should I use another type than an array for instance, like ArrayList or Hashtable to make it easier?

Thanks.

This post has been edited by Zeddicus: 9 Oct, 2008 - 03:39 AM
User is offlineProfile CardPM

Go to the top of the page


baavgai
post 9 Oct, 2008 - 05:40 AM
Post #2


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,945



Thanked 94 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


I'm not sure what you're looking to improve. Just make the thing more concise?

There's something like this:
csharp

bool hasNoBlack(int firstIndex) {
for(int i=0; i<4; i++) {
if (currentButtonSet[firstIndex+i].BackColor == Color.Black) {
return false;
}
}
return true;
}

if (currentButton.Name.StartsWith("b1")) {
currentButton.BackColor = panel2.BackColor;
} else if (currentButton.Name.StartsWith("b2")) {
if (hasNoBlack(4)) { currentButton.BackColor = panel2.BackColor; }
} else if (currentButton.Name.StartsWith("b3")) {
//...


Taken a little farther, you can do this (assuming that first line is a bug):
csharp

int setNum = 0;
for(b=1; b<8; b++) {
if (currentButton.Name.StartsWith("b" + cool.gif) {
bool noBlack = true;
for(int i=setNum; i<setNum+4 && noBlack; i++) {
if (currentButtonSet[i].BackColor == Color.Black) {
noBlack = false;
}
}
if (noBlack) { currentButton.BackColor = panel2.BackColor; }
break;
}
setNum += 4;
}


I couldn't test this code, but it looks right. Hope this helps.
User is offlineProfile CardPM

Go to the top of the page

Zeddicus
post 11 Oct, 2008 - 06:57 PM
Post #3


New D.I.C Head

*
Joined: 14 Jul, 2008
Posts: 41


My Contributions


Thank you so much. Nice. Exactly what I was looking for. smile.gif

This post has been edited by Zeddicus: 11 Oct, 2008 - 06:58 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/20/08 01:16AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month