You are on page 1of 43

Introduction

The objective of this project is to make an offline game console

This software project is also aimed to make games with minimum requirements and lightweight
nature accessible offline. It uses MySQL to store score and show the best at the game and uses
gamer ID system to compare different gamers playing on the system.

The proposed software system is expected to do the following


functionality-

 To provide a user friendly, text-based Interface (GUI) based integrated and centralized
environment for gaming.
 The proposed system should maintain all the records and should generate the required reports
and information when required.
 To identify and switch games as per user’s choice

In its current scope, the software enables user to retrieve and update the information from centralized
database designed with MySQL. This software does not require much training time of the users due to
limited functionality and simplicity
Despite of the best effort of the developer, the following limitations and functional boundaries are
visible, which limits the scope of this application software.
1. This software’s chess game does not have trapped checkmate instead it provides 3 check
checkmate
2. There is graphical interface for ease of use
So far as future scope of the project is concerned, firstly it is open to any modular expansion i.e. other
modules or functions can be designed and embedded to handle the user need in future. Any part of
the software and reports can be modified independently without much effort.
This project mainly revolves around 4 modules that contains code of each game
Chess.py
Hangman.py
Ticktactoe.py
Main.py

1
Game console

Main.py

Chess.py Hangman.py Ticktactoe.py

The main.py program is the main program that uses all the other modules in a menu
driven program to run systematically and in harmony. The main.py contains code that
gives access to the other 3 modules which contain the code for individual games.
main.py uses the user input to determine which game to run and main.py has code to
store the scores and data into a MySQL database which is further used to show each
players stats.

2
THEORETICAL BACKGROUND

Database :
Introduction and Concepts :
A database is a collection of information related to a particular subject or purpose, such as tracking
customer orders or maintaining a product collection. Using any RDBMS application software like MS
SQL Server, MySQL, Oracle, Sybase etc, you can manage all your information from a single database
file. Within the file, divide your data into separate storage containers called tables. You may and
retrieve the data using queries.

A table is a collection of data about a specific topic, such as products or suppliers. Using a separate
table for each topic means you can store that data only once, which makes your database more
efficient and reduces data-entry errors. Table organises data into columns (called fields) and rows
(called records) A Primary key is one or more fields whose value or values uniquely identify each record
in a table. In a relationship, a primary key is used to refer to specific record in one table from another
table. A primary key is called foreign key when it is referred to from another table.

To find and retrieve just the data that meets conditions you specify, including data from multiple tables,
create a query- A query can also update or delete multiple records at the same time, and perform built-
in or custom calculations on your data.

3
Role of RDBMS Application Program :

A computer database works as an electronic filing system, which has a large number of ways of cross-
referencing, and this allows the user many different ways in which to re-organize and retrieve data- A
database can handle business inventory, accounting and filing and use the information in its files to prepare
summaries, estimates and other reports. The management of data in a database system is done by means
of a general-purpose software package called a Database Management System (DBMS). Some commercially
available DBMS are MS SQL Server, MS ACCESS, NGRES, ORACLE, and Sybase. A database management
system, therefore. is a combination of hardware and software that can be used to set up and monitor a
database, and can manage the updating and retrieval of database that has been stored in it. Most of the
database management systems have the following
capabilities:
• Creating of a table, addition, deletion, modification of records.
• Retrieving data collectively or selectively.
• The data stored can be sorted or indexed at the user's discretion and
• Various reports can be produced from the system These may be either
standardized or that may be specifically generated according to
specific user definition.
• Mathematical functions can be performed and the data stored in the database
can be manipulated with these functions to perform the desired calculations
• To maintain data integrity and database use.
The DBMS interprets and processes users' requests to retrieve information from a database. In most
cases, a query request will have to penetrate several layers of software in the DBMS and operating
system before the physical database can be accessed. The DBMS responds to a query by invoking the
appropriate subprograms, each of which performs its special function to interpret the query, or to locate
the desired data in the database and present it in the desired order.

4
My SQL:
MySQL, the most popular Open source SQL database management system, is developed, distributed, and
supported by Oracle Corporation- MySQL is named after co-founder Monty daughter, My. The name of the
MySQL Dolphin (logo) is

MySQL is a database management system.


A database is a structured collection of data. It may be anything from a simple shopping list to a picture
gallery or the vast amounts of information in a corporate network to add, access, and process data stored
in a computer database, you need a database management system such as MySQL Server. Since computers
are very good at handling large amounts of data. Database management systems play a central role in
computing, as standalone utilities, or as parts of other applications.

MySQL is based on SQL.


