Example #1
0
int main(int argc, char** argv){
    if(validInput(argc, argv) != 1){
		return 0;
	}
	char* tree= genTree(atoi(argv[1]), argv[2][0], argv[3][0]);
	printf("%s", tree);
}
Example #2
0
/* Usage: deck hand_size number_of_hands */
int main(int argc, char *argv[]) {

    /* input */
    int nhands;                   /* stores number of hands from input */
    int handSize;                 /* stores hand size from input */
    int canDisplayHands;          /* flag to indicate if the hands can be displayed */

    int foundErrors = NO_ERRORS;  /* indicate whether errors are found */

    /* validate input */
    if (validInput(argc, argv, &handSize, &nhands)) {

        Card deck[DECK_SIZE];     /* the cards deck */
        Hand hands[nhands];       /* the hands */
        Hand sortedHands[nhands];

        canDisplayHands = (handSize && nhands) > 0 ? TRUE : FALSE;

        /* create a deck of cards. */
        printf("\n   Create and display the deck.\n\n");
        createDeck(deck);
        displayDeck(deck, DECK_SIZE);

        /* shuffle deck of cards */
        printf("\n   Shuffle and display the deck.\n\n");
        shuffleDeck(deck, DECK_SIZE);
        displayDeck(deck, DECK_SIZE);

        if (canDisplayHands) {
            /* create and display hands */
            printf("\n   Create and display hands.\n\n");
            createHands(deck, hands, handSize, nhands);
            sortHands(hands, sortedHands, handSize, nhands);

            displayHands(hands, sortedHands, handSize, nhands);

            /* rank hands */
            printf("\n   Rank and display ranked hands.\n\n");
            displayRankedHands(sortedHands, nhands);

            printf("\n   Display winners.\n\n");
            displayWinners(sortedHands, nhands);
        }
        else {
            printf("\nCould not display hands because either the hand size or the number of hands is zero.\n");
        }
    }
    else {
        /* invalid input */
        printf("Usage: deck hand_size number_of_hands\n");
        foundErrors = ERRORS;
    }
    return foundErrors;
}
 CEUnitCellParameters CEParameterEditor::validateEditor()
 {
   CEUnitCellParameters p;
   p.a = ui.spin_a->value();
   p.b = ui.spin_b->value();
   p.c = ui.spin_c->value();
   p.alpha = ui.spin_alpha->value();
   p.beta  = ui.spin_beta->value();
   p.gamma = ui.spin_gamma->value();
   emit validInput();
   return p;
 }
Example #4
0
  Eigen::Matrix3d CEMatrixEditor::validateEditor()
  {
    // Editing fractional matrix is not allowed. The widget is
    // disabled to ensure that this assertion passes.
    Q_ASSERT(m_ext->matrixCartFrac() != Fractional);

    // Clear selection, otherwise there is a crash on Qt 4.7.2.
    QTextCursor tc = ui.edit_matrix->textCursor();
    tc.clearSelection();
    ui.edit_matrix->setTextCursor(tc);

    QString text = ui.edit_matrix->document()->toPlainText();
    QStringList lines = text.split("\n",
                                   QString::SkipEmptyParts);
    if (lines.size() != 3) {
      emit invalidInput();
      return Eigen::Matrix3d::Zero();
    }

    QList<QStringList> stringVecs;
    Eigen::Matrix3d mat;
    for (int row = 0; row < 3; ++row) {
      stringVecs.append(lines.at(row).simplified()
                        .split(CE_PARSE_IGNORE_REGEXP,
                               QString::SkipEmptyParts));
      QStringList &stringVec = stringVecs[row];
      if (stringVec.size() != 3) {
        emit invalidInput();
        return Eigen::Matrix3d::Zero();
      }
      for (int col = 0; col < 3; ++col) {
        bool ok;
        double val = stringVec[col].toDouble(&ok);
        if (!ok) {
          emit invalidInput();
          return Eigen::Matrix3d::Zero();
        }
        mat(row,col) = val;
      }
    }

    // Transpose if needed
    if (m_ext->matrixVectorStyle() == ColumnVectors) {
      // Warning: mat = mat.transpose() will *not* work correctly with
      // Eigen matrices. Use transposeInPlace() instead.
      mat.transposeInPlace();
    }

    emit validInput();
    return mat;
  }
