C++的一个小游戏,tic-tac-toe
本帖最后由 andalousie 于 2014-2-22 13:16 编辑书上的再加上自己写的终于完成的一个控制台小游戏tic-tac-toe(已经改正更新)
**** Hidden Message *****
看看什么小游戏 本帖最后由 andalousie 于 2014-2-22 12:49 编辑
有点点小bug,改了应该会好。其实就是好几次输入同样的move也会可以。。
board.h#if !defined (BOARD_H)
#define BOARD_H
#include "move.h"
#include "stack.h"
class Board {
public:
Board(); //constructor for initialization
bool done()const; //Test whether the game is over.
void play(Move try_it);
int evaluate()const;
int legal_moves(Stack<Move> &moves)const;
int worst_case()const;
bool better(int value, int old_value) const; //Which parameter does the mover prefer?
void print()const;
void instructions()const;
intmove_num() const { return moves_done; }
bool used(Move player) const { return squares; }
private:
int squares;
int moves_done;
int the_winner() const;
};
#endifmain.cpp#include <iostream>
#include <cstdlib>
#include <ctime>
#include "stack.h"
#include "board.h"
#include "move.h"
int look_ahead(const Board &game, int depth, Move &recommended)
/*
Pre:Board game represents a legal game position.
Post: An evaluation of the game, based on looking ahead
depth moves, is returned.The best move that can be found
for the mover is recorded as Move recommended.
Uses: The classes Stack, Board, and Move, together with
function look_ahead recursively.
*/
{
if (game.done() || depth == 0)
return game.evaluate();
else {
Stack<Move> moves;
game.legal_moves(moves);
int value, best_value = game.worst_case();
while (!moves.Empty()) {
Move try_it;
Move reply;
try_it = moves.Top();
Board new_game = game;
new_game.play(try_it);
value = look_ahead(new_game, depth - 1, reply);
if (game.better(value, best_value)) { //try_it is the best move yet found
best_value = value;
recommended = try_it;
}
moves.Pop();
}
return best_value;
}
}
void wait (int seconds)
{
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait);
}
int main()
{
Board game;
Move next_move;
game.instructions();
std::cout << " How much lookahead? ";
int looka;
std::cin >> looka;
std::cout << " Which mode? a. One(first) b. One(second) c. Two d. automatic ";
char mode;
std::cin >> mode;
while (!game.done()) {
if (looka > 0)
look_ahead(game, looka, next_move);
else
{
Stack<Move> moves;
game.legal_moves(moves);
next_move = moves.Top();
}
int expected;
switch(mode)
{
case 'a':
{
if(game.move_num() % 2 == 1) goto automatic;
std::cout << " Your move(row column) ";
Move player;
expected = look_ahead(game, looka, player);
std::cin >> player.row >> player.col;
while(game.used(player))
{
std::cout << "The position has been used. Again, your move(row column) "
std::cin >> player.row >> player.col;
}
game.play(player); system("cls"); game.print();
if(expected == look_ahead(game, looka, player))
std::cout << "Your move is so right.";
else
std::cout << "Your move is not best.";
break;
}
case 'b':
{
if(game.move_num() % 2 == 0) goto automatic;
std::cout << " Your move(row column) ";
Move player;
expected = look_ahead(game, looka, player);
std::cin >> player.row >> player.col;
while(game.used(player))
{
std::cout << "The position has been used. Again, your move(row column) "
std::cin >> player.row >> player.col;
}
game.play(player); system("cls"); game.print();
if(expected == look_ahead(game, looka, player))
std::cout << "Your move is so right.";
else
std::cout << "Your move is not best.";
break;
}
case 'c':
{
std::cout << " Your move(row column) ";
Move player;
expected = look_ahead(game, looka, player);
std::cin >> player.row >> player.col;
while(game.used(player))
{
std::cout << "The position has been used. Again, your move(row column) "
std::cin >> player.row >> player.col;
}
game.play(player); system("cls"); game.print();
if(expected == look_ahead(game, looka, player))
std::cout << "Your move is so right.";
else
std::cout << "Your move is not best.";
break;
}
case 'd':
default:
automatic:
{
wait(1);
game.play(next_move);
system("cls");
game.print();
}
}
}
} 很好很强大 感谢楼主分享! 课后题在哪里? 什么小游戏,看看... ?????????????????????? 什么东西。来顶一下 xiexielouzhu (⊙o⊙)好腻害的赶脚 先看看哈谢谢分享 这个不错{:1_1:} 顶!!!!!!!!!! see yi see 谢谢楼主楼主大爱
{:1_1:}你看看你拉进来 keyihenhao 来看下咯。
。。。。。 支持好好学习
页:
[1]
2