A relational database stores data in separate tables rather than putting all the data in one big storeroom-
This adds speed and flexibility. The SQL part of "MySQL" stands for 'Structured Query Language." SQL is the
most common standardized language used to access databases and is defined by the ANSI/ISO SQL
Standard. The SQL standard has been evolving since 1986 and several versions exist- In this manual, "SQL-
92" refers to the standard released in 1992, "SQL:1999" refers to the standard released in 1999, and
"SQL:2003" refers to the current version of the standard

MySQL software is Open Source.


means that it is possible for anyone to use and modify the software. Anybody can download the MySQL
software from the Internet and use it without paying anything- If you wish, you may study the source code
and change it to suit your needs. The MySQL software uses the GPL (GNU General Public License)

The MySQL Database Server is very fast, reliable, and easy to use.
If that is what you are looking for, you should give it a try. MySQL Server also has a practical set of features
developed in close cooperation with our users. You can find a performance comparison of MySQL Server
with other database managers on our benchmark page MySQL Server was originally Developed to handle
large databases much faster than existing solutions and has been successfully used in highly demanding
production environments for several years. Although under constant development. MySQL Server today
offers a rich and useful set of functions- Its connectivity, speed, and security make MySQL Server highly
suited for accessing databases on the Internet.

MySQL Server works in client/server or embedded systems.


The MySQL Database Software is a client/server system that consists of a multi-threaded SQL server that
supports different backends, several different client programs and libraries, administrative tools, and a
wide range of application programming interfaces (APIs).

5
The Main Features of MySQL
 Works on many different platforms.
 Uses multi-layered server design with independent modules.
 Provides transactional and storage engines
 Designed to make it relatively easy to add other storage engines. This is
 useful if you want to provide an SQL interface for an in-house database.
 Uses a very fast thread-based memory allocation system-
 Executes very fast joins using an optimized nested-loop join.
 Implements SQL functions using a highly optimized class library that should be as fast as possible.
Usually there is no memory allocation at all after query initialization-
 Provides the server as a separate program for use in a client/server networked environment, and
as a library that can be embedded (linked) into standalone applications. Such applications can be
used in isolation or in environments where no network is available.
 Password security by encryption of all password traffic when you connect to a server.
 Support for large databases- We use MySQL Server with databases that contain 50 million records.
We also know of users who use MySQL Server with 200,000 tables and about 5,000,000,000 rows.
 MySQL client programs can be written in many languages. A client library written in C is available
for clients written in C or C++, or for any language provides c bindings
 APIs for C, C++, Eiffel, Java, Perl, PHP, Python, Ruby, and are available, enabling MySQL clients to
be written in many languages.
 The Connector/ODBC (MyOBDC) interface provides MySQL support for client programs that use
ODBC (Open Database Connectivity) connections.
 The Connector/J interface provides MySQL support for Java client programs that use JDBC
connections. Clients can be run on Windows or Unix. Connector/J source is available.

6
Visual Studio Code :
Visual Studio Code, also commonly referred to as VS Code, is a source-code editor made
by Microsoft with the Electron Framework, for Windows, Linux and macOS. Features include support
for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and
embedded Git. Users can change the theme, keyboard shortcuts, preferences, and
install extensions that add additional functionality.
In the Stack Overflow 2021 Developer Survey, Visual Studio Code was ranked the most popular
developer environment tool among 82,000 respondents, with 70% reporting that they use it.

Features of Visual Studio Code:


 Support for multiple programming languages
 Intelli-Sense: It can detect if any snippet of code is left incomplete
 Cross-Platform Support
 Extensions and Support
 Web-Support: Comes with built-in support for Web applications.

 Hierarchy Structure: The code files are located in files and folders.

 Improving Code: Some code snippets can be declared a bit differently, which might help the user

in the code

 Terminal Support

 Multi-Projects: Multiple projects containing multiple files/folders can be opened

simultaneously.

7
Problem Definition & Analysis

The hardest part of building a software system is deciding precisely what to build. No other part of the
conceptual work is so difficult as establishing the detailed technical requirement. Defining and applying
good, complete requirements are hard to work, and success in this endeavour has eluded many of us.
Yet, we continue to make progress.
Problem definition describes the What of a system, not How. The quality of a software product is only as
good as the process that creates it. Problem definition is one of the most crucial steps in this creation
process. Without defining a problem, developers do not know what to build, customers do not know what
to expect, and there is no way to validate that the built system satisfies the requirement. Problem
definition and Analysis is the activity that encompasses learning about the problem to be solved,
understanding the needs of customer and users, trying to find out who the user really is, and
understanding all the constraints on the solution. It includes all activities related to the following:

 Identification and documentation of customer's or user's needs.


 Creation of a document that describes the external and the association constraints that will
