473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

4 x 4 tic tac toe

Expand|Select|Wrap|Line Numbers
  1. //=====================================================================================
  2. // Program Name: TicTacToe
  3. // Author: Gonzales Cenelia
  4. // Website: www.ai-search.4t.com
  5. //
  6. // This is a complete Tictactoe game, it include many functionalities, you can play games
  7. // against another human, you can play against the computer, you can also even let the computer
  8. // play against itself. The A.I is very good, actualy it is unbeatable, the most you can do is to 
  9. // draw the games. Also, the game include statistics (number of win, draw an lost games) for the
  10. // current match between players. The A.I for this tictactoe game is an implementation of the
  11. // MiniMax Algorithm, the algorithm was implemented by using three functions, Minimax(...),
  12. // MinMove(...) and MaxMove(...), as you probably may have guest it, the main function in this
  13. // algorithm is MiniMax(...), it returns the best move for the current computer player.
  14. // To be capable of choosing the best move, the program takes time to generate all the possible
  15. // outcomes for the current board position and at the same time, it decide wich one is the best.
  16. //
  17. // This code is copyrighted and has limited warranty. //****************
  18. //*********************************************
  19.  
  20. #include <windows.h>
  21. #include <iostream>
  22. #include <string>
  23. #include <ctime>
  24. #include <list>
  25.  
  26.  
  27. static const int INFINITY = 1000000;
  28.  
  29. // declare the possible states for the game session
  30. static enum {START, PLAYING, QUIT, OWIN, XWIN, DRAW} state;
  31.  
  32. // declare possible display mode
  33. static enum {REGULAR, PROGRESSIVE} display_mode;
  34.  
  35. // this stucture simply defines the characteristic of each player
  36. typedef struct {
  37.     std::string name;
  38.     char symbol;
  39.     int move;
  40.     int game_win;
  41.     int draw_num;
  42.     bool selected;
  43.     bool win;
  44. } player;
  45.  
  46.  
  47. void display_board();                // display the current board position on the screen
  48. void seed_random_generator();       // seed the random generator (rand function) with the current time
  49. void get_move();                    // get move for the current player, the computer also use that function
  50. void display_result();                // display the final result of the game on the screen
  51. void select_game_type();            // this function generate the menu for the game
  52. void get_player_symbol();           // get symbol (X, O) for the first player selected
  53. void get_player_name();             // gets the name of the current player
  54. void reset_player_name();           // reset the name of the players
  55. void set_game_statistic();          // this function makes update of the stististic for the game
  56. void display_game_statistic();      // this function display the statistic for the current games
  57. void reset_game_statistic();        // resets the game statistic
  58. void find_winner();                 // this function finds the winner once the agme has ended
  59. void reset_winner();                // resets the winners before starting new game
  60. void update_board();                // this function makes an update of the current board position                 
  61. void update_screen();               // updates the screen
  62. void select_display_mode();         // selects different display style for displaying tictactoe board
  63. void set_game_level();              // will probably be be featured in the next version of this program
  64. void verify_move();                 // verifies if the current move is legal
  65. void reset_state();                 // resets the state of the game
  66. void reset_board();                 // clears the board, removes all move that has been made
  67. void initialise_player_move();      // reinitialise players move        
  68. void display_intro();               // display some text on the screen to introduce the game
  69. void display_game_progress();       // displays who made the last and also current position
  70. void update_game();                 // makes updates for the current position
  71. void setup_game_screen();            // reset the dimension of the dos console window
  72. bool wrong_symbol();                // check if the symbols that are choose by players are valid
  73. bool wrong_selection();             // verifies the validity of current selection made by the player
  74. bool game_over();                   // checks if the game is over
  75. bool free_square();                 // verifies if the square chosen by the current player is free
  76.  
  77.  
  78. // generates the list of legal moves for the current position
  79. void generate_moves(char _board[16], std::list<int> &move_list);
  80.  
  81. // determin current game state (XWIN, OWIN, DRAW, PLAYING)
  82. void check_game_state(char board[16]);
  83.  
  84. // makes an evaluation of the current board position,
  85. // the function returns +INFINITY if the computer is winning,
  86. // -INFINITY if the computer is loosing or 0 if it is a draw
  87. int evaluate_position(char _board[16], player _player);
  88.  
  89.  
  90. // the three functions below implements the MiniMax algorithm
  91.  
  92. int MiniMax(char _board[16], player _player);    // main function for MiniMax algorithm
  93. int MinMove(char _board[16], player _player);    // helper function for MiniMax algorithm
  94. int MaxMove(char _board[16], player _player);    // helper function for MiniMax algorithm
  95.  
  96. static player player1, player2, current_player; // 'player1' represent the first player and 'player2' the second one
  97. static std::string game_type; // 'human vs human', 'human vs computer', 'computer vs computer'
  98. static std::string prev_game_type; // variable for storing previous 'game type'
  99.  
  100. static char board[16] = {0}; // this variable is used to represent the board for the game
  101. static char cSymbol; // this variable represent the symbol for the current player
  102. static int nMove; // represents the last move made by the current player
  103.  
  104.  
  105. int main() {
  106.     seed_random_generator();
  107.     setup_game_screen();
  108.  
  109.     display_intro();
  110.     select_game_type();
  111.  
  112.     if(state != QUIT) {
  113.         get_player_name();
  114.         get_player_symbol();
  115.  
  116.         while(state != QUIT) {
  117.             while(state == PLAYING) {
  118.                 initialise_player_move();
  119.                 get_move();
  120.                 update_game();
  121.             }
  122.  
  123.             if(game_over()) {
  124.                 find_winner();
  125.                 display_result();
  126.                 set_game_statistic();
  127.                 reset_state();
  128.                 reset_board();
  129.                 display_intro();
  130.             }
  131.  
  132.             select_game_type();
  133.         }
  134.     }
  135.     return 0;
  136. }
  137.  
  138. // selects game type
  139. void select_game_type() {
  140.     std::cout << "   1 - play a game against the computer" << std::endl;
  141.     std::cout << "   2 - continue with the same player" << std::endl;
  142.     std::cout << "   3 - play against another player" << std::endl;
  143.     std::cout << "   4 - start automatic game" << std::endl;
  144.     std::cout << "   5 - display game statistic" << std::endl;
  145.     std::cout << "   6 - set display mode" << std::endl;
  146.     std::cout << "   7 - quit the program" << std::endl;
  147.     std::cout << "\nselection: ";
  148.     int choice;
  149.     std::cin >> choice;
  150.     if(!std::cin.good()) {
  151.         std::cout << "please notice that you can only enter integers for the selection" << std::endl;
  152.         update_screen();
  153.     }
  154.     switch(choice) {
  155.         case 1:
  156.             game_type = "human vs computer";
  157.             break;
  158.         case 2:
  159.             if(game_type == "") {
  160.                 update_screen();
  161.             }
  162.             break;
  163.         case 3:
  164.             game_type = "human vs human";
  165.             break;
  166.         case 4:
  167.             game_type = "computer vs computer";
  168.             break;
  169.         case 5:
  170.             display_game_statistic();
  171.             update_screen();
  172.             break;
  173.         case 6:
  174.             select_display_mode();
  175.             update_screen();
  176.             break;
  177.         case 7:
  178.             state = QUIT;
  179.             break;
  180.         default:
  181.             std::cout << "Invalid Selection." << std::endl;
  182.             update_screen();
  183.     }
  184.     if(choice > 0 && choice < 7) {
  185.         if(prev_game_type != "" && game_type != prev_game_type) {
  186.             reset_game_statistic();
  187.             reset_player_name();
  188.             get_player_name();
  189.             get_player_symbol();
  190.         }
  191.         if(game_type.length() > 0) {
  192.             prev_game_type = game_type;
  193.         }
  194.     }
  195. }
  196.  
  197. // gets current player name
  198. void get_player_name() {
  199.     std::cin.sync();
  200.     if(game_type == "human vs computer") {
  201.         std::cout << "\nplease enter your name: ";
  202.         std::getline(std::cin, player1.name);
  203.         if(player1.name.length() == 0) {
  204.             get_player_name();
  205.         }
  206.         player2.name = "the computer";
  207.     } else if(game_type == "human vs human") {
  208.         while(player1.name.length() == 0) {
  209.             std::cout << "\nplayer1 please enter your name: ";
  210.             std::getline(std::cin, player1.name);
  211.         }
  212.         while(player2.name.length() == 0) {
  213.             std::cout << "player2 please enter your name: ";
  214.             std::getline(std::cin, player2.name);
  215.         }
  216.     } else if(game_type == "computer vs computer") {
  217.         player1.name = "computer player1";
  218.         player2.name = "computer player2";
  219.     }
  220. }
  221.  
  222. void reset_player_name() {
  223.     player1.name.erase();
  224.     player2.name.erase();
  225. }
  226.  
  227. // gets symbol for the current player
  228. void get_player_symbol() {
  229.     if(game_type == "human vs computer") {
  230.         int selection = rand() % 2;
  231.         if(selection == 0) {
  232.             rand() % 2 == 0 ? player2.symbol = 'X' : player2.symbol = 'O';
  233.             cSymbol = player2.symbol;
  234.             player2.selected = 1;
  235.             std::cout << player2.name << " will play \'" << player2.symbol << "\'" << std::endl;
  236.         } else if(selection == 1) {
  237.             std::cout << player1.name << " please enter your symbol (X, O): ";
  238.             std::cin >> player1.symbol;
  239.             player1.symbol = toupper(player1.symbol);
  240.             cSymbol = player1.symbol;
  241.             player1.selected = 1;
  242.         }
  243.     } else if(game_type == "human vs human") {
  244.         int sel = rand() % 2;
  245.         std::string player_name = "";
  246.         if(sel == 0) {
  247.             player_name = player1.name;
  248.             player1.selected = 1;
  249.         } else if(sel == 1) {
  250.             player_name = player2.name;
  251.             player2.selected = 1;
  252.         }
  253.         std::cout << "\n" << player_name << " please enter your symbol (X, O): ";
  254.         if(sel == 0) {
  255.             std::cin >> player1.symbol;
  256.             player1.symbol = toupper(player1.symbol);
  257.             cSymbol = player1.symbol;
  258.         } else {
  259.             std::cin >> player2.symbol;
  260.             player2.symbol = toupper(player2.symbol);
  261.             cSymbol = player2.symbol;
  262.         }
  263.     } else if(game_type == "computer vs computer") {
  264.         std::string player_name;
  265.         int sel = rand() % 2;
  266.         if(sel == 0) {
  267.             rand() % 2 == 0 ? player1.symbol = 'X' : player1.symbol = 'O';
  268.             player_name = player1.name;
  269.             player1.selected = 1;
  270.             cSymbol = player1.symbol;
  271.         } if(sel == 1) {
  272.             rand() % 2 == 0 ? player2.symbol = 'X' : player2.symbol = 'O';
  273.             player_name = player2.name;
  274.             player2.selected = 1;
  275.             cSymbol = player2.symbol;
  276.         }
  277.         std::cout << player_name << " will play \'" << cSymbol << "\'" << std::endl;
  278.     }
  279.     if(!std::cin.good() || wrong_symbol()) {
  280.         std::cout << "please notice that your symbol can only be X or O" << std::endl;
  281.         system("pause");
  282.         get_player_symbol();
  283.     }
  284.     if(!player2.selected) {
  285.         player1.symbol == 'X' ? player2.symbol = 'O' : player2.symbol = 'X';
  286.         player1.symbol == 'O' ? player2.symbol = 'X' : player2.symbol = 'O';
  287.     } else if(!player1.selected) {
  288.         player2.symbol == 'X' ? player1.symbol = 'O' : player1.symbol = 'X';
  289.         player2.symbol == 'O' ? player1.symbol = 'X' : player1.symbol = 'O';
  290.     }
  291.     state = PLAYING;
  292. }
  293.  
  294. // gets move for the current player
  295. void get_move() {
  296.     std::cin.sync();
  297.     if(game_type == "human vs human") {
  298.         if(player1.selected) {
  299.             std::cout << player1.name << " please enter your move (1 - 16): ";
  300.             std::cin >> player1.move;
  301.             nMove = player1.move;
  302.             cSymbol = player1.symbol;
  303.             player1.selected = 0;
  304.             player2.selected = 1;
  305.             current_player = player1;
  306.         } else if(player2.selected) {
  307.             std::cout << player2.name << " please enter your move (1 - 16): ";
  308.             std::cin >> player2.move;
  309.             nMove = player2.move;
  310.             cSymbol = player2.symbol;
  311.             player1.selected = 1;
  312.             player2.selected = 0;
  313.             current_player = player2;
  314.         }
  315.     } else if(game_type == "human vs computer") {
  316.         if(player1.selected) {
  317.             std::cout << "\n" << player1.name << " please enter your move (1 - 16): ";
  318.             std::cin >> player1.move;
  319.             if(!std::cin.good()) {
  320.                 std::cin.clear();
  321.                 std::cin.sync();
  322.             }
  323.             nMove = player1.move;
  324.             cSymbol = player1.symbol;
  325.             current_player = player1;
  326.             player1.selected = 0;
  327.             player2.selected = 1;
  328.             Sleep(1000);
  329.         } else if(player2.selected) {
  330.             player2.move = MiniMax(board, player2);
  331.             nMove = player2.move;
  332.             cSymbol = player2.symbol;
  333.             current_player = player2;
  334.             player1.selected = 1;
  335.             player2.selected = 0;
  336.             reset_state();
  337.             Sleep(1500);
  338.         }
  339.     } else if(game_type == "computer vs computer") {
  340.         if(player1.selected) {
  341.             player1.move = MiniMax(board, player1);
  342.             nMove = player1.move;
  343.             cSymbol = player1.symbol;
  344.             current_player = player1;
  345.             player1.selected = 0;
  346.             player2.selected = 1;
  347.             reset_state();
  348.             Sleep(2500);
  349.         } else if(player2.selected) {
  350.             player2.move = MiniMax(board, player2);
  351.             nMove = player2.move;
  352.             cSymbol = player2.symbol;
  353.             current_player = player2;
  354.             player1.selected = 1;
  355.             player2.selected = 0;
  356.             reset_state();
  357.             Sleep(2500);
  358.         }
  359.     }
  360.     verify_move();
  361.     if(game_over()) {
  362.         return;
  363.     }
  364. }
  365.  
  366. // set game statististic for current match
  367. void set_game_statistic() {
  368.     if(state == START) {
  369.         player1.game_win = 0;
  370.         player1.draw_num = 0;
  371.         player1.win = 0;
  372.         player2.game_win = 0;
  373.         player2.draw_num = 0;
  374.         player2.win = 0;
  375.     } else if(state == XWIN || state == OWIN) {
  376.         if(player1.win) {
  377.             player1.game_win++;
  378.             player1.win = 0;
  379.         } else if(player2.win) {
  380.             player2.game_win++;
  381.             player2.win = 0;
  382.         }
  383.     } else if(state == DRAW) {
  384.         player1.draw_num++;
  385.         player2.draw_num++;
  386.     }
  387. }
  388.  
  389. // resets game statistic
  390. void reset_game_statistic() {
  391.     player1.game_win = 0;
  392.     player1.draw_num = 0;
  393.     player1.win = 0;
  394.     player2.game_win = 0;
  395.     player2.draw_num = 0;
  396.     player2.win = 0;
  397.     player1.selected = 0;
  398.     player2.selected = 0;
  399. }
  400.  
  401. // displayes game statistic on the screen
  402. void display_game_statistic() {
  403.     if(state != START) {
  404.         std::cout << "\ngame statistic" << std::endl;
  405.         std::cout << "==============" << std::endl;
  406.         std::cout << player1.name << " has won " << player1.game_win << " game(s)." << std::endl; 
  407.         std::cout << player2.name << " has won " << player2.game_win << " game(s)." << std::endl;
  408.         std::cout << player1.draw_num << " game(s) ended with a draw." << std::endl;
  409.     }
  410. }
  411.  
  412. // finds the winner once the game is over
  413. void find_winner() {
  414.     if(state == XWIN && player1.symbol == 'X') {
  415.         player1.win = 1;
  416.     } else if(state == OWIN && player1.symbol == 'O') {
  417.         player1.win = 1;
  418.     } else if(state == XWIN && player2.symbol == 'X') {
  419.         player2.win = 1;
  420.     } else if(state == OWIN && player2.symbol == 'O') {
  421.         player2.win = 1;
  422.     }
  423. }
  424.  
  425. // resets winners
  426. void reset_winner() {
  427.     player1.win = 0;
  428.     player2.win = 0;
  429. }
  430.  
  431. // verifies validity of symbols (X, O)
  432. bool wrong_symbol() {
  433.     return (cSymbol != 'X' && cSymbol != 'O');
  434. }
  435.  
  436. // checks for wrong selection
  437. bool wrong_selection() {
  438.     return !(nMove > 0 && nMove < 10);
  439. }
  440.  
  441. // reinitialise player moves
  442. void initialise_player_move() {
  443.     player1.move = -1;
  444.     player2.move = -1;
  445. }
  446.  
  447. // check for ending of the game
  448. bool game_over() {
  449.     return (state == XWIN || state == OWIN || state == DRAW);
  450. }
  451.  
  452. // resets state of the game
  453. void reset_state() {
  454.     state = PLAYING;
  455. }
  456.  
  457. // clears the board
  458. void reset_board() {
  459.     for(int i = 0; i < 16; ++i) {
  460.         board[i] = 0;
  461.     }
  462. }
  463.  
  464. // updates currrent board position
  465. void update_board() {
  466.     if(state == PLAYING) {
  467.         if(player1.move != -1 && player2.move == -1) {
  468.             board[player1.move - 1] = player1.symbol;
  469.         } else if(player2.move != -1) {
  470.             board[player2.move - 1] = player2.symbol;
  471.         }
  472.     }
  473. }
  474.  
  475. // selects display mode
  476. void select_display_mode() {
  477.     std::cout << "\n===================" << std::endl;
  478.     std::cout << "seting display mode" << std::endl;
  479.     std::cout << "===================" << std::endl;
  480.     std::cout << "   1 - regular" << std::endl;
  481.     std::cout << "   2 - progressive" << std::endl;
  482.     std::cout << "\nchoice: ";
  483.     int choice;
  484.     std::cin >> choice;
  485.     while(choice != 1 && choice != 2) {
  486.         std::cin.clear();
  487.         std::cin.sync();
  488.         std::cout << "wrong selection, choose '1' or '2': ";
  489.         std::cin >> choice;
  490.     }
  491.     std::cout << "the display mode was sucessfuly changed!" << std::endl;
  492.     if(choice == 1) {
  493.         display_mode = REGULAR;
  494.     } else if(choice == 2) {
  495.         display_mode = PROGRESSIVE;
  496.     }
  497. }
  498.  
  499. // displays outcome the game on the screen
  500. void display_result() {
  501.     if(player1.win) {
  502.         std::cout << player1.name << " has won the game!" << std::endl;
  503.     } else if(player2.win) {
  504.         std::cout << player2.name << " has won the game!" << std::endl;
  505.     } else if(player1.win == 0 && player2.win == 0) {
  506.         std::cout << "no winner, this game is a draw." << std::endl;
  507.     }
  508.     system("pause");
  509.     system("cls");
  510. }
  511.  
  512. // make updates of the current game
  513. void update_game() {
  514.     update_board();
  515.     display_game_progress();
  516.     check_game_state(board);
  517. }
  518.  
  519. // checks is the square selected by the current player is free
  520. bool free_square() {
  521.     if(player1.move != -1 && player2.move == -1) {
  522.         return board[player1.move - 1] == 0;
  523.     } else if(player2.move != -1) {
  524.         return board[player2.move - 1] == 0;
  525.     }
  526.     return 0;
  527. }
  528.  
  529. // displays board on the screen
  530. void display_board() {
  531.     std::cout << std::endl;
  532.     std::cout << " " << board[0] << " | " << board[1] << " | " << board[2] << " | "<< board[3]<<std::endl;
  533.     std::cout << "-----------------" << std::endl;
  534.     std::cout <<  " " << board[4] << " | " << board[5] << " | " << board[6] << " | "<< board[7]<< std::endl;
  535.     std::cout << "-----------------" << std::endl;
  536.     std::cout <<  " " << board[8] << " | " << board[9] << " | " << board[10] << " | "<< board[11]<< std::endl;
  537.     std::cout << "-----------------" << std::endl;
  538.     std::cout <<  " " << board[12] << " | " << board[13] << " | " << board[14]<< " | " << board[15]<< std::endl;
  539.     std::cout << std::endl;
  540.     std::cin.sync();
  541. }
  542.  
  543. // displays the progress of the game
  544. void display_game_progress() {
  545.     if(display_mode == PROGRESSIVE) {
  546.         system("cls");
  547.         display_intro();
  548.     }
  549.     std::cout << "\n\nboard position after " << current_player.name << "\'s move" << std::endl;
  550.     display_board();
  551. }
  552.  
  553. // verifies validity of the current move
  554. // if the player makes an invalid move
  555. // the function will ask the player
  556. // to select another move
  557. void verify_move() {
  558.     if(wrong_selection() || !free_square()) {
  559.         std::cout << "Invalid Move." << std::endl;
  560.         if(player2.move == -1) {
  561.             player1.selected = 1;
  562.             player2.selected = 0;
  563.         } else {
  564.             player1.selected = 0;
  565.             player2.selected = 1;
  566.         }
  567.         system("pause");
  568.         if(game_type == "human vs computer") {
  569.             player1.selected = 1;
  570.             player2.selected = 0;
  571.         }
  572.         get_move();
  573.     }
  574. }
  575.  
  576. // seeds random generator with current time
  577. void seed_random_generator() {
  578.     srand((unsigned) time(NULL));
  579. }
  580.  
  581. // displays intro for the game
  582. void display_intro() {
  583.     std::cout << "\n=========================================" << std::endl;
  584.     std::cout << "  TicTacToe game v1.0 - Gonzales Cenelia    " << std::endl;
  585.     std::cout << "=========================================" << std::endl;
  586. }
  587.  
  588. // resize the dimension of the dos console window
  589. void setup_game_screen() {
  590.     system("mode con: cols=99 lines=300");
  591. }
  592.  
  593. // refresh game screen
  594. void update_screen() {
  595.     system("pause");
  596.     system("cls");
  597.     std::cin.clear();
  598.     std::cin.sync();
  599.     display_intro();
  600.     select_game_type();
  601. }
  602.  
  603. // determins if the current board position is final, if it is a win, draw
  604. void check_game_state(char board[16]) {
  605.     if ((board[0] == cSymbol && board[1] == cSymbol && board[2] == cSymbol && board[3] == cSymbol) ||
  606.         (board[4] == cSymbol && board[5] == cSymbol && board[6] == cSymbol && board[7] == cSymbol) ||
  607.         (board[8] == cSymbol && board[9] == cSymbol && board[10] == cSymbol && board[11] == cSymbol) ||
  608.         (board[12] == cSymbol && board[13] == cSymbol && board[14] == cSymbol && board[15] == cSymbol) ||
  609.         (board[0] == cSymbol && board[4] == cSymbol && board[8] == cSymbol && board[12] == cSymbol) ||
  610.         (board[1] == cSymbol && board[5] == cSymbol && board[9] == cSymbol &&  board[13] == cSymbol) ||
  611.         (board[2] == cSymbol && board[6] == cSymbol && board[10] == cSymbol && board[14] == cSymbol) ||
  612.         (board[3] == cSymbol && board[7] == cSymbol && board[11] == cSymbol && board[15] == cSymbol)||
  613.         (board[0] == cSymbol && board[5] == cSymbol && board[10] == cSymbol && board[15] == cSymbol)||
  614.         (board[12] == cSymbol && board[9] == cSymbol && board[6] == cSymbol && board[3] == cSymbol))
  615.     {
  616.         if(cSymbol == 'X') {
  617.             state = XWIN;
  618.         } else if(cSymbol == 'O') {
  619.             state = OWIN;
  620.         }
  621.     }
  622.     else {
  623.         state = DRAW;
  624.         for(int i = 0; i < 16; ++i) {
  625.             if(board[i] == 0) {
  626.                 state = PLAYING;
  627.                 break;
  628.             }
  629.         }
  630.     }
  631. }
  632.  
  633. // generates all the possible moves for the current board position
  634. void generate_moves(char _board[16], std::list<int> &move_list) {
  635.     for(int i = 0; i < 16; ++i) {
  636.         if(_board[i] == 0) {
  637.             move_list.push_back(i);
  638.         }
  639.     }
  640. }
  641.  
  642. // evaluates the current board position
  643. int evaluate_position(char _board[16], player _player) {
  644.     check_game_state(_board);
  645.     if(game_over()) {
  646.         if((state == XWIN && _player.symbol == 'X') ||
  647.             (state == OWIN && _player.symbol == 'O')) {
  648.             return +INFINITY;
  649.         } else if((state == XWIN && _player.symbol == 'O') ||
  650.             (state == OWIN && _player.symbol == 'X')) {
  651.             return -INFINITY;
  652.         } else if(state == DRAW) {
  653.             return 0;
  654.         }
  655.     }
  656.     return -1;
  657. }
  658.  
  659. // returns best move for the current computer player
  660. int MiniMax(char _board[16], player _player) {
  661.     int best_val = -INFINITY, index = 0;
  662.     std::list<int> move_list;
  663.     char best_moves[16] = {0};
  664.     generate_moves(_board, move_list);
  665.     while(!move_list.empty()) {
  666.         _board[move_list.front()] = _player.symbol;
  667.         cSymbol = _player.symbol;
  668.         int val = MinMove(_board, _player);
  669.         if(val > best_val) {
  670.             best_val = val;
  671.             index = 0;
  672.             best_moves[index] = move_list.front() + 1;
  673.         } else if(val == best_val) {
  674.             best_moves[++index] = move_list.front() + 1;
  675.         }
  676.         _board[move_list.front()] = 0;
  677.         move_list.pop_front();
  678.     }
  679.     if(index > 0) {
  680.         index = rand() % index;
  681.     }
  682.     return best_moves[index];
  683. }
  684.  
  685. // finds best move for 'min player'
  686. int MinMove(char _board[16], player _player) {
  687.     int pos_value = evaluate_position(_board, _player);
  688.     if(pos_value != -1) {
  689.         return pos_value;
  690.     }
  691.     int best_val = +INFINITY;
  692.     std::list<int> move_list;
  693.     generate_moves(_board, move_list);
  694.     while(!move_list.empty()) {
  695.         _player.symbol == 'X' ? cSymbol = 'O' : cSymbol = 'X';
  696.         _board[move_list.front()] = cSymbol;
  697.         int val = MaxMove(_board, _player);
  698.         if(val < best_val) {
  699.             best_val = val;
  700.         }
  701.         _board[move_list.front()] = 0;
  702.         move_list.pop_front();
  703.     }
  704.     return best_val;
  705. }
  706.  
  707. // finds best move for 'max player'
  708. int MaxMove(char _board[16], player _player) {
  709.     int pos_value = evaluate_position(_board, _player);
  710.     if(pos_value != -1) {
  711.         return pos_value;
  712.     }
  713.     int best_val = -INFINITY;
  714.     std::list<int> move_list;
  715.     generate_moves(_board, move_list);
  716.     while(!move_list.empty()) {
  717.         _player.symbol == 'X' ? cSymbol = 'X' : cSymbol = 'O';
  718.         _board[move_list.front()] = cSymbol;
  719.         int val = MinMove(_board, _player);
  720.         if(val > best_val) {
  721.             best_val = val;
  722.         }
  723.         _board[move_list.front()] = 0;
  724.         move_list.pop_front();
  725.     }
  726.     return best_val;
  727. }
  728.  
ok ive copied this person's tic tac toe game which is designed for the normal 3 x 3 board. but i wanted it to play a 4 x 4 board so i adjusted some values and added some lines but now it doesnt seem to work (select 1 play against com) after placing my turn it doesnt do anythin!
Feb 13 '09 #1
4 5010
JosAH
11,448 Expert 8TB
Cute: 728 line of code and a message "now it doesnt seem to work ".

kind regards,

Jos
Feb 13 '09 #2
manontheedge
175 100+
that code is a bit bloated. Have you considered writing your own? To make the board change size, you can have a board class, and in the constructor have your board size set dynamically. If it's written properly, it's very simple. It's reasons like this that object oriented programming exist. Just throwing that out there.
Feb 13 '09 #3
@JosAH
hahah this is for pure fun so wonderin how to solve it...haha cheers!
Feb 13 '09 #4
donbock
2,426 Expert 2GB
@1051109210
Don't make it harder than necessary for somebody to help you.
Feb 14 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.