Skip to content

Commit 88de893

Browse files
committed
PCC12 code-monk08
1 parent e8322ec commit 88de893

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

12/code-monk08/CROSS.png

2.22 KB
Loading

12/code-monk08/NOUGHT.png

4.72 KB
Loading

12/code-monk08/Screen.png

613 KB
Loading

12/code-monk08/Tic Tac Toe.py

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import sys
2+
import math
3+
import pygame
4+
import numpy as np
5+
6+
rows = 3
7+
cols = 3
8+
squaresize = 250
9+
width = cols * squaresize
10+
height = rows * squaresize
11+
size = (width, height)
12+
game_over = False
13+
black = (0, 0, 0)
14+
white = (255, 255, 255)
15+
turn = 0
16+
17+
pygame.init()
18+
pygame.display.set_caption('Tic Tac Toe | code-monk08')
19+
20+
crossimg = pygame.image.load('CROSS.png')
21+
noughtimg = pygame.image.load('NOUGHT.png')
22+
square = [[0 for i in range(3)] for j in range(3)]
23+
for c in range(cols):
24+
for r in range(rows):
25+
square[r][c] = pygame.Rect(
26+
r*squaresize, c*squaresize, squaresize, squaresize)
27+
28+
29+
def nought(x, y):
30+
screen.blit(noughtimg, (x, y))
31+
32+
33+
def cross(x, y):
34+
screen.blit(crossimg, (x, y))
35+
36+
37+
def winning_move(board, piece):
38+
for c in range(cols-2):
39+
for r in range(rows):
40+
if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece:
41+
print("player "+str(piece)+" wins!")
42+
return True
43+
44+
for c in range(cols):
45+
for r in range(rows-2):
46+
if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece:
47+
print("player "+str(piece)+" wins!")
48+
return True
49+
50+
for r in range(rows-2): # 0
51+
if board[r][2-r] == piece and board[r+1][2-r-1] == piece and board[r+2][2-r-2] == piece:
52+
print("player "+str(piece)+" wins!")
53+
return True
54+
55+
for c in range(cols-2):
56+
for r in range(rows-2):
57+
if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece:
58+
print("player "+str(piece)+" wins!")
59+
return True
60+
61+
62+
def draw_board(board):
63+
for c in range(cols):
64+
for r in range(rows):
65+
pygame.draw.rect(screen, black, square[r][c], 8)
66+
pygame.display.update()
67+
68+
for c in range(cols):
69+
for r in range(rows):
70+
if board[r][c] == 1:
71+
nought(int(c*squaresize) + 6, int(r*squaresize)+6)
72+
elif board[r][c] == 2:
73+
cross(int(c*squaresize)+5, int(r*squaresize)+5)
74+
pygame.display.update()
75+
76+
77+
def tie_move():
78+
pass
79+
80+
81+
def create_board():
82+
board = np.zeros((rows, cols))
83+
return board
84+
85+
86+
def play_move(board, row, col, piece):
87+
board[row][col] = piece
88+
89+
90+
def is_valid_location(board, row, col):
91+
return board[row][col] == 0
92+
93+
94+
def print_board(board):
95+
print(board)
96+
print(" ----------")
97+
print(" "+str([1, 2, 3]))
98+
print()
99+
100+
101+
board = create_board()
102+
print_board(board)
103+
screen = pygame.display.set_mode(size)
104+
screen.fill(white)
105+
draw_board(board)
106+
107+
108+
while not game_over:
109+
for event in pygame.event.get():
110+
111+
if event.type == pygame.QUIT:
112+
sys.exit()
113+
114+
if event.type == pygame.MOUSEBUTTONDOWN:
115+
if turn == 0: # disc
116+
posx = event.pos[0]
117+
posy = event.pos[1]
118+
row = int(math.floor(posy/squaresize))
119+
col = int(math.floor(posx/squaresize))
120+
print(row, col)
121+
if is_valid_location(board, row, col):
122+
play_move(board, row, col, 1)
123+
turn += 1
124+
turn = turn % 2
125+
print_board(board)
126+
draw_board(board)
127+
if winning_move(board, 1):
128+
game_over = True
129+
else:
130+
posx = event.pos[0]
131+
posy = event.pos[1]
132+
row = int(math.floor(posy/squaresize))
133+
col = int(math.floor(posx/squaresize))
134+
print(row, col)
135+
if is_valid_location(board, row, col):
136+
play_move(board, row, col, 2)
137+
turn += 1
138+
turn = turn % 2
139+
print_board(board)
140+
draw_board(board)
141+
if winning_move(board, 2):
142+
game_over = True
143+
144+
if game_over:
145+
pygame.time.wait(3000)

0 commit comments

Comments
 (0)