├── grid.png ├── README.md ├── o-icon.png ├── x-icon.png ├── requirements.txt ├── LICENSE └── main.py /grid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/carmensantos/tic-tac-toe/HEAD/grid.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tic-tac-toe 2 | Simplified Tic-Tac-Toe Game in Python - incomplete 3 | -------------------------------------------------------------------------------- /o-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/carmensantos/tic-tac-toe/HEAD/o-icon.png -------------------------------------------------------------------------------- /x-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/carmensantos/tic-tac-toe/HEAD/x-icon.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pip~=22.3.1 2 | wheel~=0.38.4 3 | setuptools~=65.5.1 4 | pygame~=2.5.2 -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 carmensantos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import pygame 2 | 3 | pygame.init() 4 | 5 | WINDOW_WIDTH = 720 6 | PIXEL_WIDTH = WINDOW_WIDTH // 3 7 | 8 | screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_WIDTH)) 9 | clock = pygame.time.Clock() 10 | 11 | font = pygame.font.Font('freesansbold.ttf', 32) 12 | winner_text = font.render('', True, 'green') 13 | textRect = winner_text.get_rect() 14 | textRect.center = (WINDOW_WIDTH // 2 - PIXEL_WIDTH // 2, WINDOW_WIDTH // 2) 15 | 16 | running = True 17 | 18 | board = [ 19 | [None, None, None], 20 | [None, None, None], 21 | [None, None, None] 22 | ] 23 | 24 | 25 | def load_icon(path, resolution): 26 | icon = pygame.image.load(path) 27 | return pygame.transform.scale(icon, resolution) 28 | 29 | 30 | ICON_X = load_icon('x-icon.png', [PIXEL_WIDTH, PIXEL_WIDTH]) 31 | ICON_O = load_icon('o-icon.png', [PIXEL_WIDTH, PIXEL_WIDTH]) 32 | GRID = load_icon('grid.png', [WINDOW_WIDTH, WINDOW_WIDTH]) 33 | 34 | PLAYER_1 = 0 35 | PLAYER_2 = 1 36 | player = PLAYER_1 37 | 38 | 39 | def play_turn(current_player): 40 | curr_coordinate = pygame.math.Vector2(pygame.mouse.get_pos()) 41 | normalized_coordinate = curr_coordinate // PIXEL_WIDTH 42 | if pygame.mouse.get_pressed()[0]: 43 | col, row = map(int, normalized_coordinate) 44 | board[row][col] = current_player 45 | global player 46 | player = 1 - player 47 | 48 | 49 | def draw_icons(): 50 | for i, row in enumerate(board): 51 | for j, col in enumerate(board[i]): 52 | if board[i][j] == 0: 53 | screen.blit(ICON_O, (j * PIXEL_WIDTH, i * PIXEL_WIDTH)) 54 | if board[i][j] == 1: 55 | screen.blit(ICON_X, (j * PIXEL_WIDTH, i * PIXEL_WIDTH)) 56 | 57 | 58 | def has_equal_icons(elements, game_player): 59 | for element in elements: 60 | if element != game_player: 61 | return False 62 | return True 63 | 64 | 65 | def has_winning_row(game_player): 66 | return has_equal_icons(board[0], game_player) \ 67 | or has_equal_icons(board[1], game_player) \ 68 | or has_equal_icons(board[2], game_player) 69 | 70 | 71 | def has_winning_col(game_player): 72 | return has_equal_icons([board[0][0], board[1][0], board[2][0]], game_player) \ 73 | or has_equal_icons([board[0][1], board[1][1], board[2][1]], game_player) \ 74 | or has_equal_icons([board[0][2], board[1][2], board[2][2]], game_player) 75 | 76 | 77 | def has_winning_diagonal(game_player): 78 | return has_equal_icons([board[0][0], board[1][1], board[2][2]], game_player) \ 79 | or has_equal_icons([board[0][2], board[1][1], board[2][0]], game_player) 80 | 81 | 82 | def is_winner(game_player): 83 | return has_winning_row(game_player) \ 84 | or has_winning_col(game_player) \ 85 | or has_winning_diagonal(game_player) 86 | 87 | 88 | def check_victory(): 89 | global winner_text 90 | if is_winner(PLAYER_1): 91 | winner_text = font.render('Player 1 WON!', True, 'green') 92 | return True 93 | if is_winner(PLAYER_2): 94 | winner_text = font.render('Player 2 WON!', True, 'green') 95 | return True 96 | 97 | 98 | while running: 99 | for event in pygame.event.get(): 100 | if event.type == pygame.QUIT: 101 | running = False 102 | 103 | # RENDER YOUR GAME HERE 104 | pygame.display.flip() 105 | screen.fill("white") 106 | screen.blit(GRID, (0, 0)) 107 | pygame.event.wait() 108 | play_turn(player) 109 | draw_icons() 110 | if check_victory(): 111 | screen.blit(winner_text, textRect) 112 | 113 | pygame.quit() 114 | --------------------------------------------------------------------------------