def tictactoe(): #check for existing session if 'key' in session: # Clear the session session.clear() cache.clear() #simple caching for demo purposes only key = get_session_key() session['key'] = key board = Board() rules = Rules(board) player = ArtificialIntelligence(board) #initialize rules.next_play = 'O' state = { 'board': board, 'rules': rules, 'player': player, 'next': rules.get_current_player() } cache.set(key, state) return render_template('index.html')
def apply_rules( self, rule_file: Dict ) -> None: """ Apply rules to the dataset. Will ignore any field where manuallyMapped is set to YES. args: - rule_file: path to the rule file returns: N/A Note - See rules/rules.py for further information """ r = Rules(rule_file) for row in self._data: #add output headers for output_header in _REQ_OUTPUT_HEADERS: if output_header not in row.keys(): row[output_header] = "" self._std_header_map[output_header] = output_header #add manuallyMapped if 'manuallymapped'not in row.keys(): row['manuallymapped'] = '' self._std_header_map['manuallymapped'] = "manuallymapped" #skip manuallyMapped rows if row['manuallymapped'] == 'YES': continue #apply rules else: r.ApplyRules(row)
def apply_rules(self, rule_file: Dict) -> None: """ Apply rules to the dataset. Will ignore any field where manuallyMapped is set to YES. args: - rule_file: path to the rule file returns: N/A Note - See rules/rules.py for further information """ r = Rules(rule_file) for row in self._data: if row['manuallyMapped'] == 'YES': continue else: r.ApplyRules(row)
class RulesTest(unittest.TestCase): m_rules = Rules() def test_init(self): self.assertEqual(self.m_rules.PossibleMoves([]), self.m_rules.ALLMOVES) def test_mid(self): self.assertEqual(sorted(self.m_rules.PossibleMoves([1, 2, 3, 4])), [5, 6, 7, 8, 9]) def test_end(self): self.assertEqual(self.m_rules.PossibleMoves(self.m_rules.ALLMOVES), [])
def print_board(grid): print grid[0:3] print grid[3:6] print grid[6:9] if __name__ == "__main__": starter = raw_input("Pick player 1(1) or 2(2) (Player 1 starts...): ") while starter not in ['1', '2']: starter = raw_input("Invalid: Pick either 1 or 2: ") opponent = raw_input("Do you want to play X(X) or O(O): ") while opponent not in ['X', 'O']: opponent = raw_input("Invalid: Pick either X or O: ") board = Board() rules = Rules(board) player = ArtificialIntelligence(board) ai_mark = 'O' if opponent == 'X' else 'X' next_play = opponent if starter == '1' else ai_mark #initialize rules.next_play = next_play player.set_mark(ai_mark) #computer's mark (i.e., X) end_game = False while not end_game: print_board(board.grid) mark = rules.get_current_player() if mark == ai_mark: