[go: up one dir, main page]

Menu

[r14]: / maker.py  Maximize  Restore  History

Download this file

52 lines (43 with data), 1.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
#
# Mastermind 1.0
# Copyright (C) 2008
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# file: maker.py
# purpose: provide the behavior of a code maker
######################################################
import random
class Maker:
# parametrize by the number of colors and pegs
def __init__(self, colors=6, pins=4):
self.colors = colors
self.pins = pins
self.seq = ""
# use a fake brain to score the input
from brains import Brain
self.b = Brain(self.colors, self.pins, False)
# prepare a secret key
def generate(self, seq=""):
self.seq = ""
if len(seq) != self.pins:
for i in range(self.pins):
self.seq += "%d" % random.randint(0,self.colors-1)
else:
self.seq = seq
# use the brain to compute the score of an input
def reply(self, seq):
return self.b.diff( seq, self.seq )