本文整理汇总了C++中Board::AITurn方法的典型用法代码示例。如果您正苦于以下问题:C++ Board::AITurn方法的具体用法?C++ Board::AITurn怎么用?C++ Board::AITurn使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Board
的用法示例。
在下文中一共展示了Board::AITurn方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main() {
sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Tic Tac Toe!");
window.setFramerateLimit(FPS);
Board* board = new Board(3, 3, SIZE);
bool p1Turn = true;
bool waiting = false;
sf::Clock clock;
sf::Time waitTime = sf::seconds(3);
sf::Font font;
if (!font.loadFromFile("/usr/share/fonts/truetype/droid/DroidSans.ttf")) return 1;
sf::Text endMessage;
endMessage.setFont(font);
endMessage.setColor(sf::Color::Red);
endMessage.setPosition(SIZE/2, SIZE/4);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
} else if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape) {
window.close();
}
} else if (event.type == sf::Event::MouseButtonReleased ) {
if (event.mouseButton.button == sf::Mouse::Left) {
if (p1Turn && !waiting) {
if (board->handleClick(event.mouseButton.x,
event.mouseButton.y)) {
p1Turn = !p1Turn;
}
}
}
}
}
if (waiting && clock.getElapsedTime() > waitTime) {
window.close();
}
tile winner;
if (!waiting && (((winner = board->checkWin()) != tile::empty) || board->allFull())) {
switch (winner) {
case tile::empty:
std::cout << "It's a tie!" << std::endl;
endMessage.setString("It's a tie!");
break;
case tile::cross:
std::cout << "AI wins!" << std::endl;
endMessage.setString("AI wins!");
break;
case tile::naught:
std::cout << "You win!" << std::endl;
endMessage.setString("You win!");
break;
default:
break;
}
endMessage.setOrigin(endMessage.getLocalBounds().width / 2, 0);
waiting = true;
clock.restart();
}
if (!waiting && !p1Turn) {
if (!board->AITurn()) {
waiting = true;
clock.restart();
}
p1Turn = !p1Turn;
}
window.clear(sf::Color::White);
board->draw(window);
if (waiting) {
window.draw(endMessage);
}
window.display();
}
delete board;
return 0;
}