#include "stdafx.h"
#include <iostream>
#include <string>
#include <Windows.h>
#include <conio.h>
using namespace std;
class RandomChoice
{
private:
char option[6][20];
short size;
HANDLE handle_out;
COORD crd;
short pointer;
short delay;
public:
RandomChoice(char*option0, char*option1, char*option2 = NULL, char*option3 = NULL, char*option4 = NULL, char*option5 = NULL)
{
strcpy_s(option[0], option0);
strcpy_s(option[1], option1);
size = 2;
handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
crd.X = 5;
crd.Y = 4+(pointer<<1);
if (option2!=NULL)
{
strcpy_s(option[2], option2);
size++;
}
else return;
if (option3 != NULL)
{
strcpy_s(option[3], option3);
size++;
}
else return;
if (option4 != NULL)
{
strcpy_s(option[4], option4);
size++;
}
else return;
if (option5 != NULL)
{
strcpy_s(option[5], option5);
size++;
}
else return;
}
void Start()
{
cout << "\n您今天的活动安排是什么?做个选择吧\n\n\n";
for (short i = 0; i < size; i++)
{
cout << " " << option[i] << endl << endl;
}
}
void Refresh()
{
SetConsoleCursorPosition(handle_out, crd);
cout << " ";
pointer = (pointer + 1) % size;
crd.Y = 4 + (pointer << 1);
SetConsoleCursorPosition(handle_out, crd);
cout << "->";
}
short SetDelay(short d) { return delay = d; }
void Wait() { Sleep(delay); }
short Play()
{
while (!_kbhit())
{
Refresh();
Wait();
}
return pointer;
}
void ShowResult()
{
crd.Y = 18;
SetConsoleCursorPosition(handle_out, crd);
cout << "今天我就" << option[pointer] << "吧。天意啊!\n\n\n";
}
};
int main()
{
RandomChoice rc("看电影", "写代码", "逛公园", "约妹子", "睡懒觉", "打豆豆");
rc.SetDelay(200);
char choice;
do
{
system("cls");
rc.Start();
rc.Play();
rc.ShowResult();
_getch();
cout << "是否再来一次?Y/N";
cin >> choice;
} while (choice!='q');
system("pause");
return 0;
}