satisfies those needs.
 Analysis and validation of the requirements documents to ensure consistency, completeness, and
feasibility.
 Evolution of needs.
After the analysis of the functioning of the proposed System is expected to do the following:

 To provide a user friendly, integrated and centralized environment for gaming


 The proposed system should maintain all the records
 should generate the required reports and run games smoothly
 To provide offline, CPU light gaming board game experience
 To identify and switch games as per user’s choice

8
System implementation

The Hardware used :


While developing the system, the used software is

 Dell Inspirion 15 3000 series, 12GB ram Intel(R) Core(TM) i5-7200U CPU @
2.50GHz
The Software used :

 Microsoft Windows® 10 as Operating System.


 Visual Studio Code version 1.74
 MySQL for backend server with databases for testing
 Microsoft Word 2019 for documentation

9
System design and development

Database Design:
An important aspect of system design is the design of data storage structure. To begin with a logical
model of data structure is developed first. A database is a container object which contains tables,
queries, reports and data validation policies enforcement rules or constraints etc. A logical data often
represented as records are kept in different tables after reducing anomalies and redundancies. The
goodness of data base design lies in the table structure and its relationship. This software project
maintains a database named “score” which contains the “score_count” table.

Table Design :
The database of “score” System contains 1 table. The tables are normalized to minimize the
redundancies of data and enforcing the validation rules of the organization. Most of the tables are
designed to store master records. The tables and their structure are given below.

Modules used from python library :


 random
 os
 pymysql

10
Coding
chess.py
from math import *
import os
def game() :
global
turn,dummy_board,black_check,white_check,flag,gamer_id1,gamer_id2,winner,win_id,loser,lose_id
winner = ""
win_id = ""
loser = ""
lose_id = ""
flag = True
clear = lambda : os.system('cls')
white_name = input('Enter player name playing white : ')
gamer_id1 = input('Enter gamer ID of '+white_name+" : ")
black_name = input('Enter player name playing black : ')
gamer_id2 = input('Enter gamer ID of '+black_name+" : ")
bc = '\033[30m'
wc = '\033[37m'
turn = False
black_check = 0
white_check = 0
chance = {False:'[37m',True:'[30m'}

pawn = "♟"

knight = "♞"

bishop = "♝"

rook = "♜"

queen = "♛" ;king = "♚"

11
a = [[bc+"♜"],[bc+"♞"],[bc+"♝"],[bc+"♛"],[bc+"♚"],[bc+"♝"],[bc+"♞"],[bc+"♜"]]

b = [[bc+pawn],[bc+pawn],[bc+pawn],[bc+pawn],[bc+pawn],[bc+pawn],[bc+pawn],[bc+pawn]]

c = [[],[],[],[],[],[],[],[]]

d = [[],[],[],[],[],[],[],[]]

e = [[],[],[],[],[],[],[],[]]

f = [[],[],[],[],[],[],[],[]]

g = [[wc+pawn],[wc+pawn],[wc+pawn],[wc+pawn],[wc+pawn],[wc+pawn],[wc+pawn],[wc+pawn]]

h = [[wc+"♜"],[wc+"♞"],[wc+"♝"],[wc+"♛"],[wc+"♚"],[wc+"♝"],[wc+"♞"],[wc+"♜"]]

board = [a,b,c,d,e,f,g,h]

dummy_board = board.copy()

def print_board() :

if turn == False :

print(" A B C D E F G H"+"\n-------------------------------------------")

for i in range(len(board)) :

print(f"{i+1} | ",end="")

for j in board[i] :

if len(j) == 1 :

print(j[0],end=wc+" | "+wc)

else :

print(" ",end=wc+" | "+wc)

print()

print("-------------------------------------------")

else :

print(" A B C D E F G H"+"\n-------------------------------------------")

for i in range(len(board)) :

print(f'{8-i} | ',end="")

for j in board[::-1][i] :

if len(j) == 1 :

print(j[0],end=wc+" | "+wc)

else :

print(" ",end=wc+" | "+wc)

print()

print("-------------------------------------------")
12
def pos_point(pos) :

y = int(pos[1])-1

x = ord(pos[0])-97

return [x,y]

def point_pos(pos) :

m2=str(pos[1]+1)

m1=chr(pos[0]+97)

return m1+m2

def king_func(piece,sc) :

t_val = [] ; val = []

if bc in piece :

color = bc

opp_color = wc

else :

color = wc

opp_color = bc

for i in range(8) :

for j in range(8) :

try :

