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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay