mardi 4 février 2020

Is there any good logic I can use to stop computer from placing O on a existing X in Tic-Tac-Toe

I have created a Tic-Tac-Toe game as my first project while learning C++. I haven't watched any video of creating tic-tac-toe its my own try and I don't know the real logic programmers use to make this game. But from my own I managed to get my game working good and its almost 80% done.

All winning possibilities are working fine but the problem is that we have only 9 chances and during the game CPU places an 'O' where the player have already placed a 'X' in his previous input. I have stopped the CPU from doing this on the current input of the CPU but I can't stop CPU from placing 'O' on 'X' of the older inputs of the player.

I have used meaningful names of the variables and added comments on my code for easy understanding for you people. Thanks.

This code is here sorry for the full code but I can't explain without my code.

#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;
}



Aucun commentaire:

Enregistrer un commentaire