我在学习C ++时创建了一个Tic-Tac-Toe游戏,这是我的第一个项目。我还没有看过自己亲自尝试制作井字游戏的视频,而且我不知道程序员用来制作这款游戏的真正逻辑。但是从我自己的角度来看,我设法使我的游戏运转良好,并且将近80%的游戏完成了。
[所有获胜的可能性都很好,但问题是我们只有9次机会,并且在游戏中,[[CPU
放置一个'O',而玩家已经在他的先前输入中放置了一个'X' 。我已经停止CPU在CPU的当前输入上执行此操作,但是我无法停止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游戏,这是我的第一个项目。我还没有看过自己亲自尝试制作井字游戏的视频,也不知道程序员用来制作真正的逻辑的...