Skip to content

Commit

Permalink
Merge pull request #2 from bricefriha/feature/play
Browse files Browse the repository at this point in the history
Feature/play
  • Loading branch information
bricefriha authored Jan 27, 2024
2 parents 0b91688 + 0041f81 commit 418752f
Show file tree
Hide file tree
Showing 16 changed files with 779 additions and 54 deletions.
4 changes: 4 additions & 0 deletions Wordster/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Wordster"
xmlns:conv="clr-namespace:Wordster.Tools.Converters"
x:Class="Wordster.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
<conv:IntegerToStringConverter x:Key="IntegerToStringConverter"/>

</ResourceDictionary>

</Application.Resources>
</Application>
2 changes: 1 addition & 1 deletion Wordster/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<ShellContent
Title="Home"
ContentTemplate="{DataTemplate views:MenuPage}"
Route="MainPage" />
Route="MenuPage" />

</Shell>
6 changes: 5 additions & 1 deletion Wordster/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
namespace Wordster
using Wordster.Views;

namespace Wordster
{
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();

Routing.RegisterRoute("gamePage", typeof(GamePage));
}
}
}
58 changes: 58 additions & 0 deletions Wordster/Models/Letter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wordster.Models
{
public class Letter : NotifiableModel
{
// Static variable to keep track of the index
private static int nextIndex = 0;

private string _value;

public string Value
{
get { return _value; }
set
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
private double _elevation;

public double Elevation
{
get { return _elevation; }
set
{
_elevation = value;
OnPropertyChanged(nameof(Elevation));
}
}
private Color _backgroundColour ;

public Color BackgroundColour
{
get { return _backgroundColour; }
set
{
_backgroundColour = value;
OnPropertyChanged(nameof(BackgroundColour));
}
}


public Letter()
{
Index = nextIndex;
nextIndex++;
}

// Auto-increment integer index property but overidable
public int Index { get; set; }
}
}
21 changes: 21 additions & 0 deletions Wordster/Models/NotifiableModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Wordster.Models
{
public class NotifiableModel : INotifyPropertyChanged
{

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
28 changes: 28 additions & 0 deletions Wordster/Models/Slot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wordster.Models
{
public class Slot : NotifiableModel
{
private ObservableCollection<Letter> _letters;

public ObservableCollection<Letter> Letters
{
get
{
return _letters;
}
set
{
_letters = value;
OnPropertyChanged(nameof(Letters));

}
}
}
}
1 change: 1 addition & 0 deletions Wordster/Resources/Styles/Colors.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

<!--In game-->
<Color x:Key="DefaultButtonBrush">#e3edf7</Color>
<Color x:Key="DarkButtonBrush">#9EA5AC</Color>
<Color x:Key="AlmostRightButtonBrush">#A7CBF6</Color>
<Color x:Key="NailedButtonBrush">#8fce00</Color>

Expand Down
45 changes: 45 additions & 0 deletions Wordster/Resources/Styles/Styles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,49 @@
<Setter Property="SelectedTabColor" Value="{AppThemeBinding Light={StaticResource Gray950}, Dark={StaticResource Gray200}}" />
</Style>

<x:Double x:Key="TitleFontSize">30</x:Double>
<x:Double x:Key="TitleTileSize">50</x:Double>
<x:Double x:Key="ButtonTileSize">48</x:Double>
<x:Double x:Key="ActionButtonWidth">70</x:Double>
<x:Double x:Key="ActionButtonHeight">50</x:Double>
<x:Double x:Key="SlotTileSize">70</x:Double>
<x:Double x:Key="TileRadius">10</x:Double>
<Style TargetType="NeoButton"
x:Key="TileButton">
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="Elevation" Value=".25" />
<Setter Property="CornerRadius" Value="{StaticResource TileRadius}" />
<Setter Property="WidthRequest" Value="{StaticResource TitleTileSize}" />
<Setter Property="HeightRequest" Value="{StaticResource TitleTileSize}" />
</Style>
<Style TargetType="NeoButton"
BasedOn="{StaticResource TileButton}"
x:Key="TileGameButton">
<Setter Property="Elevation" Value=".25" />
<Setter Property="Margin" Value="-5,2" />
<Setter Property="WidthRequest" Value="{StaticResource ButtonTileSize}" />
<Setter Property="HeightRequest" Value="{StaticResource SlotTileSize}" />
</Style>
<Style TargetType="NeoButton"
BasedOn="{StaticResource TileGameButton}"
x:Key="ActionGameButton">
<Setter Property="WidthRequest" Value="{StaticResource ActionButtonWidth}" />
</Style>
<Style TargetType="NeoButton"
x:Key="TileSlot">
<Setter Property="HorizontalOptions" Value="Center" />
<Setter Property="Elevation" Value=".10" />
<Setter Property="CornerRadius" Value="{StaticResource TileRadius}" />
<Setter Property="WidthRequest" Value="{StaticResource SlotTileSize}" />
<Setter Property="HeightRequest" Value="{StaticResource SlotTileSize}" />
</Style>
<Style TargetType="Label"
x:Key="TileText">
<Setter Property="TextColor" Value="Black" />
<Setter Property="FontSize" Value="{StaticResource TitleFontSize}" />
<Setter Property="FontFamily" Value="MouseMemoirs-Regular" />
<Setter Property="VerticalOptions" Value="Center" />
<Setter Property="HorizontalOptions" Value="Center" />
</Style>

</ResourceDictionary>
22 changes: 22 additions & 0 deletions Wordster/Tools/Converters/IntegerToStringConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Wordster.Tools.Converters
{
public class IntegerToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
161 changes: 161 additions & 0 deletions Wordster/ViewModels/GameViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using Microsoft.Maui.Controls;
using MvvmHelpers;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wordster.Models;

namespace Wordster.ViewModels
{
class GameViewModel : BaseViewModel
{
private const int characterCountMax = 5;
private const int slotCount = 6;
private ObservableCollection<Slot> _slots;
private int _currentLine = 0;

public ObservableCollection<Slot> Slots
{
get { return _slots; }
set
{
_slots = value;
OnPropertyChanged(nameof(Slots));
}
}
private Command<string> _addLetterCommand;

public Command<string> AddLetterCommand
{
get
{
return _addLetterCommand;
}
}
private Command<string> _removeLetterCommand;

public Command<string> RemoveLetterCommand
{
get
{
return _removeLetterCommand;
}
}
private Color _emptyColour ;
private Color _filledColour ;

public GameViewModel()
{
_slots = new ObservableCollection<Slot>();
GetResourceColours();
for (int i = 0; i < slotCount; i++)
{
Slot slot = new()
{
Letters = new ObservableCollection<Letter>()
};
for (int si = 0; si < characterCountMax; si++)
slot.Letters.Add(new Letter
{
BackgroundColour = _emptyColour,
Elevation = 0,
Value = ""
});

// Add another line
Slots.Add(slot);
}


_addLetterCommand = new Command<string>((character) =>
{
AddLetter(character);
});

_removeLetterCommand = new Command<string>((pos) =>
{
RemoveLetter(Convert.ToInt16(pos));
});
}

private void GetResourceColours()
{
App.Current.Resources.TryGetValue("DarkButtonBrush", out var emptycolorvalue);
App.Current.Resources.TryGetValue("DefaultButtonBrush", out var filledcolorvalue);
_emptyColour = emptycolorvalue as Color;
_filledColour = filledcolorvalue as Color;
}

/// <summary>
/// Add a letter at the end of the current row
/// </summary>
/// <param name="letter">Letter to add</param>
private void AddLetter(string letter)
{
ObservableCollection<Letter> letters = Slots[_currentLine].Letters;
int index = letters.IndexOf(letters.FirstOrDefault(l => string.IsNullOrEmpty(l.Value)));

if (index == -1)
return;

Slots[_currentLine].Letters[index].Value = letter;
Slots[_currentLine].Letters[index].BackgroundColour = _filledColour;
Slots[_currentLine].Letters[index].Elevation = .25;
}

/// <summary>
/// Remove a letter from the current line
/// </summary>
/// <param name="pos">position of the letter to delete. (-1 to remove the last letter)</param>
private void RemoveLetter(int pos)
{
ObservableCollection<Letter> letters = Slots[_currentLine].Letters;
int index = letters.IndexOf(letters.LastOrDefault(l => !string.IsNullOrEmpty(l.Value)));

bool isRegular = pos == -1;
if (isRegular)
pos = index;

if (!isRegular && !string.IsNullOrEmpty(letters[pos + 1].Value))
{
int nextSlotsCount = characterCountMax - pos;

for (int i = pos; i < characterCountMax; i++)
{
int nextIndex = i + 1;
Letter letter = nextIndex > letters.Count - 1 || nextSlotsCount < i ? new Letter
{
Index = i,
BackgroundColour = _emptyColour,
Elevation = 0,
Value = ""
} :
new Letter
{
Index = i,
BackgroundColour = letters[nextIndex].BackgroundColour,
Elevation = letters[nextIndex].Elevation,
Value = letters[nextIndex].Value
};

// Update the letter
Slots[_currentLine].Letters[i] = letter;
}
}
else
{
// Add empty slot
Slots[_currentLine].Letters[pos] = new Letter
{
Index = pos,
BackgroundColour = _emptyColour,
Elevation = 0,
Value = ""
};
}
}
}
}
Loading

0 comments on commit 418752f

Please sign in to comment.