SlideShare uma empresa Scribd logo
1 de 5
Baixar para ler offline
In C++ , you have to implement minimax algorithm with Alpha Beta pruning to play to Othello
game in this part of code in the main function . PLZ MAKE SURE TO ADD ALL THE
FUNCTIONS NEEDED TO IMPLEMENT THIS CODE :
if(moi == player) {
auto actionsPossible = listDesCoupsPossible();
// TODO : CHOOSE THE BEST MOVE ////////////
std::tie(row, col) = actionsPossible[ rand()%actionsPossible.size() ]; // Choix alatoire
std::cout << row << col << 'n' << std::flush;
//////////////////////////////////////////////
}
Here is the whole code to understand :
#include
#include
#include
#include
#include
std::vector> board;
char player = 'X';
void initBoard() {
board.resize(8, std::vector(8, '-'));
board[3][3] = 'X';
board[3][4] = 'O';
board[4][3] = 'O';
board[4][4] = 'X';
}
bool isValidMove(int x, int y, char player) {
if (x < 0 || x >= board.size() || y < 0 || y >= board[x].size()) return false; // Vrifier si la case est
dans les limites du plateau de jeu
if (board[x][y] != '-') return false; // Vrifier si la case est vide
// Vrifier les huit directions autour de la case
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue; // On ignore la case actuelle
int k = 1;
while (true) {
int newX = x + i * k;
int newY = y + j * k;
if (newX < 0 || newX >= board.size() || newY < 0 || newY >= board[newX].size())
break; // Sortir si on est en dehors du plateau
if (board[newX][newY] == '-') break; // Sortir si la case est vide
if (board[newX][newY] == player) {
if (k > 1) {
return true; // Si on a trouv une pice du joueur actuel aprs avoir trouv une pice de
l'autre joueur, alors le coup est valide
}
break;
}
k++;
}
}
}
return false;
}
void playMove(int x, int y) {
assert( isValidMove(x, y, player) );
board[x][y] = player; // Placer la pice du joueur actuel sur la case choisie
// Vrifier les huit directions autour de la case
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue; // On ignore la case actuelle
int k = 1;
while (true) {
int newX = x + i * k;
int newY = y + j * k;
if (newX < 0 || newX >= board.size() || newY < 0 || newY >= board[newX].size())
break;
if (board[newX][newY] == '-') break; // Sortir si la case est vide
if (board[newX][newY] == player) {
if (k > 1) {
for(int k2 = k-1; k2 > 0; k2--) {
int newX = x + i * k2;
int newY = y + j * k2;
board[newX][newY] = player;
}
break;
}
break;
}
k++;
}
}
}
}
// Switch players if the other player can play, if not the player keeps the hand. If no one can play,
returns false
bool switchPlayer() {
char newPlayer = (player == 'X') ? 'O' : 'X';
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
if (isValidMove(i, j, newPlayer)) {
player = newPlayer;
return true;
}
}
}
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
if (isValidMove(i, j, player)) {
return true;
}
}
}
return false;
}
int scoreX() {
int X = 0;
int O = 0;
for(auto &line: board) {
for(auto c: line) {
X += (c == 'X');
O += (c == 'O');
}
}
return X-O;
}
std::vector< std::tuple > listDesCoupsPossible() {
std::vector< std::tuple > result;
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
if (isValidMove(i, j, player)) {
result.push_back({i,j});
}
}
}
return result;
}
int main(int argc, char *argv[]) {
if( argc != 2 ) {
std::cerr << "Utilisation :" << std::endl;
std::cerr << argv[0] << " [X|O]" << std::endl;
return 0;
}
if((argv[1][0] != 'X') && (argv[1][0] != 'O')) {
std::cerr << "Utilisation :" << std::endl;
std::cerr << argv[0] << " [X|O]" << std::endl;
return 0;
}
char moi = argv[1][0];
srand(time(NULL));
initBoard();
while (true) {
int row;
int col;
if(moi == player) {
auto actionsPossible = listDesCoupsPossible();
// TODO : CHOOSE THE BEST MOVE ////////////
std::tie(row, col) = actionsPossible[ rand()%actionsPossible.size() ]; // Choix alatoire
std::cout << row << col << 'n' << std::flush;
//////////////////////////////////////////////
} else {
std::string coups;
std::cin >> coups;
std::cerr << "coups: "" << coups << """ << std::endl;
row = coups[0]-'0';
col = coups[1]-'0';
}
if (isValidMove(row, col, player)) {
playMove(row, col);
if(!switchPlayer()) {
break;
}a
} else {
std::cerr << "ERREUR: coups " << row << " " << col << " est invalide !" << std::endl;
return 0;
}
}
return 0;
}

