Wednesday, August 28, 2013

A Simple TicTacToe C++ game code

Everyone has played Tic Tac Toe right? On phone, on paper etc... (During class hours too:))
Well we got a little bored of the game and couldn't find anything else so decided to write a program for it.


We as usual wrote an Algorithm and Coded it in C++ with the intension of modifying it later.

Here's the code:


//
//  main.cpp
//  tictactoe
//
//  Created by Kevin Prasanna and Akash Ashok on 24/08/13.
//  Copyright (c) 2013 Kevin Prasanna. All rights reserved.
//

#include <iostream>
using namespace std;

int i,j;
char mat[3][3];
void display(int i,int j)
{
    for (i=0; i<3; i++) {
        for (j=0; j<3; j++) {
            cout<<mat[i][j]<<"\t\t";
        }
        cout<<"\n\n";
    }
}
void check(int count)
{
    char c;
    if(count%2==0)
        c='X';
    else
        c='O';
    
    if((mat[0][0]==c&&mat[0][1]==c&&mat[0][2]==c)||(mat[1][0]==c&&mat[1][1]==c&&mat[1][2]==c)||(mat[2][0]==c&&mat[2][1]==c&&mat[2][2]==c)||(mat[0][0]==c&&mat[1][0]==c&&mat[2][0]==c)||(mat[0][1]==c&&mat[1][1]==c&&mat[2][1]==c)||(mat[0][2]==c&&mat[1][2]==c&&mat[2][2]==c)||(mat[0][0]==c&&mat[1][1]==c&&mat[2][2]==c)||(mat[0][2]==c&&mat[1][1]==c&&mat[2][0]==c))
    {
        cout<<"Player "<<c<<"\tWins\n\n";
    }
    else return;
    exit(0);
}

int main(int argc, const char * argv[])
{
    int count=0;
    for (i=0; i<3; i++)
        for (j=0; j<3; j++)
            mat[i][j]='_';
    cout<<"Tic Tac Toe 2 players\n"<<"Player 1: X \nPlayer 2: O\n\n\n";
    display(i, j);
    while (count!=9) {
        if(count%2==0)
        {
            cout<<"Player 1 enter co-ordinates\n";
            cin>>i>>j;
            if(mat[i][j]=='_')
            {
                mat[i][j]='X';
            }
            else{
                
               cout<<"Cheater!!!!!!!!!!!!\n";
               count--;
            }
            cout<<"\n\n";
            display(0, 0);
            check(count);
            
        }
        else
        {
            cout<<"Player 2 enter co-ordinates\n";
            cin>>i>>j;
            if(mat[i][j]=='_')
            {
                mat[i][j]='O';
            }
            else
            {
                cout<<"Cheater!!!!!!!!!!!!\n";
                count--;
            }
            cout<<"\n\n";
            display(0, 0);
            check(count);
            
        }
        count++;
    }
    cout<<"\n\n\t\tSHAME!! Its a draw\n";
    return 1;
    
}

The cheating condition:


Winning Condition:


Draw Condition:


This program of of course not an awesome piece of work! But we posted it that way without comments so that you analyse and spend a couple of minutes understanding it. The basic step is very straight forward making this a basic program for beginners. We came up with much more complex data structures that will increase the efficiency but we will bug you with those later.

Any questions? Do comment!

Stay tuned for more AND do look up our other blogs:
For python
For C
For C++

No comments:

Post a Comment