我编写了一个程序,用于检查对象的颜色,并根据结果向对象发送信号以更改颜色,然后检查新颜色等。根据可能颜色的HSL范围,无色组件经常被误解为黑色,因此我必须使用对象的行为模式来推断测量为黑色的组件是否实际上应该是无色的(反之亦然)。我还必须检查以确保对象确实改变了之前状态的颜色,因为滞后是可能的。我分别用于这两个任务的陈述是......
if (scaleColorsValue != previousArrayValue + previouslySelectedPlay && (scaleColorsValue - previousArrayValue - previouslySelectedPlay) % 7 == 0) // If expected result is off by a multiple of seven
...和...
if (!areArraysEqual(previousArray, scaleColors))
...其中areArraysEqual()
返回true
当且仅当对象的颜色与先前的测量相同时。
问题是如果我在滞后检查if语句中放置颜色校正if语句,当Colorless组件被解释为Black时,滞后检查将自动通过,因为这会导致它不等于之前的测量,但如果我在滞后检查之前放置颜色校正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
}
}