Here is what you have now.
# tic-tac-toe.py
# Tic-Tac-Toe
# Created by Mr.Yergler
# Typed by Jimmy
# March 24, 2010
import pygame
import sys
import time
from pygame import *
pygame.init()
ttt = pygame.display.set_mode((300,325))
pygame.display.set_caption('Tic-Tac-Toe')
# X will go first
XO = "X"
# Empty Grid
grid = [[ None, None, None],
[ None, None, None],
[ None, None, None,]]
def initBoard(ttt):
# Initialize the board and return it
# As a variable
# ttt : a properly initialized display variable
background = pygame.Surface(ttt.get_size())
background = background.convert()
background.fill((255,255,255))
# Draw the grid lines
# Vertical lines
pygame.draw.line(background, (0,0,0), (100,0), (100, 300), 2)
pygame.draw.line(background, (0,0,0), (200,0), (200, 300), 2)
# Horizontal lines
pygame.draw.line(background, (0,0,0), (0,100), (300, 100), 2)
pygame.draw.line(background, (0,0,0), (0,200), (300, 200), 2)
# Return the board
return background
def showBoard(ttt, board):
# Redraw the board on the screen(ttt)
drawStatus(board)
ttt.blit(board, (0,0))
pygame.display.flip()
def boardPos(mouseX, mouseY):
# Determine the row
if mouseY < 100:
row = 0
elif mouseY < 200:
row = 1
else:
row = 2
# Determine column
if mouseX < 100:
col = 0
elif mouseX < 200:
col = 1
else:
col = 2
# Return the two
return row, col
def clickBoard(board):
# Determine where clicked and draw
# tell Python to acess globals grid and XO
global grid
global XO
mouseX, mouseY = pygame.mouse.get_pos()
row, col = boardPos(mouseX, mouseY)
# Don't use this space
if grid[row][col] == "X" or grid[row][col] == "O":
return
drawMove(board, row, col, XO)
if XO == "X":
XO = "O"
else:
XO = "X"
def drawMove (board, boardRow, boardCol, Piece):
# Draw an X or O on the board at the place
# Find center of space
global grid
centerX = boardCol * 100 + 50
centerY = boardRow * 100 + 50
if Piece == "O":
pygame.draw.circle(board, (0,0,0), (centerX, centerY), 44, 2)
else:
pygame.draw.line(board, (0,0,0), (centerX - 22, centerY - 22), (centerX + 22, centerY + 22), 2)
pygame.draw.line(board, (0,0,0), (centerX + 22, centerY - 22), (centerX - 22, centerY + 22), 2)
grid[boardRow][boardCol] = Piece
def drawStatus(board):
global XO, winner, grid
if winner is None and checkGrid(grid) == False:
message = XO + "'s turn."
elif winner is not None:
message = winner + " won!"
else:
message = "Cat's game!"
# Render the message
font = pygame.font.Font(None, 24)
text = font.render(message, 1, (0,0,0))
# Copy it onto the board
board.fill ((255,255,255), (0, 300, 300, 25))
board.blit(text, (10,300))
winner = None
mouseX, mouseY = pygame.mouse.get_pos()
board = initBoard()
running = True
while (running == True):
for event in pygame.event.get():
if event.type is pygame.QUIT:
running = False
elif event.type is pygame.MOUSEBUTTONDOWN:
clickBoard(board)
[INDENT]showBoard(ttt, board)[/INDENT]
So now we add the winners.
We need a function to check the Grid and a function to check for a winner.
checkGrid:
def checkGrid(grid): for row in range(0,3): for col in range(0,3): if grid[row][col] == None: return False break return TruecheckGrid simply checks our list of squares(or our grid) and returns True if it is full otherwise it returns False.
Now we need to check for a winner.
gameWon:
def gameWon(board): # Determine if won global grid, winner # check for winning row for row in range(0,3): if grid[row][0] == grid[row][1] == grid[row][2] and grid[row][0] is not None: winner = grid[row][0] pygame.draw.line(board, (250,0,0), (0, (row + 1) * 100 - 50), (300, (row + 1) * 100 - 50), 2) return True break for col in range(0,3): if grid[0][col] == grid[1][col] == grid[2][col] and grid[0][col] is not None: winner = grid[0][col] pygame.draw.line(board, (250,0,0), ((col+1)* 100 - 50, 0), ((col+1) * 100 -50, 300), 2) return True break if grid[0][0] == grid[1][1] == grid[2][2] and grid[0][0] is not None: winner = grid[0][0] pygame.draw.line(board, (250,0,0), (50, 50), (250,250), 2) return True if grid[0][2] == grid[1][1] == grid[2][0] and grid[0][2] is not None: winner = grid[0][2] pygame.draw.line(board, (250,0,0), (250,50), (50, 250), 2) return TrueThis funciton is long, but fairly self-explainatory.
It simply checks the columns for a winner, the rows, and the two diagonals.
Congrats you have finished the program.
If you need to reference a problem just ask.


Sign In
Create Account


Back to top