Example #5
0
int processInput(const char * input) {

	bool val = validInput(input);
	if(val == 1) {
		//printf("%s\n", "valid input");
	} else {
		//printf("%s\n", "invalid input");
	}

	if(val == 0) {
		return -1;
	} else {
		int elems = numOfElements(input);
		return elems;
	}
}
void plugBoard::setPlugBoard()
{
	cout << "Enter the Plugboard settings (13 Pairs):" << endl;
	char firstLetter, secondLetter;
	for (int i = 0;i < 13;i++)
	{
		cin >> firstLetter >> secondLetter;
		while (!validInput(firstLetter, secondLetter))
		{
			cout << "One of the letters is already paired, input another pair :" << endl;
			cin >> firstLetter >> secondLetter;
		}
		firstLetter = tolower(firstLetter);
		secondLetter = tolower(secondLetter);
		board.insert(make_pair(firstLetter, secondLetter));
	}
}
  CEAbstractEditor::CEAbstractEditor(CrystallographyExtension *ext)
    : CEAbstractDockWidget(ext),
      m_isLocked(false)
  {
    connect(this, SIGNAL(invalidInput()),
            this, SLOT(markAsInvalid()));
    connect(this, SIGNAL(validInput()),
            this, SLOT(markAsValid()));

    connect(m_ext, SIGNAL(cellChanged()),
            this, SLOT(refreshEditor()));

    connect(this, SIGNAL(visibilityChanged()),
            m_ext, SLOT(refreshActions()));

    connect(this, SIGNAL(editStarted()),
            m_ext, SLOT(lockEditors()));
    connect(this, SIGNAL(editAccepted()),
            m_ext, SLOT(unlockEditors()));
    connect(this, SIGNAL(editRejected()),
            m_ext, SLOT(unlockEditors()));

  }
Example #8
0
/**
* The Slot to gather input from the dialog, store it in the propertyValue
* and then emit the signal for valid input. Preparing for accept() to be run.
*/
void GetNegMuMuonicXRDDialog::parseInput() {
  // getting a list of strings of elements selected from periodicTableWidget
  QString elementsSelectedStr = m_periodicTable->getAllCheckedElementsStr();
  // if no elements are selected from the PeriodicTableWidget, a pop-up appears
  // to the user.
  if (elementsSelectedStr == "") {
    QMessageBox::information(
        this, "GetNegMuMuonicXRDDialog",
        "No elements were selected, Please select an element from the table");
  }
  // If elements have been selected and y-position text is non-empty then
  // store the inputs as the corresponding propertyValues and emit validInput
  // signal.
  if (elementsSelectedStr != "") {
    storePropertyValue("Elements", elementsSelectedStr);
    if (m_yPosition->text() != "") {
      storePropertyValue("YAxisPosition", m_yPosition->text());
    }
    if (m_groupWorkspaceNameInput->text() != "") {
      storePropertyValue("OutputWorkspace", m_groupWorkspaceNameInput->text());
    }
    emit validInput();
  }
}
  void CEParameterEditor::refreshEditor()
  {
    CEUnitCellParameters params = m_ext->currentCellParameters();

    QString lengthSuffix;
    QString angleSuffix;
    switch (m_ext->lengthUnit()) {
    case Angstrom:
      lengthSuffix = " " + CE_ANGSTROM;
      break;
    case Bohr:
      lengthSuffix = " a" + CE_SUB_ZERO;
      break;
    case Nanometer:
      lengthSuffix = " nm";
      break;
    case Picometer:
      lengthSuffix = " pm";
      break;
    default:
      lengthSuffix = "";
      break;
    }

    switch(m_ext->angleUnit()) {
    case Degree:
      angleSuffix = CE_DEGREE;
      break;
    case Radian:
      angleSuffix = " rad";
      break;
    default:
      angleSuffix = "";
      break;
    }

    ui.spin_a->blockSignals(true);
    ui.spin_b->blockSignals(true);
    ui.spin_c->blockSignals(true);
    ui.spin_alpha->blockSignals(true);
    ui.spin_beta->blockSignals(true);
    ui.spin_gamma->blockSignals(true);

    ui.spin_a->setValue(params.a);
    ui.spin_b->setValue(params.b);
    ui.spin_c->setValue(params.c);
    ui.spin_alpha->setValue(params.alpha);
    ui.spin_beta->setValue(params.beta);
    ui.spin_gamma->setValue(params.gamma);

    ui.spin_a->setSuffix(lengthSuffix);
    ui.spin_b->setSuffix(lengthSuffix);
    ui.spin_c->setSuffix(QString(lengthSuffix));
    ui.spin_alpha->setSuffix(QString(angleSuffix));
    ui.spin_beta->setSuffix(QString(angleSuffix));
    ui.spin_gamma->setSuffix(QString(angleSuffix));

    ui.spin_a->blockSignals(false);
    ui.spin_b->blockSignals(false);
    ui.spin_c->blockSignals(false);
    ui.spin_alpha->blockSignals(false);
    ui.spin_beta->blockSignals(false);
    ui.spin_gamma->blockSignals(false);

    ui.spin_a->setEnabled(true);
    ui.spin_b->setEnabled(true);
    ui.spin_c->setEnabled(true);
    ui.spin_alpha->setEnabled(true);
    ui.spin_beta->setEnabled(true);
    ui.spin_gamma->setEnabled(true);
    ui.push_params_apply->setEnabled(false);
    ui.push_params_reset->setEnabled(false);
    emit validInput();
  }
