Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tic_tac_toe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Tic Tac Toe

Tic Tac Toe is a popular 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 of their marks in a diagonal, horizontal,
or vertical row is the winner.

For more information about the the game, check [here](https://en.wikipedia.org/wiki/Tic-tac-toe).

The game was implemented in Python, using **Numpy** library.

## How to launch
first you need to install *numpy*:\
`pip install -r requirements.txt`\
then you can directly launch the main script:\
`python main.py`

good luck :)


#### Game Sample
each play, in turn, will choose one of the available positions by
entering the corresponding number. Numbers are 0-8 from going from top-left
to bottom-right corner.

*start*

![Start](media/start.JPG)

*first move*

![first-move](media/first-move.JPG)

*winner*

![winner](media/winner.JPG)
60 changes: 60 additions & 0 deletions tic_tac_toe/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import numpy as np


grid = np.full((3,3), '-')
cells = {'0': (0,0), '1': (0,1), '2': (0,2),
'3': (1,0), '4': (1,1), '5': (1,2),
'6': (2,0), '7': (2,1), '8': (2,2)}

diagonal_1 = [0,1,2], [0,1,2] # main diagonal
diagonal_2 = [0,1,2], [2,1,0] # reverse diagonal
ver_hor_lines = {'v1': grid[:,0], 'v2': grid[:,1], 'v3': grid[:,2], # verticals
'h1': grid[0,:], 'h2': grid[1,:], 'h3': grid[2,:]} # horizontals

player = ''
turn = 1
free_spots = ['0', '1', '2', '3', '4', '5', '6', '7', '8']
spot = ''

while True:
# printing the grid
for el in grid:
print(' '.join(el.astype(str)))

# check if player won
if np.all(grid[diagonal_1] == player) or np.all(grid[diagonal_2] == player):
print(f"player {player.upper()}, you won !")
quit()
for line in ver_hor_lines:
if np.all(ver_hor_lines[line] == player):
print(f"player {player.upper()}, you won !")
quit()
print('available positions: {}'.format(' '.join(free_spots)))

# check if game ended as a tie
if not free_spots:
print('END GAME: TIE')
break

# update the player
if turn % 2 == 0:
player = 'o'
else:
player = 'x'

# ask the input
spot = input('player {}, enter a position: '.format(player.upper()))
# entering 'out' will end the game at anytime
if spot == 'out':
quit()
# check if input is valid
if spot in free_spots:
# update the grid
position = cells[spot]
grid[position] = player
free_spots.remove(spot)
turn += 1
else:
print('not valid. Enter again.')

print()
Binary file added tic_tac_toe/media/first-move.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tic_tac_toe/media/start.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tic_tac_toe/media/winner.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tic_tac_toe/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numpy==1.19.4