if (fabs(sc[0] - i) == 1.0) and (fabs(sc[1] - j) == 0.0) :

t_val.append([i,j])

except :

pass

try :

if (fabs(sc[0] - i) == 0.0) and (fabs(sc[1] - j) == 1.0) :

t_val.append([i,j])

except :

pass

try :

if (fabs(sc[0] - i) == 1.0) and (fabs(sc[1] - j) == 1.0) :

t_val.append([i,j])

except : pass ; pieces_seen = []

13
for i in t_val :

if board[i[1]][i[0]] != [] and opp_color in board[i[1]][i[0]][0] :

pieces_seen.append(board[i[1]][i[0]][0])

if board[i[1]][i[0]] != [] and color in board[i[1]][i[0]][0] :

pass

else :

val.append(i)

return [val,pieces_seen,opp_color]

def rook_func(piece,sc) :

if bc in piece :

color = bc

opp_color = wc

else :

color = wc

opp_color = bc

left = []

right = []

up = []

down = []

for i in range(8) :

for j in range(8) :

try :

if sc[0] == i :

if sc[1] > j :

up.append([i,j])

else :

down.append([i,j])

if sc[1] == j :

if sc[0] > i :

left.append([i,j])

else :

right.append([i,j])

except :
14
pass
path_st = [left[::-1],right,up[::-1],down]

val = [] ; pieces_seen = []

for i in path_st :

for j in i :

if j == [sc[0],sc[1]] :

continue

if board[j[1]][j[0]] != [] and color in board[j[1]][j[0]][0] :

break

if board[j[1]][j[0]] != [] and opp_color in board[j[1]][j[0]][0] :

val.append(j)

pieces_seen.append(board[j[1]][j[0]][0])

break

else :

val.append(j)

return [val,pieces_seen,opp_color]

def bishop_func(piece,sc) :

if bc in piece :

color = bc

opp_color = wc

else :

color = wc

opp_color = bc

up_left = []

up_right = []

down_left = []

down_right = []

15
for i in range(8) :

for j in range(8) :

try :

if fabs(sc[0] - i) == fabs(sc[1]-j) :

if sc[0] > i :

if sc[1] < j :

down_left.append([i,j])

else :

up_left.append([i,j])

else :

if sc[1] < j :

down_right.append([i,j])

else :

up_right.append([i,j])

except :

pass

path_diag = [up_left[::-1],up_right,down_left[::-1],down_right]

val = [] ; pieces_seen = []

for i in path_diag :

for j in i :

if j == [sc[0],sc[1]] :

continue

if board[j[1]][j[0]] != [] and color in board[j[1]][j[0]][0] :

break

if board[j[1]][j[0]] != [] and opp_color in board[j[1]][j[0]][0] :

val.append(j)

pieces_seen.append(board[j[1]][j[0]][0])

break

else :

val.append(j)

return [val,pieces_seen,opp_color]

16
def knight_func(piece,sc) :

t_val = []

val = []

if bc in piece :

color = bc

opp_color = wc

else :

color = wc

opp_color = bc

for i in range(8) :

for j in range(8) :

try :

if fabs(sc[1] - j) == 2.0 and fabs(sc[0] - i) == 1.0:

t_val.append([i,j])

except :

pass

try :

if fabs(sc[1] - j) == 1.0 and fabs(sc[0]-i) == 2.0 :

t_val.append([i,j])

except :

pass

pieces_seen = []

for i in t_val :

if board[i[1]][i[0]] != [] and opp_color in board[i[1]][i[0]][0] :

pieces_seen.append(board[i[1]][i[0]][0])

if board[i[1]][i[0]] != [] and color in board[i[1]][i[0]][0] :

pass

else :

val.append(i)

return [val,pieces_seen,opp_color]

17
def queen_func(piece,sc) :

if bc in piece :

color = bc

opp_color = wc

else :

color = wc

opp_color = bc

up_left = []

up_right = []

down_left = []

down_right = []

left = []

right = []

up = []

down = []

for i in range(8) :

for j in range(8) :

try :

if fabs(sc[0] - i) == fabs(sc[1]-j) :

if sc[0] > i :

if sc[1] < j :

down_left.append([i,j])

else :

up_left.append([i,j])

else :

if sc[1] < j :

down_right.append([i,j])

else :

up_right.append([i,j])

except :

pass

18
try :

if sc[0] == i :

if sc[1] > j :

up.append([i,j])

else :

down.append([i,j])

if sc[1] == j :

if sc[0] > i :

left.append([i,j])

else :

right.append([i,j])

except :

pass

path_diag = [up_left[::-1],up_right,down_left[::-1],down_right]

