评论

收藏

[Android] 使用Xamarin开发移动应用示例——数独游戏(七)添加新游戏

移动开发 移动开发 发布于:2022-02-08 10:55 | 阅读数:218 | 评论:0

项目代码可以从Github下载:https://github.com/zhenl/ZL.Shudu 。代码随项目进度更新。
现在我们增加添加新游戏的功能,创建一个页面,编辑初始局面,并保存到数据库。
我们首先了解一下Xamarin中页面如何跳转。首先,需要为跳转的页面增加路由,这需要在AppShell中增加下面的代码:
public AppShell()
    {
      InitializeComponent();
      Routing.RegisterRoute(nameof(GameEdit), typeof(GameEdit));
      Routing.RegisterRoute(nameof(GameList), typeof(GameList));
    }
GameEdit和GameList是两个页面,GameList中显示数据库中现有的游戏列表,GameEdit用来编辑或新建游戏。通过GameList中的新建游戏按钮或者选择列表中现有的项目,可以跳转到GameEdit。在GameEdit中可以返回到GameList。导航的代码如下:
GameList中新建游戏的代码如下:
async void btn_NewGame_Clicked(object sender, EventArgs e)
    {
      await Shell.Current.GoToAsync($"{nameof(GameEdit)}?{nameof(GameEdit.ItemId)}=0");
    }
GameList中选中现有项目,跳转到GameEdit的代码如下:
async void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
    {
      if (e.Item == null)
        return;
      await Shell.Current.GoToAsync($"{nameof(GameEdit)}?{nameof(GameEdit.ItemId)}={e.Item}");
    }
在GameEdit中,还需要定义接收传入的参数,这里传入的参数是ItemId,在GameEdit的声明中使用QueryProperty标记声明传入参数:
[QueryProperty(nameof(ItemId), nameof(ItemId))]
  [XamlCompilation(XamlCompilationOptions.Compile)]
  public partial class GameEdit : ContentPage
在接收ItemId时,从数据库中读取相应的记录并初始化页面:
public string ItemId
    {
      get
      {
        return currentId.ToString();
      }
      set
      {
        currentId = int.Parse( value);
        if (currentId > 0)
        {
          var game = App.Database.GetGameAsync(currentId).Result;
          if(game != null) EditGame(game);
        }
        
      }
    }
下面是GameList和GameEdit的完整代码。
GameList页面代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms" xmlns:d1="http://xamarin.com/schemas/2014/forms/design"
       x:Class="ZL.Shudu.Views.GameList">
  <ContentPage.Content>
    <StackLayout>
      <Button Text="新游戏"  Clicked="btn_NewGame_Clicked"></Button>
      <ListView x:Name="MyListView"
      d1:ItemsSource="{Binding Items}"
      ItemTapped="Handle_ItemTapped"
      CachingStrategy="RecycleElement"
           IsVisible="True">
        <d:ListView.ItemsSource>
          <x:Array Type="{x:Type x:String}">
            
          </x:Array>
        </d:ListView.ItemsSource>
      </ListView>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>
