有没有什么好的逻辑可用来阻止计算机将O放置在井字游戏中的现有X上

问题描述 投票:0回答:1

我在学习C ++时创建了一个Tic-Tac-Toe游戏,这是我的第一个项目。我还没有看过自己亲自尝试制作井字游戏的视频,而且我不知道程序员用来制作这款游戏​​的真正逻辑。但是从我自己的角度来看,我设法使我的游戏运转良好,并且将近80%的游戏完成了。

[所有获胜的可能性都很好,但问题是我们只有9次机会,并且在游戏中,[[CPU

放置一个'O',而玩家已经在他的先前输入中放置了一个'X' 。我已经停止CPUCPU的当前输入上执行此操作,但是我无法停止CPU在播放器的较早输入的'X'上放置'O' 。我使用了有意义的变量名称,并在代码中添加了注释,以方便您理解。谢谢。

此代码是完整代码的抱歉,但如果没有我的代码,我无法解释。

#include <iostream> #include <ctime> #include <cstdlib> using namespace std; char board[3][7]; void display(){ int rows, columns; //********************For Player*********************** int n=9; while(n-- > 0) { int x,y; cin>>x; x = (x == 1) ? 0 : x; x = (x == 2) ? 1 : x; x = (x == 3) ? 2 : x; cin>>y; y = (y == 3) ? 5 : y; y = (y == 0) ? 1 : y; y = (y == 2) ? 3 : y; for ( rows = 0 ; rows < 3 ; rows++ ){ for ( columns = 1 ; columns < 7 ; columns = columns+2 ){ board[ x ][ y ] = 'X'; } } for ( rows = 0 ; rows < 3 ; rows++ ) { for ( columns = 0 ; columns < 7 ; columns++ ) { cout<< board[rows][columns] ; } cout<<"\n"; } cout<<"\n"; //***********************For CPU**************************** srand(time(NULL)); int randX = (rand() % 3) + 1; //To check randX and player x should not be same while(randX==x){ randX = (rand() % 3) + 1; } randX = (randX == 1) ? 0 : randX; randX = (randX == 2) ? 1 : randX; randX = (randX == 3) ? 2 : randX; int randY = (rand() % 3) + 1; //To check randY and player y should not be same while(randY==y){ randY = (rand() % 3) + 1; } randY = (randY == 3) ? 5 : randY; randY = (randY == 0) ? 1 : randY; randY = (randY == 2) ? 3 : randY; for ( rows = 0 ; rows < 3 ; rows++ ){ for ( columns = 1 ; columns < 7 ; columns = columns+2 ){ board[ randX ][ randY ] = 'O'; } } for ( rows = 0 ; rows < 3 ; rows++ ) { for ( columns = 0 ; columns < 7 ; columns++ ) { cout<< board[rows][columns] ; } cout<<"\n"; } /*Check Winning*/ // All Column Winning Cases if((board[0][1]=='X') && (board[1][1]=='X') && (board[2][1]=='X')){ cout<<"You won the game"<<endl; break; } else if((board[0][3]=='X') && (board[1][3]=='X') && (board[2][3]=='X')){ cout<<"You won the game"<<endl; break; } else if((board[0][5]=='X') && (board[1][5]=='X') && (board[2][5]=='X')){ cout<<"You won the game"<<endl; break; } // All Row Winning Cases else if((board[0][1]=='X') && (board[0][3]=='X') && (board[0][5]=='X')){ cout<<"You won the game"<<endl; break; } else if((board[1][1]=='X') && (board[1][3]=='X') && (board[1][5]=='X')){ cout<<"You won the game"<<endl; break; } else if((board[2][1]=='X') && (board[2][3]=='X') && (board[2][5]=='X')){ cout<<"You won the game"<<endl; break; } // All Diagnal Winning Cases else if((board[0][1]=='X') && (board[1][3]=='X') && (board[2][5]=='X')){ cout<<"You won the game"<<endl; break; } else if((board[0][5]=='X') && (board[1][3]=='X') && (board[2][1]=='X')){ cout<<"You won the game"<<endl; break; } else continue; } }/*End of function display()*/ void drawBoard() { int rows, columns; for ( rows = 0 ; rows < 3 ; rows++ ){ for ( columns = 0 ; columns < 7 ; columns=columns+2 ){ // to fill every second elemnt of the array .. just increment the counter by 2 board[ rows ][ columns ] = '|'; } } for ( rows = 0 ; rows < 3 ; rows++ ){ for ( columns = 1 ; columns < 7 ; columns = columns+2 ){ board[ rows ][ columns ] = ' '; } } for ( rows = 0 ; rows < 3 ; rows++ ) { for ( columns = 0 ; columns < 7 ; columns++ ) { cout<< board[rows][columns] ; } cout<<"\n"; } display(); }/* end function drawboard */ int main() { drawBoard(); return 0; }

我在学习C ++时创建了一个Tic-Tac-Toe游戏,这是我的第一个项目。我还没有看过自己亲自尝试制作井字游戏的视频,也不知道程序员用来制作真正的逻辑的...
c++ function random tic-tac-toe
1个回答
0
投票
这是从您的代码派生的有效示例。要查看的重要位是numberPlacesRemaing和cpuPlays。第一个功能计算剩余可用位置数。然后,CPU选择介于1和剩余数字之间的数字。使用数字播放,它将按顺序遍历所有可用空间,然后选择要播放的值,同时跳过已放置的空间。我保留了基本的调试语句,但已将其注释掉。您可以取消注释它们并查看输出,该输出将确切详细显示此方法的工作原理。
© www.soinside.com 2019 - 2024. All rights reserved.