在“数字战争游戏”(如下所述)中,我需要为计算机制定策略,以使获胜的机会最大化。
游戏说明如下:
显示从0到9的20个随机数字。用户和计算机轮流使用。玩家可以在游戏中执行以下两种可能的动作之一:
例如:
start 5 3 7 8 9 1 2 0 8 9 4 2 1 5 0 3 6 7 9 8.
1 turn 5 3 7 8 9 1 2 0 8 9 4 2 1 5.
2 turn 5 3 7 8 9 0 2 0 8 9 4 2 1 5.
3 turn 5 3 7 8 9.
4 turn 5 2 7 8 9.
5 turn 5 1 7 8 9.
6 turn 5 0 7 8 9.
7 turn 5 0 7 8 8.
etc.
我发现,如果有两个偶数或两个奇数,并且轮到计算机了,那就赢了。但是,我不能为此提出算法。鉴于以下“数字大战”的实现,我需要程序的这一部分帮助:
#include <iostream>
#include <ctime>
using namespace std;
int coinFlip()
{
return rand() % 2;
}
void getInitialArray(int* array, int length)
{
srand(time(0));
do
for (int index = 0; index < length; index++)
array[index] = rand() % 10;
while (array[0] == 0);
}
void writeArray(int* array, int length)
{
for (int index = 0; index < length; index++)
if (array[index] >= 0)
cout << array[index];
cout << '\n';
}
int getUserDigit(int* array, int length)
{
cout << "Choose a digit``: ";
while (true)
{
int digit;
cin >> digit;
if (cin.fail())
{
cin.clear();
cin.ignore(100, '\n');
cout << "You entered incorect value.Try again. ";
}
else if (digit > length || digit <= 0)
cout << "You entered incorect value.Try again. ";
else
return digit;
}
}
void userTurn(int* array, int &length)
{
if (length > 0)
{
int digit = getUserDigit(array, length);
if (array[0] == 0 && length == 1)
{
cout << "You lost!\n";
length = 0;
return;
}
if (array[digit - 1] == 0)
{
for (int index = digit - 1; index < length; index++)
length = digit - 1;
}
else
array[digit - 1]--;
writeArray(array, length);
}
}
void computerTurn(int* array, int& length)
{
if (length > 0)
{
cout << "Computer turn: \n";
if (array[0] == 0 && length == 1)
{
cout << "You won\n";
length = 0;
return;
}
if (length == 1) array[0]--;
else if (array[0] == 0 && array[1] == 0) length = 1;
else if (array[2] > 1) array[2]--;
else if (array[0] % 2 == 0 && array[1] % 2 == 1 && array[2] == 0) length = 2;
else if (array[0] % 2 == 1 && array[1] % 2 == 0 && array[2] == 0) length = 2;
else if (length > 3 && array[0] % 2 == 1 && array[1] % 2 == 1 && array[2] == 0)
{
int random = rand() % (length - 3) + 3;
if (array[random] == 0) length = random;
else array[random]--;
}
else if (length > 3 && array[0] % 2 == 0 && array[1] % 2 == 0 && array[2] == 0)
{
int random = rand() % (length - 3) + 3;
if (array[random] == 0) length = random;
else array[random]--;
}
else if (length == 2 && array[0] % 2 == 0 && array[1] % 2 == 0) array[1]--;
else if (length == 2 && array[0] % 2 == 1 && array[1] % 2 == 1) array[1]--;
else if (length == 2 && array[0] % 2 == 0 && array[1] % 2 == 1)
{
int random = rand() % length;
if (array[random] == 0) length = random;
else array[random]--;
}
else if (length == 2 && array[0] % 2 == 1 && array[1] % 2 == 0)
{
int random = rand() % length;
if (array[random] == 0) length = random;
else array[random]--;
}
else if (length > 2 && array[0] % 2 == 0 && array[1] == 0) array[0]--;
else
{
int random = rand() % (length-2)+2;
if (array[random] == 0) length = random;
else array[random]--;
}
writeArray(array, length);
}
}
int main()
{
int length = 20;
int* array = new int[length];
getInitialArray(array, length);
writeArray(array, length);
if (coinFlip())
{
cout << "Player goes first.";
while (length > 0)
{
userTurn(array, length);
computerTurn(array, length);
}
}
else
{
cout << "Computer goes first.";
while (length > 0)
{
computerTurn(array, length);
userTurn(array, length);
}
}
delete[]array;
array = nullptr;
}