Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 11th, 2005, 10:13 AM   #1
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Naughts and Crosses

Ok it may not seem so amazing to you, but this is my first ever proper pygame game. It took me 9 hours (with potty breaks and lunch break).

You can download the source code and files here: http://jackf.cogia.net/coldDeath-XandO.rar

UnRAR the file, then doubleclick game.py to run it.
It will require Python and pyGame to run.

Here is the source code so you can check it out:
#Name: Naughts and Crosses
#Author: ColdDeath
#Date: 11/09/2005

import os
import pygame
import random
import sys
from pygame.locals import *

pygame.init()

#Window Setup
gameWindow = pygame.display.set_mode((150, 150))
pygame.display.set_caption('X\'s and O\'s')
fileName_Icon = os.path.join('Data', 'x.png')
surface_Icon = pygame.image.load(fileName_Icon)
pygame.display.set_icon(surface_Icon)
gameScreen = pygame.display.get_surface()

#Make image paths into variables
fileName_Cross = os.path.join('Data', 'x.png')
fileName_Naught = os.path.join('Data', 'o.png')
fileName_Board = os.path.join('Data', 'board.png')
fileName_Win = os.path.join('Data', 'win.png')
fileName_Lose = os.path.join('Data', 'lose.png')
fileName_Draw = os.path.join('Data', 'draw.png')

#Make images into surfaces
surface_Cross = pygame.image.load(fileName_Cross)
surface_Naught = pygame.image.load(fileName_Naught)
surface_Board = pygame.image.load(fileName_Board)
surface_Win = pygame.image.load(fileName_Win)
surface_Lose = pygame.image.load(fileName_Lose)
surface_Draw = pygame.image.load(fileName_Draw)

#Draw the grid/board
gameScreen.blit(surface_Board, (0,0))

#Update Screen
pygame.display.flip() 

class game:

	Board = ['n','n','n','n','n','n','n','n','n']
	availibleMoves = [0,1,2,3,4,5,6,7,8]
	turn = 'x'
	def checkQuit(self,events):
		#When game is over check to see if Enter is press or if Exit button is clicked, then exits
		for event in events: 
			if event.type == QUIT: 
				sys.exit(0)
			if event.type == KEYDOWN:
				if event.key == 13:
					sys.exit(0)
	def input(self,events): 
		#Checks for keyboard input and make a move if it finds it
		for event in events:
			if event.type == QUIT: 
				sys.exit(0)
			if event.type == KEYUP:
				if event.key == 49:
					self.Board[0] = 'x'
					gameScreen.blit(surface_Cross, (0,0))
					self.availibleMoves[0] = 'n'
					self.turn = 'o'
				elif event.key == 50:
					self.Board[1] = 'x'
					gameScreen.blit(surface_Cross, (50,0))
					self.availibleMoves[1] = 'n'
					self.turn = 'o'
				elif event.key == 51:
					self.Board[2] = 'x'
					gameScreen.blit(surface_Cross, (100,0))
					self.availibleMoves[2] = 'n'
					self.turn = 'o'
				elif event.key == 52:
					self.Board[3] = 'x'
					gameScreen.blit(surface_Cross, (0,50))
					self.availibleMoves[3] = 'n'
					self.turn = 'o'
				elif event.key == 53:
					self.Board[4] = 'x'
					gameScreen.blit(surface_Cross, (50,50))
					self.availibleMoves[4] = 'n'
					self.turn = 'o'
				elif event.key == 54:
					self.Board[5] = 'x'
					gameScreen.blit(surface_Cross, (100,50))
					self.availibleMoves[5] = 'n'
					self.turn = 'o'
				elif event.key == 55:
					self.Board[6] = 'x'
					gameScreen.blit(surface_Cross, (0,100))
					self.availibleMoves[6] = 'n'
					self.turn = 'o'
				elif event.key == 56:
					self.Board[7] = 'x'
					gameScreen.blit(surface_Cross, (50,100))
					self.availibleMoves[7] = 'n'
					self.turn = 'o'
				elif event.key == 57:
					self.Board[8] = 'x'
					gameScreen.blit(surface_Cross, (100,100))
					self.availibleMoves[8] = 'n'
					self.turn = 'o'
				pygame.display.flip()
			else:
				#print event
				pass
				
	def aiMove(self):
		new = random.choice(self.availibleMoves)
		try:
			if new == 'n':
					self.aiMove()
			else:
				if self.Board[new] == 'n':
					if new == 0:
						self.Board[0] = 'o'
						gameScreen.blit(surface_Naught, (0,0))
						self.availibleMoves[0] = 'n'
					elif new == 1:
						self.Board[1] = 'o'
						gameScreen.blit(surface_Naught, (50,0))
						self.availibleMoves[1] = 'n'
					elif new == 2:
						self.Board[2] = 'o'
						gameScreen.blit(surface_Naught, (100,0))
						self.availibleMoves[2] = 'n'
					elif new == 3:
						self.Board[3] = 'o'
						gameScreen.blit(surface_Naught, (0,50))
						self.availibleMoves[3] = 'n'
					elif new == 4:
						self.Board[4] = 'o'
						gameScreen.blit(surface_Naught, (50,50))
						self.availibleMoves[4] = 'n'
					elif new == 5:
						self.Board[5] = 'o'
						gameScreen.blit(surface_Naught, (100,50))
						self.availibleMoves[5] = 'n'
					elif new == 6:
						self.Board[6] = 'o'
						gameScreen.blit(surface_Naught, (0,100))
						self.availibleMoves[6] = 'n'
					elif new == 7:
						self.Board[7] = 'o'
						gameScreen.blit(surface_Naught, (50,100))
						self.availibleMoves[7] = 'n'
					elif new == 8:
						self.Board[8] = 'o'
						gameScreen.blit(surface_Naught, (100,100))
						self.availibleMoves[8] = 'n'
		except TypeError:
			self.aiMove()
		pygame.display.flip()
		self.turn = 'x'
		
		
			
	def checkStatus(self):
	#Checks if anyone has won
	#X win conditions
		if self.Board[0:3] == ['x','x','x']: return 'x'
		if self.Board[3:6] == ['x','x','x']: return 'x'
		if self.Board[6:10] == ['x','x','x']: return 'x'
		if (self.Board[0]+self.Board[3]+self.Board[6] == 'xxx'): return 'x'
		if (self.Board[1]+self.Board[4]+self.Board[7] == 'xxx'): return 'x'
		if (self.Board[2]+self.Board[5]+self.Board[8] == 'xxx'): return 'x'
		if (self.Board[0]+self.Board[4]+self.Board[8] == 'xxx'): return 'x'
		if (self.Board[2]+self.Board[4]+self.Board[6] == 'xxx'): return 'x'
	#O win conditions
		if self.Board[0:3] == ['o','o','o']: return 'o'
		if self.Board[3:6] == ['o','o','o']: return 'o'
		if self.Board[6:10] == ['o','o','o']: return 'o'
		if (self.Board[0]+self.Board[3]+self.Board[6] == 'ooo'): return 'o'
		if (self.Board[1]+self.Board[4]+self.Board[7] == 'ooo'): return 'o'
		if (self.Board[2]+self.Board[5]+self.Board[8] == 'ooo'): return 'o'
		if (self.Board[0]+self.Board[4]+self.Board[8] == 'ooo'): return 'o'
		if (self.Board[2]+self.Board[4]+self.Board[6] == 'ooo'): return 'o'
	#Draw conditions
		if (self.availibleMoves == ['n','n','n','n','n','n','n','n','n']):
			return 'd'
		else: 
			return 'n'

