I have a void function that has a lot of if statements in it and all of them are required I really can't remove anything. But I feel like that it could be done better. Using some LINQ.Where
, classes or something like this. I want to optimize and express void Smooth
in the fewest characters possible :
void Smooth(ref int botChips, ref bool botTurn, Label botStatus, int name, int n, int r) {
Random rand = new Random();
int rnd = rand.Next(1, 3);
if (rounds == 0 || rounds == 1)
{
if (call <= 0)
{
Check(ref botTurn, botStatus);
}
else
{
if (call >= RoundN(botChips, n))
{
Call(ref botChips, ref botTurn, botStatus);
}
else
{
if (botChips >= call * 2)
{
Raise *= 2;
Raised(ref botChips, ref botTurn, botStatus);
}
else
{
Call(ref botChips, ref botTurn, botStatus);
}
}
}
}
if (rounds == 2 || rounds == 3)
{
if (call <= 0)
{
if (rnd == 1)
{
Raise = RoundN(botChips, r);
Raised(ref botChips, ref botTurn, botStatus);
}
else if (rnd!=1 && rounds==2)
{
Check(ref botTurn, botStatus);
}
}
else
{
if (call >= RoundN(botChips, r))
{
if (botChips > call)
{
Call(ref botChips, ref botTurn, botStatus);
}
if (botChips <= call)
{
raising = false;
botTurn = false;
botChips = 0;
botStatus.Text = "Call " + call;
tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
}
}
else
{
if (Raise <= (RoundN(botChips, r)) / 2)
{
Raise = RoundN(botChips, r);
Raised(ref botChips, ref botTurn, botStatus);
}
else
{
Raise *= 2;
Raised(ref botChips, ref botTurn, botStatus);
}
}
}
}
}
RoundN
method
private static double RoundN(int sChips, int n) {
double a = Math.Round((sChips / n) / 100d, 0) * 100;
return a;
}
Fold
method
private void Fold(ref bool sTurn, ref bool SFTurn, Label sStatus) {
raising = false;
sStatus.Text = "Fold";
sTurn = false;
SFTurn = true;
}
Check
method
private void Check(ref bool cTurn, Label cStatus) {
cStatus.Text = "Check";
cTurn = false;
raising = false;
}
Call
method
private void Call(ref int sChips, ref bool sTurn, Label sStatus) {
raising = false;
sTurn = false;
sChips -= call;
sStatus.Text = "Call " + call;
tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
}
Raised
method
private void Raised(ref int sChips, ref bool sTurn, Label sStatus) {
sChips -= Convert.ToInt32(Raise);
sStatus.Text = "Raise " + Raise;
tbPot.Text = (int.Parse(tbPot.Text) + Convert.ToInt32(Raise)).ToString();
call = Convert.ToInt32(Raise);
raising = true;
sTurn = false;
}
The Smooth
method can be simplified (or in your term: optimized?) in some ways:
if-else
) nested blocks, consider the use of early return
for conditions which are simpler among the two or has no further continuation. This way, you may remove "difficult-to-read" nested blocks.if (a || b)
case, the left expression (that is: a
) will be evaluated first - this is known as Short Circuit Evaluation.if-else
block.For your case, the simplified code can be something like this
uint rounds = 0; //read 8.
void Smooth(ref int botChips, ref bool botTurn, Label botStatus, int name, int n, int r) {
Random rand = new Random();
int rnd = rand.Next(1, 3);
if (rounds <= 1) { //read 8.
if (call <= 0) {
Check(ref botTurn, botStatus); //since your Check doesn't change rounds, this is legal
return; //read 1. early return
} //beyond this call > 0
if (call >= RoundN(botChips, n) || botChips < call * 2) { //read 2., 3., 4., and 7.
Call(ref botChips, ref botTurn, botStatus);
return; //read 1.
} //beyond this is the opposite of both conditions
Raise *= 2;
Raised(ref botChips, ref botTurn, botStatus);
}
if (rounds == 2 || rounds == 3) {
if (call <= 0) {
if (rnd == 1) { //call <= 0, rnd == 1, similar to the block on call < rNBChips, may potentially be further simplified
Raise = RoundN(botChips, r);
Raised(ref botChips, ref botTurn, botStatus);
} else if (rounds == 2) //read 7. rnd is definitely not 1, no need for further check
Check(ref botTurn, botStatus);
return; //read 1. this is valid since you don't want to continue
}
double rNBChips = RoundN(botChips, r); //read 6. this way you avoid multiple calls. It both shorter and faster
if (call < rNBChips) { //read 3.
Raise = Raise <= rNBChips / 2 ? rNBChips : Raise * 2; //read 5.
Raised(ref botChips, ref botTurn, botStatus);
return; // read 1.
}
if (botChips > call) {
Call(ref botChips, ref botTurn, botStatus);
return; //read 1.
}
raising = false;
botTurn = false;
botChips = 0;
botStatus.Text = "Call " + call;
tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
}
}
Without the comments it even looks a lot more compact, like this
uint rounds = 0;
void Smooth(ref int botChips, ref bool botTurn, Label botStatus, int name, int n, int r) {
Random rand = new Random();
int rnd = rand.Next(1, 3);
if (rounds <= 1) {
if (call <= 0) {
Check(ref botTurn, botStatus);
return;
}
if (call >= RoundN(botChips, n) || botChips < call * 2) {
Call(ref botChips, ref botTurn, botStatus);
return;
}
Raise *= 2;
Raised(ref botChips, ref botTurn, botStatus);
}
if (rounds == 2 || rounds == 3) {
if (call <= 0) {
if (rnd == 1) {
Raise = RoundN(botChips, r);
Raised(ref botChips, ref botTurn, botStatus);
} else if (rounds == 2)
Check(ref botTurn, botStatus);
return;
}
double rNBChips = RoundN(botChips, r);
if (call < rNBChips) {
Raise = Raise <= rNBChips / 2 ? rNBChips : Raise * 2;
Raised(ref botChips, ref botTurn, botStatus);
return;
}
if (botChips > call) {
Call(ref botChips, ref botTurn, botStatus);
return;
}
raising = false;
botTurn = false;
botChips = 0;
botStatus.Text = "Call " + call;
tbPot.Text = (int.Parse(tbPot.Text) + call).ToString();
}
}