UI 和游戏逻辑的分离

本文关键字:分离 游戏 UI | 更新日期: 2023-09-27 18:33:42

我刚刚为大学作业创建了一个基本的石头剪刀布游戏。我让它工作得很好,它的外观和行为都很完美。

但是,我意识到我没有正确阅读需求,并且使用Visual Studio编写了MainWindow.xaml.cs

事实证明,UI 应该从MainWindow.xaml.cs运行,但这还应该包含RockPaperScissorsGame.cs类的实例,其中所有游戏逻辑都应与 UI 分开。

就目前而言,我在RockPaperScissorsGame.cs中没有任何内容,MainWindow.xaml.cs中没有任何内容,我在下面附上了代码。

对于我来说,分离游戏逻辑和 UI 的最简单(或最快)方法是什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Rock_Paper_Scissors
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
        }
        private void Rock_Click(object sender, RoutedEventArgs e)
        {
            rockButton.IsEnabled = false;
            paperButton.IsEnabled = false;
            scissorsButton.IsEnabled = false;
            newButton.IsEnabled = true;
            Random rnd = new Random();
            int ai = rnd.Next(1, 3);
            rockBG.Source = new BitmapImage(new Uri(@"green.png", UriKind.Relative));
            if (ai == 2)
            {
                resultBlock.Text = "Computer chose paper. Paper beats rock! You lose!";
                paperBG.Source = new BitmapImage(new Uri(@"yellow.png", UriKind.Relative));
                bigResult.Text = "WINNER!";
                bigResult.Foreground = Brushes.Green;
            }
            if (ai == 1)
            {
                resultBlock.Text = "Computer also chose rock. Have another go!";
                rockBG.Source = new BitmapImage(new Uri(@"orange.png", UriKind.Relative));
                bigResult.Text = "DRAW";
                bigResult.Foreground = Brushes.Orange;
            }
            if (ai == 3) {
                resultBlock.Text = "Computer chose scissors. Rock beats Scissors! You win!";
                scissorsBG.Source = new BitmapImage(new Uri(@"yellow.png", UriKind.Relative));
                bigResult.Text = "LOSER!";
                bigResult.Foreground = Brushes.Red;
            }
        }
        private void Paper_Click(object sender, RoutedEventArgs e)
        {
            rockButton.IsEnabled = false;
            paperButton.IsEnabled = false;
            scissorsButton.IsEnabled = false;
            newButton.IsEnabled = true;
            Random rnd = new Random();
            int ai = rnd.Next(1, 3);
            paperBG.Source = new BitmapImage(new Uri(@"green.png", UriKind.Relative));
            if (ai == 2)
            {
                resultBlock.Text = "Computer also chose paper. Have another go!";
                paperBG.Source = new BitmapImage(new Uri(@"orange.png", UriKind.Relative));
                bigResult.Text = "DRAW";
                bigResult.Foreground = Brushes.Orange;
            }
            if (ai == 1)
            {
                resultBlock.Text = "Computer chose rock. Paper beats rock. You win!";
                rockBG.Source = new BitmapImage(new Uri(@"yellow.png", UriKind.Relative));
                bigResult.Text = "WINNER!";
                bigResult.Foreground = Brushes.Green;
            }
            if (ai == 3) {
                resultBlock.Text = "Computer chose scissors. Scissors beats paper. You lose!!";
                scissorsBG.Source = new BitmapImage(new Uri(@"yellow.png", UriKind.Relative));
                bigResult.Text = "LOSER!";
                bigResult.Foreground = Brushes.Red;
            }
        }
        private void Scissors_Click(object sender, RoutedEventArgs e)
        {
            rockButton.IsEnabled = false;
            paperButton.IsEnabled = false;
            scissorsButton.IsEnabled = false;
            newButton.IsEnabled = true;
            Random rnd = new Random();
            int ai = rnd.Next(1, 3);
            scissorsBG.Source = new BitmapImage(new Uri(@"green.png", UriKind.Relative));
            if (ai == 2)
            {
                resultBlock.Text = "Computer chose paper. Scissors beats paper! You win!";
                paperBG.Source = new BitmapImage(new Uri(@"yellow.png", UriKind.Relative));
                bigResult.Text = "WINNER!";
                bigResult.Foreground = Brushes.Green;
           }
            if (ai == 1)
            {
                resultBlock.Text = "Computer chose rock. Rock beats scissors! You lose!";
                rockBG.Source = new BitmapImage(new Uri(@"yellow.png", UriKind.Relative));
                bigResult.Text = "LOSER!";
                bigResult.Foreground = Brushes.Red;
            }
            if (ai == 3) {
                resultBlock.Text = "Computer also chose scissors. Have another go!";
                scissorsBG.Source = new BitmapImage(new Uri(@"orange.png", UriKind.Relative));
                bigResult.Text = "DRAW";
                bigResult.Foreground = Brushes.Orange;
            }
        }
        private void newButton_Click(object sender, RoutedEventArgs e)
        {
            rockButton.IsEnabled = true;
            paperButton.IsEnabled = true;
            scissorsButton.IsEnabled = true;
            newButton.IsEnabled = false;
            resultBlock.Text = "Choose Rock, Paper or Scissors to play";
            bigResult.Text = "";
            paperBG.Source = new BitmapImage(new Uri(@"blue.png", UriKind.Relative));
            rockBG.Source = new BitmapImage(new Uri(@"blue.png", UriKind.Relative));
            scissorsBG.Source = new BitmapImage(new Uri(@"blue.png", UriKind.Relative));
        }
        }
    }

UI 和游戏逻辑的分离

你的代码包含一些错误。
1.int ai = rnd.Next(1, 3);从未选择3,则应使用int ai = rnd.Next(1, 4);
2.考虑你的命名,AI意味着有Artificial Intelligence的东西,你只是在随机使用。

这是一个非常简单的RPS游戏,

public class RPSMatch
{
    private static readonly Random random = new Random();
    public Result MatchResult { get; private set; }
    public Choice ComputerChoice { get; private set; }
    public Choice PlayerChoice { get; private set; }
    public enum Choice
    {
        Rock = 1,
        Paper = 2,
        Scissors = 3,
    }
    public enum Result
    {
        Lose = -1,
        Draw = 0,
        Win = 1,
    }
    public RPSMatch(Choice playerChoice)
    {
        var computerChoice = (Choice)random.Next(1, 4);
        this.PlayerChoice = playerChoice;
        this.ComputerChoice = computerChoice;
        var diff = (int)playerChoice - (int)ComputerChoice;
        this.MatchResult = (Result)(Math.Sign(diff) * (Math.Abs(diff) == 2 ? -1 : 1));
    }
}

像这样使用它

var match = new RPSMath(RPSMatch.Rock);

然后,您可以从match属性处理 UI 更改

最简单的方法是使用 MVVM 模式并引入视图模型(请参阅本教程)

有了它,RockPaperScissorsGame.cs就变成了一个视图模型,其中包含 UI 层对应的逻辑。这主要涉及将所有单击事件下的方法移动到视图模型类中的公共方法中,以及使用Bindings来实现视图和视图模型之间的轻松连接。