#!/usr/bin/python
# -*- coding: latin-1 -*-
# Copyright 2008 Hedvig Kamp
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
import GameManager
import ConsoleViewer
import Team
import Game
import Player
import sys
class ConsoleController:
def __init__(self, game_mgr, console_viewer):
print "Console Controller started"
self.gm = game_mgr
self.cv = console_viewer
self.game = Game
self.team1 = Team
self.team2 = Team
self.teams = [self.team1, self.team2]
self.run()
def init_game_data(self):
return
def read_game_data(self):
return
def print_info(self, info):
sys.stdout.write(str(info))
def run(self):
while True:
sys.stdout.write(">> ")
sys.stdout.flush()
line = sys.stdin.readline().strip()
if line == "exit":
sys.exit()
elif line == "help" or line == "h":
self.print_info("To start a new game, use command 'newgame'")
elif line == "":
sys.stdout.write("\n>> ")
sys.stdout.flush()
elif line == "newgame" or line == "ng":
x = 0
self.print_info("\nNumber of ends:")
ends = sys.stdin.readline().strip()
while x < 2:
sys.stdout.write( "\nTeam")
sys.stdout.write( str(x+1) )
sys.stdout.write("\nTeam name: ")
name = sys.stdin.readline().strip()
sys.stdout.write("\nNumber of players in team: ")
no_players = sys.stdin.readline().strip()
self.teams[x] = Team.Team(int(no_players), name)
self.print_info("\nAdd team players:")
players = 0
while int(players) < int(no_players):
self.print_info("\nPlayer ")
self.print_info( players+1 )
self.print_info("\nName: ")
name = sys.stdin.readline().strip()
self.print_info("\nPosition: ")
pos = sys.stdin.readline().strip()
self.print_info("\nNumber of stones per end: ")
no_stones = sys.stdin.readline().strip()
self.print_info("\nLeft or right? ")
hand = sys.stdin.readline().strip()
player = Player.Player(name, no_stones, hand )
players = players + 1
self.teams[x].add_player(int(pos), player)
x = x+1
game = Game.Game(self.team1, self.team2, ends)
elif line == "showteams" or line == "st":
self.print_info("\nTeam 1 or team 2? ")
t = sys.stdin.readline().strip()
if t == "1" or t == "2":
print "Team is: ", self.teams[int(t)].get_name()
else:
self.print_info("Please answer '1' or '2'")
elif line == "startgame":
#self.cv.print_info("Game starting")
print "Game starting"
curr_end = 1
curr_player = 1
curr_team = 1
curr_stone = 1
while curr_end < game.get_no_ends():
cv.print_info(str("End ",curr_end," starting"))
while curr_player < self.teams[int(curr_team)].get_no_players():
cv.print_info(str("\nPlayer ",curr_player," in team ", teams[curr_team].get_name(), "is playing stone number ", curr_stone))
cv.print_info("Score: ")
score = sys.stdin.readline().strip()
old_score = self.teams[int(curr_team)].get_player(curr_player).get_score()
stones_played = int(self.teams[curr_team].get_player(curr_player).get_stones_played()) + 1
self.teams[curr_team].get_player(curr_player).set_stones_played(int(stones_played))
new_score = self.gm.calc_score( old_score, score )
self.teams[int(curr_team)].get_player(curr_player).update_total_score(new_score)
curr_stone = curr_stone + 1
if curr_team == 1:
curr_team = 2
else:
curr_team = 1
curr_player = curr_player + 1
curr_end = curr_end + 1
else:
self.print_info("Use command 'help' for help")