Sie sind auf Seite 1von 23

Tic Tac Toe

Project Report

Submitted in Partial Fulfilment of the Requirements for

the Award of Degree of

Bachelor in Technology

Submitted by:

Name of Student (Roll No)

Nikunj

1703323

Department of Information Technology


Chandigarh Engineering College
Landran, Mohali

1703323 Page 1
CERTIFICATE

This is to certify that the work presented in the thesis entitled “Tic Tac Toe” is a
bonafide record of the work done during the period from July, 2017 to December, 2017
at Chandigarh Engineering College Landran, Mohali, Punjab, Nikunj , 1703323..

The project work is an authentic record of my own work and is carried out under
the supervision and guidance or Bachittarjeet Singh, instructor. The matter presented
in the report has not been submitted elsewhere, wholly or in part, for the award of any
other degree or diploma.

Nikunj (1703323)

This is to certify that the above statement made by the candidate is correct to the best
of my knowledge.

Bachittarjeet Singh

Instructor

Department of Information & Technology

Dr. Shashi Bhushan

Professor and Head

Department of Information & Technology

Chandigarh Engineering College, Landran, Mohali, PUNJAB

1703323 Page 2
ACKNOWLEDGEMENT

I take this opportunity to express my sincere gratitude to the Principal, Chandigarh Engineering
College, Landran, for providing this opportunity to carry out the present thesis work.

The constant guidance and encouragement received from Dr. Shashi Bhushan,
Professor and Head, Department of Information Technology, has been of great help in
carrying our present work and helped us in completing this project with success.

I would like to express a deep sense of gratitude to my Project Advisor Bachittarjeet


Singh, for his guidance and support in defining the design problem and towards the completion
of my project work. Without his wise counsel and able guidance, it would have been
impossible to complete the project in this manner.

I am also thankful to all the faculty and staff members of Department of Information
Technology, Chandigarh Engineering College, Landran for their intellectual support
throughout the course of this work.

1703323 Page 3
ABSTRACT

Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a


paper and pencil game for two players, X and O, who take turns
marking the spaces in a 3 × 3 grid. The player who achieves Placing
three of your marks in a horizontal, vertical or diagonal row wins the
game.
The following example of the game is won by the first player, X:

Players soon discover that the best game on both sides leads to a draw.
Therefore, tic-tac-toe is most often played by young children.
Due to the simplicity of the tic-tac-toe, it is often used as a
pedagogical tool to teach the concepts of good sportsmanship and the
branch of artificial intelligence that deals with the search for game
trees. It is easy to write a computer program to play perfectly to
tic-tac-toe, to list the 765 essentially different positions (the spatial
complexity of the state), or the 26,830 possible games to rotations and
reflections (the complexity of the game tree) in this Space.
The game can be generalized to a m, n, k-game in which two players
alternate placing stones of their own color on a m × n board, in order
to get k of their own color in a row.
Tic-tac-toe is the (3,3,3) -game. Harary's generalized tic-tac-toe is an
even wider generalization of tic tac toe. It can also be generalized as
and game. Tic-tac-toe is the game where n is equal to 3 and d is equal
to 2. If played correctly, the game will end in a tie by tic-tac-toe a
useless game.

1703323 Page 4
Table of Contents

I
Abstract
Ii
Certificate
iii
Acknowledgement

Chapter 1 Introduction 9-10


1.1 Motivation 10

Chapter 2 Concept 11-13


2.1 Design specification 11
2.2 Functionality and user interface 12
2.3 Data path 13
2.4 Use case diagram 14
2.5 Context diagram 15

Chapter 3 Implementation 14-21

3.1 Code 14-19

3.2 Output 20

3.3 Risk management 21

Chapter 4 Conclusions and Future Work 22

References 23

1703323 Page 5
1703323 Page 6
List of Figures

Sr. Figure Description Page No.


No.

1 Figure 2.1 11
2 Figure 2.2 13
3 Figure 2.3 14
4 Figure 2.4 15

1703323 Page 7
CHAPTER 1 - INTRODUCTION
TIC-TAC-TOE (OR NOUGHTS AND CROSSES, XS AND OS) IS A
PENCIL-AND-PAPER GAME FOR TWO PLAYERS, X AND O, WHO TAKE
TURNS MARKING THE SPACES IN A 3×3 GRID. THE PLAYER WHO
SUCCEEDS IN PLACING THREE RESPECTIVE MARKS IN A HORIZONTAL,
VERTICAL, OR DIAGONAL ROW WINS THE GAME.

