Example #1
0
// Pre-condition: board is in a valid configuration for a tic-tac-toe game,
//                right after piece just moved.
// Post-condition: A score will be returned corresponding to the score of 
//                 piece's position in the current board.
int score(char board[][3], char piece) {
 
    // Figure out the status now.
    int curstatus = status(board);
    int nextmove, answer;
    
    // If the game is over, we can easily return our answer.
    if (curstatus == WINX && piece == 'X')
        return WIN;
    if (curstatus == WINO && piece == 'O')
        return WIN;
    if (curstatus == WINX && piece == 'O')
        return LOSS;
    if (curstatus == WINO && piece == 'X')
        return LOSS;
    if (curstatus == CATS)
        return TIE;
        
    // Otherwise, assume the opponents move will be the one the computer
    // selects.
    nextmove = compmove(board, other(piece));
    
    // Place this new move on the board.
    board[nextmove/3][nextmove%3] = other(piece);
    
    // Return the OPPOSITE score, since this time, the score function will be
    // scoring our opponent!
    answer = -score(board, other(piece));
    
    // Restore the board to what it looked like before.
    board[nextmove/3][nextmove%3] = '_';
    
    return answer;          
}
Example #2
0
static unsigned makemove (enum EPlayer player)
{
    if (player == USER)
	return usermove();
    else {
	unsigned m = compmove();
	_asked[COMPUTER][m] = true;
	wattron (_wmsg, A_BOLD);
	wprintw (_wmsg, "I ask you for %s. ", c_CardNames[m]);
	wattroff (_wmsg, A_BOLD);
	return m;
    }
}
Example #3
0
int
main(int argc, char *argv[])
{
	int ch, move;

	while ((ch = getopt(argc, argv, "ph")) != -1)
		switch(ch) {
		case 'p':
			promode = 1;
			break;
		case '?':
		case 'h':
		default:
			usage();
		}

	srandomdev();
	instructions();
	init();

	if (nrandom(2) == 1) {
		printplayer(COMPUTER);
		(void)printf("get to start.\n");
		goto istart;
	}
	printplayer(USER);
	(void)printf("get to start.\n");
	
	for (;;) {
		move = usermove();
		if (!comphand[move]) {
			if (gofish(move, USER, userhand))
				continue;
		} else {
			goodmove(USER, move, userhand, comphand);
			continue;
		}

istart:		for (;;) {
			move = compmove();
			if (!userhand[move]) {
				if (!gofish(move, COMPUTER, comphand))
					break;
			} else
				goodmove(COMPUTER, move, comphand, userhand);
		}
	}
	/* NOTREACHED */
}
Example #4
0
// Executes a move for the player specified by the second parameter and
// reflects those changes on the board.
void domove(char board[][3], char player) {

    int done = 0;
    int r, c, temp;

    // Continue until a move has been executed.
    while (!done) {

        // Human player is X.
        if (player == 'X') {
                   
            // Prompt for and read in the player's move.
            printf("\nHere is the current board:\n");
            printboard(board);   
            printf("\nEnter your move, player %c.\n", player);
            printf("Enter the row(0-2) followed by a space and the col(0-2).\n");
            scanf("%d%d", &r, &c);
        }
    
        // Computer Player is O
        else {
        
            // Get the computer's move, split into row&col and print it.     
            temp = compmove(board, player);
            r = temp/3;
            c = temp%3;   
            printf("\nThe computer has chosen to take square %d, %d.\n", r, c);
        }
    
        // Check for invalid indexes.
        if (r<0 || r>2 || c<0 || c>2)
            printf("Sorry, that is not a valid square.\n");

        // Check for a taken square.
        else if (board[r][c] != '_')
            printf("Sorry, that square has been taken already.\n");

        // Execute the move.
        else {
      
            board[r][c] = player;
            done=1;
        }
        
    } // end while loop
}
Example #5
0
int main(int argc, char *argv[]) {
	/* Seed randomness. */
	srand(time(NULL) ^ getpid());

	bake_args(argc, argv);
	initgame();

	do {
		if(thegame.players == 1)
			whatside();
		else
			player = X;

		thegame.restart = false;
		cleargame();

		while(thegame.running) {
			if(thegame.players == 0) {
				printboard(false);
				compmove(player);

				if(checkwinner())
					break;

				compmove(-player);
			}

			else if(thegame.players == 1) {
				if(player == X){
					printboard(true);
					makemove(player);

					if(checkwinner())
						break;

					compmove(-player);
				}

				else {
					compmove(-player);

					if(checkwinner())
						break;

					printboard(true);
					makemove(player);
				}
			}

			else {
				printboard(true);

				makemove(player);
				player = -player;

			}

			checkwinner();
		}
		printwinner();
		restart();
	} while(thegame.restart);

	/* we so funny */
	if(thegame.players == 0)
		wargames();

	return 0;
}