path_st = [left[::-1],right,up[::-1],down]

val = [] ; pieces_seen = []

for i in path_diag :

for j in i :

if j == [sc[0],sc[1]] :

continue

if board[j[1]][j[0]] != [] and color in board[j[1]][j[0]][0] :

break

if board[j[1]][j[0]] != [] and opp_color in board[j[1]][j[0]][0] :

val.append(j)

pieces_seen.append(board[j[1]][j[0]][0])

break

else :

val.append(j)

19
for i in path_st :

for j in i :

if j == [sc[0],sc[1]] :

continue

if board[j[1]][j[0]] != [] and color in board[j[1]][j[0]][0] :

break

if board[j[1]][j[0]] != [] and opp_color in board[j[1]][j[0]][0] :

val.append(j)

pieces_seen.append(board[j[1]][j[0]][0])

break

else :

val.append(j)

return [val,pieces_seen,opp_color]

def pawn_func(piece,sc) :

if bc in piece :

color = bc

opp_color = wc

num = -1 ; doubnum = -2

doubmove = ["a2","b2","c2","d2","e2","f2","g2","h2"]

promote = ["a8","b8","c8","d8","e8","f8","g8","h8"]

else :

color = wc

opp_color = bc

num = 1 ; doubnum = 2

doubmove = ["a7","b7","c7","d7","e7","f7","g7","h7"]

promote = ["a1","b1","c1","d1","e1","f1","g1","h1"]

t_val = [] ; val = []

20
try :

if board[sc[1] - num][sc[0]+1] != [] :

t_val.append([sc[0]+1,sc[1]-num])

except :

pass

try :

if board[sc[1]-num][sc[0]-1] != [] :

t_val.append([sc[0]-1,sc[1]-num])

except :

pass

try :

if board[sc[1]-num][sc[0]] == [] :

t_val.append([sc[0],sc[1]-num])

if point_pos(sc) in doubmove and (board[sc[1]-doubnum][sc[0]] == []) :

t_val.append([sc[0],sc[1]-doubnum])

except :

pass

pieces_seen = []

for i in t_val :

if board[i[1]][i[0]] != [] and opp_color in board[i[1]][i[0]][0] :

pieces_seen.append(board[i[1]][i[0]][0])

if board[i[1]][i[0]] != [] and color in board[i[1]][i[0]][0] :

pass

else :

val.append(i)

return [val,color,promote,pieces_seen,opp_color]

21
def valid_move(piece,startpos,endpos) :

sc = pos_point(startpos)

ec = pos_point(endpos)

if len(piece) == 0 :

R = False

else :

piece = piece[0]

if pawn in piece :

R = False

pawn_fun = pawn_func(piece,sc)

if ec in pawn_fun[0] :

R = True

if R :

if endpos in pawn_fun[2] :

pro = input("Enter what u want the pawn to become (Q,K,R,B) : ").upper()

if pro == "Q" :

piece = pawn_fun[1]+"♛"

elif pro == "R" :

piece = pawn_fun[1]+"♜"

elif pro == "K" :

piece = pawn_fun[1]+"♞"

elif pro == "B" :

piece = pawn_fun[1]+"♝"

else :

print("err")

elif knight in piece :

R = False

knight_fun = knight_func(piece,sc)

if ec in knight_fun[0] :

R = True

22
elif bishop in piece :

R = False

bishop_fun = bishop_func(piece,sc)

if ec in bishop_fun[0] :

R = True

elif rook in piece :

R = False

rook_fun = rook_func(piece,sc)

if ec in rook_fun[0] :

R = True

elif queen in piece :

R = False

queen_fun = queen_func(piece,sc)

if ec in queen_fun[0] :

R = True

elif king in piece :

R = False

king_fun = king_func(piece,sc)

if ec in king_fun[0] :

R = True

return [R,piece]

def check_check(dummy,turn,AorB) :

global see

#if AorB == 'A' :

turn = not(turn)

if turn :

col = bc

else :

col = wc

ret = False

23
for i in range(len(dummy)) :

for j in range(len(dummy[i])) :

if dummy[i][j]!=[] and col in dummy[i][j][0] :

piece = dummy[i][j][0]

sc = [j,i]

if bishop in piece :

see = bishop_func(piece,sc)[1]

elif rook in piece :

see = rook_func(piece,sc)[1]

elif pawn in piece :

see = pawn_func(piece,sc)[3]

elif queen in piece :

see = queen_func(piece,sc)[1]

elif knight in piece :

see = knight_func(piece,sc)[1]

for x in see :

if king in x :

ret = True

return ret

