C# 如何构造看似冲突的条件语句,以便正确地计算两者?
我编写了一个程序,检查对象的颜色,并根据结果向对象发送信号以改变颜色,然后检查新颜色,等等。根据可能颜色的HSL范围,无色成分通常被误解为黑色,因此,我必须使用对象的行为模式来推断测量为黑色的组件是否应该是无色的(反之亦然)。我还必须检查以确保对象确实从以前的状态改变了颜色,因为延迟是可能的。我分别用于这两个任务的语句是C# 如何构造看似冲突的条件语句,以便正确地计算两者?,c#,logic,conditional-statements,C#,Logic,Conditional Statements,我编写了一个程序,检查对象的颜色,并根据结果向对象发送信号以改变颜色,然后检查新颜色,等等。根据可能颜色的HSL范围,无色成分通常被误解为黑色,因此,我必须使用对象的行为模式来推断测量为黑色的组件是否应该是无色的(反之亦然)。我还必须检查以确保对象确实从以前的状态改变了颜色,因为延迟是可能的。我分别用于这两个任务的语句是 if (scaleColorsValue != previousArrayValue + previouslySelectedPlay && (scaleCol
if (scaleColorsValue != previousArrayValue + previouslySelectedPlay && (scaleColorsValue - previousArrayValue - previouslySelectedPlay) % 7 == 0) // If expected result is off by a multiple of seven
……还有
if (!areArraysEqual(previousArray, scaleColors))
…其中ararraysequal()
返回true
当且仅当对象的颜色与之前的测量值相同时
问题是,如果我将color correction if语句放在lag check if语句中,当无色组件被解释为黑色时,lag check将自动通过,因为这将导致它不等于先前的测量值,但是如果我将颜色校正if语句放在延迟检查之前来解决这个问题,那么
(scaleColorsValue - previousArrayValue - previouslySelectedPlay) % 7 == 0
当存在滞后时,将无法解析为正确的值
如何修改代码以避免这种不希望出现的困境,从而正确地计算这两个条件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
namespace macroScopic
{
public partial class Form1 : Form
{
Random rn1_ = new Random();
bool state = false; // True means on, false means off
string strategyFileText;
double cost;
double baseBet;
OrderedColors[] previousArray = { OrderedColors.NULL, OrderedColors.NULL, OrderedColors.NULL, OrderedColors.NULL, OrderedColors.NULL, OrderedColors.NULL };
int previouslySelectedPlay = 2;
int consecutiveLagFailures = 0;
public Form1()
{
InitializeComponent();
using (StreamReader strategyFile = new StreamReader("strategy.txt"))
{
strategyFileText = strategyFile.ReadToEnd();
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
state = onOffButton.Checked;
this.Cursor = new Cursor(Cursor.Current.Handle);
cost = Convert.ToDouble(textBox2.Text);
baseBet = Convert.ToDouble(textBox3.Text);
}
// Code
private void timer1_Tick(object sender, EventArgs e)
{
if (state)
{
takeTurn();
delay(1700, 5200, 5, timer1);
}
}
private void takeTurn()
{
/// Thread.Sleep(4000);
Bitmap screenShot = takeScreenShot();
// Code
if (/*Code*/)
{
// Code to set values of scaleColors elements, based on screenshot
int previousArrayValue = value(previousArray);
int scaleColorsValue = value(scaleColors);
if (previouslySelectedPlay == 0)
{
for (int scale = 0; scale < scaleColors.Length; scale++)
{
scaleColors[scale] = OrderedColors.Colorless;
}
}
else if (previouslySelectedPlay == 2 || previouslySelectedPlay == 1 || previouslySelectedPlay == 3 || previouslySelectedPlay == 6)
{
if (scaleColorsValue != previousArrayValue + previouslySelectedPlay && (scaleColorsValue - previousArrayValue - previouslySelectedPlay) % 7 == 0) // If expected result is off by a multiple of seven
{
int numRequiredChanges = Math.Abs((scaleColorsValue - previousArrayValue - previouslySelectedPlay) / 7);
int numCompletedChanges = 0;
bool isDirectionBToC = true;
for (int scale = 0; scale < scaleColors.Length; scale++)
{
if (scaleColorsValue < previousArrayValue + previouslySelectedPlay)
{
if (scaleColors[scale] == OrderedColors.Colorless)
{
scaleColors[scale] = OrderedColors.Black;
numCompletedChanges++;
}
isDirectionBToC = false;
}
else if (scaleColorsValue > previousArrayValue + previouslySelectedPlay)
{
if (scaleColors[scale] == OrderedColors.Black)
{
scaleColors[scale] = OrderedColors.Colorless;
numCompletedChanges++;
}
isDirectionBToC = true;
}
if (numCompletedChanges >= numRequiredChanges)
{
if (isDirectionBToC == true)
{
scaleColorsValue -= (7 * numCompletedChanges);
}
else
{
scaleColorsValue += (7 * numCompletedChanges);
}
break;
}
}
}
}
else
{
textBox1.Text += previouslySelectedPlay + " was not a valid play.\r\n";
}
if (!areArraysEqual(previousArray, scaleColors) || consecutiveLagFailures >= 4)
{
// Code
int selectedPlay = selectPlay(strats, scaleColors);
// Code
if (selectedPlay == 0 || selectedPlay == 1 || selectedPlay == 3 || selectedPlay == 6)
{
SendKeys.Send(selectedPlay.ToString());
Array.Copy(scaleColors, previousArray, previousArray.Length);
previouslySelectedPlay = selectedPlay;
}
else
{
textBox1.Text += selectedPlay + " is not a valid play.\r\n";
}
}
else // If there was lag
{
// Code
takeTurn();
}
}
else
{
// Code
takeTurn();
}
}
private Bitmap takeScreenShot()
{
// Code
}
private string rgbToHsl(int r, int g, int b)
{
// Code
}
private hslColor medianHSAndL(hslColor[] inputs)
{
// Code
}
// Retrieves the appropriate game play for the array of medians
private int selectPlay(string[] strats, OrderedColors[] scaleColors)
{
// Code
}
private void delay(int min, int max, int maxDepth)
{
// Code
}
private void delay(int min, int max, int maxDepth, System.Windows.Forms.Timer timer)
{
// Code
}
private int value(OrderedColors[] scaleColors)
{
int numColorless = 0;
int numPurple = 0;
int numBlue = 0;
int numGreen = 0;
int numYellow = 0;
int numOrange = 0;
int numRed = 0;
int numBlack = 0;
int numNull = 0;
for (int scale = 0; scale <= 5; scale++)
{
if (scaleColors[scale] == OrderedColors.Colorless)
{
numColorless++;
}
if (scaleColors[scale] == OrderedColors.Purple)
{
numPurple++;
}
if (scaleColors[scale] == OrderedColors.Blue)
{
numBlue++;
}
if (scaleColors[scale] == OrderedColors.Green)
{
numGreen++;
}
if (scaleColors[scale] == OrderedColors.Yellow)
{
numYellow++;
}
if (scaleColors[scale] == OrderedColors.Orange)
{
numOrange++;
}
if (scaleColors[scale] == OrderedColors.Red)
{
numRed++;
}
if (scaleColors[scale] == OrderedColors.Black)
{
numBlack++;
}
if (scaleColors[scale] == OrderedColors.NULL)
{
numNull++;
}
}
if (numNull == 0)
{
return (7 * numBlack) + (6 * numRed) + (5 * numOrange) + (4 * numYellow) + (3 * numGreen) + (2 * numBlue) + numPurple;
}
else
{
return -2;
}
}
private double reward(OrderedColors[] scaleColors)
{
// Code
}
private bool areArraysEqual<T>(T[] array1, T[] array2)
{
Array.Sort(array1);
Array.Sort(array2);
if (Object.ReferenceEquals(array1, array2))
{
return true;
}
if (array1.Length != array2.Length)
{
return false;
}
for (int i = 0; i < array1.Length; i++)
{
if (array1[i].Equals(array2[i]) == false)
{
return false;
}
}
return true;
}
}
public class hslColor
{
// Code
}
public class hslRange
{
// Code
}
public enum OrderedColors
{
Colorless,
Purple,
Blue,
Green,
Yellow,
Orange,
Red,
Black,
NULL
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统、绘图、成像;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用System.IO;
使用系统线程;
命名空间宏
{
公共部分类Form1:Form
{
随机rn1=新随机();
bool state=false;//True表示打开,false表示关闭
字符串策略文件文本;
双重成本;
双基投注;
OrderedColors[]previousArray={OrderedColors.NULL,OrderedColors.NULL,OrderedColors.NULL,OrderedColors.NULL,OrderedColors.NULL,OrderedColors.NULL};
int先前选择的播放=2;
int continuencevelagfailures=0;
公共表格1()
{
初始化组件();
使用(StreamReader strategyFile=newstreamreader(“strategy.txt”))
{
strategyFileText=strategyFile.ReadToEnd();
}
}
私有无效复选框1\u CheckedChanged(对象发送方,事件参数e)
{
状态=打开关闭按钮。选中;
this.Cursor=新光标(Cursor.Current.Handle);
成本=Convert.ToDouble(textBox2.Text);
baseBet=Convert.ToDouble(textBox3.Text);
}
//代码
私有无效计时器1_刻度(对象发送方,事件参数e)
{
如果(州)
{
takeTurn();
延迟(17005200,5,定时器1);
}
}
私隐权
{
///睡眠(4000);
位图截图=截图();
//代码
如果(/*代码*/)
{
//基于屏幕截图设置scaleColors元素值的代码
int previousArrayValue=值(previousArray);
int scaleColorsValue=值(scaleColors);
如果(先前选择的播放==0)
{
对于(int scale=0;scale先前阵列值+先前选择的播放)
{
if(比例颜色[比例]==OrderedColor.Black)
{
鳞片颜色[鳞片]=有序颜色。无色;
numCompletedChanges++;
}
isDirectionBToC=true;
}
如果(numCompletedChanges>=numRequiredChanges)
{
如果(isDirectionBToC==true)
{
比例颜色值-=(7*numCompletedChanges);
}
其他的
{
ScaleColor值+=(7*numCompletedChanges);
}
打破