Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/data.py
blob: f799fd418f44c241c454f08aa6cb41f958f93304 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
    Copyright (C) 2009 Mike Major <ossfm@yahoo.com>
    
    This file is part of Hop-A-Round.

    Hop-A-Round 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.

    HopAround 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 random
import math

class LevelData():
  def __init__(self):
    self.question_max = 3  # number of questions in each section (slider, multiple choice & entry)
    self.thresh_slider = 0  # if question count matches this number, change to this section
    self.min_level = 1  # minimum levels in the activity; also the exponent of the current level
    self.max_level = 5  # maximum levels in the activity; also the exponent of the current level
    self.current_level = 1  # starting and current level indicator
    self.thresh_down = 0  # level score at/below which the level will be reduced
    self.level_change = 0  # flag indicating a level change
    self.digits = []  # collection of digits to create the question from
    self.set_question_count(self.question_max)
    random.seed()

  def set_question_count(self, max):
    self.question_max = max
    self.thresh_mult = self.question_max  # if question count matches this number, change to this section
    self.thresh_entry = self.question_max * 2 # if question count matches this number, change to this section
    self.thresh_up = self.question_max * 3  # level score at/above which the level will be increased
    self.question_count = 0  # number of questions asked on current level
    self.level_score = 0  # number of questions correct on current level

  def check_answer(self, response):
    if response == self.correct_answer:
      self.level_score += 1
    else:
      self.level_score -= 1
    if self.question_count == self.question_max * 3:
      if self.level_score >= self.thresh_up and self.current_level < self.max_level:
        self.level_change = 1
      elif self.level_score <= self.thresh_down and self.current_level > self.min_level:
        self.level_change = -1
      self.question_count = self.level_score = 0
    return self.correct_answer == response

  def increase_level(self):
    if self.current_level < self.max_level:
      self.current_level += 1
    self.answer_decade = int(10**self.current_level)
    self.level_change = 0

  def decrease_level(self):
    if self.current_level > self.min_level:
      self.current_level -= 1
    self.answer_decade = int(10**self.current_level)
    self.level_change = 0

  def gen_random(self):
    if self.level_change == 1:
      self.increase_level()
    elif self.level_change == -1:
      self.decrease_level()
    self.question_count += 1
    # generate a random number. don't use digits 0 or 9; 
    # they cause duplicates in the mult choice answer set.
    self.digits = range(1,9)
    random.shuffle(self.digits)
    str_num = ""
    for x in range(0, self.current_level + 2):
      str_num += str(random.choice(self.digits))
    self.random_number = int(str_num)
    self.answer_decade = int(math.pow(10, self.current_level))
    self.correct_answer = int(round(self.random_number/(self.answer_decade*1.0), 0) * self.answer_decade)
    # create the multiple choice possibilities
    self.mult=[]
    self.mult.append(int(math.floor(self.random_number/(self.answer_decade*1.0)) * self.answer_decade))
    self.mult.append(int(math.ceil(self.random_number/(self.answer_decade*1.0)) * self.answer_decade))
    if self.current_level == self.min_level:
      self.mult.append(int(math.floor(self.random_number/(self.answer_decade*10.0)) * self.answer_decade * 10.0))
      self.mult.append(int(math.ceil(self.random_number/(self.answer_decade*10.0)) * self.answer_decade * 10.0))
    elif self.current_level == self.max_level:
      self.mult.append(int(math.floor(self.random_number/(self.answer_decade*0.1)) * self.answer_decade * 0.1))
      self.mult.append(int(math.ceil(self.random_number/(self.answer_decade*0.1)) * self.answer_decade * 0.1))
    else:
      factor = random.choice([0.1,10.0])
      self.mult.append(int(math.floor(self.random_number/(self.answer_decade*factor)) * self.answer_decade * factor))
      self.mult.append(int(math.ceil(self.random_number/(self.answer_decade*factor)) * self.answer_decade * factor))

  def get_game_data(self):
    temp = "Level: " + str(self.current_level)
    #temp += "\nRandom Number: " + str(self.random_number)
    #temp += "\nDecade: " + str(self.answer_decade)
    #temp += "\nCorrect Answer: " + str(self.correct_answer)
    temp += "\nQuestion max: " + str(self.question_max)
    temp += "\nScore: " + str(self.level_score)
    temp += "\nCount: " + str(self.question_count)
    return temp