对于我被要求提交的大部分工作应用程序(通常是游戏程序员)的代码示例,我创建了这个控制台应用程序,在这个应用程序中,我试图找出在我提出的简单的基于回合的战斗系统中,机械设备赢得比赛的最佳负载是什么。由于我将此作为代码示例提交,对我来说,重要的是它的工作和外观最好,我将非常感谢所有的输入,您可以给我。
我想提一下,我已经在去年夏天的某个时候发布了这,但从那时起,我对它进行了大量的修改(主要是因为我在C++11上学习过),所以我希望可以重新提交。
我感兴趣的是当你看到这个的时候,你会想到的所有东西--关于设计,实现,表示,命名,或者你认为重要的任何东西。
特别是,我是否真的利用了我所能使用的所有C+11概念,并且我是否以正确的方式使用它们?
代码和规则都可以在线获得:http://www.mz1net.com/code-sample
(更新:根据Jamal的回应,我用编辑更新了在线版本。)
这是Main.cpp -请看“MechArena.cpp”和“MechArena.h”在线:
#include <conio.h>
#include <iostream>
#include <sstream>
#include <time.h>
#include "MechArena.h"
// ======================================================================================
void init()
{
srand((int)time(0));
std::cout << "=== MECH ARENA ============================" << std::endl;
std::cout << "===========================================" << std::endl;
std::cout << std::endl;
}
// ======================================================================================
int promptPopulationSize()
{
std::cout << "Enter the population size (10K recommended): ";
// Loop until we have a valid result:
while (true)
{
std::string input;
getline(std::cin, input);
int size;
std::stringstream parser (input);
if (parser >> size)
{
if (size % 2 > 0)
{
std::cout << "Population size was rounded up to an even number: " << size+1 << std::endl;
size++;
}
std::cout << std::endl;
return size;
}
else
{
std::cout << "Please enter a valid number: ";
}
}
}
// ======================================================================================
void run()
{
init();
int populationSize = promptPopulationSize();
std::cout << "Random population seed... " << std::endl;
Population population (populationSize);
std::cout << "Population created." << std::endl;
std::cout << std::endl;
// This is the main application loop.
// Repeatedly, we prompt the user to choose a command, and than we act on his or her decision.
// We break the loop when the user chooses the 'exit' command.
while (true)
{
std::cout << "Instructions:" << std::endl;
std::cout << "-------------" << std::endl;
std::cout << "[Q/W/E/R]: Run 50/25/5/1 evolution steps." << std::endl;
std::cout << "[A/S/D]: Display the 10/5/1 best loadouts in the current population." << std::endl;
std::cout << "[X]: Exit." << std::endl;
char command = _getch();
std::cout << std::endl;
// NEXT GENERATION
int evolutionSteps = 0;
if (command == 'q') evolutionSteps = 50;
if (command == 'w') evolutionSteps = 25;
if (command == 'e') evolutionSteps = 5;
if (command == 'r') evolutionSteps = 1;
// Run the evolution steps:
for (int m = 0; m < evolutionSteps; m++)
{
std::cout << "Generation " << m+1 << "/" << evolutionSteps << ": ";
population.createNextGeneration();
std::cout << "Done." << std::endl;
}
// DISPLAY WINNERS
size_t displayWinners = 0;
if (command == 'a') displayWinners = 10;
if (command == 's') displayWinners = 5;
if (command == 'd') displayWinners = 1;
if (displayWinners > 0)
{
// Make sure that we were not asked to display more
// mechs than how many there are in the entire population.
size_t populationSize = population.getSize();
if (displayWinners > populationSize)
{
std::cout << "There are not that many mechs in the entire population - only " << populationSize << " will be shown.";
std::cout << std::endl;
displayWinners = populationSize;
}
std::cout << "TOP " << displayWinners << ((displayWinners > 1)? " MECHS" : " MECH") << " IN THE POPULATION: " << std::endl;
std::cout << "--------------------------------------" << std::endl;
std::cout << std::endl;
for (size_t m = 0; m < displayWinners; m++)
{
// Retrieve the mech to show.
const Mech& mech = population.getMech(m);
// Print out the mech's position and its score in the last match round.
std::cout << "TOP #" << m+1 << " (Last score: " << mech.getScore() << ")" << std::endl;
// Print out the mech's component setup.
std::cout << mech;
}
}
// EXIT
// Break the main application loop.
if (command == 'x')
break;
// CLEAR
std::cout << std::endl;
}
}
// ======================================================================================
int main (int argc, char* argv[])
{
try
{
run();
}
catch (const std::exception& e)
{
std::cout << std::endl;
std::cout << "[!] RUNTIME EXCEPTION ==============================" << std::endl;
std::cout << e.what() << std::endl;
std::cout << "Press any key to exit." << std::endl;
std::cin.ignore();
}
return 0;
}发布于 2014-01-12 21:25:40
对于将它用作作业应用程序的示例,我有些担心:它不包含任何virtual方法和子类。
无论您的代码写得多么清晰,它都不能显示对继承的理解:在工作申请中,继承(我不知道)可能是一个重要的标准。
如果这是正确的批评,也许改变游戏的设计,以便以一种自然的方式使用继承。
一种(先进的)方法可能是使武器的类型根据盔甲的类型而产生不同的效果(如在游戏或“岩石、纸、姐妹”中,武器的效果因目标而异)。我认为这是一个例子,因为“实现电子游戏”是用于教授一种名为双调的‘高级’技术的典型例子。在示例中使用该实现(如果游戏规则有对该实现技术的自然需求),可能会给审查简历/示例的人留下深刻印象。
人们也可能希望了解(即适当地使用一些)常见的设计模式。
我对这个建议表示歉意:它是基于对一些面试官可能需要的代码类型的愤世嫉俗的猜测。
同样,根据面试问题的经验,他们也可能想知道:
https://codereview.stackexchange.com/questions/39097
复制相似问题