def check_mate() :

global flag,winner,win_id,loser,lose_id

if white_check == 3 :

print(white_name+' WINS')

winner = white_name

win_id = gamer_id1

loser = black_name

lose_id = gamer_id2

flag = False

elif black_check == 3 :

print(black_name+' WINS')

winner = black_name

win_id = gamer_id2

loser = white_name; lose_id = gamer_id1 ; flag = False

24
def move() :

global turn,dummy_board,white_check,black_check

startpos = input("Enter the starting position of ur piece : ")

endpos = input("Enter the ending position of ur piece : ")

piece = board[int(startpos[1])-1][ord(startpos[0])-97]

validation = valid_move(piece,startpos,endpos)

if validation[0] and (chance[turn] in piece[0]) :

dummy_board[int(startpos[1])-1][ord(startpos[0])-97] = []

dummy_board[int(endpos[1])-1][ord(endpos[0])-97] = [validation[1]]

check = check_check(dummy_board,turn,'A')

dummy_board[int(startpos[1])-1][ord(startpos[0])-97] = [validation[1]]

dummy_board[int(endpos[1])-1][ord(endpos[0])-97] = []

clear()

if check == True :

print('CANNOT MOVE !!! CHECK')

else :

board[int(startpos[1])-1][ord(startpos[0])-97] = []

board[int(endpos[1])-1][ord(endpos[0])-97] = [validation[1]]

turn = not(turn)

check2 = check_check(dummy_board,turn,'B')

print_board()

if check2 == True :

if turn == True :

white_check += 1

else :

black_check += 1

print('CHECK !!!')

print(white_name+" CHECK's REMAINING : "+str(3-black_check),end=" ")

print(black_name+" CHECK's REMAINING : "+str(3-white_check))

else :

clear()

print_board()

print("INVALID MOVE !!!")

print_board() 25
while flag :

move()

check_mate()

hangman.py
import random

import os

def game2() :

global name,name_id,win

clear = lambda : os.system('cls')

name = input("Enter your name: ")

name_id = input('Enter your gamer ID : ')

win = False

print("Hello " + name + "! Best of Luck!")

def main():

global count,display,word,already_guessed,length,play_game

words_to_guess = ["laptop","python","java","october","kerala","nitte"]

word = random.choice(words_to_guess)

length = len(word)

count = 0

display = '_' * length

already_guessed = []

play_game = ""

def hangman():

global count,display,word,already_guessed,play_game,win

limit = 9

guess = input("This is the Hangman Word: " + display + "\nEnter your guess: ")

guess = guess.strip()

if len(guess.strip()) == 0 or len(guess.strip()) >= 2 or guess <= "9":

print("Invalid Input, Try a letter")

hangman()

26
elif guess in word:

already_guessed.extend([guess])

index = word.find(guess)

word = word[:index] + "_" + word[index + 1:]

display = display[:index] + guess + display[index + 1:]

print(display + "\n")

elif guess in already_guessed:

print("Try another letter.\n")

else:

count += 1

clear()

if count == 1:

print(" _____ \n"

" | \n"

" | \n"

" | \n"

" | \n"

" | \n"

" | \n"

"__|__\n")

print("Wrong guess. " + str(limit - count) + " guesses remaining\n")

elif count == 2:

print(" _____ \n"

" | | \n"

" | |\n"

" | \n"

" | \n"

" | \n"

" | \n"

"__|__\n")

print("Wrong guess. " + str(limit - count) + " guesses remaining\n")

27
elif count == 3:

print(" _____ \n"

" | | \n"

" | |\n"

" | | \n"

" | \n"

" | \n"

" | \n"

"__|__\n")

print("Wrong guess. " + str(limit - count) + " guesses remaining\n")

elif count == 4:

print(" _____ \n"

" | | \n"

" | |\n"

" | | \n"

" | O \n"

" | \n"

" | \n"

"__|__\n")

print("Wrong guess. " + str(limit - count) + " last guess remaining\n")

elif count == 5:

print(" _____ \n"

" | | \n"

" | |\n"

" | | \n"

" | O \n"

" | | \n"

" | \n"

"__|__\n")

elif count == 6:

