-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from bricefriha/feature/play
Feature/play
- Loading branch information
Showing
16 changed files
with
779 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "" | ||
}; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.