Simple Python Projects Multiplayer Tic-Tac-Toe














































Simple Python Projects Multiplayer Tic-Tac-Toe



Hello, Rishabh here, this time I bring to you:

Continuing the series - 'Simple Python Project'. These are simple projects with which beginners can start with. This series will cover beginner python, intermediate and advanced python, machine learning and later deep learning.

Comments recommending other to-do python projects are supremely recommended.

Anyways, let's crack on with it!


Python project to implement
Multiplayer Tic-Tac-Toe


Hello everyone and welcome to this project. In this project we're going to implement a simple tow player Tic-Tac-Toe Game


So, lets start!

First we are going to define a board in which we will play; Since we have a 3x3 board, we'll name it from 0 to 8.

Tic Tac Toe Game

In [1]:
executed in 327ms, finished 15:20:51 2020-05-25
In [2]:
executed in 2.02s, finished 15:20:53 2020-05-25
Out[2]:
list
In [3]:
executed in 9ms, finished 15:20:53 2020-05-25
In [4]:
executed in 241ms, finished 15:20:54 2020-05-25
0 1 2
3 4 5
6 7 8


Next we are defining player 1 of our game.

It is defined by X

Player #1 Input

In [5]:
executed in 9ms, finished 15:20:56 2020-05-25
In [6]:
executed in 598ms, finished 15:20:57 2020-05-25
0 1 2
3 4 5
6 7 8

In [7]:
executed in 3.62s, finished 15:21:01 2020-05-25
Player#1, Enter your desired board location: 
2
0 1 X
3 4 5
6 7 8

Next we will define the Player 2. It is defined by O.

