Documentation
classes:Hashable
Making hashes from hashable objects
To make a tic-tac-toe game it was necessary to create every move, cell, board, game position as a hash
. In a sense, it would be very useful to be able to reproduce these basic things :
Cell
: A place where a player could place a mark.Move
: Player 1's or Player 2's moveBoard
: An array containing all the cells in the board.
Plus, you could also represent different kind of states
using hashable objects, such hypothetical states could be:
Game State
: This can be a set of bits representing what cells are taken (by Player 1 or Player 2) or free(unmarked!). They can be useful to AI components for avoiding useless computation from having to get the state of eachcell
.
A super basic abstract meta class
would be the way to go for all hashable objects to inherit from.
So, a Hashable
object provides 5 basic features :
- Construction method from a hash
- Another construction method to create a
Hashable
object but this time from a binary(0's and/or 1's) representation. - Two properties:
hash
: an integer representing the hashbinary
: a representation of the propertyhash
as 0's and 1's.
- 2 important magic methods :
eq: Essential method for comparing the instance to any other Hashable instances.This will result to
True
if bothHashable
instances have the samehash
property hash : Using set() makes this a killing feature. You can easily do intersection and union of sets of hashes
The two properties(hash
and binary
) are instance methods turned into properties through the python @property
decorator.In addition,each new property has a complimentary @setter method that protects hash
or the binary
property from changing values when they are assigned a new value.
Any class subclassing from
Hashable
will implement the above outlined interface in addition to any extra features the class might also provide.
Here is an example of a Hashable
instance
#Represent a move on cell 5 by player 1.
Move(cell=5,player=1)
CLASS:Hashable
bar help window. default:True
dfdfd help window. defaults auto: true
###Attributes:
bar
- Is something people use sometimes.
bar
- Is something
people use sometimes.
bar
- Is something people use sometimes.
Changing Values
auto: true
heyddfd : dfd
Methods:
Function | Type | Description |
---|---|---|
hash() | Abstract Property | This should return an int as a hash representation for the hashable object.. |
hash() | N | help window. defaults auto: true |
Name | Description |
---|---|
bar | help window. default:True |
dfdfd | help window. defaults auto: true |
Methods:
Here's a definition list:
bar help window. default:True
dfdfd help window. defaults auto: true
bar
- Is something people use sometimes.
bar
- Is something
people use sometimes.
bar
- Is something people use sometimes.
Changing Values
auto: true
heyddfd : dfd
class :
class Hashable(object):
__metaclass__ = abc.ABCMeta
@property
@abc.abstractmethod
def hash(self):
"""
This should return an int as a hash representation for the
hashable object.
"""
@hash.setter
@abc.abstractmethod
def hash(self,value):
""" This method should really be left with a pass and nothing else.
Changing the hash value of a hashable object after it has been
created will create undefined behaviour when other objects
operate on them.
"""
@property
@abc.abstractmethod
def binary(self):
"""
This should return an string consisting of 0's and 1's representing
the binary value of the hashable object.
Example : bin(hash)[2:].zfill(bits_size_of_hash)
"""
@binary.setter
@abc.abstractmethod
def binary(self,value):
""" This method should really be left with a pass and nothing else.
Changing the binary value of a hashable object after it has been
created will create undefined behaviour when other objects
operate on them.
"""
@classmethod
@abc.abstractmethod
def from_hash(cls,hash):
"""
All hashable classes inheriting this class MUST implement from_hash method,
which becomes the DEFAULT method for instantiating new Hashable instances
or subclasses
"""
b