Spring 2012 CSCI 220 Week 14

Monday
Player class class Player: def __init__(self,n,p): # Constructor self.name = n       self.password = p        self.tiles = [] self.num_wins = 0 self.total_games = 0

def getName(self): return self.name

def setName(self,n): self.name = n   def getPassword(self): return self.password

def setPassword(self,p): if len(p) >= 4: self.password = p

def __str__(self): return self.name

def main: player = Player("Paul","Anderson") player.setPassword("blah") print(player.getPassword) #print(p) #p.setPassword("a") #p.password = "a" #print(p.getPassword)

main

Database class

from Player import *

class Database: def __init__(self,fn): self.filename = fn

def read(self): infile = open(self.filename,"r") lines = infile.readlines self.players = [] #self.users = [] #self.passwords = [] #self.wins = [] #self.total_games = [] for i in range(len(lines)): # Loop through lines if i == 0: continue lines[i] = lines[i].strip current_line = lines[i].split(",") if len(current_line) == 4: new_player = Player(current_line[0],current_line[1]) self.players.append(new_player) #self.users.append #self.passwords.append #self.wins.append(int(current_line[2])) #self.total_games.append(int(current_line[3])) infile.close

#return users,passwords,wins,total_games


 * 1)    def write_db(filename,users,passwords,wins,total_games):
 * 2)        outfile = open(filename,'w')
 * 3)        print('user,password,wins,total games',file=outfile,end="")
 * 4)        for i in range(len(users)):
 * 5)            print("\n"+users[i],passwords[i],wins[i],total_games[i],end="",sep=',',file=outfile)
 * 6)        outfile.close

def main: db = Database("db.txt") db.read print(db.players[0])

main

Board Class from graphics import *

class Board: def __init__(self,w,h): self.win = GraphWin('Scrabble',w,h) self.win.setCoords(-30,-30,180,180) self.markers = [] tws_j = [0,0,0,7,7,14,14,14] tws_i = [0,7,14,0,14,0,7,14] dls_j = [0,0,2,2,3,3,3,6,6,6,6,7,7,8,8,8,8,11,11,11,12,12,14,14] dls_i = [3,11,6,8,0,7,14,2,6,8,12,3,11,2,6,8,12,0,7,14,6,8,3,11] dws_j = [1,1,2,2,3,3,4,4,10,10,11,11,12,12,13,13] dws_i = [1,13,2,12,3,11,4,10,4,10,3,11,2,12,1,13] tls_j = [1,1,5,5,5,5,9,9,9,9,13,13] tls_i = [5,9,1,5,9,13,1,5,9,13,5,9] for i in range(15): for j in range(15): if self.find_ij(i,j,tws_i,tws_j): self.markers.append(TripleWordScoreMarker(i,j,self.win)) elif i == 7 and j == 7: self.markers.append(StartMarker(i,j,self.win)) elif self.find_ij(i,j,dls_i,dls_j): self.markers.append(DoubleLetterScoreMarker(i,j,self.win)) elif self.find_ij(i,j,tls_i,tls_j): self.markers.append(TripleLetterScoreMarker(i,j,self.win)) elif self.find_ij(i,j,dws_i,dws_j): self.markers.append(DoubleWordScoreMarker(i,j,self.win)) else: self.markers.append(BlankMarker(i,j,self.win))

# Linear search def find_ij(self,i,j,list_i,list_j): for z in range(len(list_i)): if i == list_i[z] and j == list_j[z]: return True return False

def get_marker_clicked(self,p): for marker in self.markers: if marker.clicked(p): return marker return None

def score(self,markers,tiles): sc = 0 mult = 1 for i in range(len(markers)): if isinstance(markers[i],DoubleWordScoreMarker): mult = mult*2 elif isinstance(markers[i],TripleWordScoreMarker): mult = mult*3 if isinstance(markers[i],DoubleLetterScoreMarker): sc = sc + tiles[i]*2 elif isinstance(markers[i],TripleLetterScoreMarker): sc = sc + tiles[i]*3 else: sc = sc + tiles[i] sc = mult*sc return sc

def __del__(self): self.win.close

class Marker: def clicked(self,p): p1 = self.rect.getP1 p2 = self.rect.getP2 big_x = max([p1.getX,p2.getX]) small_x = min([p1.getX,p2.getX]) big_y = max([p1.getY,p2.getY]) small_y = min([p1.getY,p2.getY]) x = p.getX y = p.getY if y <= big_y and y >= small_y and x <= big_x and x >= small_x: return True return False

class BlankMarker(Marker): def __init__(self,i,j,win): self.rect = Rectangle(Point(10*i,10*j),Point(10*i+10,10*j+10)) self.rect.draw(win)

class StartMarker(Marker): def __init__(self,i,j,win): self.rect = Rectangle(Point(10*i,10*j),Point(10*i+10,10*j+10)) self.rect.draw(win) self.rect.setFill('purple') Text(Point((10*i+10*i+10)/2,(10*j+10*j+10)/2),"@").draw(win)

class TripleWordScoreMarker(Marker): def __init__(self,i,j,win): self.rect = Rectangle(Point(10*i,10*j),Point(10*i+10,10*j+10)) self.rect.draw(win) self.rect.setFill('red') Text(Point((10*i+10*i+10)/2,(10*j+10*j+10)/2),"x3").draw(win) class DoubleWordScoreMarker(Marker): def __init__(self,i,j,win): self.rect = Rectangle(Point(10*i,10*j),Point(10*i+10,10*j+10)) self.rect.draw(win) self.rect.setFill('purple') Text(Point((10*i+10*i+10)/2,(10*j+10*j+10)/2),"x2").draw(win)

class DoubleLetterScoreMarker(Marker): def __init__(self,i,j,win): self.rect = Rectangle(Point(10*i,10*j),Point(10*i+10,10*j+10)) self.rect.draw(win) self.rect.setFill('cyan') Text(Point((10*i+10*i+10)/2,(10*j+10*j+10)/2),"DL").draw(win)

class TripleLetterScoreMarker(Marker): def __init__(self,i,j,win): self.rect = Rectangle(Point(10*i,10*j),Point(10*i+10,10*j+10)) self.rect.draw(win) self.rect.setFill('blue') Text(Point((10*i+10*i+10)/2,(10*j+10*j+10)/2),"TL").draw(win)

def main: b = Board(500,500) # Get three markers word_markers = [] tiles = [1,2,1] while len(word_markers) < 3: p = b.win.getMouse marker = b.get_marker_clicked(p) if marker != None: word_markers.append(marker) print(b.score(word_markers,tiles))

if __name__=="__main__": main

Wednesday
So it is getting hard to distribute the code via the webpage, so here is a public link that we can all use: http://dl.dropbox.com/u/2920856/Scrabble.zip