Giter Site home page Giter Site logo

project-so2's Introduction

Tic Tac Toe - Multithreaded Game

Project Description

This project is an implementation of the popular game "Tic Tac Toe", leveraging the concepts of multithreading. The game involves a competition between two computer-controlled players who place their marks (cross or circle) on an expanded 10x10 board. The player who first places three of their marks in a line (horizontally, vertically, or diagonally) wins.

Concept graphic of the game Graphical User Interface of the game

How to Run the Game

  1. Navigate to the TicTacToe folder in your terminal.
  2. Run the game with this command: python ./source/gui.py
  3. Once the application has launched, click on the Start button to begin the game.

Remember to ensure that your python environment is set up correctly and all necessary dependencies are installed for the script to run.

Threads Usage

We use two threads in the project, representing the individual players. Each player is represented by a thread that calls the computer method. This method simulates the computer's move, where it randomly selects a field on the board and places its mark.

player1 = threading.Thread(target=self.game.computer, args=('x',))
player2 = threading.Thread(target=self.game.computer, args=('o',))

Each thread operates on a "queue" basis - player 1 (thread player1) starts first, followed by player 2 (thread player2). Each thread is secured by the join() operation, which ensures that the thread is executed to completion before the next thread starts. This cycle continues until one of the players wins or there is a tie.

The computer Method

The computer method represents a player's move in the game. It selects a random field on the board for a move. If the field is unoccupied, it places its mark there. If this move results in a win, the win_event flag is set.

def computer(self, player):
   while not self.win_event.is_set():
       self.player_turn = player
       move = random.randint(0, 99)
       if self.board[move] is None:
           self.board[move] = player
           if self.check_win():
               self.win_event.set()
           break

project-so2's People

Contributors

murad-sh avatar

Watchers

Tomasz Szandała avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.