由於是考前臨時完成的,程式完整度相較前幾個會低些
HW09_exe
HW09
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HW_09
{
public partial class Form1 : Form
{
//
private Ball ball = new Ball();
private Umpire umpire = new Umpire();
private Pitcher pitcher = new Pitcher();
private SecondBaseMan secondBaseMan = new SecondBaseMan();
private CenterFielder centerFielder = new CenterFielder();
private Fans fans = new Fans();
private Random rand = new Random();
bool isNewRound = true;
bool isRandom = false;
public Form1()
{
InitializeComponent();
}
private void btn_Set_Click(object sender, EventArgs e)
{
//Create obj. by selection
switch(cbo_Player.SelectedIndex){
case 0 : //Pitcher
pitcher = new Pitcher(double.Parse(txt_AngleLeft.Text), double.Parse(txt_AngleRight.Text),
double.Parse(txt_DistanceMin.Text), double.Parse(txt_DistanceMax.Text));
break;
case 1: //SecondBaseMan
secondBaseMan = new SecondBaseMan(double.Parse(txt_AngleLeft.Text), double.Parse(txt_AngleRight.Text),
double.Parse(txt_DistanceMin.Text), double.Parse(txt_DistanceMax.Text));
break;
case 2: //CenterFielder
centerFielder = new CenterFielder(double.Parse(txt_AngleLeft.Text), double.Parse(txt_AngleRight.Text),
double.Parse(txt_DistanceMin.Text), double.Parse(txt_DistanceMax.Text));
break;
default:
MessageBox.Show("請選擇要設定的球員!!");
return;
}
MessageBox.Show(cbo_Player.Text + "已設定!");
isNewRound = false;
}
private void btn_Start_Click(object sender, EventArgs e)
{
//Determine whether the ball is hit
rtx_Result.AppendText((rand.NextDouble() > 0.4) ?
Hit() : "打者揮空, 好球!!" + "\n");
isNewRound = false;
}
private string Hit()
{
CheckStatus();
//Check I/P
if (isRandom)
{
ball = new Ball("Random");
}
else
{
ball = new Ball(double.Parse(txt_AngleBall.Text), double.Parse(txt_DistanceBall.Text));
}
//Build the play event
EventBuilder();
return ball.Hit() + "\n";
}
private void CheckStatus()
{
//I/P Check
isRandom = chk_Random.Checked;
//Check the game status
if (!isNewRound)
{
ball.CleanAllEvent();
umpire.CleanAllEvent();
}
}
private void EventBuilder()
{
ball.BallInPlay += pitcher.Catch;
ball.BallInPlay += secondBaseMan.Catch;
ball.BallInPlay += centerFielder.Catch;
ball.BallInCheck += umpire.Judge;
umpire.CheckResult += fans.React;
}
private void cbo_Player_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cbo_Player.SelectedIndex)
{
case 0: //Pitcher
trkb_DistanceMin.Minimum = 200;
trkb_DistanceMin.Maximum = 600;
trkb_DistanceMax.Minimum = 600;
trkb_DistanceMax.Maximum = 900;
break;
case 1: //SecondBaseMan
trkb_DistanceMin.Minimum = 900;
trkb_DistanceMin.Maximum = 1200;
trkb_DistanceMax.Minimum = 1200;
trkb_DistanceMax.Maximum = 1550;
break;
case 2: //CenterFielder
trkb_DistanceMin.Minimum = 1550;
trkb_DistanceMin.Maximum = 1900;
trkb_DistanceMax.Minimum = 1900;
trkb_DistanceMax.Maximum = 4000;
break;
}
txt_DistanceMin.Text = TrkbToTxt(trkb_DistanceMin.Maximum);
txt_DistanceMax.Text = TrkbToTxt(trkb_DistanceMax.Minimum);
}
//
//
//Link the TrackBar & the TextBox
private void trkb_AngleLeft_Scroll(object sender, EventArgs e)
{
txt_AngleLeft.Text = TrkbToTxt(trkb_AngleLeft.Value);
}
private void txt_AngleLeft_TextChanged(object sender, EventArgs e)
{
trkb_AngleLeft.Value = TxtToTrkb(txt_AngleLeft.Text);
}
private void trkb_AngleRight_Scroll(object sender, EventArgs e)
{
txt_AngleRight.Text = TrkbToTxt(trkb_AngleRight.Value);
}
private void txt_AngleRight_TextChanged(object sender, EventArgs e)
{
trkb_AngleRight.Value = TxtToTrkb(txt_AngleRight.Text);
}
private void trkb_DistanceMin_Scroll(object sender, EventArgs e)
{
txt_DistanceMin.Text = TrkbToTxt(trkb_DistanceMin.Value);
}
private void txt_DistanceMin_TextChanged(object sender, EventArgs e)
{
trkb_DistanceMin.Value = TxtToTrkb(txt_DistanceMin.Text);
}
private void trkb_DistanceMax_Scroll(object sender, EventArgs e)
{
txt_DistanceMax.Text = TrkbToTxt(trkb_DistanceMax.Value);
}
private void txt_DistanceMax_TextChanged(object sender, EventArgs e)
{
trkb_DistanceMax.Value = TxtToTrkb(txt_DistanceMax.Text);
}
private void trkb_AngleBall_Scroll(object sender, EventArgs e)
{
txt_AngleBall.Text = TrkbToTxt(trkb_AngleBall.Value);
}
private void txt_AngleBall_TextChanged(object sender, EventArgs e)
{
trkb_AngleBall.Value = TxtToTrkb(txt_AngleBall.Text);
}
private void trkb_DistanceBall_Scroll(object sender, EventArgs e)
{
txt_DistanceBall.Text = TrkbToTxt(trkb_DistanceBall.Value);
}
private void txt_DistanceBall_TextChanged(object sender, EventArgs e)
{
trkb_DistanceBall.Value = TxtToTrkb(txt_DistanceBall.Text);
}
//End of link
//
//
//Set the unit of I/P variables
private string TrkbToTxt(int value)
{
return "" + value / 10.0;
}
private int TxtToTrkb(string value)
{
bool noValue = (value.Length == 0);
if (noValue) { return 0; }
else
{
return (int)(double.Parse(value) * 10);
}
}
//Change the visibility of the I/P of ball's variable
private void chk_Random_CheckedChanged(object sender, EventArgs e)
{
pnl_Ball.Visible = !pnl_Ball.Visible;
}
}
}
Ball.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_09
{
class Ball
{
//EventHandler of Ball
public delegate string BallInPlayEventHandler
(object sender, BallInPlayEventArgs args);
public event BallInPlayEventHandler BallInPlay;
public event BallInPlayEventHandler BallInCheck;
//Field
private Random rand = new Random();
private double _angle = 0;
private double _distance = 0;
public Ball()
{
_angle = 0;
_distance = 0;
}
public Ball(double angle, double distance) {
_angle = angle;
_distance = distance;
}
public Ball(string random)
{
SetAngleAndDist();
}
public string Hit() //The ball is hit
{
StringBuilder sb = new StringBuilder();
sb.Append("球被擊出! ");
sb.AppendLine(this.ToString());
if (BallInPlay != null)
{
BallInPlayEventArgs e = new BallInPlayEventArgs();
e.Angle = _angle;
e.Distanece = _distance;
string result = "";
foreach (BallInPlayEventHandler element in BallInPlay.GetInvocationList())
{
result = element(this, e);
sb.AppendLine(result);
}
if (BallInCheck != null)
{
sb.AppendLine(BallInCheck(this, e));
}
}
return sb.ToString();
}
public void SetAngleAndDist() //Set the position of the ball randomly
{
_distance = 600.0 * rand.NextDouble();
_angle = 90.0 * 0.7 * (2 * rand.NextDouble() - 1);
}
public override string ToString() //Show the position of the ball
{
string s = "";
s += "角度為" + _angle.ToString("F2") + "度, ";
s += " ";
s += "距離為" + _distance.ToString("F2") + "英尺";
return s;
}
public void CleanAllEvent()
{
BallInPlay = null;
BallInCheck = null;
}
}
class BallInPlayEventArgs : EventArgs
{
private double _distanece = 0;
private double _angle = 0;
private bool _isCaught = false;
public double Distanece
{
get
{
return _distanece;
}
set
{
if (value > 0)
{
_distanece = value;
}
}
}
public double Angle
{
get
{
return _angle;
}
set
{
_angle = value;
}
}
public bool IsCaught
{
get
{
return _isCaught;
}
set
{
_isCaught = value;
}
}
}
}
Player.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_09
{
class Player
{
//Field
protected double _angleLeft = 0;
protected double _angleRight = 0;
private double _distanceMin = 0;
private double _distanceMax = 0;
private FieldPosition _position = FieldPosition.Unknown;
// Nested enums
public enum FieldPosition
{
Unknown = 0, Pitcher, Catcher,
FirstBaseman, SecondBaseman, ThirdBaseman, Shortstop,
LeftFielder, CenterFielder, RightFielder
};
//Constructors
public Player()
{
_angleLeft = 0;
_angleRight = 0;
_distanceMin = 0;
_distanceMax = 0;
}
public Player(double angleLeft, double angleRight, double distanceMin, double distanceMax)
{
_angleLeft = angleLeft;
_angleRight = angleRight;
_distanceMin = distanceMin;
_distanceMax = distanceMax;
}
//Property
public double AngleLeft
{
get { return _angleLeft; }
}
public double AngleRight
{
get { return _angleRight; }
}
public double DistanceMin
{
get { return _distanceMin; }
}
public double DistanceMax
{
get { return _distanceMax; }
}
//Pllayer's position
public FieldPosition Position
{
get
{
return _position;
}
protected set
{
_position = value;
}
}
//Determine whether the ball is caught
public string Catch(object sender, BallInPlayEventArgs args)
{
string reply = "";
args.IsCaught |= ((DistanceMin < args.Distanece) && (args.Distanece < DistanceMax));
args.IsCaught &= ((AngleLeft < args.Angle) && (args.Angle < AngleRight));
reply = Position.ToString() + ": ";
reply += "我" + (args.IsCaught ? "有" : "沒有") + "接到球";
return reply;
}
}
}
Pitcher.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_09
{
class Pitcher : Player
{
//Field
//Constructors
public Pitcher() {
Position = FieldPosition.Pitcher;
}
public Pitcher(double angleLeft, double angleRight, double distanceMin, double distanceMax)
:base(angleLeft, angleRight, distanceMin, distanceMax)
{
Position = FieldPosition.Pitcher;
}
}
}
SecondBaseMan.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_09
{
class SecondBaseMan : Player
{
//Field
//Constructors
public SecondBaseMan() {
Position = FieldPosition.SecondBaseman;
}
public SecondBaseMan(double angleLeft, double angleRight, double distanceMin, double distanceMax)
:base(angleLeft, angleRight, distanceMin, distanceMax)
{
Position = FieldPosition.SecondBaseman;
}
}
}
CenterFielder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_09
{
class CenterFielder : Player
{
//Field
//Constructors
public CenterFielder() {
Position = FieldPosition.CenterFielder;
}
public CenterFielder(double angleLeft, double angleRight, double distanceMin, double distanceMax)
:base(angleLeft, angleRight, distanceMin, distanceMax)
{
Position = FieldPosition.CenterFielder;
}
}
}
Umpire.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_09
{
class Umpire
{
//EventHandlers
public delegate string CheckResultEventHandler
(object sender, CheckResultEventArgs args);
public event CheckResultEventHandler CheckResult;
//Constructors
public Umpire() { }
// Methods
public string Judge(object sender, BallInPlayEventArgs args)
{
CheckResultEventArgs judgement = new CheckResultEventArgs();
StringBuilder sb = new StringBuilder();
sb.Append("Umpire: ");
if (args.IsCaught)
{
judgement.IsOut = true;
sb.AppendLine("接殺出局!");
}
else if ((args.Angle < FieldSize.LeftAngle) || (args.Angle > FieldSize.RightAngle))
{
judgement.IsFoul= true;
sb.AppendLine("界外球!");
}
else if (args.Distanece > FieldSize.DistanceMax)
{
judgement.IsHomeRun = true;
sb.AppendLine("全壘打!");
}
else
{
judgement.IsHit = true;
sb.AppendLine("安打!");
}
if (CheckResult != null)
{
sb.AppendLine(CheckResult(this, judgement));
}
return sb.ToString();
}
public void CleanAllEvent()
{
CheckResult = null;
}
}
class CheckResultEventArgs : EventArgs
{
bool isOut = false;
bool isFoul = false;
bool isHomeRun = false;
bool isHit = false;
public bool IsOut
{
get
{
return isOut;
}
set
{
isOut = value;
}
}
public bool IsFoul
{
get
{
return isFoul;
}
set
{
isFoul = value;
}
}
public bool IsHomeRun
{
get
{
return isHomeRun;
}
set
{
isHomeRun = value;
}
}
public bool IsHit
{
get
{
return isHit;
}
set
{
isHit = value;
}
}
}
static class FieldSize
{
public static double LeftAngle = -45.0;
public static double RightAngle = 45.0;
public static double DistanceMax = 400.0;
}
}
Fans.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HW_09
{
class Fans
{
public Fans() { }
public string React(object sender, CheckResultEventArgs judgement)
{
string react = "";
if (judgement.IsOut)
{
react = "Fans: Booo~~~\n";
}
else if (judgement.IsFoul)
{
react = "Fans: 打者無能!換打者!!\n";
}
else if (judgement.IsHomeRun)
{
react = "Fans: Wow!!~~~\n";
}
else if (judgement.IsHit)
{
react = "Fans: Yooo!!~~~\n";
}
else
{
react = "Fans: 裁判無能!換裁判!!\n";
}
return react;
}
}
}
沒有留言:
張貼留言