Mais conteúdo relacionado

Semelhante a In C++ , you have to implement minimax algorithm with Alpha Beta pru.pdf

I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
archanaemporium
 
Need help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdfNeed help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdf
hainesburchett26321
 
#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
singhanubhav1234
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
fonecomp
 
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdfC++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
aromalcom
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
kavithaarp
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
poblettesedanoree498
 
19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf
NayanOza
 
AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
Ankit Gupta
 
import javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfimport javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdf
ADITIEYEWEAR
 

Semelhante a In C++ , you have to implement minimax algorithm with Alpha Beta pru.pdf (10)

I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
 
Need help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdfNeed help writing the code for a basic java tic tac toe game Tic.pdf
Need help writing the code for a basic java tic tac toe game Tic.pdf
 
#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf#include stdio.h #include string.h #include stdlib.h #in.pdf
#include stdio.h #include string.h #include stdlib.h #in.pdf
 
The following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdfThe following code, is a one player battleship game in JAVA. Im tryi.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
 
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdfC++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
C++ Programming (Please help me!! Thank you!!)Problem A Win SimU.pdf
 
public interface Game Note interface in place of class { .pdf
public interface Game  Note interface in place of class { .pdfpublic interface Game  Note interface in place of class { .pdf
public interface Game Note interface in place of class { .pdf
 
i have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdfi have a code that runs, but it only lets the player to guess where .pdf
i have a code that runs, but it only lets the player to guess where .pdf
 
19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf19012011102_Nayan Oza_Practical-7_AI.pdf
19012011102_Nayan Oza_Practical-7_AI.pdf
 
AI CHALLENGE ADMIN
AI CHALLENGE ADMINAI CHALLENGE ADMIN
AI CHALLENGE ADMIN
 
import javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdfimport javautilLinkedList import javautilQueue import .pdf
import javautilLinkedList import javautilQueue import .pdf
 

Mais de manojmozy

IDEO, Amerika Birleik Devletlerindeki en b�y�k ve en etkili tasarm .pdf
IDEO, Amerika Birleik Devletlerindeki en b�y�k ve en etkili tasarm .pdfIDEO, Amerika Birleik Devletlerindeki en b�y�k ve en etkili tasarm .pdf
IDEO, Amerika Birleik Devletlerindeki en b�y�k ve en etkili tasarm .pdf
manojmozy
 
I. Assessment(s) DescriptionFor this exercise, students are supp.pdf
I. Assessment(s) DescriptionFor this exercise, students are supp.pdfI. Assessment(s) DescriptionFor this exercise, students are supp.pdf
I. Assessment(s) DescriptionFor this exercise, students are supp.pdf
manojmozy
 

Mais de manojmozy (20)

IDENTIFY AND DISCUSS ONE LOCAL PROJECT THAT USED PROJECT FINANCE OR .pdf
IDENTIFY AND DISCUSS ONE LOCAL PROJECT THAT USED PROJECT FINANCE OR .pdfIDENTIFY AND DISCUSS ONE LOCAL PROJECT THAT USED PROJECT FINANCE OR .pdf
IDENTIFY AND DISCUSS ONE LOCAL PROJECT THAT USED PROJECT FINANCE OR .pdf
 
If we wanted to use the AWS CLI outside of AWS (on our local PCs) wh.pdf
If we wanted to use the AWS CLI outside of AWS (on our local PCs) wh.pdfIf we wanted to use the AWS CLI outside of AWS (on our local PCs) wh.pdf
If we wanted to use the AWS CLI outside of AWS (on our local PCs) wh.pdf
 
Identifique los cinco principales pa�ses receptores de IED de EE. UU.pdf
Identifique los cinco principales pa�ses receptores de IED de EE. UU.pdfIdentifique los cinco principales pa�ses receptores de IED de EE. UU.pdf
Identifique los cinco principales pa�ses receptores de IED de EE. UU.pdf
 
If X is a random variable with mean 10 and standard deviation 5, whi.pdf
If X is a random variable with mean 10 and standard deviation 5, whi.pdfIf X is a random variable with mean 10 and standard deviation 5, whi.pdf
If X is a random variable with mean 10 and standard deviation 5, whi.pdf
 
Identifique la variable de control para la carga de aerosoles atmosf�r.pdf
Identifique la variable de control para la carga de aerosoles atmosf�r.pdfIdentifique la variable de control para la carga de aerosoles atmosf�r.pdf
Identifique la variable de control para la carga de aerosoles atmosf�r.pdf
 
identifying customer needs.step1 Gather raw datastep 2 Interpret.pdf
identifying customer needs.step1 Gather raw datastep 2 Interpret.pdfidentifying customer needs.step1 Gather raw datastep 2 Interpret.pdf
identifying customer needs.step1 Gather raw datastep 2 Interpret.pdf
 
If CO2 is the acceptor in anaerobic chemolithotrophy, the donor coul.pdf
If CO2 is the acceptor in anaerobic chemolithotrophy, the donor coul.pdfIf CO2 is the acceptor in anaerobic chemolithotrophy, the donor coul.pdf
If CO2 is the acceptor in anaerobic chemolithotrophy, the donor coul.pdf
 
Identify a routine task that you might need to communicate in an off.pdf
Identify a routine task that you might need to communicate in an off.pdfIdentify a routine task that you might need to communicate in an off.pdf
Identify a routine task that you might need to communicate in an off.pdf
 
If a coefficient B is estimated to be very close to zero (e.g., 0.00.pdf
If a coefficient B is estimated to be very close to zero (e.g., 0.00.pdfIf a coefficient B is estimated to be very close to zero (e.g., 0.00.pdf
If a coefficient B is estimated to be very close to zero (e.g., 0.00.pdf
 
i. If a product has nonuniform inputs, is WIP generally divided betw.pdf
i. If a product has nonuniform inputs, is WIP generally divided betw.pdfi. If a product has nonuniform inputs, is WIP generally divided betw.pdf
i. If a product has nonuniform inputs, is WIP generally divided betw.pdf
 
identify and compare three different mobile device management (MDM) .pdf
identify and compare three different mobile device management (MDM) .pdfidentify and compare three different mobile device management (MDM) .pdf
identify and compare three different mobile device management (MDM) .pdf
 
IBM�s Stretch Project. 2500 words1. Title Page2.Introduction3..pdf
IBM�s Stretch Project. 2500 words1. Title Page2.Introduction3..pdfIBM�s Stretch Project. 2500 words1. Title Page2.Introduction3..pdf
IBM�s Stretch Project. 2500 words1. Title Page2.Introduction3..pdf
 
i. Define Property, Plant and Equipment as stipulated by IAS 16 (3 .pdf
i. Define Property, Plant and Equipment as stipulated by IAS 16 (3 .pdfi. Define Property, Plant and Equipment as stipulated by IAS 16 (3 .pdf
i. Define Property, Plant and Equipment as stipulated by IAS 16 (3 .pdf
 
IDEO, Amerika Birleik Devletlerindeki en b�y�k ve en etkili tasarm .pdf
IDEO, Amerika Birleik Devletlerindeki en b�y�k ve en etkili tasarm .pdfIDEO, Amerika Birleik Devletlerindeki en b�y�k ve en etkili tasarm .pdf
IDEO, Amerika Birleik Devletlerindeki en b�y�k ve en etkili tasarm .pdf
 
Identify and explain two (2) examples of collaborative care that cou.pdf
Identify and explain two (2) examples of collaborative care that cou.pdfIdentify and explain two (2) examples of collaborative care that cou.pdf
Identify and explain two (2) examples of collaborative care that cou.pdf
 
I. Assessment(s) DescriptionFor this exercise, students are supp.pdf
I. Assessment(s) DescriptionFor this exercise, students are supp.pdfI. Assessment(s) DescriptionFor this exercise, students are supp.pdf
I. Assessment(s) DescriptionFor this exercise, students are supp.pdf
 
If the fixed costs are NOK 50,000 and the variable cost can be expre.pdf
If the fixed costs are NOK 50,000 and the variable cost can be expre.pdfIf the fixed costs are NOK 50,000 and the variable cost can be expre.pdf
If the fixed costs are NOK 50,000 and the variable cost can be expre.pdf
 
If an individual shopping for groceries opens a bottle of water from.pdf
If an individual shopping for groceries opens a bottle of water from.pdfIf an individual shopping for groceries opens a bottle of water from.pdf
If an individual shopping for groceries opens a bottle of water from.pdf
 
Identify at least eight (8) accounting software. 2. Your list must i.pdf
Identify at least eight (8) accounting software. 2. Your list must i.pdfIdentify at least eight (8) accounting software. 2. Your list must i.pdf
Identify at least eight (8) accounting software. 2. Your list must i.pdf
 
Identify the FALSE statement. Radiometric dating1. of metamorphic .pdf
Identify the FALSE statement. Radiometric dating1. of metamorphic .pdfIdentify the FALSE statement. Radiometric dating1. of metamorphic .pdf
Identify the FALSE statement. Radiometric dating1. of metamorphic .pdf
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 

Último (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

In C++ , you have to implement minimax algorithm with Alpha Beta pru.pdf

  • 1. In C++ , you have to implement minimax algorithm with Alpha Beta pruning to play to Othello game in this part of code in the main function . PLZ MAKE SURE TO ADD ALL THE FUNCTIONS NEEDED TO IMPLEMENT THIS CODE : if(moi == player) { auto actionsPossible = listDesCoupsPossible(); // TODO : CHOOSE THE BEST MOVE //////////// std::tie(row, col) = actionsPossible[ rand()%actionsPossible.size() ]; // Choix alatoire std::cout << row << col << 'n' << std::flush; ////////////////////////////////////////////// } Here is the whole code to understand : #include #include #include #include #include std::vector> board; char player = 'X'; void initBoard() { board.resize(8, std::vector(8, '-')); board[3][3] = 'X'; board[3][4] = 'O'; board[4][3] = 'O'; board[4][4] = 'X'; } bool isValidMove(int x, int y, char player) { if (x < 0 || x >= board.size() || y < 0 || y >= board[x].size()) return false; // Vrifier si la case est dans les limites du plateau de jeu if (board[x][y] != '-') return false; // Vrifier si la case est vide // Vrifier les huit directions autour de la case for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; // On ignore la case actuelle int k = 1;
  • 2. while (true) { int newX = x + i * k; int newY = y + j * k; if (newX < 0 || newX >= board.size() || newY < 0 || newY >= board[newX].size()) break; // Sortir si on est en dehors du plateau if (board[newX][newY] == '-') break; // Sortir si la case est vide if (board[newX][newY] == player) { if (k > 1) { return true; // Si on a trouv une pice du joueur actuel aprs avoir trouv une pice de l'autre joueur, alors le coup est valide } break; } k++; } } } return false; } void playMove(int x, int y) { assert( isValidMove(x, y, player) ); board[x][y] = player; // Placer la pice du joueur actuel sur la case choisie // Vrifier les huit directions autour de la case for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; // On ignore la case actuelle int k = 1; while (true) { int newX = x + i * k; int newY = y + j * k; if (newX < 0 || newX >= board.size() || newY < 0 || newY >= board[newX].size()) break; if (board[newX][newY] == '-') break; // Sortir si la case est vide if (board[newX][newY] == player) { if (k > 1) { for(int k2 = k-1; k2 > 0; k2--) {
  • 3. int newX = x + i * k2; int newY = y + j * k2; board[newX][newY] = player; } break; } break; } k++; } } } } // Switch players if the other player can play, if not the player keeps the hand. If no one can play, returns false bool switchPlayer() { char newPlayer = (player == 'X') ? 'O' : 'X'; for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[i].size(); j++) { if (isValidMove(i, j, newPlayer)) { player = newPlayer; return true; } } } for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[i].size(); j++) { if (isValidMove(i, j, player)) { return true; } } } return false; } int scoreX() { int X = 0;
  • 4. int O = 0; for(auto &line: board) { for(auto c: line) { X += (c == 'X'); O += (c == 'O'); } } return X-O; } std::vector< std::tuple > listDesCoupsPossible() { std::vector< std::tuple > result; for (int i = 0; i < board.size(); i++) { for (int j = 0; j < board[i].size(); j++) { if (isValidMove(i, j, player)) { result.push_back({i,j}); } } } return result; } int main(int argc, char *argv[]) { if( argc != 2 ) { std::cerr << "Utilisation :" << std::endl; std::cerr << argv[0] << " [X|O]" << std::endl; return 0; } if((argv[1][0] != 'X') && (argv[1][0] != 'O')) { std::cerr << "Utilisation :" << std::endl; std::cerr << argv[0] << " [X|O]" << std::endl; return 0; } char moi = argv[1][0]; srand(time(NULL)); initBoard(); while (true) { int row;
  • 5. int col; if(moi == player) { auto actionsPossible = listDesCoupsPossible(); // TODO : CHOOSE THE BEST MOVE //////////// std::tie(row, col) = actionsPossible[ rand()%actionsPossible.size() ]; // Choix alatoire std::cout << row << col << 'n' << std::flush; ////////////////////////////////////////////// } else { std::string coups; std::cin >> coups; std::cerr << "coups: "" << coups << """ << std::endl; row = coups[0]-'0'; col = coups[1]-'0'; } if (isValidMove(row, col, player)) { playMove(row, col); if(!switchPlayer()) { break; }a } else { std::cerr << "ERREUR: coups " << row << " " << col << " est invalide !" << std::endl; return 0; } } return 0; }