def play(): game = Game() print(game.current_board()) while True: prompt = "Player %s>> " % game.current_player() index = list(map(int,list(input(prompt)))) try: game.move(index) except RuntimeError: print("That position has already been used") continue except IndexError: print("Invalid position specified") continue print(game.current_board()) if game.finished(): print("Player %s wins!" % game.current_player()) break game.next()
class TTTGame(Widget): def __init__(self,**kwargs): super(TTTGame,self).__init__(**kwargs) self.size_hint = (1,1) self.internal_game = Game() def getCenter(self): c_x = int(self.center_x) c_y = int(self.center_y) return (c_x,c_y) def oneThird_region(self): onethird_x = int(self.center_x/3) onethird_y = int(self.center_y/3) return (onethird_x,onethird_y) def getRegions_x(self): (cx,cy) = self.getCenter() (onethird_x,onethird_y) = self.oneThird_region() region0 = [0,cx - onethird_x] region1 = [cx - onethird_x,cx + onethird_x] region2 = [cx + onethird_x,self.width] return [region0,region1,region2] def getRegions_y(self): (cx,cy) = self.getCenter() (onethird_x,onethird_y) = self.oneThird_region() region0 = [cy + onethird_y,self.height] region1 = [cy - onethird_y,cy + onethird_y] region2 = [0,cy - onethird_y] return [region0,region1,region2] def get_index(self,position,regions): index = 0 for region in regions: if position in range(*region): cord = index index += 1 return index def on_touch_down(self,touch): x_regions = self.getRegions_x() y_regions = self.getRegions_y() print(str(x_regions)) touchX,touchY = (int(touch.x),int(touch.y)) print("TouchX: "+str(touchX)) X_cord = get_index(touchX,x_regions) Y_cord = get_index(touchY,y_regions) cord = [X_cord,Y_cord] self.advance_game(cord) def draw_letter(self,x,y,letter): letter = Label(text=letter,font_size=70) x_region = self.getRegions_x()[x] y_region = self.getRegions_y()[y] letter.center_x = (x_region[0]+x_region[1])/2 letter.top = (y_region[0]+y_region[1])/2 self.add_widget(letter) def advance_game(self,cord): try: self.internal_game.move(cord) print(self.internal_game.current_board()) if self.internal_game.finished(): print("Player %s wins!!" % self.internal_game.current_player()) sys.exit(0) self.draw_letter(cord[0],cord[1],self.internal_game.current_player()) self.internal_game.next() except IndexError: print("Invalid board position") except RuntimeError: print("That position has already beed used")