Skip to content

Commit

Permalink
WordPuzzle sample (#437)
Browse files Browse the repository at this point in the history
* New sample.

* Fix Win support.

* Use conditional compilation.

* Fixes.

* Recreated in VS.

* Add readme.
  • Loading branch information
davidbritch authored Feb 19, 2024
1 parent 8ee3ffe commit e75fc09
Show file tree
Hide file tree
Showing 39 changed files with 1,341 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 8.0/Apps/WordPuzzle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
name: .NET MAUI - Word Puzzle
description: "Word Puzzle is a variation of the classic 14-15 puzzle, written using .NET MAUI."
page_type: sample
languages:
- csharp
- xaml
products:
- dotnet-maui
urlFragment: apps-wordpuzzle
---

# Word Puzzle

This sample is a variation of the classic 15 puzzle. On the early Mac, the 15 puzzle was called PUZZLE. In early Windows, it was the only sample for Microsoft Pascal for Windows 1.0, where it was called MUZZLE (for "Microsoft Puzzle"). This is the .NET MAUI version.

![Word Puzzle app screenshot](Screenshots/WordPuzzle-randomized.png "Word Puzzle app screenshot")

After pressing the **Randomize** button, just tap a tile to move it into an empty position. You can *tap* any tile in the row or column of the empty position to move one, two, or three tiles at once. Use the numbers in the lower-right corner of each tile as a guide.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions 8.0/Apps/WordPuzzle/WordPuzzle.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34607.119
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WordPuzzle", "WordPuzzle\WordPuzzle.csproj", "{ABAD6B8E-A804-4008-AF5B-EBBFFF1FAAFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ABAD6B8E-A804-4008-AF5B-EBBFFF1FAAFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ABAD6B8E-A804-4008-AF5B-EBBFFF1FAAFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABAD6B8E-A804-4008-AF5B-EBBFFF1FAAFE}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{ABAD6B8E-A804-4008-AF5B-EBBFFF1FAAFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABAD6B8E-A804-4008-AF5B-EBBFFF1FAAFE}.Release|Any CPU.Build.0 = Release|Any CPU
{ABAD6B8E-A804-4008-AF5B-EBBFFF1FAAFE}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {75A834AF-1866-46F9-852F-5552FC651EC4}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions 8.0/Apps/WordPuzzle/WordPuzzle/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:WordPuzzle"
x:Class="WordPuzzle.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
12 changes: 12 additions & 0 deletions 8.0/Apps/WordPuzzle/WordPuzzle/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace WordPuzzle
{
public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
}
15 changes: 15 additions & 0 deletions 8.0/Apps/WordPuzzle/WordPuzzle/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="WordPuzzle.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:WordPuzzle"
Shell.FlyoutBehavior="Disabled"
Title="WordPuzzle">

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />

</Shell>
10 changes: 10 additions & 0 deletions 8.0/Apps/WordPuzzle/WordPuzzle/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace WordPuzzle
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
}
75 changes: 75 additions & 0 deletions 8.0/Apps/WordPuzzle/WordPuzzle/GameSquare.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Microsoft.Maui.Controls.Shapes;

namespace WordPuzzle
{
class GameSquare : ContentView
{
Label label;
string normText, winText;

// Retain current Row and Col position.
public int Index { get; private set; }
public int Row { get; set; }
public int Col { get; set; }

public GameSquare(char normChar, char winChar, int index)
{
this.Index = index;
this.normText = normChar.ToString();
this.winText = winChar.ToString();

// A Border surrounding two Labels.
label = new Label
{
Text = this.normText,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};

#if MACCATALYST || WINDOWS
double fontSize = 14;
#elif ANDROID || IOS
double fontSize = 10;
#endif

Label tinyLabel = new Label
{
Text = (index + 1).ToString(),
FontSize = fontSize,
HorizontalOptions = LayoutOptions.End,
VerticalOptions = LayoutOptions.End
};

this.Padding = new Thickness(5);
this.Content = new Border
{
Stroke = Colors.Black,
StrokeThickness = 1,
StrokeShape = new RoundRectangle
{
CornerRadius = new CornerRadius(5)
},
Padding = new Thickness(5, 10, 5, 0),
Content = new VerticalStackLayout
{
label, tinyLabel
}
};
}

public async Task AnimateWinAsync(bool isReverse)
{
uint length = 150;
await Task.WhenAll(this.ScaleTo(3, length), this.RotateTo(180, length));
label.Text = isReverse ? normText : winText;
await Task.WhenAll(this.ScaleTo(1, length), this.RotateTo(360, length));
this.Rotation = 0;
}

public void SetLabelFont(double fontSize, FontAttributes attributes)
{
label.FontSize = fontSize;
label.FontAttributes = attributes;
}
}
}
25 changes: 25 additions & 0 deletions 8.0/Apps/WordPuzzle/WordPuzzle/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="WordPuzzle.MainPage"
Title="Solve the puzzle to win!">
<StackLayout x:Name="stackLayout"
Spacing="6"
Margin="5"
SizeChanged="OnStackSizeChanged">
<VerticalStackLayout VerticalOptions="Fill"
HorizontalOptions="Fill">
<Button x:Name="randomizeButton"
Text="Randomize"
Clicked="OnRandomizeButtonClicked"
HorizontalOptions="Center"
VerticalOptions="Center" />
<Label x:Name="timeLabel"
FontSize="26"
FontAttributes="Bold"
HorizontalOptions="Center"
VerticalOptions="Center" />
</VerticalStackLayout>
<AbsoluteLayout x:Name="absoluteLayout" />
</StackLayout>
</ContentPage>
Loading

0 comments on commit e75fc09

Please sign in to comment.