示例#1
0
文件: mancala.c 项目: wfei/hw
int main(int argc, char *argv[]){
    lHead = NULL;
    printFlag = 1;
    printf("Author: Muxuan Wang\n");
    printf("Program 5: Mancala\n");
    printf("TA: Sean Deitz, W 9:00 AM\n");
    printf("Nov.28,2012\n");
    printf("\n");
    
    printf("Welcome to the game of Mancala, where you are playing against\n");
    printf("a computer opponent. You may enter 'u' to undo a move or \n");
    printf("'x' to exit. Your holes are on the bottom row and you get\n");
    printf("to go first.\n");
    printf("\n");
    printf("\n");
    

    Hole board[14];
    initBoard(board);
    if (argc > 1) {
	commandLineArg(board, argv);
    }
    
    addToTail(&lHead, board);

    int player = 0;
    char userInput;
    int notDone = 1;
    int gameNum = 1;

    //each loop is a turn, and involves user and computer to play.
    while(notDone){
	printf("----------------------------------------------------------\n");
	playerToMove(player,board, &gameNum);
	notDone = checkEndGame(board);
	if (notDone == 0) {
	    break;
	}
	player = (++player) % 2;
	playerToMove(player,board, &gameNum);	    
	notDone = checkEndGame(board);
	gameNum++;
	player = (++player) % 2;
	addToTail(&lHead, board);
    }

    endGameScore(board);
    printf("The final board is: \n");
    displayBoard(board);
    if(board[13].capacity > board[6].capacity){
	printf("Computer Won!!\n");
    }else if(board[13].capacity < board[6].capacity){
	printf("You Won!!\n");
    }else{
	printf("Play Even!!");
    }

} // end of main
示例#2
0
int runTestCase(CASE* c, linked_list* list) {
    int retval = 0;
    switch(c->op) {
        case ADD_FRONT:
            retval = addToHead(list, &(c->value));
            break;
        case ADD_BACK:
            retval = addToTail(list, &(c->value));
            break;
        case REMOVE_FRONT:
            retval = *((int*) removeFromHead(list));
            break;
        case REMOVE_BACK:
            retval = *((int*) removeFromTail(list));
            break;
        case SIZE:
            retval = list -> size;
            break;
    }
    if (retval != c->expected) {
        printf("failure, want %d, got %d\n", c->expected, retval);
        return FAILURE;
    }
    return SUCCESS;
}
示例#3
0
int main()
{
	FILE *fd;
	char *line = NULL;
	size_t len = 0;
	ssize_t read;
	ListNode *fileContent = NULL;
	ListNode *pTemp;

	if((fd = fopen("test.txt", "r")) == NULL)
	{
		return -1;
	}

	while((read = getline(&line, &len, fd)) != -1)
	{
		addToTail(&fileContent, line);
	}

	if(line)
		free(line);

	pTemp = fileContent;
	while(pTemp != NULL)
	{
		printf("%s", pTemp->content);
		pTemp = pTemp->pNext;
	}

	return 0;
}
// Recursive solution to add to the tail of list l.  Note: This code is slightly
// messy!
Node *addToTail(Node *l, int v){
    if(!l)  // List is null, make a new one and return
        return makeList(v);
    if(!l->next){ // Next is null, i.e. at the tail, add node and return
        l->next = makeList(v);
        return l; // This return does nothing, simply eliminates need for wrapper
    }
    addToTail(l->next, v);
    return l;
}
示例#5
0
void* testProducer(void* llist) {
    linked_list* list = (linked_list*) llist;
    for (int i = 0; i < 10; i++) {
        int* num = malloc(sizeof(int));
        if (NULL == num) {
            printf("malloc failed");
            exit(FAILURE);
        }
        *num = i;
        addToTail(list, num);
        // sleep(1);
    }
}
示例#6
0
void testAddToTail(){
    int vals[] = {0, 3, -153, 33895, 3049, 101010};
    Node *list = NULL;
    for(int i = 0 ; i < (int)getArrLen(vals) ; i++){
        list = addToTail(list, vals[i]);
    }
    DArray test = arrayify(list);
    assert(test.size == (int)getArrLen(vals));
    for(int i = 0 ; i < (int)getArrLen(vals) ; i++){
        assert(test.contents[i] == vals[i]);
    }
    free(test.contents);
    delList(list);
    printf(">> Test add to tail completed! <<\n");
}
示例#7
0
void testApplyFunc() {
    int vals[] = {1,2,3,4,5};
    linked_list* list = initLinkedList();
    for (int i = 0; i < sizeof(vals) / sizeof(vals[0]); i++) {
        addToTail(list, &(vals[i]));
    }
    printf("Should see :\n");
    for (int i = 0; i < sizeof(vals) / sizeof(vals[0]); i++) {
        printf("%d, ", vals[i]);
    }
    printf("\nActual:\n");
    applyFunc(list, intPrinter);
    printf("\n");
    freeLinkedList(list);
}
示例#8
0
int main(int argc, const char *argv[]) {
    ListNode *node1 = new ListNode();
    node1->value = 1;
    
    ListNode *node2 = new ListNode();
    node2->value = 2;
    
    ListNode *node3 = new ListNode();
    node3->value = 3;
    
    node1->next = node2;
    node2->next = node3;
    
    addToTail(&node1, 4);
    printList(node1);
    
    // This why 'addToTail' function use pointer to pointer with the first parameter '**head'
    // Because the algorithm will change the pointer when add a new value to a NULL list.
    ListNode *node5 = NULL;
    addToTail(&node5, 5);
    printList(node5);
    
    return 0;
}
示例#9
0
void stressTests(){
    for(int i = 0 ; i <= 1000 ; i++){
        Node *list = makeRandListOfSizeN(i * 1000);
        delList(list);
        if(!(i%50)){
            printf("Made and deleted list of size %d...\n", i*1000);
        }
    }

    int hellListSize = 10000000;
    Node *list = makeRandListOfSizeN_bounded(hellListSize, 5000000);
    printList(list);

    for(int i = 0 ; i < 5000000 ; i++){
        Node *t = findElement(list, i);
        if(t){
            int pos = getNumElements(t->next);
            printf("Found %d in list at pos %d!\n", i, pos);
            break;
        }
    }
    for(int i = 0 ; i < 3000 ; i++){
        int op = arc4random() % 4;
        switch(op){
            case 0: list = insertAtN(list, arc4random(),
                              arc4random_uniform(hellListSize)); break;
            case 1: list = deleteAtN(list, arc4random_uniform(hellListSize)); break;
            case 2: list = changeValueAtN(list, arc4random(),
                                   arc4random_uniform(hellListSize)); break;
            case 3: list = addToTail(list, arc4random()); break;
        }
        if(!(i%50))
            printf("%d random operations completed...\n", i);
    }
    delList(list);
}