DEV Community

Cover image for Ten Days of Code: Task 3
Arnab Sen
Arnab Sen

Posted on

2 2

Ten Days of Code: Task 3

Task-3

Date: 05-Oct-20
Codes: task3_arnabsen1729

Problem Statement

CLI Tic Tac Toe
CLI based Tic Tac Toe with 2 players.

My Approach

Since, there are only two users, and no centralised server, the game logic will reside in each file. So I created a TicTacToe class. The class has the following methods:

METHODS DESCRIPTION
p1Move() Ask Player 1's move and will keep asking untill it gets a valid input
p2Move() Ask Player 2's move and will keep asking untill it gets a valid input
find_winner() Will find the winner at the current state of the game and return the value
display_board() Display's the board neatly
isGameOver() will decide if the game is over

Now, let's discuss how I have decided the logic of the game. I am storing the board in a linear array of 9 elements, initially they have 0 else if p1 made a move it will be +1 else for p2 it will be -1. Now to check we will check if the rows, cols, diagonals and anti-diagonals have +3 or -3 if so then there is a winner else no winner.

One optimisation instead of going through the entire board every single time, I am looking at the corresponding rows, and cols, and diagonals of the last move, because only that will decide the game.

For the client and server script its simple socket programming.

Sample

Task3

Resources

  1. Task 3 Problem Statement and guide

Thanks to @lugnitdgp for their awesome guidance <3

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay