Thursday, April 19, 2007

C# Baseball Simulation Program

// The following program assumes a Form called Form1 which has three checkboxes
// and two buttons and two labels. All objects should use their default names
// and there should have been no other objects on the form.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MyProject
{
public partial class Form1 : Form
{
int runs = 0;
int outs = 0;
int innings = 0;
Random RandomClass = new Random();

//A basepath is a string of zeroes and ones. The first three elements are the three bases.
//A one means the base is occupied. A zero means it is empty.
//Any 1s beyond the first three elements represent runners who scored.
//Zeroes beyond the first three elements can be ignored.

Stack basepath = new Stack();

int[] intArray;





public Form1()
{
InitializeComponent();
//create the three bases and set them to empty.
basepath.Push(0);
basepath.Push(0);
basepath.Push(0);
}


public void oneBagger()
{
//advance runners two bases.
basepath.Push(0);
basepath.Push(1);

}


public void twoBagger()
{
//advance runners three bases.
basepath.Push(0);
basepath.Push(1);
basepath.Push(0);

}

public void threeBagger()
{
basepath.Push(1);
basepath.Push(0);
basepath.Push(0);

}

public void homer()
{
basepath.Push(1);
basepath.Push(0);
basepath.Push(0);
basepath.Push(0);
}

public void walk()
{
// look for first empty base and
// place a runner there.
// if all bases are occupied move all runners forward one base.
if (basepath.Pop() == 0)
basepath.Push(1);
else if (basepath.Pop() == 0)
{
basepath.Push(1);
basepath.Push(1);
}
else if (basepath.Pop() == 0)
{
basepath.Push(1);
basepath.Push(1);
basepath.Push(1);
}
else
{
basepath.Push(1);
basepath.Push(1);
basepath.Push(1);
basepath.Push(1);
}
}

public void addRuns()
{
//for use at end of game.
//Throw away baserunners since they can never score.
basepath.Pop();
basepath.Pop();
basepath.Pop();
intArray = basepath.ToArray();
for (int i = 0; i < intArray.GetLength(0); i++)
{
if (intArray[i] == 1)
{
runs++;
}
}

label1.Text = runs.ToString();

}

public void strikeout()
{
outs = outs + 1;
label2.Text = outs.ToString();
if (outs > 2)
{
// end of inning;
outs = 0;
label2.Text = outs.ToString();
innings = innings + 1;
basepath.Pop();
basepath.Pop();
basepath.Pop();
basepath.Push(0);
basepath.Push(0);
basepath.Push(0);
checkBox1.Checked = false;
checkBox2.Checked = false;
checkBox3.Checked = false;
}

if (innings > 8)
label2.Text = "Game Over";

}

public void bat()
{
double num = RandomClass.NextDouble();
if (label2.Text == "Game Over")
{
label1.Text = "Press For Totals";
}
else if (num < .225)
{
oneBagger();
}
else if (num < .275)
{
twoBagger();
}
else if (num < .300)
{
threeBagger();
}

else if (num < .325)
{
homer();
}
else if (num < .400)
{
walk();
}

else
{
strikeout();
}

intArray = basepath.ToArray();

// set checkboxes based upon whether bases are occupied.
if (intArray[0] == 1)
{
checkBox1.Checked = true;
}
else
{
checkBox1.Checked = false;
}


if (intArray[1] == 1)
{
checkBox2.Checked = true;
}
else
{
checkBox2.Checked = false;
}


if (intArray[2] == 1)
{
checkBox3.Checked = true;
}
else
{
checkBox3.Checked = false;
}




}



private void button1_Click(object sender, EventArgs e)
{

addRuns();
}

private void button2_Click(object sender, EventArgs e)
{

bat();



}

private void label1_Click(object sender, EventArgs e)
{

}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{

}
}
}

0 Comments:

Post a Comment

<< Home