PLAYERS SOON DISCOVER THAT BEST PLAY FROM BOTH PARTIES LEADS
TO A DRAW (OFTEN REFERRED TO AS CAT OR CAT'S GAME). HENCE,
TIC-TAC-TOE IS MOST OFTEN PLAYED BY YOUNG CHILDREN.

THE FRIENDLINESS OF TIC-TAC-TOE GAMES MAKES THEM IDEAL AS A


PEDAGOGICAL TOOL FOR TEACHING THE CONCEPTS OF GOOD
SPORTSMANSHIP AND THE BRANCH OF ARTIFICIAL INTELLIGENCE THAT
DEALS WITH THE SEARCHING OF GAME TREES. IT IS
STRAIGHTFORWARD TO WRITE A COMPUTER PROGRAM TO PLAY
TIC-TAC-TOE PERFECTLY, TO ENUMERATE THE 765 ESSENTIALLY
DIFFERENT POSITIONS (THE STATE SPACE COMPLEXITY), OR THE 26,830
POSSIBLE GAMES UP TO ROTATIONS AND REFLECTIONS (THE GAME
TREE COMPLEXITY) ON THIS SPACE.

1703323 Page 8
CHAPTER 1.1 – MOTIVATION
In this project, we have created two different agents who compete
against
 player vs player
 Player vs computer
Once I was playing this game so I thought of making a game that is
similar to tic tac toe. Then I searched on the internet about the idea
with 2 or 3 hrs.I found a game named blackjack which is played in
zero cross and Is very similar to tic tac toe but also differ.
In this I found it an interesiting thing that 2 peoples can compete.
In this there Is a 0 and * .one can choose either 0 or *.
Tic-tac-toe (or Noughts and crosses, X’s and O’s) is a
pencil-and-paper game for two players, X and O, who take turns
marking the spaces in a 3×3 grid. The player who succeeds in placing
three respective marks in a horizontal, vertical, or diagonal row wins
the game.

Players soon discover that best play from both parties leads to a draw
(often referred to as cat or cat's game). Hence, Tic-tac-toe is most
often played by young children.

1703323 Page 9
CHAPTER 2-CONCEPT
CHAPTER 2.1 – DESIGN SPECIFICATION

1703323 Page 10
Chapter 2.2 –Functionality and User interface
Tic-tac-toe on 8*8 matrix consists of 9 blocks (3 rows and 3
columns).
 PLAYER VS PLAYER
In the player vs player version two players turn by turn enter the
input from keyboard. Keyboard keys (1-9) map to the 9 blocks of
tic-tac-toe. So, in order to select an it blocks on tic-tac-toe player has
to press it key on the keyboard.
Once the display on 8*8 gets updated, turn switches to the next
player. Moves of players are shown by / and \ on the chosen block. In
case one of the players wins or the game gets draw, the 8*8 display
and score on LED display get refreshed automatically.
Pressing * or # at any point of time will restart the whole game.
Condition for win –
A player wins the game when he completes a row or a column or a
diagonal on the tic-tac-toe.

 PLAYER VS COMPUTER
In the player vs computer player, a single person can play the game
vs an automated player. Here the automated player has been
designed with always win strategy i.e. it won’t ever allow the other
player to win the game.
In both these versions, score on the LED display will specify
number of games won by each player and also show which
player is currently playing.

1703323 Page 11
CHAPTER 2.3 – DATA PATH

1703323 Page 12
CHAPTER 2.4 – USE CASE DIAGRAM

1703323 Page 13
CHAPTER 2.5 – CONTEXT DIAGRAM

1703323 Page 14
CHAPTER 3– IMPLEMENTATION
Code:-

from IPython.display import clear_output

def display_board(board):
clear_output() # Remember, this only works in jupyter!

print(' | |')
print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6])
print(' | |')
print('-----------')
print(' | |')
print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3])
print(' | |')
In [2]:
test_board = ['#','X','O','X','O','X','O','X','O','X']
display_board(test_board)
| |
X | O | X
| |
-----------
| |
O | X | O
| |
-----------
| |
X | O | X
| |

Step 2: player input and assign their marker as 'X' or


'O'.
In [4]:
def player_input():
marker = ''

while not (marker == 'X' or marker == 'O'):


marker = input('Player 1: Do you want to be X or O? ').upper()

if marker == 'X':
return ('X', 'O')
else:

1703323 Page 15
return ('O', 'X')
In [5]:
player_input()
Player 1: Do you want to be X or O? X
Out[5]:
('X', 'O')

Step 3: a desired position (number 1-9) and assigns it


to the board.
In [6]:
def place_marker(board, marker, position):
board[position] = marker
In [7]:
place_marker(test_board,'$',8)
display_board(test_board)
| |
X | $ | X
| |
-----------
| |
O | X | O
| |
-----------
| |
X | O | X
| |

