• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

TicTacToe Game As Asked in Flipkart

August 17, 2014 by Dhaval Dave

Write a running code in any language to implement the famous tic-tac-toe game.
Decide which basic functions which would be required to implement the same. 
Then try to code it your self else basic code is given. below

Design this game as per following:
       1) Game has 3 modes: Human Vs Human, Human Vs Computer and Computer Vs Computer.
       2) Initially start with 3X3 grid, but it can be generalized to NXN grid. So don’t hard-code any variable.
       3) Minimize Code Redundancy and try to make it as modular as possible.
       4) Try to use abstraction and expose lesser number of functions(APIs) to outside world.
       5) Try to cover maximum number of edge cases, like when to abort the game, draw condition, win condition, overwriting existing value in grid etc)

Currently I have implemented with conditions from above
1),3),4),5)
-Trying to make it for NXN, if some one can comeup with that please ping below.
Thanks Dhaval for providing code : 
Code : 

#include <stdio.h>
#include <stdlib.h>
#define gc getchar_unlocked
inline int scan(){register int n=0,c=gc();while(c<‘0’||c>’9′)c=gc();while(c<=’9’&&c>=’0’)n=(n<<1)+(n<<3)+c-‘0’,c=gc();return n;} 

char matrix[3][3];  /* the tic tac toe matrix */

char check(void);
void init_matrix(void);
void get_player_move(int);
void get_computer_move(int);
void disp_matrix(void);

int main(void)
{
  int choice;
  char done;
  printf(” 1 – Human Vs Humann 2 – Human Vs Computer n 3- Comp Vs Comp n”);
  choice = scan();
  done =  ‘ ‘;
  init_matrix();    
  
        do {
        disp_matrix();
        get_player_move(choice);
        done = check(); /* see if winner */
        if(done!= ‘ ‘) break; /* winner!*/
        get_computer_move(choice);
        done = check(); /* see if winner */
        }while(done== ‘ ‘);

        if(done==’X’) {disp_matrix(); if (choice==1||2)printf(“1st Human Won!nn”); else printf(“1st Computer Wonnn”);}
        else {disp_matrix(); if (choice ==1) printf(“2nd Human Won!!!!nn”); else printf(“2nd Computer Won!!!!nn”);}
        // /* show final positions */
      
  return 0;
}

/* Initialize the matrix. */
void init_matrix(void)
{
  int i, j;

  for(i=0; i<3; i++)
    for(j=0; j<3; j++) matrix[i][j] =  ‘ ‘;
}

/* Get a player’s move. */
void get_player_move(int choice)
{
  int x, y;
  int i,j;
  //printf(“Enter X,Y coordinates for your move: n”);
  
  if ( choice == 1 || choice == 2 ){
  x=scan();
  y=scan();
  }//get X for human
  else 
  {
   for(i=0; i<3; i++){
     for(j=0; j<3; j++)
       if(matrix[i][j]==’ ‘) break;
     if(matrix[i][j]==’ ‘) break;
    }//for
  }//else  
  x–; y–;
  
  if(matrix[x][y]!= ‘ ‘){
    printf(“Invalid move, try again.n”);
    get_player_move(choice);
  }
  else matrix[x][y] = ‘X’;
}

/* Get a move from the computer. */
void get_computer_move(int choice)
{
  int i, j ,x, y;
    
 if ( choice == 1 ){
  x=scan();
  y=scan();
  }//get 0 for human
 else {
   for(i=0; i<3; i++){
     for(j=0; j<3; j++)
       if(matrix[i][j]==’ ‘) break;
     if(matrix[i][j]==’ ‘) break;
    }//for
  }//else
  
  if(i*j==9)  {
    printf(“drawn”);
    exit(0);
  }
  else
    matrix[i][j] = ‘O’;
}

/* Display the matrix on the screen. */
void disp_matrix(void)
{
  int t;
  printf(“n”);
  for(t=0; t<3; t++) {
    printf(” %c | %c | %c “,matrix[t][0],
            matrix[t][1], matrix [t][2]);
    if(t!=2) printf(“n—|—|—n”);
  }
  printf(“n”);
}

/* See if there is a winner. */
char check(void)
{
  int i;

  for(i=0; i<3; i++)  /* check rows */
    if(matrix[i][0]==matrix[i][1] &&
       matrix[i][0]==matrix[i][2]) return matrix[i][0];

  for(i=0; i<3; i++)  /* check columns */
    if(matrix[0][i]==matrix[1][i] &&
       matrix[0][i]==matrix[2][i]) return matrix[0][i];

  /* test diagonals */
  if(matrix[0][0]==matrix[1][1] &&
     matrix[1][1]==matrix[2][2])
       return matrix[0][0];

  if(matrix[0][2]==matrix[1][1] &&
     matrix[1][1]==matrix[2][0])
       return matrix[0][2];

  return ‘ ‘;
}

//STD INPUT 
// Choice 1 ) 1 1 1 2 1 3 1 2 2 3 3 3 1 2 1 3 2 2 3
// Choice 2 ) 2 1 1 2 1 3 1 2 2 3 3 3 1 2 1 3 2 2 3
// Choice 3 ) 3
Works well with http://www.compileonline.com/compile_c_online.php

Similar Articles

Filed Under: Flipkart Interview Questions, Interview Experience, problem Tagged With: 2d matrix

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Find Percentage of Words matching in Two Strings

Connect n ropes with minimum cost

Leetcode: Merge Intervals

Search element in a matrix with all rows and columns in sorted order

Python Array String

Regular Expression Matching

Coin Collection Dynamic Programming

Given a sorted array and a number x, find the pair in array whose sum is closest to x

Apriori algorithm C Code Data Mining

Sort Stack in place

Templates in C++

SAP Off Campus Hiring_ March 2015 Sample Questions

Add Sub Multiply very large number stored as string

Number of Islands BFS/DFS

SAP Off Campus Hiring_ March 2015 Computer Skills

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Find Nearest Minimum number in left side in O(n)

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

System Design: Designing a LLD for Hotel Booking

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Trie Dictionary

Implement a generic binary search algorithm for Integer Double String etc

ADOBE Aptitude C Language Test

Get Minimum element in O(1) from input numbers or Stack

write a c program that given a set a of n numbers and another number x determines whether or not there exist two elements in s whose sum is exactly x

Introduction To Number Theory ( Part 1 )

Linked List V/S Binary Search Tree

Subset Sum Problem Dynamic programming

Generic Object Oriented Stack with Template

Count number of ways to reach a given score in a game

Copyright © 2025 · Genesis Framework · WordPress · Log in