GameList后台代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace ZL.Shudu.Views
{
  [XamlCompilation(XamlCompilationOptions.Compile)]
  public partial class GameList : ContentPage
  {
    public ObservableCollection<string> Items { get; set; }
    public GameList()
    {
      InitializeComponent();
       
    }
    protected override async void OnAppearing()
    {
      await RefreshList();
    }
    public async Task RefreshList()
    {
      Items = await GetItems();
      MyListView.ItemsSource = Items;
      MyListView.IsVisible = true;
    }
    public async Task<ObservableCollection<string>> GetItems()
    {
      var items = new ObservableCollection<string>();
      var lst = await App.Database.GetGamesAsync();
      foreach (var obj in lst)
      {
        items.Add(obj.ID.ToString());
      }
      return items;
    }
    async void Handle_ItemTapped(object sender, ItemTappedEventArgs e)
    {
      if (e.Item == null)
        return;
      await Shell.Current.GoToAsync($"{nameof(GameEdit)}?{nameof(GameEdit.ItemId)}={e.Item}");
    }
    async void btn_NewGame_Clicked(object sender, EventArgs e)
    {
      await Shell.Current.GoToAsync($"{nameof(GameEdit)}?{nameof(GameEdit.ItemId)}=0");
    }
  }
}
GameEdit页面代码:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
       x:Class="ZL.Shudu.Views.GameEdit">
  <ContentPage.Content>
    <StackLayout>
      <Grid x:Name="myGrid" IsVisible="True" >
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="25"  />
          <RowDefinition Height="25" />
          <RowDefinition Height="25" />
          <RowDefinition Height="25" />
          <RowDefinition Height="25" />
          <RowDefinition Height="25" />
          <RowDefinition Height="25" />
          <RowDefinition Height="25" />
          <RowDefinition Height="25" />
          <RowDefinition Height="40" x:Name="rowButton" />
          <RowDefinition Height="40" x:Name="rowResult" />
        </Grid.RowDefinitions>
    
        <Button Text="保存" Grid.Row="9" Grid.Column="2"  Grid.ColumnSpan="2" Clicked="btn_Save_Clicked"></Button>
        <Button Text="删除" x:Name="btn_Delete" Grid.Row="9" Grid.Column="4"  Grid.ColumnSpan="2" Clicked="btn_Delete_Clicked"></Button>
        <Button Text="返回" Grid.Row="9" Grid.Column="6"  Grid.ColumnSpan="2" Clicked="btn_Back_Clicked"></Button>

        <Label x:Name="lbMessage" Grid.Row="10" Grid.Column="5" Grid.ColumnSpan="4" Text="" IsVisible="False"></Label>
      </Grid>
      <Grid x:Name="grdNumber" IsVisible="false">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="*" />
          <RowDefinition Height="*" />
        </Grid.RowDefinitions>
      </Grid>
    </StackLayout>
  </ContentPage.Content>