Computer (Player #2) Input

In [8]:
executed in 19ms, finished 15:21:05 2020-05-25
In [9]:
executed in 3.14s, finished 15:21:08 2020-05-25
Player#2, Enter your desired board location: 
1
0 O X
3 4 5
6 7 8

Finally an if condition to see who wins.

Check if any player wins!

In [10]:


Finally, putting it all together.

Let's put it all together

In [11]:
executed in 19.9s, finished 15:21:34 2020-05-25
Player#1, Enter your desired board location: 
1
This spot is taken, please choose somewhere else
0 O X
3 4 5
6 7 8

Player#2, Enter your desired board location:
4
0 O X
3 O 5
6 7 8

Player#1, Enter your desired board location:
7
0 O X
3 O 5
6 X 8

Player#2, Enter your desired board location:
3
0 O X
O O 5
6 X 8

Player#1, Enter your desired board location:
8
0 O X
O O 5
6 X X

Player#2, Enter your desired board location:
5
0 O X
O O O
6 X X

Player #2 Wins

------------------------

Entire Code:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
board = [0, 1, 2, 3, 4, 5, 6, 7, 8]
def TicTacToe_draw():
print(board[0], board[1], board[2])
print(board[3], board[4], board[5])
print(board[6], board[7], board[8])
print()
def Player_1():
print("Player#1, Enter your desired board location: ")
location = int(input())
if board[location] != 'X' and board[location] != 'O':
board[location] = 'X'
else:
print('This spot is taken, please choose somewhere else')
def Player_2():
print("Player#2, Enter your desired board location: ")
location = int(input())
if board[location] != 'X' and board[location] != 'O':
board[location] = 'O'
else:
print('This spot is taken, please choose somewhere else')
def Check_Winner(X_or_O):
if board[0] == X_or_O and board[1] == X_or_O and board[2] == X_or_O:
return True
if board[3] == X_or_O and board[4] == X_or_O and board[5] == X_or_O:
return True
if board[6] == X_or_O and board[7] == X_or_O and board[8] == X_or_O:
return True
if board[0] == X_or_O and board[3] == X_or_O and board[6] == X_or_O:
return True
if board[1] == X_or_O and board[4] == X_or_O and board[7] == X_or_O:
return True
if board[2] == X_or_O and board[5] == X_or_O and board[8] == X_or_O:
return True
if board[0] == X_or_O and board[4] == X_or_O and board[8] == X_or_O:
return True
if board[2] == X_or_O and board[4] == X_or_O and board[6] == X_or_O:
return True
while True:
Player_1()
TicTacToe_draw()
if Check_Winner('X')==True:
print("Player #1 Wins")
break;
Player_2()
TicTacToe_draw()
if Check_Winner('O')==True:
print("Player #2 Wins")
break;


Output:

Player#1, Enter your desired board location: 
0
X 1 2
3 4 5
6 7 8

Player#2, Enter your desired board location:
1
X O 2
3 4 5
6 7 8

Player#1, Enter your desired board location:
2
X O X
3 4 5
6 7 8

Player#2, Enter your desired board location:
3
X O X
O 4 5
6 7 8

Player#1, Enter your desired board location:
4
X O X
O X 5
6 7 8

Player#2, Enter your desired board location:
5
X O X
O X O
6 7 8

Player#1, Enter your desired board location:
6
X O X
O X O
X 7 8

Player #1 Wins

Please comment below any questions or article requests.
Like the articles and Follow me to get notified when I post another article.

Cheers!



More Articles of Rishabh Karmakar:

Name Views Likes
Basic Date and Time in Python 167 1
Using Defaultdict in Python 55 1
Floor Division in Python 208 1
Python time sleep function 77 1
Immutable Objects in Python 78 1
Mutable Objects in Python 81 1
Python Escape Sequences 70 1
Escape Sequence in Python 0 0
Python Bytearray 66 1
Plotting Histogram in Python 141 1
Using Voluptuous in Python 177 1
TRAINING MACHINE LEARNING MODEL WITH TENSORFLOW CLOUD 260 1
Pandas - ModuleNotFoundError - No Module Named Pandas and Functions Explained 5531 5
OpenCV - ModuleNotFoundError - No Module Named cv2 and Algorithms Explained 344 5
OpenCV - ModuleNotFoundError - No Module Named 274 9
Simple Python Project Mario Shooter Game 389 4
Web crawling using Python and Scrapy 267 6
CNN vs ANN vs RNN 295 6
Deep Learning - Activation and Loss Function 249 6
CNN vs. RNN vs. ANN 42 10
Advanced Python Projects Predicting and Forecasting Stock Market Prices 564 6
Intermediate Python Projects Tensorflow Live Object Detection 331 7
Intermediate Python Projects Optical Character and Text Recognizer from Live Images 336 7
Advanced Python Projects Analysing Music Trends and Recommendations 469 7
Advanced Python Projects Python Chatbot using NLTK and Keras 760 7
Advanced Python Projects CIFAR-10 Image Classification using Deep Learning 554 9
Advanced Python Projects Image Caption Generator using CNN and LSTM 454 10
Advanced Python Project Breast Cancer Classification using SVR 373 10
Advanced Python Project Smiling Face Detector using CNN 445 10
Advanced Python Project Handwritten Digit Recognizer 509 11
Intermediate Python Project Speed Typing in Python 456 10
Advanced Python Project Next Alphabet or Word Prediction using LSTM 365 12
Intermediate Python Project Find most similar word using Word2Vec 288 10
Advanced Python Projects IMDB Movie Review Sentiment Analysis 261 10
Intermediate Python Project Detection of Real or Fake News 282 10
Advanced Python Project Credit Card Fraud Detection 264 10
Intermediate Python Project Snake Game Pygame 326 10
Simple Python Projects Make a multi-colored rainbow pattern in Python 400 10
Advanced Project Analysis with Sentiment Classification using Bidirectional Recurrent Neural Network 244 10
Advanced Project Forest Fire Prediction using SVR Random Forest and Deep NN 381 10
Advanced Project Sentiment and WordCloud Analysis of Online Reviews 338 12
Python - Synthetic Data Generator for Machine Learning and Artificial Intelligence 263 9
Advanced Project Deep Dream AI 271 10
Python - Symbolic regression classification generator 271 9
Generate Polynomial Functions and Random Function Generator - Python 289 10
Intermediate Project Sentiment and WordCloud Analysis of Women E-Commerce 288 10
Intermediate Project Iris Data Classification 226 10
Intermediate Project Titanic Classification using Decision Tree 248 10
Machine Learning - Decision Tree in Python 298 9
Python - All about lambda functions 251 9
Machine Learning - Polynomial Regression in Python 278 9
Machine Learning - Multiple Linear Regression in Python 260 11
Machine Learning - Simple Linear Regression in Python 245 9
Implement all 2D and 3D types plots in Python 236 9
Implement 1D, 2D and 3D CNN in Python 311 9
Simple Python Projects Multiplayer Tic-Tac-Toe 274 11
Problem - Validating Credit Card Numbers Hackerrank 280 9
Problem - Linear Algebra using Numpy Hackerrank 715 9
Problem - Basic Spell Checker Hackerrank 214 9
Implement Binary Tree in Python 277 9
Implement Graph in Python 262 9
Python for Data Science 213 9
Simple Python Projects Select Region of Interest - OpenCV 324 10
Simple Python Projects Code to mask white pixels in a coloured image - OpenCV 336 10
Simple Python Projects Code to mask white pixels in a gray scale image - OpenCV 298 10
Simple Python Projects Convert colour image to gray scale and apply cartoon effects - OpenCV 348 10
Advanced Project - Automatic Facial Recognition Based Attendance System 721 9
Simple Python Projects Live Talking Counting Clock - Pyttsx3, Tkinter 264 10
Python Simple and Fast Text to Speech 228 9
Simple Python Projects Simple Zodiac Sign Teller 233 10
Simple Python Projects Simple Fortune Teller Game 276 10
Simple Python Projects Simple Guessing Game 390 10
Implementing Multi-line Strings 202 9
Save and Load Machine Learning Models in Python with scikit-learn 401 9
Use of Ternary operator for conditional assignment. 351 9
Chaining comparison operators 216 9
Implement Circular Singly Linked List 241 9
Implementing Command Line Arguments 356 9
Solve Knapsack Problem Using Dynamic Programming 390 10

Comments