44using System . Text ;
55using Xamarin . Forms ;
66using System . Threading . Tasks ;
7+ using System . Text . RegularExpressions ;
8+ using System . Linq ;
9+ using MatchingGame . Vistas . GamePage ;
10+ using MatchingGame . Vistas ;
711
812namespace MatchingGame . Clases
913{
10- internal class Matrix
14+ public class Matrix
1115 {
1216 private int rows , columns ;
13- private string match ;
17+ public string match ;
18+ public int Level ;
1419 private string [ , ] m ;
1520
1621 public Matrix ( )
@@ -22,40 +27,103 @@ public void initializeMatrix(int r, int c)
2227 {
2328 rows = r ; columns = c ;
2429 m = new string [ r , c ] ;
25- match = "_1" ;
30+ match = "defaultcard" ;
31+ }
32+
33+ public void SetLevel ( int level )
34+ {
35+ Level = level ;
36+ }
37+
38+ public void SetMatchImage ( ref ImageButton MatchImageRandom )
39+ {
40+ Random numRnd = new Random ( ) ;
41+ match = m [ numRnd . Next ( 0 , rows - 1 ) , numRnd . Next ( 0 , columns - 1 ) ] ;
42+ MatchImageRandom . Source = match ;
2643 }
2744
2845 public void SetData ( int r , int c , int min )
2946 {
47+ Random numRandom = new Random ( ) ;
3048 initializeMatrix ( r , c ) ;
3149 int countImg = min ;
3250 for ( int r1 = 0 ; r1 < rows ; r1 ++ )
3351 for ( int c1 = 0 ; c1 < columns ; c1 ++ )
3452 {
35- m [ r1 , c1 ] = "_" + countImg ;
53+ m [ r1 , c1 ] = "_" + numRandom . Next ( min , 34 ) ;
3654 countImg ++ ;
3755 }
3856 }
3957
58+ public void GenerateDefaultCards ( ref Grid grid )
59+ {
60+ for ( int r = 0 ; r < rows ; r ++ )
61+ for ( int c = 0 ; c < columns ; c ++ )
62+ {
63+ // BoxView boxview = new BoxView { BackgroundColor = Color.FromHex("#7b5b9c"), CornerRadius = 16, HeightRequest = };
64+ ImageButton imageButton = new ImageButton { Source = "question" , Padding = new Thickness ( 0 , 30 ) , Aspect = Aspect . AspectFit , BackgroundColor = Color . FromHex ( "#7b5b9c" ) , BorderWidth = 2 , BorderColor = Color . FromHex ( "#aaa" ) , CornerRadius = 16 } ;
65+
66+ grid . Children . Add ( imageButton , c , r ) ;
67+ }
68+ }
69+
4070 public void GenerateCards ( ref Grid gridContent )
4171 {
4272 for ( int r = 0 ; r < rows ; r ++ )
4373 for ( int c = 0 ; c < columns ; c ++ )
4474 {
45- ImageButton imageButton = new ImageButton { Source = m [ r , c ] , Aspect = Aspect . AspectFit , BackgroundColor = Color . FromHex ( "#7b5b9c" ) , BorderWidth = 2 , BorderColor = Color . FromHex ( "#aaa" ) , CornerRadius = 16 , ClassId = m [ r , c ] , } ;
75+ ImageButton imageButton = new ImageButton { Source = m [ r , c ] , Aspect = Aspect . AspectFit , BackgroundColor = Color . FromHex ( "#7b5b9c" ) , BorderWidth = 2 , BorderColor = Color . FromHex ( "#aaa" ) , CornerRadius = 16 , ClassId = m [ r , c ] } ;
4676 gridContent . Children . Add ( imageButton , c , r ) ;
4777 imageButton . Clicked += async ( sender , args ) =>
4878 {
79+ // ((ImageButton)sender).Source = ((ImageButton)sender).ClassId;
80+ imageButton . Source = ( ( ImageButton ) sender ) . ClassId ;
4981 this . IsMatch ( imageButton . ClassId ) ;
82+ await Task . Delay ( 1000 ) ;
83+ ( ( ImageButton ) sender ) . Source = "question" ;
5084 } ;
5185 }
5286 }
5387
54- async public void IsMatch ( string id )
88+ public async void IsMatch ( string id )
5589 {
5690 if ( id == match )
5791 {
58- await App . Current . MainPage . DisplayAlert ( "Is Matching!" , id , "Ok" ) ;
92+ await Task . Delay ( 500 ) ;
93+ // await App.Current.MainPage.DisplayAlert("Is Matching!", id, "Ok");
94+ await App . Current . MainPage . Navigation . PushAsync ( new VictoryPage ( Level + 1 ) ) ;
95+ }
96+ }
97+
98+ public void SwapItems ( int r1 , int c1 , int r2 , int c2 )
99+ {
100+ string aux = m [ r1 , c1 ] ;
101+ m [ r1 , c1 ] = m [ r2 , c2 ] ;
102+ m [ r2 , c2 ] = aux ;
103+ }
104+
105+ public void Dispersion ( )
106+ {
107+ Random numRandom = new Random ( ) ;
108+ int r1 , c1 , r2 , c2 ;
109+ for ( int r = 0 ; r < Math . Sqrt ( rows ) ; r ++ )
110+ {
111+ for ( int c = 0 ; c < Math . Sqrt ( columns ) ; c ++ )
112+ {
113+ r1 = numRandom . Next ( 0 , rows ) ;
114+ c1 = numRandom . Next ( 0 , columns ) ;
115+ r2 = numRandom . Next ( 0 , rows ) ;
116+ c2 = numRandom . Next ( 0 , columns ) ;
117+ SwapItems ( r1 , c1 , r2 , c2 ) ;
118+ }
119+ }
120+ }
121+
122+ public void HideCards ( ref Grid grid )
123+ {
124+ foreach ( ImageButton item in grid . Children )
125+ {
126+ item . Source = "question" ;
59127 }
60128 }
61129 }
0 commit comments