</ContentPage>
GameEdit后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using ZL.Shudu.Services;
namespace ZL.Shudu.Views
{
  [QueryProperty(nameof(ItemId), nameof(ItemId))]
  [XamlCompilation(XamlCompilationOptions.Compile)]
  public partial class GameEdit : ContentPage
  {
    private bool IsSaved=false;
    private int currentId = 0;
    private static int[,] chess = new int[9, 9];
    private Button[,] buttons = new Button[9, 9];
    private Button[,] numbuttons = new Button[2, 5];
    private Button currentButton;
    private Button currentNumBtn;
    public string ItemId
    {
      get
      {
        return currentId.ToString();
      }
      set
      {
        currentId = int.Parse( value);
        if (currentId > 0)
        {
          var game = App.Database.GetGameAsync(currentId).Result;
          if(game != null) EditGame(game);
        }
        
      }
    }
    public GameEdit()
    {
      InitializeComponent();
      SetLayout();
      SetNumButtons();
    }
    internal void EditGame(InputGameInfo game)
    {
      currentId = game.ID;
      for (var i = 0; i < 9; i++)
        for (var j = 0; j < 9; j++)
        {
          chess[i, j] = int.Parse(game.Sudoku.Substring(i * 9 + j, 1));
          buttons[i, j].Text = chess[i, j] > 0 ? chess[i, j].ToString() : "";
          buttons[i, j].IsEnabled = true;
        }
    }
    private void SetNumButtons()
    {
      var num = 1;
      for (var i = 0; i < 2; i++)
      {
        for (var j = 0; j < 5; j++)
        {
          var btn = new Button();
          if (num == 10)
          {
            btn.Text = "清除";
            btn.Clicked += Clear_Clicked;
            btn.FontSize = 15;
          }
          else
          {
            btn.Text = num.ToString();
            btn.Clicked += Num_Clicked;
            btn.FontSize = 16;
          }
          btn.Padding = 0;
          grdNumber.Children.Add(btn, j, i);
          numbuttons[i, j] = btn;
          num++;
        }
      }
    }
    private void SetLayout()
    {
      for (var i = 0; i < 9; i++)
      {
        for (var j = 0; j < 9; j++)
        {
          int m = i / 3;
          int n = j / 3;
          var btn = new Button();
          var c = new Color(0.9, 0.9, 0.9);
          if ((m + n) % 2 == 0)
          {
            c = new Color(0.7, 0.7, 0.7);
          }
          btn.BackgroundColor = c;
          btn.Padding = 0;
          btn.Margin = 0;
          btn.FontSize = 20;
          myGrid.Children.Add(btn, i, j);
          btn.Clicked += Btn_Clicked;
          buttons[i, j] = btn;

        }
      }
    }
    private async void btn_Save_Clicked(object sender, EventArgs e)
    {
      var str = "";
      for (var i = 0; i < 9; i++)
        for (var j = 0; j < 9; j++)
        {
          if (string.IsNullOrEmpty(buttons[i, j].Text))
            chess[i, j] = 0;
          else
            chess[i, j] = int.Parse(buttons[i, j].Text);
          str += chess[i, j].ToString();
        }
      var newgame = new InputGameInfo
      {
        Sudoku = str,
        InputDate = DateTime.Now
      };
      if (currentId > 0)
      {
        newgame.ID = currentId;
        await App.Database.UpdateGameAsync(newgame);
      }
      else
      {
        currentId = await App.Database.SaveGameAsync(newgame);
      }
      lbMessage.Text = "保存成功";
      lbMessage.IsVisible = true;
    }
    private void btn_New_Clicked(object sender, EventArgs e)
    {
      lbMessage.Text = "";
      currentId = 0;
      for (var i = 0; i < 9; i++)
        for (var j = 0; j < 9; j++)
        {
          buttons[i, j].Text = "";
          buttons[i, j].IsEnabled = true;
          chess[i, j] = 0;
        }
    }
    private void Btn_Clicked(object sender, EventArgs e)
    {
      currentButton = sender as Button;
      rowResult.Height = 1;
      rowButton.Height = 1;
      grdNumber.IsVisible = true;
    }
    private async void btn_Delete_Clicked(object sender, EventArgs e)
    {
      if (currentId > 0)
      {
        await App.Database.DeleteGameAsync(new InputGameInfo { ID = currentId });
        await Shell.Current.GoToAsync($"///{nameof(GameList)}");
      }
    }
    private void Num_Clicked(object sender, EventArgs e)
    {
      currentNumBtn = sender as Button;
      int x = -1, y = -1;
      for (var i = 0; i < 9; i++)
      {
        for (var j = 0; j < 9; j++)
        {
          if (buttons[i, j] == currentButton)
          {
            x = i;
            y = j;
            break;
          }
        }
      }
      var num = int.Parse(currentNumBtn.Text);
      currentButton.Text = currentNumBtn.Text;
      myGrid.IsVisible = true;
      grdNumber.IsVisible = false;
      rowResult.Height = 40;
      rowButton.Height = 40;
      
    }
    private void Clear_Clicked(object sender, EventArgs e)
    {
      if (currentButton == null) return;
      currentButton.Text = "";
      grdNumber.IsVisible = false;
      myGrid.IsVisible = true;
      rowResult.Height = 40;
      rowButton.Height = 40;
    }
    private async void btn_Back_Clicked(object sender, EventArgs e)
    {
      
      await Shell.Current.GoToAsync($"///{nameof(GameList)}");
    }
  }
}
编辑功能完成了,但还有一个问题,如果输入的游戏无法完成怎么办?这需要增加判断游戏是否可以完成的逻辑,如果无法完成,需要将UsedInGame属性设置为false,避免无效游戏。
在输入新的游戏时,我们需要确保输入的游戏合法并且能够完成,在输入过程中,可能需要随时检查合法性,在保存之前,需要确保游戏合法且可完成,然后才能保存。验证的方法就是使用计算机算法完成数独游戏,如果可以完成,就是合法的,否则就需要修改。相关的算法已经封装在程序包中,实现细节在这里不做讨论,将来会作为独立的文章详细介绍实现过程。可以使用Nuget程序包管理器进行安装:ZL.Sudoku.Lib。使用方法如下:
var comp = new FindOneSolution(cinp);
        var res = comp.Comp();
        var fchess = comp.Matrix;
