コード例 #1
0
ファイル: test_main.c プロジェクト: austinglaser/sudoku
/* Input:		An empty queue, and an initialized sudoku s, an empty queue
 *					for the solutions, and a pointer to a variable to store the
 *					number of guesses
 * Returns: NULL if no possible solutions exist or the queue is not
 *					empty, and otherwise the solution to the input puzzle.
 *		-----------------------------------------------------------
 * Solves the puzzle using backtracking. The solver is initialized by
 * putting the puzzle into the empty queue. Then, each iteration will
 * pull a board out of the queue, perform a simple reduction on that
 * board, and then make a guess on the cell which has the least number
 * of possibilities. If verbose is set, it prints each board before
 * making a guess, giving a sense of the whole solution process.
 * 
 * If there is nothing to pull out of the queue, there are no possible
 * solutions to the puzzle, and the function returns an error value of
 * NULL. If the queue is not empty at initialization, the function
 * prints an error message regardless of the state of the flags, and
 * returns an error.
 */
sudoku solve(queue q, sudoku s, int * guesses)
{
	if (!isEmptyQueue(q)) {
		printf("Error: call to solve with a non-empty queue");
		return NULL;
	}
	
  putQueue(q, (void *) s);
	
	*guesses = 0;
	
  int slvd = 0;
  while (1)
	{
		if (getQueue(q, (void **) &s)){
			return NULL;
		}
		
    reduce(s);
		
    if(verbose) {
      system("clear");
			printSudoku(s, pretty);
			printf("\n");
		}
		
    slvd = checkSudoku(s);
    if (slvd == -1) deleteSudoku(s);
		else if (slvd == 1) return s;
    else
    {
      if(guess(q, s)) {
				printf("Error: Full queue");
				return NULL;
			}
      deleteSudoku(s);
      (*guesses)++;
    }
	}
}
コード例 #2
0
ファイル: my_uart.c プロジェクト: gcs278/embedded_PIC
void uart_recv_state(unsigned char byte) {
    
    switch (uc_ptr->state) {

        case UART_STATE_HEADER1:
            if ( byte == UART_HEADER1) {
//                WriteUSART(0x01);
                uc_ptr->state = UART_STATE_HEADER2;
            }
            break;

        case UART_STATE_HEADER2:
            if ( byte == UART_HEADER2 ) {
                //WriteUSART(0x02);
#if defined (ARM_PIC)
                uc_ptr->state = UART_STATE_COUNT;
#elif defined (MAIN_PIC)
                uc_ptr->state = UART_STATE_MSGTYPE;
#endif
            }
            else {
                uc_ptr->state = UART_STATE_HEADER1;
            }
            break;

        case UART_STATE_MSGTYPE:
            uc_ptr->buflen = 0;
            uc_ptr->data_read = 0;
//            WriteUSART(0x03);
            uc_ptr->msgtype = byte;
            uc_ptr->buffer[0] = byte;
            uc_ptr->state = UART_STATE_COUNT;
            break;

        case UART_STATE_COUNT:
//            WriteUSART(0x04);
            uc_ptr->count = byte;
#if defined (ARM_PIC)
                uc_ptr->state = UART_STATE_LENGTH;
                uc_ptr->buffer[0] = byte;
#elif defined (MAIN_PIC)
                uc_ptr->buffer[1] = byte;
                uc_ptr->state = UART_STATE_FOOTER;
#endif    
             break;

        case UART_STATE_LENGTH:
//            WriteUSART(0x05);
            uc_ptr->buffer[1] = byte;
            uc_ptr->data_length = byte;
            uc_ptr->buflen = 0;
            uc_ptr->data_read = 0;
            uc_ptr->state = UART_STATE_DATA;
            break;
            
        case UART_STATE_DATA:
//            WriteUSART(0x06);
            
            // Store the byte into buffer
            if ( uc_ptr->data_read + 2 < MAXUARTBUF ) {             
                uc_ptr->buffer[uc_ptr->data_read+2] = byte;
            }
            
            // Increment because we read 1 more byte
            uc_ptr->data_read += 1;
            
            // Keep reading data until, 9 BYTES for now
            if(UARTDATALEN == uc_ptr->data_read) {
                uc_ptr->state = UART_STATE_FOOTER;
            }
            
            break;
        case UART_STATE_FOOTER:
            // WriteUSART(0x07);
            if ( byte == UART_FOOTER ) {
            // WriteUSART(0x08);
                uc_ptr->state = UART_STATE_HEADER1;
                //LATBbits.LATB7 = !LATBbits.LATB7;

#if defined (ARM_PIC)
                // Copy over into a buffer before passing
                unsigned char temp[I2CMSGLEN];
                int i;
                for (i = 0; i < I2CMSGLEN; i++) {
                    temp[i] = uc_ptr->buffer[i];
                }
                LATB = 7; // Sequence 7
                LATAbits.LA0 = 0;
                // Put it in the roverDataBuf
                ToMainLow_sendmsg(I2CMSGLEN, MSGT_BUF_PUT_DATA, (void *) temp);
#elif defined (MAIN_PIC)
                // Wait for first to finish
                
                LATBbits.LATB0 = 1;
                LATBbits.LATB1 = 0;
                LATBbits.LATB2 = 0; // Sequence 1
                LATAbits.LA0 = 0;

                i2c_master_cmd message;

                message.msgType = uc_ptr->buffer[0];
                message.msgCount = uc_ptr->buffer[1];


                //if ( i2c_master_busy() == 0 )
                    // Put the message in the queue
                while(i2c_master_busy());
                putQueue(&i2c_q,message);
                //ToMainLow_sendmsg(2, MSGT_BUF_PUT_DATA, (void *) uc_ptr->buffer);
#endif
            }
            else {
                uc_ptr->state = UART_STATE_HEADER1;
            }
            uc_ptr->buflen = 0;
            uc_ptr->data_read = 0;
            break;
        default:
            break;
    }
    
}