NewGame = game()
#Main program loop
while 1: 
	#if x has won show the win image
	if NewGame.checkStatus() == 'x':
		gameScreen.blit(surface_Win, (0,0))
		pygame.display.flip()
		NewGame.checkQuit(pygame.event.get())
	#if o has won show the lose image
	elif NewGame.checkStatus() == 'o':
		gameScreen.blit(surface_Lose, (0,0))
		pygame.display.flip()
		NewGame.checkQuit(pygame.event.get())
	#if its a draw show draw image
	elif NewGame.checkStatus() == 'd':
		gameScreen.blit(surface_Draw, (0,0))
		pygame.display.flip()
		NewGame.checkQuit(pygame.event.get())
	#otherwise make a move
	else:
		if NewGame.turn == 'x':
			NewGame.input(pygame.event.get())
		elif NewGame.turn == 'o':
			NewGame.aiMove()
Woot i'm really happy

Here is a screenshot of it in action:
Attached Images
File Type: jpg screenshot-xando.jpg (8.0 KB, 93 views)
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Sep 11th, 2005, 10:51 AM   #2
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
Is naughts and crosses anything like tic-tac-toe?
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old Sep 11th, 2005, 10:56 AM   #3
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Its the english name for Tic-Tac-Toe.
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Sep 11th, 2005, 11:24 AM   #4
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
Looks good, I'm switching everything over to a new computer, so once I get Pygame installed again, I'll check it out
thechristelegacy is offline   Reply With Quote
Old Sep 11th, 2005, 11:30 AM   #5
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Cool thanks.
Also it doesn't have any AI as such, it chooses a random place to go, if there is already a naught or cross there it uses recursion to choose again.
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Sep 11th, 2005, 2:07 PM   #6
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
Quote:
Its the english name for Tic-Tac-Toe.
I think tic-tac-toe is already in English :p :p
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old Sep 11th, 2005, 2:36 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Lol @ Iignotus! I thought it was Ceros y Equis. Or maybe that's a beer.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 11th, 2005, 2:46 PM   #8
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
I've never heard it called Tic-Tac-Toe in England. It's Naughts and Crosses.
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Sep 11th, 2005, 2:47 PM   #9
Eryk
Programmer
 
Join Date: Jul 2005
Posts: 62
Rep Power: 4 Eryk is on a distinguished road
He's talking about the England form of english.

Looks pretty cool.
Eryk is offline   Reply With Quote
Old Sep 11th, 2005, 2:56 PM   #10
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Thanks. =)
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC