Ejemplo n.º 1
0
bool validBoggle(string s, Lexicon &lex) { return validLength(s) && validWord(s,lex); } 
Ejemplo n.º 2
0
void MainWindow::addToList() {
    QTextStream out(stdout);
    if(gameRunning) {
        if(ui->newWord->text().length() > 0) {
            //            *wordList << ui->newWord->text().toUpper();
            if(ui->newWord->text().length() > 2) {
                if(Hunspell_spell(spellChecker,ui->newWord->text().toAscii())) {
                    //                out << "correct spelling\n";
                    out.flush();

                    if(validWord()) {
                        bool isNewWord = true;
                        for(int i=0;i<letterItems[ui->newWord->text().length()]->childCount();i++) {
                            if(letterItems[ui->newWord->text().length()]->child(i)->text(0) == ui->newWord->text().toLower()) {
                                isNewWord = false;
                            }
                        }
                        if(isNewWord) {
                            QTreeWidgetItem *newItem = new QTreeWidgetItem(letterItems[ui->newWord->text().length()]);
                            newItem->setText(0,ui->newWord->text().toLower());
                            ui->wordTree->scrollToItem(newItem);
                            ui->wordTree->clearSelection();
                            newItem->setSelected(true);
                        }
                    }
                    else {
                        QTreeWidgetItem *newItem = new QTreeWidgetItem(letterItems[1]);
                        newItem->setText(0,ui->newWord->text().toLower());
                        ui->wordTree->scrollToItem(newItem);
                        ui->wordTree->clearSelection();
                        newItem->setSelected(true);
                    }
                }
                else {
                    //                out << "incorrect spelling\n";
                    out.flush();
                    QTreeWidgetItem *newItem = new QTreeWidgetItem(letterItems[0]);
                    newItem->setText(0,ui->newWord->text().toLower());
                    ui->wordTree->scrollToItem(newItem);
                    ui->wordTree->clearSelection();
                    newItem->setSelected(true);
                }
            }
            ui->wordTree->expandAll();
            //            ui->wordTable->setRowCount(10);
            //            ui->wordTable->setColumnCount(1);
            //            QTableWidgetItem *newItem = new QTableWidgetItem(ui->newWord->text());
            //            ui->wordTable->setItem(1,1,newItem);
            //            wordListModel->setStringList(*wordList);
            //            ui->wordList->scrollToBottom();
            //ui->wordList->itemDelegateForRow(0)->setProperty("color",QColor(255,0,0));
        }
    }
    int score = 0;
    int words = 0;
    int lettersScore[18] = {-1,-1,0,1,1,2,3,5,8,8,8,8,8,8,8,8,8,8};
    for(int i = 0; i<18; i++) {
        if(i != 2) {
            score += letterItems[i]->childCount()*lettersScore[i];
        }
        if(i > 2) {
            words += letterItems[i]->childCount();
        }
    }
    ui->score->display(score);
    ui->numWords->display(words);
    
    ui->newWord->setText("");
    for(int i = 0;i<16;i++) {
        button[i]->setStyleSheet("* { color: black;} ");
    }
    ui->newWord->setFocus();
}
Ejemplo n.º 3
0
command_stream_t
make_command_stream (int (*get_next_byte) (void *),
		     void *get_next_byte_argument)
{
  if ( DEBUG == 1 )
    printf("make_command_stream: Starting!\n");
  //create return command_stream
  struct command_stream *c_stream = (struct command_stream *) checked_malloc(sizeof(struct command_stream));
  //read line from script
  char *c_line = (char *) checked_malloc(sizeof(char *) * SIZE_BUFFER);
  c_line = pullBytes(*get_next_byte, get_next_byte_argument);

  //create token list and initialize to NULL
  struct token_list *t_list = (struct token_list *) checked_malloc(sizeof(struct token_list));
  t_list->start = NULL;
  t_list->end = NULL;

  int c_line_pos = 0;
  int start_pos = 0;
  int num_lines = 1;
  int num_parens = 0;
  bool new_word = false;
  bool line_empty = true;
  bool line_ignore = false;
  bool line_NULL = true;
  bool special_token_finished = false;


  while( c_line != NULL )
  {
    if ( DEBUG == 1 )
      printf("make_command_stream: current line:%d contents: %s\n", num_lines, c_line);
    line_NULL = false;
    while ( true )
    {
      //check if line is a comment or null byte and go to next lin
      if ( c_line[0] == '#' || c_line[0] == '\0')
      {
        num_lines++;
        line_ignore = true;
        break;
      }
      //check for special tokens
      if ( specialToken(c_line[c_line_pos]) )
      {
        if ( DEBUG == 1 )
          printf("make_command_stream: is special token\n");
        if ( c_line_pos == 0 && c_line[c_line_pos] != '(' )
          printError(num_lines);
        if ( new_word )
        {
          char *word_string = getString(c_line, start_pos, c_line_pos-1);
          struct token *new_token = (struct token *) checked_malloc(sizeof(struct  token));
          new_token->word = word_string;
          new_token->next = NULL;
          addToken(new_token, t_list);
          if ( DEBUG == 1 )
          {
            printf("make_command_stream: new token added\n");
          }
        }
        start_pos = c_line_pos;
        //make special token to add to list
        struct token *token_special = (struct token *) checked_malloc(sizeof(struct token));
        token_special->next = NULL;
        // OR "||" operator and PIPE '|' operator
        if ( c_line[c_line_pos] == '|' ) //PIPE token
        {
          if ( c_line[c_line_pos + 1] == '|' ) // OR token
          {
            token_special->word = getString(c_line, start_pos, c_line_pos + 1);
            if ( DEBUG == 1 )
              printf("make_command_stream: added token is OR operator\n");
            c_line_pos += 2;
          }
          else
          {
            if ( DEBUG == 1 )
              printf("make_command_stream: token is PIPE operator\n");
            token_special->word = getString(c_line, start_pos, c_line_pos);
            c_line_pos++;
          }
          special_token_finished = true;
        }
        else if ( c_line[c_line_pos] == '&' )
        {
          if ( c_line[c_line_pos + 1] == '&' )
          {
            token_special->word = getString(c_line, start_pos, c_line_pos + 1);
            c_line_pos += 2;
            special_token_finished = true;
            if ( DEBUG == 1 )
              printf("make_command_stream: token is AND operator\n");
          } 
          else
            printError(num_lines);
        }
        else if ( c_line[c_line_pos] == '(' )
        {
          token_special->word = getString(c_line, start_pos, c_line_pos);
          num_parens++;
          if ( DEBUG == 1 )
            printf("make_command_stream: added token is LEFT_PAREN operator\n");
          c_line_pos++;
          special_token_finished = true;
        }
        else if ( c_line[c_line_pos] == ')' )
        {
          num_parens--;
          if ( num_parens == 0 )
            special_token_finished = false;
          else
            special_token_finished = true;
          if ( num_parens < 0 )
            printError(num_lines);
          token_special->word = getString(c_line, start_pos, c_line_pos);
          if ( DEBUG == 1 )
            printf("make_command_stream: added token is a RIGHT_PAREN operator\n");
          c_line_pos++;
        }
        else if ( c_line[c_line_pos] == ';' )
        {
          if ( DEBUG == 1 )
            printf("make_command_stream: added token is a SEMI operator\n");
          token_special->word = getString(c_line, start_pos, c_line_pos);
          special_token_finished = false;
          c_line_pos++;
        }
        else 
        {
          token_special->word = getString(c_line, start_pos, c_line_pos);
          c_line_pos++;
          if ( DEBUG == 1 )
            printf("make_command_stream: added token is a %s operator", token_special->word);
        }
        start_pos = c_line_pos;
        addToken(token_special, t_list);
        new_word = false;
        line_empty = false;
      }
      else //not a special token
      {
        if ( validWord(c_line[c_line_pos]) || c_line[c_line_pos] == ' ' )
        {
          if ( c_line[c_line_pos] != ' ' )
            if ( new_word == false )
            {
              new_word = true;
              line_empty = false;
            }
        }
        else
        {
          printError(num_lines);
        }
        c_line_pos++;
      }
      if ( c_line[c_line_pos] == '#' || c_line[c_line_pos] == '\0' )
      {
        if ( new_word )
        {
          char *word_string = getString(c_line, start_pos, c_line_pos - 1);
          struct token *token_simple = (struct token *) checked_malloc(sizeof(struct token));
          token_simple->word = word_string;
          token_simple->next = NULL;
          addToken(token_simple, t_list);
          special_token_finished = false;
          line_empty = false;
        }
        if ( (c_line[c_line_pos] == '\0' && special_token_finished) || (num_parens > 0) || line_empty) 
        {
          if ( DEBUG == 1 )
            printf("make_command_stream: Last token reached\n");
          line_ignore = true;
          if ( num_parens > 0 ) //newline in between sub-shell commands
          {
            struct token *semi_token = (struct token *) checked_malloc(sizeof(struct token));
            semi_token->next = NULL;
            char *s_colon = (char *) checked_malloc(sizeof(char *) * 2);
            s_colon[0] = ';';
            s_colon[1] = '\0';
            semi_token->word = s_colon;
            addToken(semi_token, t_list);
            if ( DEBUG == 1 )
              printf("make_command_stream: semi colon token created from newline\n");
          }
        }
        break;
      }
    }
    if ( DEBUG == 1 )
    {
      printf("make_command_stream: Tokens created and put into t_list\n");
      int i = 0;
      struct token *t_list_h = t_list->start;
      struct token *t_list_t = t_list->end;
      while ( t_list->start != NULL )
      {
        printf("make_command_stream: Word %d:%s\n", i, t_list->start->word);
        t_list->start = t_list->start->next;
        i++;
      }
      t_list->start = t_list_h;
      t_list->end = t_list_t;
      printf("\n\n");
    }
    // c_line is not empty/ignored
    if ( line_ignore == false )
    {
      struct node_list *n_list = tokensToNodes(t_list);
      if ( n_list == NULL )
        printError(num_lines);
      // create stack tree from our node list and add command
      struct command *command_root = create_stack_tree(n_list);
      if ( command_root == NULL )
      {
        if ( DEBUG == 1 )
          printf("make_command_stream: command_root is NULL\n");
        printError(num_lines);
      }
      addCommand(c_stream, command_root);
    }
    free(c_line);
    c_line = pullBytes(*get_next_byte, get_next_byte_argument);
    num_lines++;
    c_line_pos = 0;
    start_pos = 0;
    new_word = false;
    line_ignore = false;
    line_empty = true;
    line_NULL = true;
    if ( DEBUG == 1 )
    printf("make_command_stream: Iterations completed\n");
  }
  if ( num_parens > 0 && line_NULL )
  {
    printError(num_lines);
  }
  if ( special_token_finished && line_NULL )
  {
    printError(num_lines);
  }
  if ( DEBUG == 1 )
    printf("make_command_stream: Reached end of function\n");
  return c_stream;
}