Skip to content

Commit fdc9f42

Browse files
authored
Merge pull request #5 from codeficct/development
Finish project
2 parents 867978b + caf0490 commit fdc9f42

File tree

11 files changed

+334
-43
lines changed

11 files changed

+334
-43
lines changed

MatchingGame.Android/MatchingGame.Android.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,8 @@
220220
<ItemGroup>
221221
<AndroidResource Include="Resources\drawable\checked.png" />
222222
</ItemGroup>
223+
<ItemGroup>
224+
<AndroidResource Include="Resources\drawable\defaultcard.png" />
225+
</ItemGroup>
223226
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
224227
</Project>
41.1 KB
Loading

MatchingGame/Clases/GameSetting.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Text;
4+
using System.ComponentModel;
45

56
namespace MatchingGame.Clases
67
{
7-
internal class GameSetting
8+
public class GameSetting : INotifyPropertyChanged
89
{
9-
private int level;
10+
public event PropertyChangedEventHandler PropertyChanged;
1011

11-
GameSetting()
12+
private static int level = 0;
13+
14+
public int Level
15+
{
16+
get => level;
17+
set
18+
{
19+
if (level == value)
20+
return;
21+
level = value;
22+
onPropertyChanged(nameof(Level));
23+
onPropertyChanged(nameof(GetLevel));
24+
}
25+
}
26+
27+
public int GetLevel => level;
28+
29+
void onPropertyChanged(string name)
1230
{
13-
level = 0;
31+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
1432
}
33+
1534
}
1635
}

MatchingGame/Clases/Matrix.cs

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
using System.Text;
55
using Xamarin.Forms;
66
using System.Threading.Tasks;
7+
using System.Text.RegularExpressions;
8+
using System.Linq;
9+
using MatchingGame.Vistas.GamePage;
10+
using MatchingGame.Vistas;
711

812
namespace 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
}

MatchingGame/MatchingGame.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@
2525
<EmbeddedResource Update="Vistas\HomePage\Home.xaml">
2626
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
2727
</EmbeddedResource>
28+
<EmbeddedResource Update="Vistas\VictoryPage.xaml">
29+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
30+
</EmbeddedResource>
2831
</ItemGroup>
2932
</Project>

MatchingGame/Vistas/GamePage/Cards.xaml.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,28 @@ namespace MatchingGame.Vistas.GamePage
1414
[XamlCompilation(XamlCompilationOptions.Compile)]
1515
public partial class Cards : ContentView
1616
{
17-
Matrix m1;
18-
Grid grid1 = new Grid
19-
{
20-
VerticalOptions = LayoutOptions.FillAndExpand,
21-
HorizontalOptions = LayoutOptions.FillAndExpand,
22-
};
17+
//Matrix m1;
18+
//Grid grid1 = new Grid
19+
//{
20+
// VerticalOptions = LayoutOptions.FillAndExpand,
21+
// HorizontalOptions = LayoutOptions.FillAndExpand,
22+
//};
23+
2324
public Cards()
2425
{
25-
grid1.RowDefinitions.Add(new RowDefinition());
26-
grid1.RowDefinitions.Add(new RowDefinition());
27-
grid1.RowDefinitions.Add(new RowDefinition());
26+
//grid1.RowDefinitions.Add(new RowDefinition());
27+
//grid1.RowDefinitions.Add(new RowDefinition());
28+
//grid1.RowDefinitions.Add(new RowDefinition());
2829

29-
grid1.ColumnDefinitions.Add(new ColumnDefinition());
30-
grid1.ColumnDefinitions.Add(new ColumnDefinition());
30+
//grid1.ColumnDefinitions.Add(new ColumnDefinition());
31+
//grid1.ColumnDefinitions.Add(new ColumnDefinition());
3132

32-
m1 = new Matrix();
33-
m1.SetData(3, 2, 1);
34-
m1.GenerateCards(ref grid1);
33+
//m1 = new Matrix();
34+
//m1.SetData(3, 2, 1);
35+
//m1.GenerateCards(ref grid1);
3536

36-
Content = grid1;
37+
//Content = grid1;
3738
InitializeComponent();
3839
}
39-
40-
//public void generateCard(ref Grid grid)
41-
//{
42-
// m1.SetData(3, 2, 1);
43-
// m1.GenerateCards(ref grid1);
44-
//}
4540
}
4641
}

MatchingGame/Vistas/GamePage/Game.xaml

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,42 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
44
x:Class="MatchingGame.Vistas.GamePage.Game"
55
NavigationPage.HasNavigationBar="False"
6-
xmlns:vistas="clr-namespace:MatchingGame.Vistas.GamePage">
6+
xmlns:vistas="clr-namespace:MatchingGame.Vistas.GamePage" xmlns:local="clr-namespace:MatchingGame.Clases">
7+
<ContentPage.BindingContext>
8+
<local:GameSetting />
9+
</ContentPage.BindingContext>
710
<ContentPage.Content>
811
<Grid BackgroundColor="#140429" RowDefinitions="148,*,92">
912
<StackLayout Orientation="Vertical" Padding="0,20" HorizontalOptions="Center" VerticalOptions="Start">
10-
<Frame
11-
BackgroundColor="Aquamarine"
13+
<!-- <Frame
14+
BackgroundColor="#a95aad"
1215
CornerRadius="8"
1316
Padding="20,14"
1417
WidthRequest="150"
1518
HeightRequest="80"
1619
VerticalOptions="Center"
1720
HorizontalOptions="Center"
18-
HasShadow="True"
21+
BorderColor="#4d1e50"
1922
>
20-
<Image Source="_1" Aspect="AspectFit" />
21-
</Frame>
23+
<Image Source="defaultcard" x:Name="MatchImageRandom" Aspect="AspectFit" />
24+
</Frame> -->
25+
<ImageButton
26+
WidthRequest="230"
27+
BackgroundColor="#a95aad"
28+
HeightRequest="120"
29+
Padding="0,10"
30+
VerticalOptions="Center"
31+
HorizontalOptions="Center"
32+
CornerRadius="24"
33+
BorderWidth="4"
34+
BorderColor="#4d1e50"
35+
Source="defaultcard"
36+
x:Name="MatchImageRandom"
37+
Aspect="AspectFit" />
2238
</StackLayout>
2339

2440
<StackLayout Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="10,0">
25-
<vistas:Cards />
41+
<vistas:Cards x:Name="CardContainer" />
2642
</StackLayout>
2743

2844
<Frame Grid.Row="2" BackgroundColor="Transparent">
@@ -45,6 +61,8 @@
4561
FontAttributes="Bold"
4662
BorderColor="#0e72bd"
4763
BorderWidth="4"
64+
x:Name="playGame"
65+
Clicked="playGame_Clicked"
4866
>
4967
<Button.Background>
5068
<LinearGradientBrush>

0 commit comments

Comments
 (0)