我刚为大学任务创建了一个基本的Rock Paper Scissors游戏。 我有它工作得很好,它看起来和行为完美。
但是,我已经意识到我没有正确阅读这些要求并且使用Visual Studio我已经在MainWindow.xaml.cs
事实证明,UI应该从MainWindow.xaml.cs
运行,但是这也应该包含RockPaperScissorsGame.cs
类的实例,其中所有游戏逻辑应该与UI分开。
目前我在RockPaperScissorsGame.cs
和MainWindow.xaml.cs
中的所有内容都没有任何内容,我已附上下面的代码。
对于我来说,分离游戏逻辑和用户界面最简单(或最快捷)的方式是什么?
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));
}
}
}
答案 0 :(得分:1)
您的代码包含一些错误
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
属性
答案 1 :(得分:0)
最简单的方法是使用MVVM模式并引入视图模型(参见tutorial)
使用它,RockPaperScissorsGame.cs
成为一个视图模型,其中包含UI层对应的逻辑。这主要涉及将单击事件中的所有方法移动到视图模型类中的公共方法中,同时使用Bindings
来实现视图和视图模型之间的轻松连接。