上面算法中,cinp是输入的数独数组,使用Comp方法进行计算,如果res=2,说明计算完成,输出的结果使用Matrix属性获得,也是一个二维数组。如果res=1,说明无法完成,如果是其它值说明输入有错误。
安装完这个程序包后,我们可以改造GameEdit页面。首先增加一个按钮,用来在输入过程中验证是否合法和能够完成。
<Button Text="检查" x:Name="btn_Check" Grid.Row="9" Grid.Column="0"  Grid.ColumnSpan="2" Clicked="btn_Check_Clicked"></Button>
        <Button Text="保存" Grid.Row="9" Grid.Column="2"  Grid.ColumnSpan="2" Clicked="btn_Save_Clicked"></Button>
        <Button Text="删除" x:Name="btn_Delete" Grid.Row="9" Grid.Column="4"  Grid.ColumnSpan="2" Clicked="btn_Delete_Clicked"></Button>
        <Button Text="返回" Grid.Row="9" Grid.Column="6"  Grid.ColumnSpan="2" Clicked="btn_Back_Clicked"></Button>
后台代码:
private void btn_Check_Clicked(object sender, EventArgs e)
    {
      if (btn_Check.Text == "检查")
      {
        var cinp = getChess();
        lastinput = cinp;
        var comp = new FindOneSolution(cinp);
        var res = comp.Comp();
        var fchess = comp.Matrix;
        for (var i = 0; i < 9; i++)
        {
          for (var j = 0; j < 9; j++)
          {
            var btn = buttons[i, j];
            if (cinp[i, j] > 0)
            {
              btn.Text = cinp[i, j].ToString();
            }
            else
            {
              btn.Text = fchess[i, j] > 0 ? fchess[i, j].ToString() : "";
            }
          }
        }
        if (res == 0) lbMessage.Text = "不合法";
        else if (res == 1) lbMessage.Text = "计算不出来";
        else if (res == 2) lbMessage.Text = "计算完成";
        else lbMessage.Text = "其它错误";
        btn_Check.Text = "继续";
      }
      else
      {
        lbMessage.Text = "";
        btn_Check.Text = "检查";
        for (var i = 0; i < 9; i++)
        {
          for (var j = 0; j < 9; j++)
          {
            var btn = buttons[i, j];
            if (lastinput[i, j] > 0)
            {
              btn.Text = lastinput[i, j].ToString();
            }
            else
            {
              btn.Text ="";
            }
          }
        }
      }
    }
    private int[,] getChess()
    {
      var res = new int[9, 9];
      for (var i = 0; i < 9; i++)
      {
        for (var j = 0; j < 9; j++)
        {
          var btn = buttons[i, j];
          if(string.IsNullOrEmpty(btn.Text)) res[i,j]= 0;
          else res[i, j]=int.Parse(btn.Text);
        }
      }
      return res;
    }
在保存前,也需要进行检查,如果不合法或者无法完成,就提示继续编辑,不能保存:
private async void btn_Save_Clicked(object sender, EventArgs e)
    {
      if(btn_Check.Text=="继续") btn_Check_Clicked(null,null);
      var str = "";
      var chess = getChess();
      for (var i = 0; i < 9; i++)
        for (var j = 0; j < 9; j++)
        {
          str +=  chess[i, j].ToString();
        }
      var comp = new FindOneSolution(chess);
      var res = comp.Comp();
      if(res != 2)
      {
        lbMessage.Text = "不合法或者无法完成的游戏,请修改后保存";
        return;
      }
      var newgame = new InputGameInfo
      {
        Sudoku = str,
        InputDate = DateTime.Now,
        UsedInGame = true
      };
      if (currentId > 0)
      {
        newgame.ID = currentId;
        await App.Database.UpdateGameAsync(newgame);
      }
      else
      {
        currentId = await App.Database.SaveGameAsync(newgame);
      }
      lbMessage.Text = "保存成功";
    }
到此,我们的数独游戏基本完成。下一步的工作是增加完成历史列表页面,让玩家查看已经完成的历史,并且能够复盘。

关注下面的标签,发现更多相似文章