Answer to Exercise 6

Not so easy, this one! The key to it is to fill a multidimensional array. The array is set up on the first line, in the code below. You then use a double for loop to access each position in the array. The inner for loop is where the action is. You use modulus to check if a row variable can be divided by 2. If it can, you again use modulus to check the col variable. If col mod 2 gives you an answer of 1 then add "black" to that array position. If col mod 2 gives you an answer of 0 then fill the array position with the word "white". You do the opposite if the row variable can't be divided by 2.

 

C# NET Answer

private void btnCheckerBoard_Click(object sender, EventArgs e)
{
	string[,] checker = new string[9, 9];

	for (int row = 1; row < 9; row++)
	{
		for (int col = 1; col < 9; col++)
	    {
	        if (row % 2 == 0)
	        {
				if (col % 2 == 1)
				{
		    		checker[row, col] = "black";
				}
				else
				{
		    		checker[row, col] = "white";
				}
	        }
	        else
	        {
				if (col % 2 == 1)
				{
		    		checker[row, col] = "white";
				}
				else
				{
		    		checker[row, col] = "black";
				}
	        }
	    }
	}

	//PRINT IT OUT
	string temp = "";
	for (int i = 1; i < 9; i++)
	{
		for (int j = 1; j < 9; j++)
		{
			temp = temp + checker[i, j] + "-";
		}

		listBox1.Items.Add(temp);
		temp = "";
	}

}

 

VB NET Answer

Private Sub btnChecker_Click(sender As Object, e As EventArgs) Handles btnChecker.Click

	Dim checker(9, 9) As String
	Dim row As Integer
	Dim col As Integer

	For row = 1 To 8
		For col = 1 To 8
		    If row Mod 2 = 0 Then
		        If col Mod 2 = 1 Then
					checker(row, col) = "black"
		        Else
					checker(row, col) = "white"
		        End If
		    Else
		        If col Mod 2 = 1 Then
					checker(row, col) = "white"
		        Else
					checker(row, col) = "black"
		        End If
		    End If
		Next
	Next

	'PRINT IT OUT
	Dim temp As String = ""

	For row = 1 To 8
		For col = 1 To 8
		    temp = temp & checker(row, col) & "-"
		Next

		ListBox1.Items.Add(temp)
		temp = ""
	Next
End Sub

 

Java Answer

public static void main(String[] args) {
	String[][] checker = new String[9][9];

    for (int i = 1; i < 9; i++)
    {
        for (int j = 1; j < 9; j++)
        {
            if (i % 2 == 0)
            {
                if (j % 2 == 1)
                {
                    checker[i][j] = "black";
                }
                else
                {
                    checker[i][j] = "white";
                }
            }
            else
            {
                if (j % 2 == 1)
                {
                    checker[i][j] = "white";
                }
                else
                {
                    checker[i][j] = "black";
                }
            }
        }
    }
    
	//PRINT IT OUT
    String temp = "";
    for (int i = 1; i < 9; i++)
    {
        for (int j = 1; j < 9; j++)
        {   
             temp = temp + checker[i][j] + "-";
        }

        System.out.println(temp);
        temp = "";
    }
}