我试图查看已经存在的Tic Tac Toe代码,并发现有一种名为MiniMax的算法,虽然我已经了解它是如何工作的,但我似乎无法使用5 * 5 Tic Tac Toe表。
我已经知道我将如何做到这一点,但我似乎找不到适合它的方法。
我正在尝试检查1个col / row / diagonals中的每4个移动,以便在那里获胜,或者如果是另一个玩家则有一个阻止。但我似乎无法找到我能做到的,过去6个小时我一直在努力。
#include <iostream>
#include <cstdlib>
#include <time.h>
char arr[5][5];
char player1 = 'X';
char player2 = 'O';
char player = player1;
using namespace std;
void display() {
cout << " 1 " << "2 " << "3 "<<"4 "<<"5 " << endl;
cout << " ----------------" << endl;
for (int row = 0; row < 5; row++) {
cout << row + 1 << " | ";
for (int col = 0; col < 5; col++) {
cout << arr[row][col] << " ";
}
cout << endl;
}
}
void new_turn();
int firstEmptyRow(int c){
int i;
for(i=0;i<5;i++){
if(arr[i][c])
return i;
}
}
int firstEmptyCol(int r){
int i;
for(i=0;i<5;i++){
if(arr[r][i])
return i;
}
}
bool canWin(int mat[5][5]){
int row,col;
int countSteps;
// FOR VERTICAL |
//trying to find a count of 4 moves in 1 row, so It can be won.
for(row=0;row<5;row++){
countSteps=0;
for(col=0;col<5;col++){
if( (arr[row][col] == arr[row+1][col])
&&(arr[row][col] ==player2)){
countSteps++;
}
}
if(countSteps==4){ cout << "MOVE IS WIN-ABLE" << endl; return true;}
}
return false;
}
int computer_move(){
char temp;
int test[5][5], tempo[5][5];
int row, col;
for(row=0;row<5;row++){
for(col=0;col<5;col++){
test[row][col] = arr[row][col];
tempo[row][col] = arr[row][col];
}
}
for(row=0;row<5;row++){
for(col=0;col<5;col++){
if(arr[row][col] == '-'){
temp = arr[row][col];
if(canWin(test)){ // an attempt to test the move
}
}
}
}
}
void player_move() {
if(player==player1){
int his_moveRow, his_moveCol;
cout << "please enter your move row player " << player << endl;
cin >> his_moveRow;
cout << "please enter your move col player " << player << endl;
cin >> his_moveCol;
if (his_moveRow < 0 || his_moveRow > 5 || his_moveCol < 0 || his_moveCol > 5) {
cout << "please enter a number from 1 to 5 player " << player << endl;
player_move();
}
--his_moveRow;
--his_moveCol;
if (arr[his_moveRow][his_moveCol] == '-') {
arr[his_moveRow][his_moveCol] = player;
} else {
cout << "please try again player " << player << endl;
player_move();
}
}else{
//computer move!!
cout <<"Computer Move!"<< endl;
computer_move();
}
if (player == player1) {
player = player2;
} else {
player = player1;
}
new_turn();
}
bool check_win();
void new_turn() {
display();
if (check_win() == true) {
cout << "congratulation player " << player << " you won!" << endl;
return;
} else {
int row, col, count = 0;
for (row = 0; row < 5; row++) {
for (col = 0; col < 5; col++) {
if (arr[row][col] != '-') count++;
}
}
if (count == 25) {
cout << "No one won. That's a draw." << endl;
return;
} else {
cout << "next turn" << endl;
player_move();
}
}
}
bool check_win() {
//Vertical |
int row, col;
for (row = 0; row < 5; row++) {
for (col = 0; col < 5; col++) {
if ((arr[row][col] == arr[row + 1][col] && arr[row + 1][col] == arr[row + 2][col])
&&(arr[row+2][col] == arr[row + 3][col] && arr[row + 3][col] == arr[row + 4][col])
&& arr[row][col] != '-') {
cout << "player won" << endl;
player = arr[row][col];
return true;
}
}
}
//Horizontal -
for (row = 0; row < 5; row++) {
for (col = 0; col < 5; col++) {
if ((arr[row][col] == arr[row][col + 1] && arr[row][col + 1] == arr[row][col + 2])
&&(arr[row][col+2] == arr[row][col + 3] && arr[row][col + 3] == arr[row][col + 4])
&& arr[row][col] != '-') {
cout << "player won" << endl;
player = arr[row][col];
return true;
}
}
}
//Diagonal "\"
row=0,col=0;
if((arr[row][col] == arr[row+1][col+1] && arr[row+1][col+1] == arr[row+2][col+2]
&& arr[row+2][col+2] == arr[row+3][col+3])
&&(arr[row+3][col+3] == arr[row+4][col+4]) && arr[row][col] != '-'){
cout << "player won" << endl;
player = arr[row][col];
return true;
}
// Diagonal " / "
for (row = 4; row >= 0; row--) {
for (col = 0; col < 5; col++) {
if ((arr[row][col] == arr[row - 1][col + 1] && arr[row - 1][col + 1] == arr[row - 2][col + 2]
&& arr[row - 2][col + 2] == arr[row - 3][col + 3]
&& arr[row - 3][col + 3] == arr[row - 4][col + 4]) && arr[row][col] != '-') {
cout << "player won" << endl;
player = arr[row][col];
return true;
}
}
}
return false;
}
void game_start() {
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
arr[row][col] = '-';
}
}
display();
player_move();
}
int main() {
srand(time(NULL));
game_start();
return 0;
}
答案 0 :(得分:-1)
我曾做过类似的事情,但没有使用MiniMax算法。我使用了xkcd的漫画“指南”,讲述了最佳的tic tac toe动作。 如果你的程序遵循这个,你永远不会输,只有赢或平。