The other day, I was looking for some GNU/Linux administration reference, I ended up viewing a "tic tac toe" program in Ruby at a Linux admin's blog.
He wrote that in ruby and took him 90mins. That was written using class.
But "Zen of Python in mind", I wrote my own version without class.
I prefer this way (sparse is better than dense), and I enjoy Python more when, I think of my Java days.
I haven't read ruby code before hand so, I did not wrote a clone of ruby code.
And this took me around 40min. Because, I realized some missing features only when I played it several times.
Then only I added those.
Specifically, drawing complete board after someone wins and stopping the game when all fields are filled out.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# All right reserved. Distributed Under BSD Lisence | |
__author__="Arun.K.R <the1.arun@gmail.com>" | |
__date__ ="$May 11, 2011 6:23:17 PM$" | |
import os, time | |
from random import choice | |
WIDTH = 3 | |
BOARD = [str(i+1) for i in range(WIDTH*WIDTH)] | |
USERS = {True:'X', False:'O'} | |
def check_for_win(player, position, marked): | |
row=(position-1)/WIDTH | |
#horizontal equality test | |
if len(filter(marked, BOARD[row*WIDTH: row*WIDTH+WIDTH])) == WIDTH: | |
game_over(player) | |
#vertical equality test | |
if len(filter(marked, [BOARD[itm] | |
for itm in range((position-1) % WIDTH, WIDTH*WIDTH, WIDTH)])) == WIDTH: | |
game_over(player) | |
#diagonal (forward slash) test | |
if len(filter(marked, [BOARD[itm] | |
for itm in range(0, WIDTH*WIDTH, WIDTH+1)])) == WIDTH: | |
game_over(player) | |
#diagonal (backward slash) test | |
if len(filter(marked, [BOARD[itm] | |
for itm in range(WIDTH-1, WIDTH*WIDTH, WIDTH-1)[:-1]])) == WIDTH: | |
game_over(player) | |
def check_for_tie(): | |
if len(filter((lambda x: x not in ['X', 'O']), BOARD)) == 0: | |
draw(ask=False) | |
print "\n", "Game is in tie!!!" | |
exit() | |
def ask_input(player): | |
try: | |
print "Player " + USERS[player] + "'s turn ->", | |
pos = int(raw_input("Enter a number from board: ")) | |
assert pos>0 and pos<=WIDTH*WIDTH | |
except Exception: | |
print "Enter a valied number in the board!" | |
time.sleep(1) | |
draw(player) | |
else: | |
play(pos, player) | |
def draw(player=choice([True, False]), ask=True): | |
os.system('clear') | |
for i in range(WIDTH): | |
print ' | '.join(BOARD[i*WIDTH:i*WIDTH+WIDTH]) | |
print '-'*(WIDTH * WIDTH) | |
if ask: | |
ask_input(player) | |
def play(position, player): | |
if BOARD[position-1] == str(position): | |
BOARD[position-1] = USERS[player] | |
else: | |
draw(player) | |
#check for win | |
check_for_win(player, position, marked=(lambda x: x==USERS[player])) | |
#check for draw | |
check_for_tie() | |
#switch player (simple LOGIC ANDing) | |
draw(not player) | |
def game_over(player): | |
draw(ask=False) | |
print "\n", "Player " + USERS[player] + " WON!!!" | |
exit() | |
if __name__ == "__main__": | |
draw() |
I'll not say, the program is beautiful and very readable.
However, it's good enough for playing with it.
Comments
Post a Comment