我们必须在Uni的C++中为Nim开发一款游戏,并且我正在很好地处理它,它只是我遇到了一个问题,当玩家选择一个堆栈时,它将以阵列号而不是屏幕上的数字。我很确定这只是一个“-1”,但是我无法找到它,在我试图放置“-1”的任何地方,它最终以最后一个计数器计数。下面Nim Game - 堆栈选择
代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string>
#include <iomanip>
char star = '*';
const int HEAPS = 3;
int heaps[HEAPS];
int heapNumber[] = {1,2,3};
int playerHeapChoice;
int playerCounterChoice;
int playerTurn()
{
std::cout << "Which stack would you like to take counters from?";
std::cin >> playerHeapChoice;
std::cout << "How many counters would you like to take from the heap?";
std::cin >> playerCounterChoice;
heaps[playerHeapChoice] = heaps[playerHeapChoice] - playerCounterChoice;
std::cout << "There are " << heaps[playerHeapChoice] << " counters left in this stack.";
return heaps[playerHeapChoice];
}
int main()
{
srand(time(NULL));
for (int i = 0; i < HEAPS; i++)
{
heaps[i] = (rand() % 20) + 1;
}
std::cout << "Stack" << std::setw(8) << " Number" << std::setw(8) << " Counters" << std::endl;
for (int count = 0; count < HEAPS; count++)
{
std::cout << heapNumber[count] << std::setw(8) << heaps[count] << std::setw(8);
for (int count = 0; count < heaps[count]; count++)
{
std::cout << star;
}
std::cout << std::endl;
}
playerTurn();
_getch();
return 0;
}
我看到有几个问题。 'for(int count = 0; count> playerHeapChoice;'将playerHeapChoce递减1之前,用'playerHeapChoice - ;'' –
2014-10-09 17:18:46