Interview Solution

Premature optimization is the root of all evil in programming. C.A.R. Hoare

Tic Tac Toe

Language Java | Level Intermediate | Category Interview Problems | July 24, 2015 4:52 pm

Comments


Interview Problem

Tic-tac-toe is a game for two players, who take turns marking the spaces in a grid.
Tic Tac Toe is a game use to play by two players using 3x3 grid. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game.

  • The player is known as X and the Computer play is O.
  • Players alternate placing Xs and Os on the game board until either opponent has three in a row or all nine squares are filled.
  • User plays always first


Output

   					 
This is a tic-tac-toe game 
You are playing against the computer!
Enter 1-9 to indicate your move
 | | 
-----
 | | 
-----
 | | 
Your move
Please enter your move(0-9): 
6
 | | 
-----
 | | 
-----
X| | 
Computer move: 1
 |O| 
-----
 | | 
-----
X| | 
Your move
Please enter your move(0-9): 
7
 |O| 
-----
 | | 
-----
X|X| 
Computer move: 4
 |O| 
-----
 |O| 
-----
X|X| 
Your move
Please enter your move(0-9): 
8
 |O| 
-----
 |O| 
-----
X|X|X
You won!


				    

Comments



Please login to add comments.