using namespace std;#include#includeclass Game{public: Game(); void print..._c++井字棋人机">
当前位置:   article > 正文

Easyx-----c++实现控制台井字棋简单人机对弈_c++井字棋人机

c++井字棋人机

 main.cpp

  1. #include"TicTacToe.h"
  2. int main()
  3. {
  4. Game game;
  5. game.getWinner();
  6. return 0;
  7. }

 TicTacToe.h

  1. #pragma once
  2. #include<iostream>
  3. using namespace std;
  4. #include<array>
  5. #include<ctime>
  6. class Game
  7. {
  8. public:
  9. Game();
  10. void print();
  11. char getCurrentPlayer();
  12. void getWinner();
  13. bool isDone(int row,int col);
  14. void makeMove();
  15. void computer_move(int row, int col);
  16. protected:
  17. array <array< char, 3 >, 3 > board;
  18. int row;
  19. int col;
  20. };

TicTacToe.cpp

  1. #include"TicTacToe.h"
  2. Game::Game()
  3. {
  4. for (int i = 0; i < 3; i++)
  5. {
  6. for (int j = 0; j < 3; j++)
  7. {
  8. board[i][j] = '-';
  9. }
  10. }
  11. this->col = 3;
  12. this->row = 3;
  13. }
  14. void Game::print()
  15. {
  16. cout << "\t1\t2\t3\n";
  17. for (int i = 0; i < 3; i++)
  18. {
  19. cout << i + 1;
  20. for (int j = 0; j < 3; j++)
  21. {
  22. cout << "\t";
  23. cout << board[i][j];
  24. }
  25. cout << endl;
  26. }
  27. }
  28. char Game::getCurrentPlayer()
  29. {
  30. int i = 0;
  31. for (; i < 3; i++)//判断第i行是否全都相同
  32. {
  33. if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != '-')
  34. return board[i][0];//将第i行的内容返回
  35. }
  36. for (i = 0; i < 3; i++)//判断第i列是否全都相同
  37. {
  38. if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != '-')
  39. return board[0][i];//将第i列的内容返回
  40. }
  41. if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != '-')//判断捺对角线(\)的内容是否全都相同
  42. return board[0][0];
  43. else if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != '-')//判断撇对角线(/)的内容是否全都相同
  44. return board[0][2];
  45. else if (isDone(row,col))//判断是否是平局,如果是平局返回‘q’
  46. return 'q';
  47. else //判断是否还未产生游戏结果,如果还未产生游戏结果返回‘-’
  48. return '-';
  49. }
  50. void Game::getWinner()
  51. {
  52. char a;
  53. do
  54. {
  55. print(); //屏幕上打印一个棋盘
  56. makeMove(); //打印出棋盘之后,玩家开始下棋
  57. a = getCurrentPlayer(); //玩家下完棋后,开始判断游戏结果
  58. if (a != '-') // ‘-’:没人赢
  59. {
  60. break; //如果已经分出胜负,跳出循环
  61. }
  62. computer_move(row,col); //如果没有分出胜负,电脑下棋
  63. a = getCurrentPlayer(); //下完之后判断游戏结果
  64. } while (a == '-');
  65. if (a == 'X') //判断玩家是否获胜:‘x’代表玩家获胜
  66. printf("Congratulations,you win!\n");
  67. else if (a == 'O') //判断玩家是否获胜:‘o’代表电脑获胜
  68. printf("It's too bad,you lose!\n");
  69. else //判断是否是平局
  70. printf("Draw!\n");
  71. }
  72. bool Game::isDone(int row,int col)
  73. {
  74. //判断数组当中每一个元素是否有'-',如果有'-',说明没有满,返回0;否则返回1
  75. int i, j;
  76. for (i = 0; i < row; i++)
  77. {
  78. for (j = 0; j < col; j++)
  79. if (board[i][j] == '-') //判断是否有'-'
  80. return 0; //有'-'返回0
  81. }
  82. return 1; //没有'-'返回1
  83. }
  84. void Game::makeMove()
  85. {
  86. int x, y;//先定义两个变量,以便接收玩家下棋的坐标
  87. do
  88. {
  89. printf("Please input your coordinate:(x,y)!");//提示玩家下棋
  90. scanf("%d%d", &x, &y); //接收玩家所下的位置
  91. if (x >= 1 && x <= 3 && y >= 1 && y <= 3) //判断玩家输入坐标是否有误
  92. if (board[x - 1][y - 1] == '-') //判断玩家输入的位置是否已经被占
  93. {
  94. board[x - 1][y - 1] = 'X'; //将玩家输入的位置用‘x’占用
  95. break;
  96. }
  97. else//玩家输入位置被占,提示玩家重新输入位置
  98. printf("Error!This place was be used!\n");
  99. else//玩家输入坐标有误,直接提示玩家error
  100. printf("Error!");
  101. } while (1);
  102. }
  103. void Game::computer_move(int row,int col)
  104. {
  105. srand((unsigned long)time(NULL));//利用函数生成随机数
  106. do
  107. {
  108. int x = rand() % row;//控制随机数小于3并把结果赋给横坐标
  109. int y = rand() % col;//控制随机数小于3并把结果赋给纵坐标
  110. if (board[x][y] == '-')//判断电脑选择的位置是否被占
  111. {
  112. board[x][y] = 'O';//将电脑下棋的位置用‘O’占用
  113. break;
  114. }
  115. } while (1);
  116. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/94045
推荐阅读
相关标签