#include using namespace std; // ====================================================================== // ===== Définissez ici les types demandés : constexpr Game_rep MAX_GAME_REP(495); // game_rep(1,3,5,7) --> 495 typedef int Memory; // À redéfinir (sous-section 2.1 du sujet) // ====================================================================== /// preamble (and help) text void foreword() { cout << "Entrez vos coups sous la forme : ligne nombre\n" "p.ex. 3 2 pour prendre deux allumettes dans la troisième ligne.\n" "Les lignes sont numérotées de 1 à 4.\n" "Pour abandonner, indiquez un nombre de 0 allumette sur n'importe quelle ligne;\n" "p.ex. 1 0 (ou même 0 0)." << endl << endl; } // ====================================================================== /// tool-tool function used by sort(Game&) void cmp(Game& g, size_t i, size_t j) { if (g[i] > g[j]) swap(g[i], g[j]); } // ====================================================================== /// tool function to sort a game by increasing lines void sort(Game& g) { cmp(g, 0, 1); cmp(g, 2, 3); cmp(g, 0, 2); cmp(g, 1, 3); cmp(g, 1, 2); } // ====================================================================== /// tool function to have a unique integer representation of game states Game_rep game_to_rep(Game const& game) { Game g(game); sort(g); return g[0] * 0x100 // 256 + g[1] * 0x040 // 64 + g[2] * 0x008 // 8 + g[3] // 1 ; } // ====================================================================== /// tool-tool function used to display the game void skip(size_t k) { for (size_t i(1); i <= k; ++i) cout << " "; } /// tool function that displays the game void display(Game const& game) { if (max(game) == 0) { cout << "" << endl; return; } for (size_t i(0); i < game.size(); ++i) { cout << (i+1) << ". "; skip(3-i); for (Count k(1); k <= game[i]; ++k) cout << "| "; skip(4+i - game[i]); cout << " (" << game[i] << ')' << endl; } } // ====================================================================== bool play(Game& g, Move const& move) // returns true if the game can continue { if (move.taken == 0) return false; // resign update(g, move); cout << endl; display(g); if (not validate(g)) { cout << "Jeu invalide !" << endl; cout << "Quelque chose n'a pas joué..." << endl; return false; } return max(g) > 0; } // ====================================================================== Move learned_move(Game const& game, Memory const& mem) { return query(game, "le coup de l'IA"); } // ====================================================================== Move ia(Game const& game, Memory const& mem) { // unexpected failsafe if (game.empty() or (game_to_rep(game) == 0)) { cout << "J'abandonne..." << endl; return {0,0}; } const Move move(learned_move(game, mem)); cout << "Je prends " << move.taken << " en ligne " << move.row << endl; return move; } // ====================================================================== // Situation de test donnée dans l'énoncé 2.2. /* Décommentez si vous souhaitez l'utiliser. void test_exemple_section_2_2() { Memory test_memory{}; test_memory[game_to_rep({ 0,0,2,5 })] = 1; test_memory[game_to_rep({ 1,0,1,5 })] = 2; test_memory[game_to_rep({ 1,0,0,5 })] = 3; test_memory[game_to_rep({ 1,0,2,4 })] = 2; test_memory[game_to_rep({ 1,0,2,3 })] = 3; test_memory[game_to_rep({ 1,0,2,2 })] = 2; test_memory[game_to_rep({ 1,0,2,1 })] = 1; test_memory[game_to_rep({ 1,0,2,0 })] = 2; cout << "### Test Section 2.2 :" << endl; (void) ia({ 1,0,2,5 }, test_memory); cout << "### ==========" << endl << endl; } */ // ====================================================================== // Situation de test donnée dans l'énoncé 2.3. /* Décommentez si vous souhaitez l'utiliser. void t23_tool(string const& s, Game_rep x, Play_record const& R, Memory const& M) { cout << "### " << s << " : "; cout << M[x] << " ; "; for (auto rep : R) cout << M[rep] << ' '; cout << endl; } void test_exemple_section_2_3() { Play_record test_record({ game_to_rep({ 1,3,5,6 }), // a joué 4 1 game_to_rep({ 0,3,5,6 }), // a joué 1 1 game_to_rep({ 0,3,5,4 }), // a joué 4 2 game_to_rep({ 0,1,5,4 }), // a joué 2 2 game_to_rep({ 0,1,3,4 }), // a joué 3 2 game_to_rep({ 0,1,3,2 }), // a joué 4 2 game_to_rep({ 0,1,2,2 }), // a joué 3 1 game_to_rep({ 0,1,2,1 }), // a joué 4 1 game_to_rep({ 0,1,0,1 }), // a joué 3 2 game_to_rep({ 0,1,0,0 }), // a joué 4 1 game_to_rep({ 0,0,0,0 }), // a joué 2 1 --> perdu }); Memory test_memory{}; cout << "### Test Section 2.3 :" << endl; const Game_rep whatever(game_to_rep({ 1,2,3,4 })); t23_tool("avant", whatever, test_record, test_memory); learn(test_memory, test_record, true); t23_tool("après", whatever, test_record, test_memory); learn(test_memory, test_record, true); t23_tool("un tour de plus", whatever, test_record, test_memory); learn(test_memory, test_record, false); t23_tool("retour en arrière", whatever, test_record, test_memory); cout << "### ==========" << endl << endl; } */ // ====================================================================== int main() { foreword(); Memory ia_memory{}; bool repeat(true); do { Game game({ 1,3,5,7 }); display(game); bool move_on(true); do { move_on = play(game, query(game)); if (not move_on) { cout << "Vous avez perdu :-(" << endl; } else { move_on = play(game, ia(game, ia_memory)); if (not move_on) { cout << "Vous avez gagné !" << endl; } } } while (move_on); cout << endl << "---- Nouvelle partie ----" << endl; } while (repeat); return 0; }