Need help improving solution for code challenge – Checkerboard pattern.

So I have been coding for a while and have built a few applications most < 500 lines of code mostly command line. I recently wrote my most in depth application a 3000+ line windows form tool that grants temporary admin rights on remote machines. However I have come to realize I need to work on my logic/algorithms. With this in mind I have been focusing on solving simple logic puzzles I find on the web. However in my recent solution I have broken a cardinal rule – "Don't repeat yourself". Just looking for suggestions on how to improve my solution below.

 

I would like to combine the two nested IF statements together. This was my original plan but after many attempts I took the easy way out and added another IF statement. I also wonder if I can omit the use of the Bool swap.

 

Below is my working solution:

 

Code challenge # 6 found @ http://ift.tt/1A6v0DQ

 

using System;
namespace checkerboard
{
class Program
{ static void Main(string[] args)
{
string w = "white", b = "black", d = "-";
string[,] arr = new string[16, 8];
string temp = string.Empty;
bool swap = true;

 for (int row = 0; row < 8; row++) { for (int col = 0; col < 16; col++) { if ((col + 1) % 2 == 0) //Insert '-' every other char { temp = d; } else { if ((row + 1) % 2 != 0) //Every odd row { if (swap) { temp = w; swap = false; } else { temp = b; swap = true; } } if ((row + 1) % 2 == 0) //Every even row { if (swap) { temp = b; swap = false; } else { temp = w; swap = true; } } /* Original plan not working if ((i + 1) % 2 != 0 & swap) { temp = w; swap = false; } else //Need Else IF logic { temp = b; swap = true; } */ } arr[col, row] = temp; Console.Write(temp); } Console.Write("\n"); } Console.ReadLine(); } } 

}

by xxsiriusxburnxx via /r/csharp

Leave a Reply