Tony041010

井字遊戲(with MiniMax)

Jun 17th, 2021 (edited)
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <time.h>
  6. #include <windows.h>
  7. using namespace std;
  8. char k;
  9. int p=0;
  10. vector<string> map(9, "");
  11. string chess;
  12.  
  13. bool IsMoveLeft(){ //判定還有沒有格子可以下
  14.     for(int i = 0 ; i < 9 ; i++){
  15.         if( map[i] == "" ){
  16.             return true;
  17.         }
  18.     }
  19.     return false;
  20. }
  21.  
  22. //XO
  23. int evalboard(){ //算分
  24.     for( int i = 0 ; i <=6 ; i+=3 ){
  25.         if( map[i] != "" && map[i] == map[i+1] && map[i] == map[i+2] ){
  26.             if( map[i] == "O"){
  27.                 return 10;
  28.             }
  29.             else{
  30.                 return -10;
  31.             }
  32.         }
  33.     }
  34.     for( int i = 0 ; i <=2 ; i++ ){
  35.         if( map[i] != "" && map[i] == map[i+3] && map[i] == map[i+6] ){
  36.             if( map[i] == "O"){
  37.                 return 10;
  38.             }
  39.             else{
  40.                 return -10;
  41.             }
  42.         }
  43.     }
  44.     if( map[0] != "" && map[0] == map[4] && map[0] == map[8] ){
  45.         if( map[0] == "O"){
  46.                 return 10;
  47.             }
  48.             else{
  49.                 return -10;
  50.             }
  51.     }
  52.     if( map[2] != "" && map[2] == map[4] && map[2] == map[6] ){
  53.         if( map[2] == "O"){
  54.                 return 10;
  55.             }
  56.             else{
  57.                 return -10;
  58.             }
  59.     }
  60.     return 0;
  61. }
  62.  
  63. void draw() {
  64.     system("cls");  //cls means clear screen
  65.     cout << IsMoveLeft() <<endl;
  66.     cout << evalboard() <<endl;
  67.     for( int i = 0 ; i <9 ; i++ ){
  68.         if(map[i] != ""){
  69.             cout << map[i];
  70.         }
  71.         else if(i ==p){
  72.             cout << "□";
  73.         }
  74.         else{
  75.             cout << "■";
  76.         }
  77.         if( i % 3 == 2 ){
  78.             cout << endl;
  79.         }
  80.     }
  81. }
  82.  
  83. int minimax( int depth , bool ismax ){ //MiniMax決策樹
  84.     int score = evalboard();
  85.     if( score != 0 ){
  86.         return score;
  87.     }
  88.     if( IsMoveLeft() == false ){
  89.         return 0;
  90.     }
  91.     int best , a;
  92.     if(ismax){
  93.         best = -11;
  94.         for(int i = 0 ; i < 9 ; i++){
  95.             if(map[i] == ""){
  96.                 map[i] = "O";
  97.                 a = minimax(depth+1 , not ismax) - depth;
  98.                 if( a > best ){
  99.                     best = a;
  100.                 }
  101.                 map[i] = "";
  102.             }
  103.         }
  104.     }
  105.     else{
  106.         best = 11;
  107.         for(int i = 0 ; i < 9 ; i++){
  108.             if( map[i] == "" ){
  109.                 map[i] = "X";
  110.                 a = minimax(depth+1 , not ismax ) + depth;
  111.                 if( a < best ){
  112.                     best = a;
  113.                 }
  114.                 map[i] = "";
  115.             }
  116.         }
  117.     }
  118.     return best;
  119. }
  120.  
  121. void BestMove(){
  122.     if( IsMoveLeft() == false ){
  123.         draw();
  124.         cout << "tie\n";
  125.     }
  126.     else{
  127.         int bestval = -11;
  128.         int bestidx = -1;
  129.         for(int i = 0 ; i < 9 ; i++){
  130.             if(map[i] == ""){
  131.                 map[i] = "O";
  132.                 int moveval = minimax(0 , false);
  133.                 map[i] = "";
  134.                 if( moveval > bestval ){
  135.                     bestval = moveval;
  136.                     bestidx = i;
  137.                 }
  138.             }
  139.         }
  140.         if( bestidx > -1 ){
  141.             map[bestidx] = "O";
  142.         }
  143.     }
  144. }
  145.  
  146. int main() {
  147.    
  148.     chess="X";  //XO全形字元,不然棋盤會歪掉,shift+space可以切換全形半形
  149.     draw();
  150.     string whowin="";
  151.     while (1) {
  152.         if (kbhit()) {
  153.             k=_getch();
  154.             if( k == 'q' ){
  155.                 break;
  156.             }
  157.             else if( k == 'e' ){
  158.                 p = p-3;
  159.             }
  160.             else if( k == 'd' ){
  161.                 p = p+3;
  162.             }
  163.             else if( k == 's' ){
  164.                 p = p-1;
  165.             }
  166.             else if( k == 'f' ){
  167.                 p = p+1;
  168.             }
  169.             else if( k == 'c' ){
  170.                 if( map[p] == "" ){
  171.                     map[p] = chess;
  172.                     if( chess == "X" ){
  173.                         BestMove();
  174.                         //chess = "O";
  175.                     }
  176.                     else{
  177.                         chess = "X";
  178.                     }
  179.                 }
  180.             }
  181.            
  182.             for( int i = 0 ; i <=6 ; i+=3 ){
  183.                 if( map[i] != "" && map[i] == map[i+1] && map[i] == map[i+2] ){
  184.                     whowin = map[i];
  185.                 }
  186.             }
  187.             for( int i = 0 ; i <=2 ; i++ ){
  188.                 if( map[i] != "" && map[i] == map[i+3] && map[i] == map[i+6] ){
  189.                     whowin = map[i];
  190.                 }
  191.             }
  192.             if( map[0] != "" && map[0] == map[4] && map[0] == map[8] ){
  193.                 whowin = map[0];
  194.             }
  195.             if( map[2] != "" && map[2] == map[4] && map[2] == map[6] ){
  196.                 whowin = map[2];
  197.             }
  198.            
  199.            
  200.             draw();
  201.             if( whowin != "" ){
  202.                 cout << whowin << "win";
  203.                 break;
  204.             }
  205.         }
  206.  
  207.     }
  208.  
  209.     return 0;
  210. }
  211.  
Add Comment
Please, Sign In to add comment