C # simple implementation code of the anti-password protection card function

Source: Internet
Author: User

However, I wrote it rashly. The code structure is not very good and it does not reflect the idea of OOP. I will refactor it in the past few days.

First, send the Code:

Copy codeThe Code is as follows: public class MatrixCardManager
{
Public static int [,] ReadMatrixCardFromString (string matrixStr)
{
Int [,] arr1 = new int [5, 5];
Int [] tempArr = new int [25];
Int k = 0;
String [] tempArrStr = matrixStr. Split (',');
For (int I = 0; I <tempArr. Length; I ++)
{
TempArr [I] = Convert. ToInt32 (tempArrStr [I]);
}
For (int I = 0; I <5; I ++)
{
For (int j = 0; j <5; j ++)
{
Arr1 [I, j] = tempArr [k];
K ++;
}
}
Return arr1;
}

Public static string SaveMatrixIntoString (int [,] arr)
{
String matrixStr = String. Empty;
Int [] lineArr = new int [25];
Int k = 0;
For (int I = 0; I <5; I ++)
{
For (int j = 0; j <5; j ++)
{
LineArr [k] = arr [I, j];
K ++;
}
}
For (int I = 0; I <lineArr. Length; I ++)
{
MatrixStr + = lineArr [I];
If (I <24)
{
MatrixStr + = ",";
}
}
Return matrixStr;
}

Public static void PrintMatrix (int [,] arr)
{
Console. WriteLine ("| A \ tB \ tC \ tD \ tE ");
Console. WriteLine ("-------------------------------------------");
For (int k = 0; k <5; k ++)
{
Console. Write (k + "| ");
For (int l = 0; l <5; l ++)
{
Console. Write (arr [k, l] + "\ t ");
}
Console. WriteLine ();
}
}

Public static int [,] GenerateRandomMatrix ()
{
Random r = new Random ();
Int [,] arr = new int [5, 5];
For (int I = 0; I <5; I ++)
{
For (int j = 0; j <5; j ++)
{
Arr [I, j] = r. Next (0,100 );
}
}
Return arr;
}

Public static char GetColCode (int colIndex)
{
Char colCode = '-';
Switch (colIndex)
{
Case 0:
ColCode = 'a ';
Break;
Case 1:
ColCode = 'B ';
Break;
Case 2:
ColCode = 'C ';
Break;
Case 3:
ColCode = 'D ';
Break;
Case 4:
ColCode = 'E ';
Break;
Default:
Break;
}
Return colCode;
}

Public static bool Validate (int [,] arr, int colIndex1, int rowIndex1, int colIndex2, int rowIndex2, int colIndex3, int rowIndex3, string userInput, bool validFlag)
{
Try
{
String [] inputArr = userInput. Split (',');

Bool OK0 = arr [rowIndex1, colIndex1] = Convert. ToInt32 (inputArr [0]);
Bool OK1 = arr [rowIndex2, colIndex2] = Convert. ToInt32 (inputArr [1]);
Bool OK2 = arr [rowIndex3, colIndex3] = Convert. ToInt32 (inputArr [2]);

If (OK0 & OK1 & OK2)
{
ValidFlag = true;
}
Else
{
ValidFlag = false;
}
}
Catch (Exception)
{
Console. WriteLine ("Oh ,**! ");
}
Return validFlag;
}
}

Call:

Copy codeThe Code is as follows: static void Main (string [] args)
{
Console. WriteLine ("Generate and Print Matrix Card: \ n ");
Int [,] arr = MatrixCardManager. GenerateRandomMatrix ();
MatrixCardManager. PrintMatrix (arr );
Console. WriteLine ("\ n ");

Console. WriteLine ("Save Matrix Card into string for storage: \ n ");
String matrixStr = MatrixCardManager. SaveMatrixIntoString (arr );
Console. WriteLine (matrixStr );
Console. WriteLine ("\ n ");

Console. WriteLine ("Read Matrix Card from string: \ n ");
Int [,] arr1 = MatrixCardManager. ReadMatrixCardFromString (matrixStr );
MatrixCardManager. PrintMatrix (arr1 );
Console. WriteLine ("\ n ");

Console. WriteLine ("Matrix Card Validation: \ n ");
Random r = new Random ();

Int colIndex1 = r. Next (0, 4 );
Int rowIndex1 = r. Next (0, 4 );
Char colCode1 = MatrixCardManager. GetColCode (colIndex1 );

Int colIndex2 = r. Next (0, 4 );
Int rowIndex2 = r. Next (0, 4 );
Char colCode2 = MatrixCardManager. GetColCode (colIndex2 );

Int colIndex3 = r. Next (0, 4 );
Int rowIndex3 = r. Next (0, 4 );
Char colCode3 = MatrixCardManager. GetColCode (colIndex3 );

Console. writeLine ("Please Input Card Number At {0} {1}, {2} {3}, {4} {5}: \ n", colCode1, rowIndex1, colCode2, rowIndex2, colCode3, rowIndex3 );

String userInput = Console. ReadLine ();

Bool validFlag = false;
ValidFlag = MatrixCardManager. Validate (arr, colIndex1, rowIndex1, colIndex2, rowIndex2, colIndex3, rowIndex3, userInput, validFlag );
If (validFlag)
{
Console. WriteLine ("All input are correct! ");
}
Else
{
Console. WriteLine ("Sorry, your input were wrong! ");
}
Console. ReadKey ();
}

Effect:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.