diff --git a/tic_tac_toe/README.md b/tic_tac_toe/README.md new file mode 100644 index 00000000..e3508541 --- /dev/null +++ b/tic_tac_toe/README.md @@ -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) \ No newline at end of file diff --git a/tic_tac_toe/main.py b/tic_tac_toe/main.py new file mode 100644 index 00000000..8d03769c --- /dev/null +++ b/tic_tac_toe/main.py @@ -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() \ No newline at end of file diff --git a/tic_tac_toe/media/first-move.JPG b/tic_tac_toe/media/first-move.JPG new file mode 100644 index 00000000..2d862f3e Binary files /dev/null and b/tic_tac_toe/media/first-move.JPG differ diff --git a/tic_tac_toe/media/start.JPG b/tic_tac_toe/media/start.JPG new file mode 100644 index 00000000..31bf9c33 Binary files /dev/null and b/tic_tac_toe/media/start.JPG differ diff --git a/tic_tac_toe/media/winner.JPG b/tic_tac_toe/media/winner.JPG new file mode 100644 index 00000000..6da02a68 Binary files /dev/null and b/tic_tac_toe/media/winner.JPG differ diff --git a/tic_tac_toe/requirements.txt b/tic_tac_toe/requirements.txt new file mode 100644 index 00000000..ac495ece --- /dev/null +++ b/tic_tac_toe/requirements.txt @@ -0,0 +1 @@ +numpy==1.19.4 \ No newline at end of file