print(" _____ \n"

" | | \n"

" | |\n"

" | | \n" 28

" | O \n"

" | |\ \n"
elif count == 7:

print(" _____ \n"

" | | \n"

" | |\n"

" | | \n"

" | O \n"

" | /|\ \n"

" | \n"

"__|__\n")

elif count == 8 :

print(" _____ \n"

" | | \n"

" | |\n"

" | | \n"

" | O \n"

" | /|\ \n"

" | / \n"

"__|__\n")

elif count == 9:

print(" _____ \n"

" | | \n"

" | |\n"

" | | \n"

" | O \n"

" | /|\ \n"

" | / \ \n"

"__|__\n")

print("Wrong guess. You are hanged!!!\n")

print("The word was:",word)

win = False

if word == '_' * length:

print("Congrats! You have guessed the word correctly!")

win = True

29
elif count != limit:

hangman()

main()

Ticktactoe.py
import os

def game3() :

global winner,loser,win_id,lose_id,flag,draw,id1,id2,name1,name2

moves = ["a1","a2","a3","b1","b2","b3","c1","c2","c3"]

win =
(("a1","a2","a3"),("a1","b1","c1"),("a1","b2","c3"),("b1","b2","b3"),("c1","c2","c3"),("a2","b2","c2"),("a3","b3","c3"),("a
3","b2","c1"))

name1 = input("Enter player1's name : ")

id1 = input('Enter '+name1+"'s gamer ID : ")

name2 = input("Enter player2's name : ")

id2 = input('Enter '+name2+"'s gamer ID : ")

def board(a1,a2,a3,b1,b2,b3,c1,c2,c3) :

os.system('cls')

print(f' {a1} | {a2} | {a3} \n-----------\n {b1} | {b2} | {b3} \n-----------\n {c1} | {c2} | {c3} ')

player1 = []

player2 = []

flag = False

def dec(x,y):

global flag,a,b,win_id,lose_id,loser,winner,draw

for j in win :

a=0;b=0

for h in j :

if h in x :

a+=1

if h in y :

b+=1

if a == 3 :

print(f"{name1} wins!!!")

winner = name1; win_id = id1; lose_id = id2; loser = name2


30
flag = True

draw = False

if b == 3 :

print(f"{name2} wins!!!")

winner = name2

win_id = id2

lose_id = id1

loser = name1

flag = True

draw = False

def move():

global draw

a1=' ';a2=' ';a3=' ';b1=' ';b2=' ';b3=' ';c1=' ';c2=' ';c3=' '

for i in range(9):

if flag:

break

if i%2 == 0 :

move1 = input(f"enter move {name1} : ")

while ((move1 in player1) or (move1 in player2)) or ((move1 not in moves)) :

print('already used square or non existent')

move1 = input(f"enter move {name1} : ")

player1.append(move1)

if move1 == 'a1':

a1 = 'X'

elif move1 == 'a2':

a2 = 'X'

elif move1 == 'a3':

a3 = 'X'

elif move1 == 'b1':

b1 = 'X'

elif move1 == 'b2':

b2 = 'X'

elif move1 == 'b3':

31
b3 = 'X'

elif move1 == 'c1':

c1 = 'X'

elif move1 == 'c2':

c2 = 'X'

elif move1 == 'c3':

c3 = 'X'

board(a1,a2,a3,b1,b2,b3,c1,c2,c3)

else :

move2 = input(f"enter move {name2} : ")

while ((move2 in player1) or (move2 in player2)) or ((move2 not in moves)) :

print('already used square or non existent')

move2 = input(f"enter move {name2} : ")

player2.append(move2)

if move2 == 'a1':

a1 = 'O'

elif move2 == 'a2':

a2 = 'O'

elif move2 == 'a3':

a3 = 'O'

elif move2 == 'b1':

b1 = 'O'

elif move2 == 'b2':

b2 = 'O'

elif move2 == 'b3':

b3 = 'O'

elif move2 == 'c1':

c1 = 'O'

elif move2 == 'c2':

c2 = 'O'

elif move2 == 'c3':

c3 = 'O'

board(a1,a2,a3,b1,b2,b3,c1,c2,c3)

dec(player1,player2)
32
if not(flag) :

print("guess it's a draw, well played lol")

draw = True

print(' a1 | a2 | a3 \n--------------\n b1 | b2 | b3 \n--------------\n c1 | c2 | c3 ')

move()

Main.py
import pymysql

import os

from math import *

import chess

import hangman

import tictactoe

cnx = pymysql.connect(host='localhost',user='root',password='12345',database='score')

cur = cnx.cursor()

def scoreboard() :

cur.execute('select * from score_count;')

cnx.commit()

con = cur.fetchall()

for i in con :

print("Name : "+i[1])

print("gamer ID - "+i[0])

print("Chess score : "+str(float(i[2])))

print("Hangman score : "+str(float(i[3])))

print("Tic Tac Toe score : "+str(float(i[4])))

print()

def main() :

print('WHICH GAME WOULD YOU WANT TO PLAY')

print('1.CHESS (multiplayer)')

print('2.HANGMAN (single player)')

print('3.TIC TAC TOE (multiplayer)')

print('4.VIEW SCORE BOARD')

num = int(input('pick am option : '))

if num == 1 : 33
flag = True ; flag2 = True
flag = True ; flag2 = True

chess.game()

cur.execute('select * from score_count;')

cnx.commit()

con = cur.fetchall()

for i in con :

if chess.win_id == i[0] :

if i[2] != None :

cur.execute(f"update score_count set chess = {i[2]+1} where gamer_id = '{chess.win_id}';")

cnx.commit()

flag = False

else :

cur.execute(f"update score_count set chess = 1 where gamer_id = '{chess.win_id}';")

cnx.commit()

flag = False

if chess.lose_id == i[0] :

flag2 = False

if flag :

cur.execute(f"insert into score_count(gamer_id,name,chess) values {chess.win_id,chess.winner,1};")

cnx.commit()

if flag2 :

cur.execute(f"insert into score_count(gamer_id,name,chess) values {chess.lose_id,chess.loser,0};")

cnx.commit()

elif num == 2 :

hangman.game2()

flag = True

cur.execute('select * from score_count;')

cnx.commit()

con = cur.fetchall()

for i in con :

if hangman.name_id == i[0] :

if hangman.win :

if i[3] != None :

34
cur.execute(f"update score_count set hangman = {i[3]+1} where gamer_id =
'{hangman.name_id}';")

cnx.commit()

flag = False

break

else :

flag = False

if flag :

if hangman.win :

cur.execute(f"insert into score_count(gamer_id,name,hangman) values


{hangman.name_id,hangman.name,1};")

cnx.commit()

else :

cur.execute(f"insert into score_count(gamer_id,name,hangman) values


{hangman.name_id,hangman.name,0};")

cnx.commit()

elif num == 3 :

tictactoe.game3()

flag = True ; flag2 = True

cur.execute('select * from score_count;')

cnx.commit()

con = cur.fetchall()

for i in con :

if tictactoe.draw == True:

if tictactoe.id1 == i[0] :

if i[4] != None :

cur.execute(f"update score_count set tic = {float(i[4])+0.5} where gamer_id = '{tictactoe.id1}';")

cnx.commit()

flag = False

else :

cur.execute(f"update score_count set tic = 0.5 where gamer_id = '{tictactoe.id1}';")

cnx.commit()

flag = False

35
elif tictactoe.id2 == i[0] :

if i[4] != None :

cur.execute(f"update score_count set tic = {float(i[4])+0.5} where gamer_id = '{tictactoe.id2}';")

cnx.commit()

flag2 = False

else :

cur.execute(f"update score_count set tic = 0.5 where gamer_id = '{tictactoe.id2}';")

cnx.commit()

flag2 = False

else :

if tictactoe.win_id == i[0] :

if i[4] != None :

cur.execute(f"update score_count set tic = {i[4]+1} where gamer_id = '{tictactoe.win_id}';")

cnx.commit()

flag = False

else :

cur.execute(f"update score_count set tic = 1 where gamer_id = '{tictactoe.win_id}';")

cnx.commit()

flag = False

if tictactoe.lose_id == i[0] :

flag2 = False

if tictactoe.draw :

if flag :

cur.execute(f"insert into score_count(gamer_id,name,tic) values {tictactoe.id1,tictactoe.name1,0.5};")

cnx.commit()

if flag2 :

cur.execute(f"insert into score_count(gamer_id,name,tic) values {tictactoe.id2,tictactoe.name2,0.5};")

cnx.commit()

36
else :

if flag :

cur.execute(f"insert into score_count(gamer_id,name,tic) values {tictactoe.win_id,tictactoe.winner,1};")

cnx.commit()

if flag2 :

cur.execute(f"insert into score_count(gamer_id,name,tic) values {tictactoe.lose_id,tictactoe.loser,0};")

cnx.commit()

if num == 4 :

scoreboard()

cont = input('Do you wish to continue ? (y = yes, n = no) :').lower()

while cont not in ['y','n'] :

print('Invalid option')

cont = input('Do you wish to continue ? (y = yes, n = no) : ').lower()

if cont == 'y' :

main()

else :

print('THANK YOU FOR USING THIS GAME CONSOLE THANK YOU')

quit()

37
Outputs

38
39
40
41
42
References

 Python.org
 Geeksforgeeks (https://www.geeksforgeeks.org/)
 Stackoverflow (https://stackoverflow.com/)
 Computer science with python text book for class XII
by Sumita Arora published by Dhanpat Rai and Co.

43

You might also like