|

楼主 |
发表于 2014-2-22 12:41:07
|
显示全部楼层
本帖最后由 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;
- int move_num() const { return moves_done; }
- bool used(Move player) const { return squares[player.row][player.col]; }
- private:
- int squares[3][3];
- int moves_done;
- int the_winner() const;
- };
- #endif
复制代码 main.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();
- }
- }
- }
- }
复制代码 |
|