# Day 99 — Design Tic Tac Toe

## 100 Days to Amazon

**100 Days to Amazon — Day 99 — Design Tic Tac Toe**

Out of Free Stories? Here is my **Friend Link.**

# Introduction🛹

Hey Guys, Today is **day 99 **of the challenge that I took. **Free Link**

# Before you scroll down to today’s code:

I am thinking of quick 15/30 day challenge. Choose any one among them and comment it below. I will start working on it.

**30**day**Microsoft**Challenge

2)**30**day**Adobe**Challenge

3)**20**day**Backtracking**/ DFS problems.

4)**20**day**Tree**Problems.

5)**20**day**DP**problems

All these problems are taken from the following e-book. 🎓

*This is completely free 🆓 if you have an amazon kindle subscription.*

This** e-book contains 100 coding problems** that have been asked in **top tech interview questions**. It also has a guide to **solving **all the problems in **200+ ways. **These problems I** assure you** has been asked in previous interviews.

You have to decide whether you want to go** unprepared **for a tech interview or go ahead and **quickly search for this guide **to solve the 100 problems.

# Begin Your ascent to greatness🚀

Note:this e-book only contains thelinksto thesolutions.Code for 40 have been added.

# Day 99 — Design Tic Tac Toe🏁

# AIM🏹

Design a **Tic-tac-toe **game that is played between two players on a *n* x *n* grid.

You may assume the following rules:

- A
**move**is guaranteed to be valid and is placed on an**empty block**. **Once**a**winning condition**is reached, no more moves is allowed.- A
**player**who succeeds in**placing**of their marks in a*n***horizontal**,**vertical**, or**diagonal**row wins the game.

# Example🕶

`Given `*n* = 3, assume that player 1 is "X" and player 2 is "O" in the board.

TicTacToe toe = new TicTacToe(3);

toe.move(0, 0, 1); -> Returns 0 (no one wins)

|X| | |

| | | | // Player 1 makes a move at (0, 0).

| | | |

toe.move(0, 2, 2); -> Returns 0 (no one wins)

|X| |O|

| | | | // Player 2 makes a move at (0, 2).

| | | |

toe.move(2, 2, 1); -> Returns 0 (no one wins)

|X| |O|

| | | | // Player 1 makes a move at (2, 2).

| | |X|

toe.move(1, 1, 2); -> Returns 0 (no one wins)

|X| |O|

| |O| | // Player 2 makes a move at (1, 1).

| | |X|

toe.move(2, 0, 1); -> Returns 0 (no one wins)

|X| |O|

| |O| | // Player 1 makes a move at (2, 0).

|X| |X|

toe.move(1, 0, 2); -> Returns 0 (no one wins)

|X| |O|

|O|O| | // Player 2 makes a move at (1, 0).

|X| |X|

toe.move(2, 1, 1); -> Returns 1 (player 1 wins)

|X| |O|

|O|O| | // Player 1 makes a move at (2, 1).

|X|X|X|

# Code👇

# Algorithm👨🎓

- Initialize n rows and n cols.
- Each Move if its player 1 adds 1 to the row and col of the given position.
- if it is player 2 add -1 to the row and col of the given position.
- If col and row are the same then it is the first diagonal. Add 1 to diagonal.
- If col == col.length -row- 1 Then it is the Second diagonal. Add 1 to the second diagonal.
- If any of these 4 either equals n then the first player wins.
- If it equals to -n then the second player wins.
- Else it is a draw.
- Return the Result🔚

# Conclusion🐱🏍

Have you come across this question in your interview before? Share it in the comment section below. 🤝

Don’t forget to hit the **follow button✅**to receive updates when we post new coding challenges. Tell us how you **solved** this problem. 🔥 We would be thrilled to read them. ❤ We can feature your method in one of the blog posts.

Want to become outstanding in java programming?

Click HERE 🧨🎊🎃

I have published an** ebook**. A compilation of **100 Java(Interview) Programming problems** which have been solved.** (HackerRank) 🐱💻**

**This is completely free 🆓 if you have an amazon kindle subscription.**

# Companies

**Google****2)****Pocket Gems****3)****Capital One****4) Amazon**