Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Help with 3X3 Latin Square

$
0
0
I am having trouble with my code printing out the latin squares. We are supposed to create a program that will print out all the possible Latin squares for a 3X3 2-D array. I think my code works but when I try it, it only prints out the blank strOut. Any help would be appreciated. Thanks

   public partial class Form1 : Form
    {
        const int   ciRows = 3, ciCols = 3;
        int[,]      aiLatin;

        public Form1()
        {
            InitializeComponent();
            aiLatin = new int[ciRows, ciCols];
        }       

        private string PrintSquare(ref int[,] aiLatin)
        {
            string  strOut = "";
            int     i, j;

            for (i = 0; i < ciRows; i++)
            {
                for (j = 0; j < ciCols; j++)
                {
                    strOut += aiLatin[i, j] + "\t";
                }
                strOut += "\r\n";
            }
            return strOut;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            bool    bRow1 = true, bRow2 = true, bRow3 = true;
            int     i, j;
            string  strOut = "blank";

            for (i = 0; i < ciRows; i++)
                for (j = 0; j < ciCols; j++)
                    aiLatin[i, j] = 1;

                bRow1 = true;
                bRow2 = true;
                bRow3 = true;

                while (bRow1)
                {
                    bRow1 = UpdateRow(aiLatin, 0);
                    while (bRow2)
                    {
                        bRow2 = UpdateRow(aiLatin, 1);
                        while (bRow3)
                        {
                            bRow3 = UpdateRow(aiLatin, 2);
                            if (ValidSquare(aiLatin))
                                strOut += PrintSquare(ref aiLatin);
                        }
                        bRow3 = true;
                    }
                    bRow2 = true;
                }
            txtOutput.Text = strOut;

        }

        //returns a true if can update again. otherwise false
        //updates to the next possible row
        private bool UpdateRow(int[,] aiLatin, int iRow)
        {
            bool bUpdateRow = true;
            /*
            if (bUpdateRow)
            {
                if (aiLatin[iRow, 2] < 3)
                    aiLatin[iRow, 2]++;

                else
                {
                    aiLatin[iRow, 2] = 1;
                    if (aiLatin[iRow, 1] < 3)
                        aiLatin[iRow, 1]++;

                    else
                    {
                        aiLatin[iRow, 1] = 1;
                        if (aiLatin[iRow, 0] < 3)
                            aiLatin[iRow, 0]++;

                        else
                            aiLatin[iRow, 0] = 1;

                    }
                }
            }

            else
                bUpdateRow = false;

            return bUpdateRow;*/

            int j;

            for (j = 2; j > -1; j--)
            {
                if (aiLatin[iRow, j] == 3)
                {
                    aiLatin[iRow, j] = 1;
                    if (j == 0)
                    {
                        return bUpdateRow = false;
                    }
                }
                else
                {
                    aiLatin[iRow, j] += 1;
                     return bUpdateRow = true;
                }
            }
            return bUpdateRow;           
        }

        //returns true if valid latin square
        private bool ValidSquare(int[,] aiLatin)
        {
            //check each row to see if 2 "spots" are same
            int i, j, iTemp;

            for (i = 1; i < 3; i++)
            {
                for (j = 1; j < 3; j++)
                {
                    iTemp = aiLatin[i, 0];

                    if (iTemp == aiLatin[i, j])
                        return false;

                    else if ( j == 1 )
                        {
				            if ( aiLatin[ i, j ] == aiLatin[ i, 1 ] )
					            return false;

				            else if (aiLatin[ i, j ] == aiLatin[ i, 2 ] )
					            return false;

                        }
			        else if ( j == 2 )
				        if ( aiLatin[ i, j ] == aiLatin[ i, 2 ] )
					        return false;
		        }
	        }             
            return true;
        }
        
    }
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>