TicTacToe in Python
Finally, today I have done TicTacToe game in Python. From last few years I really wanted to do TicTiacToe game. Now here is code of game.
#----------------------------------------------
# TicTacToe game in Python
# author: [Sanosh Wadghule, santosh.wadghule@gmail.com]
# copyright: (c) 2010-2011 Santosh Wadghule
#----------------------------------------------
import sys
import random
board = [' ',' ',' ',' ',' ',' ',' ',' ',' ']
print "--------------------------"
print "Powerful TicTacToe Game"
print "--------------------------"
print
def displayPos():
print "Postion of board: "
print "0 | 1 | 2"
print "3 | 4 | 5"
print "6 | 7 | 8"
player = random.randrange(1, 3) #choose random player
def displayBoard(board):
print "========="
print board[0] + " | " + board[1] + " | " + board[2]
print board[3] + " | " + board[4] + " | " + board[5]
print board[6] + " | " + board[7] + " | " + board[8]
print "========="
def checkwin(a,b,c,player,board):
if board[a] == 'X' and board[b] == 'X' and board[c] == 'X':
print #newline
print "Player - " + str(player) + " is *** WINNER ***!!!"
displayBoard(board)
sys.exit()
elif board[a] == 'O' and board[b] == 'O' and board[c] == 'O':
print #newline
print "Machine is *** WINNER ***!!!"
displayBoard(board)
sys.exit()
def winner(board,player):
checkwin(0,1,2,player,board)
checkwin(3,4,5,player,board)
checkwin(6,7,8,player,board)
checkwin(0,3,6,player,board)
checkwin(1,4,7,player,board)
checkwin(2,5,8,player,board)
checkwin(0,4,8,player,board)
checkwin(2,4,6,player,board)
def play(board,x,player):
if board[x] != ' ':
print "--You have chosen wrong position--"
sys.exit()
else:
if player == 1:
board[x] = 'X'
winner(board,player)
else:
board[x] = 'O'
winner(board,player)
def checkPriority(a,b,c,board):
if (board[a] == 'X' and board[b] == 'X') or (board[a] == 'O' and board[b] == 'O'):
if board[c] == ' ':
return c
elif (board[a] == 'X' and board[c] == 'X') or (board[a] == 'O' and board[c] == 'O'):
if board[b] == ' ':
return b
elif (board[b] == 'X' and board[c] == 'X') or (board[b] == 'O' and board[c] == 'O'):
if board[a] == ' ':
return a
def Priority(board):
y = checkPriority(0,1,2,board)
if y == None:
y = checkPriority(3,4,5,board)
if y == None:
y = checkPriority(6,7,8,board)
if y == None:
y = checkPriority(0,3,6,board)
if y == None:
y = checkPriority(1,4,7,board)
if y == None:
y = checkPriority(2,5,8,board)
if y == None:
y = checkPriority(0,4,8,board)
if y == None:
y = checkPriority(2,4,6,board)
if y != None:
return y
else:
while True:
x = random.randrange(0, 9)
if board[x] != 'X' and board[x] != 'O':
break
else:
continue
return x
for i in range(0,9):
if player == 1:
print "Player " + str(player) + " >> Its your turn"
displayPos()
x = input("--Enter the position number: ")
play(board,x,player)
displayBoard(board)
print #newline
player = 2
else:
print "Machine >> Its your turn"
displayPos()
if i == 0:
x = random.randrange(0, 9)
if board[x] != 'X' and board[x] != 'O':
play(board,x,player)
else:
x = Priority(board)
play(board,x,player)
displayBoard(board)
print #newline
player = 1
print "--Match Draw--"
The first day in the company
Today was the first day in my new company for internship in KQInfotech. Day was gone nice with full of fun. Next time I will provide more info about me and the company.
Till then enjoy
A R Rahman Live Concert in Pune.
Unbelievable,
A R Rahman – The God of Music.
My Dream came true yesterday evening. I saw our God of Music “A R RAHMAN” at Balewadi Stadium Pune, I can’t imagine that ” was it really you?”. Me, my brother Anil and my friend Amit were there at 2.30 pm in a Q till 4.15 pm. After checking we ran to take our seats that were on first come first serve basis and we got perfect seats at 4.30 pm and waiting till 7pm for our God’s entry.
At 7.15 our Boss’ concert started with “Mausam and Escape” music and my temparature was going onnnnnnnnnnnn. For that moment I forgot all my existing world and gone into the wolrd of our God, What a Music man !!!, I don’t have any word to express the A R Rahman’s music but one thing I have to say that our God has power to dance dead bodies also.
After that, entry of our God was mind blowing. He was starting the song with “Jaage Hain” from Guru movie. What a voice man! His voice has all world music definitions to express music, no need music for A R Rahman voice, his voice is the music . Everybody were chilling at that moment.
We all enjoyed the whole show, mostly college guys were shouting with A R Rahman music and giving the slogan like ” You are a GOD, GOD of Music”. An amazing thing to see in the concert for Rahman who rules the hearts of many.
Here are list of songs played at concert by Rahman,
- Mausam & Escape (Slumdog Millionaire)
- Jaage Hain (Guru)
- Machan Machan (Tamil)
- Delhi-6 (Delhi -6)
- Roja Janeman (Roja)
- Masakalli (Delhi-6)
- Guzarish (Ghajini)
- Dil Se (Dil Se)
- Jiya Se Jiya
- Ringa Ringa ( Slumdog Millionaire)
- Genda Phool (Delhi-6)
- Tu Hi Re ( Bombay)
- Khamosh Raat ( Thakshak)
- Kahin Toh ( JTYJN)
- Sarfaroshi Ki Tamanna ( The legend of Bhagat Singh)
- Bharat Hamko Jaan Se Pyara (Roja)
- Kabhi Kabhi Aditi ( JTYJN)
- Chupke Se (Saathiys)
- Rehna Tu ( Delhi-6)
- Mayya Mayya (Guru)
- Kaise Mujhe (Ghajani)
- Kwaja Mere Khwaja (Jodha Akbar)
- Arziyaan ( Delhi-6)
- Mix of Azeem-O-Shaan Shahenshah and Tamil Song ( Awesome)
- DJ played all mixing of almost most famous rahman’s songs
- Hamma Hamma (Bombay)
- Jai Ho ( Slumdog Millionaire)
- Vande Mataram
All songs were outstanding. Importance of concert also raised by using the backdrop 150 ft * 80 ft LCD screen on stage. Sound System was great never heard like this.
Still feeling it…
Jai Ho
Santosh Wadghule.
IP address location in Ruby on Rails…
Updated Code – July 24 2010
Here, I have updated the code that converts the IP address to the location. Previously code was based on the iplocationtools.com. Now code is based on the ipinfodb.com. Code parses the XML file of IP Address Location and returns the string that contains location of IP Address. Code is below…
require 'net/http'
require 'rexml/document'
include REXML
class MapsController < ApplicationController
def index
@location = locateIp()
end
def locateIp
#ip = "123.123.123.123";
ip = request.remote_ip
ips = ip.to_s
url = "http://ipinfodb.com/ip_query.php?ip="+ips+"&timezone=false"
xml_data = Net::HTTP.get_response(URI.parse(url)).body
xmldoc = REXML::Document.new(xml_data)
# Now get the root element
root = xmldoc.root
city = ""
regionName = ""
countryName = ""
# This will take country name...
xmldoc.elements.each("Response/CountryName") {
|e| countryName << e.text
}
# Now get city name...
xmldoc.elements.each("Response/City") {
|e| city << e.text
}
# This will take regionName...
xmldoc.elements.each("Response/RegionName") {
|e| regionName << e.text
}
ipLocation = city +", "+regionName+", "+countryName
return ipLocation
end #end of method locateIp
end
Learnig Ruby…
I had decided that I will learn ruby language in this new year, but can’t wait. So I started to learn ruby. For that first I want basic knowledge about the ruby language, so I have downloaded pdf of ” The little book of Ruby” . It is very simple to learn and understand. It covered the all basic concept. My next project will be in ruby language.
PHPCamp Pune for pretty hot programmer.
Last saturday, there was PHPCamp in pune for full day. I also was a part of that camp. More than 600 guys were came there. My brother Anil, his friend Rahul and me gone there but we were late. So we not only missed some important topics but also missed free T-shirt …bad. Topics were on OpenSocial, Drupal, Joomla Framework and so on. After lunch, I liked the topic on Orkut application which had given by Pravin Nirmal. My college friends Chirag, Kunal, Hardik and me were enjoying the PHPCamp with tea in rainy day. After Directi’s seminar they had given T-shirt. Thanks to Directi. All day was fresh and powerfull.
ubuntu & macintosh
As I explained in my previous post that I have installed ubuntu on my PC. When I operated ubuntu for 2 hours, I think ubuntu is quite similar to Macintosh as prototypes,structures,effects and some functions.Thats interresting, because I like macintosh. In college I use redhat9 linux (old version) for practical, but ubuntu is very powerfull than redhat and high graphic resountion as I think.