Example #10
0
void singlePlayer(void){
    // declarations
    unsigned char keepGoing, nextRow, nextCol, playerTurn;
    char nextMove[BUF_SIZE];

    unsigned char win = 0; // 1 for player got three in a row, 0 otherwise
    unsigned char turnNum = 0; // 1 to n^2 for total turns
    
    unsigned char ticTacToeTable[N][N];
    
    // intialize and display ticTacToe array
    clearTwoDimArray(ticTacToeTable);
    printTwoDimArray(ticTacToeTable);
    
    // first, randomly decide who is going first, human or player
    srand(time(NULL));
    playerTurn = rand() % 2;

    // loop until a player wins or maximum amount of turns reached
    while(!win && turnNum++ < N*N)
    {
        // alternate between players
        switch(playerTurn)
        {
            case 0: // computer turn
                printf("Computer's Turn %d\n", turnNum);

                // calculate next best position to move
                AIplace(ticTacToeTable, nextMove);
 
             // set to player's turn
                playerTurn = 1;
                break;
            case 1:
                printf("Player's Turn %d\n", turnNum);
	do{
                    // accept user input for row and column
                    printf("Enter a row and column (1 to 3) to place, in that\n");
                    printf("order, with a dash in between the two numbers.\n");

                    scanf("%s", nextMove);
                    
                    // validate input
                    keepGoing = !validInput(nextMove, ticTacToeTable, turnNum);
                } while(keepGoing);


                // set to computer's turn
                playerTurn = 0;
                break;
            default:
                break;
        }

        nextRow = nextMove[0] - '1';
        nextCol = nextMove[2] - '1';

        // update tic tac toe table
        ticTacToeTable[nextRow][nextCol] = turnNum;

        // check for win
        win = detectWin(ticTacToeTable);
        // print tic tac toe table
        printTwoDimArray(ticTacToeTable);
    }

    if(win)
        printf("WINNER WINNER CHICKEN DINNER!!\n");
    else
        printf("NO WINNER!!\n"); 
}
Example #11
0
void loop() {
    // Though counter-intuitive, game creation cannot be in setup because of varying arduino boot times and boot gibberish
    if (!gameCreated) {
        drawGUI();
        while(!waitUntil(JOYSTICK_BUTTON_PIN, false));
        if (!startNetwork()) {
            tft.fillScreen(ST7735_BLACK); // we must clear all conflicting messages from screen
            tft.setCursor(0,0);
            dualPrint("Network connection failed!");
            dualPrint("Please ensure:");
            dualPrint("1) both arduinos are connected");
            dualPrint("2) both parties pressed the joystick");
            dualPrint("If both are true, consult someone who");
            dualPrint("looks like he knows what he's talking about");
            dualPrint("Reset both Arduinos to try again");
            while(1);
        }
        /* Extensibility Goal:
         * Enable colour selection here, time permissible
         */
        gameCreated = true;
    }
    if (!gameStarted) {
        setSpawns(&player1, &player2);
        setColour(&player1, &player2);
        tft.fillScreen(ST7735_BLACK);
        startCountdown();
        gameStarted = true;
    }
    winner = gameOver(&player1.currentPosition, &player2.currentPosition);
    if (winner) {
        tft.setCursor(0, 80);
        String message;
        switch (winner) {
        case -1:
            message = "YOU SUPER TIE";
            break;
        case 1:
            message = "YOU SUPER WIN";
            player1.score++;
            break;
        case 2:
            message = "YOU SUPER LOSE";
            player2.score++;
            break;
        }
        tft.println(message);
        tft.println("SCORES:");
        tft.print("You: ");
        tft.print(player1.score);
        tft.print(" | Him: ");
        tft.println(player2.score);
        tft.println("Again? <Press Joystick>");
        waitUntil(JOYSTICK_BUTTON_PIN, LOW);
        memset(&wallPositions, 0, 2560); // 2560 is a magic number because size_ts were acting unexpectedly
        gameStarted = false;
        tft.fillScreen(ST7735_BLACK);
    } else {
        // add a wall ad draw car at current position
        addWallPosition(player1.currentPosition);
        addWallPosition(player2.currentPosition);
        tft.fillRect(player1.currentPosition.x, player1.currentPosition.y, 2, 2, player1.colour);
        tft.fillRect(player2.currentPosition.x, player2.currentPosition.y, 2, 2, player2.colour);
        movement_t newDirection = getJoystickInput();
        if (validInput(newDirection, player1.direction)) player1.direction = newDirection;
        sendDeltas(&player1.direction);
        receiveDeltas(&player2.direction);
        player1.currentPosition.x += player1.direction.x;
        player1.currentPosition.y += player1.direction.y;
        player2.currentPosition.x += player2.direction.x;
        player2.currentPosition.y += player2.direction.y;
        delay(75); // this is how we control the game speed
        /* Extensibility Goal:
         * Find a more efficient and reliable way of controlling game speed.
         * Implement it, and allow it to be customized
         */
    }
}
Example #12
0
  void CEMatrixEditor::refreshEditor()
  {
    Eigen::Matrix3d mat;

    QString vectorStyleStr = "";

    switch (m_ext->matrixCartFrac()) {
    case Cartesian:
      switch (m_ext->matrixVectorStyle()) {
      case RowVectors:
        vectorStyleStr = "Row Vectors";
        mat = m_ext->currentCellMatrix();
        break;
      case ColumnVectors:
        vectorStyleStr = "Column Vectors";
        mat = m_ext->currentCellMatrix().transpose();
        break;
      }
      this->setWindowTitle(tr("Cell &Matrix (%1)",
                              "Unit cell matrix, arg is vector style "
                              "(row or column")
                           .arg(vectorStyleStr));
      break;
    case Fractional:
      switch (m_ext->matrixVectorStyle()) {
      case RowVectors:
        vectorStyleStr = "Row Vectors";
        mat = m_ext->currentFractionalMatrix();
        break;
      case ColumnVectors:
        vectorStyleStr = "Column Vectors";
        // TODO check math here:
        mat = m_ext->currentFractionalMatrix().transpose();
        break;
      }
      this->setWindowTitle(tr("Fractional Cell &Matrix (%1)",
                              "Unit cell matrix, arg is vector style "
                              "(row or column")
                           .arg(vectorStyleStr));
      break;
    }

    // Clean up matrix
    for (unsigned short row = 0; row < 3; ++row) {
      for (unsigned short col = 0; col < 3; ++col) {
        double &current = mat(row,col);
        // Remove negative zeros:
        if (fabs(current) < 1e-10) {
          current = 0.0;
        }
      }
    }

    char text[256];
    snprintf(text, 256,
             "%9.5f %9.5f %9.5f\n"
             "%9.5f %9.5f %9.5f\n"
             "%9.5f %9.5f %9.5f\n",
             mat(0, 0), mat(0, 1), mat(0, 2),
             mat(1, 0), mat(1, 1), mat(1, 2),
             mat(2, 0), mat(2, 1), mat(2, 2));

    ui.edit_matrix->blockSignals(true);
    ui.edit_matrix->setText(text);
    ui.edit_matrix->blockSignals(false);

    ui.edit_matrix->setCurrentFont(QFont("Monospace",
                                         CE_FONTSIZE));

    ui.edit_matrix->setEnabled(true);

    if (m_ext->matrixCartFrac() == Fractional) {
      ui.edit_matrix->setReadOnly(true);
    }
    else {
      ui.edit_matrix->setReadOnly(false);
    }
    ui.push_matrix_apply->setEnabled(false);
    ui.push_matrix_reset->setEnabled(false);
    emit validInput();
  }