Tic Tac Toe : Handling win or lose

abhi1988sri

Still Fresh
Joined
Oct 20, 2013
Messages
1
Hi,

I am new to python as well as pygame so was trying my hand on making TicTacToe.I made one with no animations in python but with animations, I see to get stuck with deciding win and loss. I want user to make use mouse click and if its in a certain region , I want to make "X" or "O". Also, how decide win or loss, how to match. Please help.


import pygame,sys
from pygame.locals import *

pygame.init()

FPS=30
fpsClock=pygame.time.Clock()

DISPLAYSURF=pygame.display.set_mode((500,500))

pygame.display.set_caption("TicTacToeAnimation")

WHITE=(255,255,255)
BLUE=(0,0,255)
GREEN=(0,255,0)
RED=(255,0,0)

xcord=120
ycord=100

myfont=pygame.font.SysFont("monopause",20)
label=myfont.render("TIC TAC TOE",1,(255,0,0,120))
#put sound here later
running=1

def drawLinesForMouse(x,y):
#do something make "X" or "O"


while running:
DISPLAYSURF.fill(WHITE)
event = pygame.event.poll()
if event.type == pygame.QUIT:
running=0
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x,y=event.pos

DISPLAYSURF.blit(label,(150,10))
pygame.draw.line(DISPLAYSURF,BLUE,(xcord,ycord),(300,ycord))
pygame.draw.line(DISPLAYSURF,BLUE,(xcord,ycord+60),(300,ycord+60))
pygame.draw.line(DISPLAYSURF,BLUE,(xcord+60,ycord-60),(xcord+60,200))
pygame.draw.line(DISPLAYSURF,BLUE,(xcord+120,ycord-60),(xcord+120,200))

drawLinesForMouse(x,y)
pygame.display.update()




Any help is appreciated..

Thanks & Regards

Abhinav
 

rohezal

Advanced Member
Joined
Oct 18, 2009
Messages
1,673
I would make an 3x3 array and check give a member a value (0 1 2). 0 ist empty 1 is a circle, 2 is a cross. If array[0][0] array[1][1] array[2][2] have the same value, the player who has his symbol in it wins. You can do all lines lines this way like array[0][0], array[0][1], array[0][2].
 

theweirdn8

Member
Joined
Aug 4, 2012
Messages
252
Location
Chicago
For my tic tac toe game I did the array thing and did about 5 if-statement checks per 0(player) and 1(pc player) for a 3 in a row check. Including diagonals of course.
 

Elw3

ƐʍlƎ
Joined
Aug 10, 2010
Messages
1,299
My IRC bot uses that line:


class feld:
        aa=[" "," "," ",]
        ab=[" "," "," ",]
        ac=[" "," "," ",]
        one=[aa,ab,ac]
 
#### part is too long to paste ###
#player.one[1] is either X or O, game.on is the game state. 
 
#check for a draw
   game.round+=1
if game.round == 9: 
    game.on=4
#check if move was successful
   diag=0
   diagz=0
   for c in [0,1,2]:
    if feld.one[c].count(player.one[1]) == 3:
      game.on=3
    vertikal=0
    for b in [0,1,2]:
     vertikal=vertikal+ feld.one[c].count(player.one[1])
    if vertikal == 3:
     game.on=3 
    diag=diag+ feld.one[c][c].count(player.one[1])
    diagz=diagz+ feld.one[c][-c+2].count(player.one[1])
    if diag == 3:
     game.on=3 
    if diagz == 3:
     game.on=3
So its just counting in the 3x3 space. The great thing is that the bot can also use a 4x4x4 field with that line.
 
Last edited by a moderator:

milkshake

Advanced Member
Joined
May 18, 2009
Messages
3,735
Age
35
Location
Rotherham, UK
834


159


672


Just count the players selections and if they hit 15 exactly they win Easy as that :)
 
Last edited by a moderator:
Top