鱼C论坛

 找回密码
 立即注册
查看: 5942|回复: 34

[技术交流] C++的一个小游戏,tic-tac-toe

[复制链接]
发表于 2014-2-22 11:30:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 andalousie 于 2014-2-22 13:16 编辑

书上的再加上自己写的终于完成的一个控制台小游戏tic-tac-toe(已经改正更新)
游客,如果您要查看本帖隐藏内容请回复

屏幕截图

屏幕截图

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-2-22 12:22:32 | 显示全部楼层
看看什么小游戏
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-22 12:41:07 | 显示全部楼层
本帖最后由 andalousie 于 2014-2-22 12:49 编辑

有点点小bug,改了应该会好。其实就是好几次输入同样的move也会可以。。
board.h
  1. #if !defined (BOARD_H)
  2. #define BOARD_H
  3. #include "move.h"
  4. #include "stack.h"

  5. class Board {
  6. public:
  7.    Board();                    //  constructor for initialization
  8.    bool done()  const;          //  Test whether the game is over.
  9.    void play(Move try_it);
  10.    int evaluate()  const;
  11.    int legal_moves(Stack<Move> &moves)  const;
  12.    int worst_case()  const;
  13.    bool better(int value, int old_value) const; //  Which parameter does the mover prefer?
  14.    void print()  const;
  15.    void instructions()  const;
  16.    int  move_num() const { return moves_done; }
  17.    bool used(Move player) const { return squares[player.row][player.col]; }
  18. private:
  19.    int squares[3][3];
  20.    int moves_done;
  21.    int the_winner() const;
  22. };

  23. #endif
复制代码
main.cpp
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include "stack.h"
  5. #include "board.h"
  6. #include "move.h"

  7. int look_ahead(const Board &game, int depth, Move &recommended)
  8. /*
  9. Pre:  Board game represents a legal game position.
  10. Post: An evaluation of the game, based on looking ahead
  11.       depth moves, is returned.  The best move that can be found
  12.       for the mover is recorded as Move recommended.
  13. Uses: The classes Stack, Board, and Move, together with
  14.       function look_ahead recursively.
  15. */
  16. {
  17.    if (game.done() || depth == 0)
  18.       return game.evaluate();
  19.    else {
  20.       Stack<Move> moves;
  21.       game.legal_moves(moves);
  22.       int value, best_value = game.worst_case();

  23.       while (!moves.Empty()) {
  24.          Move try_it;
  25.          Move reply;
  26.          try_it = moves.Top();
  27.          Board new_game = game;
  28.          new_game.play(try_it);
  29.          value = look_ahead(new_game, depth - 1, reply);
  30.          if (game.better(value, best_value)) { //  try_it is the best move yet found
  31.             best_value = value;
  32.             recommended = try_it;
  33.          }
  34.          moves.Pop();
  35.       }
  36.       return best_value;
  37.    }
  38. }

  39. void wait (int seconds)
  40. {
  41.   clock_t endwait;
  42.   endwait = clock () + seconds * CLOCKS_PER_SEC ;
  43.   while (clock() < endwait);
  44. }

  45. int main()
  46. {
  47.    Board game;
  48.    Move next_move;
  49.    game.instructions();
  50.    std::cout << " How much lookahead? ";
  51.    int looka;
  52.    std::cin >> looka;
  53.    std::cout << " Which mode? a. One(first) b. One(second) c. Two d. automatic ";
  54.    char mode;
  55.    std::cin >> mode;
  56.    while (!game.done()) {
  57.       if (looka > 0)
  58.          look_ahead(game, looka, next_move);
  59.       else
  60.       {
  61.          Stack<Move> moves;
  62.          game.legal_moves(moves);
  63.          next_move = moves.Top();
  64.       }
  65.       int expected;
  66.       switch(mode)
  67.       {
  68.          case 'a':
  69.          {
  70.             if(game.move_num() % 2 == 1) goto automatic;
  71.             std::cout << " Your move(row column) ";
  72.             Move player;
  73.             expected = look_ahead(game, looka, player);
  74.             std::cin >> player.row >> player.col;
  75.             while(game.used(player))
  76.             {
  77.                std::cout << "The position has been used. Again, your move(row column) "
  78.                std::cin >> player.row >> player.col;
  79.             }
  80.             game.play(player); system("cls"); game.print();
  81.             if(expected == look_ahead(game, looka, player))
  82.                std::cout << "Your move is so right.";
  83.             else
  84.                std::cout << "Your move is not best.";
  85.             break;
  86.          }
  87.          case 'b':
  88.          {
  89.             if(game.move_num() % 2 == 0) goto automatic;
  90.             std::cout << " Your move(row column) ";
  91.             Move player;
  92.             expected = look_ahead(game, looka, player);
  93.             std::cin >> player.row >> player.col;
  94.             while(game.used(player))
  95.             {
  96.                std::cout << "The position has been used. Again, your move(row column) "
  97.                std::cin >> player.row >> player.col;
  98.             }
  99.             game.play(player); system("cls"); game.print();
  100.             if(expected == look_ahead(game, looka, player))
  101.                std::cout << "Your move is so right.";
  102.             else
  103.                std::cout << "Your move is not best.";
  104.             break;
  105.          }
  106.          case 'c':
  107.          {
  108.             std::cout << " Your move(row column) ";
  109.             Move player;
  110.             expected = look_ahead(game, looka, player);
  111.             std::cin >> player.row >> player.col;
  112.             while(game.used(player))
  113.             {
  114.                std::cout << "The position has been used. Again, your move(row column) "
  115.                std::cin >> player.row >> player.col;
  116.             }
  117.             game.play(player); system("cls"); game.print();
  118.             if(expected == look_ahead(game, looka, player))
  119.                std::cout << "Your move is so right.";
  120.             else
  121.                std::cout << "Your move is not best.";
  122.             break;
  123.          }
  124.          case 'd':
  125.          default:
  126.             automatic:
  127.             {
  128.                wait(1);
  129.                game.play(next_move);
  130.                system("cls");
  131.                game.print();
  132.             }
  133.       }
  134.    }
  135. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-22 12:57:54 | 显示全部楼层
很好很强大
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-22 13:23:54 | 显示全部楼层
感谢楼主分享!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-22 15:00:51 | 显示全部楼层
课后题在哪里?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-22 15:12:49 | 显示全部楼层
什么小游戏,看看...
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-22 15:13:17 | 显示全部楼层
??????????????????????
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-22 15:28:24 | 显示全部楼层
什么东西。来顶一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-23 13:49:02 | 显示全部楼层
xiexielouzhu
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-23 14:52:33 | 显示全部楼层
(⊙o⊙)好腻害的赶脚
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-23 15:23:19 | 显示全部楼层
先看看哈  谢谢分享
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-23 16:32:29 | 显示全部楼层
这个不错{:1_1:}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-23 17:55:19 | 显示全部楼层
顶!!!!!!!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-23 19:20:15 | 显示全部楼层
see yi see
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-25 07:42:32 | 显示全部楼层
谢谢楼主  楼主大爱
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-25 15:38:35 | 显示全部楼层
{:1_1:}你看看你拉进来
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-25 16:10:56 | 显示全部楼层
keyihenhao
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-25 17:24:37 | 显示全部楼层
来看下咯。
。。。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-25 17:26:22 | 显示全部楼层
支持好好学习
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-15 05:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表