Step 4:checks to see if someone has won.


In [8]:
def win_check(board,mark):

return ((board[7] == mark and board[8] == mark and board[9] == mark) or


# across the top
(board[4] == mark and board[5] == mark and board[6] == mark) or # acros
s the middle
(board[1] == mark and board[2] == mark and board[3] == mark) or # acros
s the bottom
(board[7] == mark and board[4] == mark and board[1] == mark) or # down
the middle
(board[8] == mark and board[5] == mark and board[2] == mark) or # down
the middle
(board[9] == mark and board[6] == mark and board[3] == mark) or # down
the right side
(board[7] == mark and board[5] == mark and board[3] == mark) or # diago
nal
(board[9] == mark and board[5] == mark and board[1] == mark)) # diagona
l

1703323 Page 16
In [9]:
win_check(test_board,'X')
Out[9]:
True

Step 5:random module to randomly decide which


player goes first.
In [10]:
import random

def choose_first():
if random.randint(0, 1) == 0:
return 'Player 2'
else:
return 'Player 1'
In [11]:
def space_check(board, position):

return board[position] == ' '

Step 6: checks if the board is full and returns a boolean


value.
In [12]:
def full_board_check(board):
for i in range(1,10):
if space_check(board, i):
return False
return True
In [13]:
def player_choice(board):
position = 0

while position not in [1,2,3,4,5,6,7,8,9] or not space_check(board, pos


ition):
position = int(input('Choose your next position: (1-9) '))

return position
In [14]:
def replay():

return input('Do you want to play again? Enter Yes or No: ').lower().st
artswith('y')

Steps 7:
In [15]:
print('Welcome to Tic Tac Toe!')

1703323 Page 17
while True:
# Reset the board
theBoard = [' '] * 10
player1_marker, player2_marker = player_input()
turn = choose_first()
print(turn + ' will go first.')

play_game = input('Are you ready to play? Enter Yes or No.')

if play_game.lower()[0] == 'y':
game_on = True
else:
game_on = False

while game_on:
if turn == 'Player 1':
# Player1's turn.

display_board(theBoard)
position = player_choice(theBoard)
place_marker(theBoard, player1_marker, position)

if win_check(theBoard, player1_marker):
display_board(theBoard)
print('Congratulations! You have won the game!')
game_on = False
else:
if full_board_check(theBoard):
display_board(theBoard)
print('The game is a draw!')
break
else:
turn = 'Player 2'

else:
# Player2's turn.

display_board(theBoard)
position = player_choice(theBoard)
place_marker(theBoard, player2_marker, position)

if win_check(theBoard, player2_marker):
display_board(theBoard)
print('Player 2 has won!')
game_on = False
else:

1703323 Page 18
if full_board_check(theBoard):
display_board(theBoard)
print('The game is a draw!')
break
else:
turn = 'Player 1'

if not replay():
break

1703323 Page 19
Chapter 3.1-Output
1. Output when match gets draw: -
| |
X | O | X
| |
-----------
| |
O | O | X
| |
-----------
| |
X | X | O
| |
The game is a draw!
Do you want to play again? Enter Yes or No: NO

2. Output when match is won by X: -

3. Output when match is won by O: -

1703323 Page 20
Chapter 3.2– Risk Management
A common definition of risk is an uncertain event that if it occurs, can have a
positive or negative
effect on a project’s goals. It helps us to achieve the project’s
objectives, thus ensuring the
successful completion of the project. For successful development of this project
we have to need to identify the possible risk.
The possible risk for this project is described in below:
(a) The probability of moving away our-self from this project before it is finish
is low.
(b) The probability of user acceptance is great.
(c) The probability of requirement can't come in the time is low.
(d) The probability of marketing the product system is great.
(e) The probability of technology components isn’t fit for purpose of this
project is low.
(f) The probability of selecting low quality requirements are low.
(g) The probability of take wrong decisions are low.
(h) The probability of does not complete this project within a limited time is
low.

1703323 Page 21
Chapter 4– Conclusions and Future Work

The two-player machine and one player machine for tic tac
toe work fine.
Limitations: -
Score cannot be displayed properly because of limitation on number of
connectors available. For temporary basis con- 21 is being used which
does not maps the first pin and hence introduces an error in the score.
Possible refinements: -
We thought of introducing this additional feature which we could not
because of timing constraints: -
difficulty levels in one player machine. As for now we have computer
player which will always win.

1703323 Page 22
REFERENCES
 Python.com

 Pythontutorial.com

 Wikipedia

 Geeks for geeks

 W3 school

 Quora

 JavaTpoint

 Coursera
 Jupiternotebook

1703323 Page 23

Das könnte Ihnen auch gefallen