Ejemplo n.º 1
0
Archivo: tris.c Proyecto: Giamp/lezioni
int checkForWinner(int gameBoard[BOARD_SIZE][BOARD_SIZE]){
    int i, j;
    int priorityArray[BOARD_SIZE][BOARD_SIZE];

    for(i = 0; i < BOARD_SIZE; i++){
        for(j = 0; j < BOARD_SIZE; j++){
            if(checkRow(gameBoard, i, 2, 1, priorityArray) == 5 || checkColumn(gameBoard, j, 2, 1, priorityArray) == 5 || checkDiagonal(gameBoard,2,1,priorityArray) == 5){
                printf("Vittoria del giocatore 1! (O)\n\n");
                return 1;
            }
            else if(checkRow(gameBoard, i, 2, 1, priorityArray) == 6 || checkColumn(gameBoard, j, 2, 1, priorityArray) == 6 || checkDiagonal(gameBoard,2,1,priorityArray) == 6){
                printf("Vittoria del giocatore 2! (X)\n\n");
                return 2;
            }
        }
    }

    if(checkDiagonal(gameBoard, 2, 1, priorityArray) == 5){
        return 1;
    }
    else if(checkDiagonal(gameBoard, 2, 1, priorityArray) == 6){
        return 2;
    }

    return 0;
}
Ejemplo n.º 2
0
int main(){
	
	int i = 0, j = 0, q = 0;
	
	while(1){
	
		scanf("%d %d", &N, &M);
	
		if(!N && !M)break;
	
		for(i = 0; i < N; i++)
			for(j = 0; j < M; j++)
				scanf("%d", &map[i][j]);
	
		scanf("%d", &q);
	
		while(q--){
			scanf("%d %d", &L, &U);
			printf("%d\n", checkDiagonal());
		}
		
		printf("-\n");
		
	}
	
	return 0;
}
Ejemplo n.º 3
0
int checkWinner() {
    int result = 0;
    int i,j;
    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COLUMN; j++) {

            if (grid[i][j] != 0)
            {
                // Check line
                if (checkLine(i,j) != 0)
                {
                    result = checkLine(i,j);
                    break;
                // Check Diagonal
                }else if (checkDiagonal(i,j) != 0) {
                    result = checkDiagonal(i,j);
                    break;
                }
            }
        }
    }

    return result;
}
Ejemplo n.º 4
0
/*
 *	Begin game of tic-tac-toe
 */
void
playGame(int **board, int board_size)
{	
	int winner = 0;
	int current_player = PLAYER1;

	position_t input;

	// if no winner, and board is not full yet, proceed playing
	while (winner == 0 && !isBoardFull(board, board_size)) {

		int move = 0;

		// scan the player input, if it is not valid, prompt player and scan again
		while (!move) {

			printBoard(board, board_size);
			input = takePlayerInput(board, current_player, board_size);

			// input is valid, place input on the board, and escape the scan loop
			if (checkPlayerInput(board, current_player, input, board_size) == 1)
			{	
				move = 1;
				board[input.row][input.col] = current_player;
			}
		}

		// check if input is winning move, escape play loop if it is, or else switch player.
		if (checkHorizontal(board, current_player, input.row, board_size) == 1 ||
			checkVertical(board, current_player, input.col, board_size) == 1 ||
			checkDiagonal(board, current_player, input.row, input.col, board_size) == 1) {

			printf("~~~~Player %d wins!~~~~\n", current_player);
			winner = 1; 
		}
		else {
			// switch player
			if (current_player == PLAYER1)
				current_player = PLAYER2;
			else
				current_player = PLAYER1;
		}
	}

	/* print the final status of the board */
	printBoard(board, board_size);
}
void eightQueen()
     {
     for(int i=0;i<8;i++)
         for(int j=0;j<8;j++)
            chessBoard[i][j] = 0;            
     printf("Enter x,y position to place the first queen:\n");   
     int x,y,k=0;
     scanf("%d",&x);
     scanf("%d",&y);     
     chessBoard[x-1][y-1] = 1;  //Inititilize the first queen position to 1
     for(int i=x;k<8;i=(i+1)%8,k++)
         {
         for(int j=0;j<8;j++)
                 { 
                 if(checkColum(j)==0 && checkRow(i)==0 && checkDiagonal(i,j)==0)
                       {
                       //If no Conflict push the queen position to the stack               
                       push(i,j); 
                       chessBoard[i][j] = 1;                          
                       break;         
                       }
                 // When a queen cannot be placed at any position        
                 if(j==7)   
                       {
                       struct stack *temp;
                       temp = pop();
                       i = temp->x1;
                       j = temp->y1;
                       free(temp);
                       chessBoard[i][j] = 0;  
                       k--;   
                       }   
                 }       
         }  
     display();         
     }   
Ejemplo n.º 6
0
Archivo: tris.c Proyecto: Giamp/lezioni
int bestMove(int player, int gameBoard[BOARD_SIZE][BOARD_SIZE], int bestCell[2]){
    int mPriority[BOARD_SIZE][BOARD_SIZE];
    int i = 0, j = 0;
    int enemy;
    int highestPriority = 0;

    long rnd;   //Variabile per contenere il risultato della funzione rand()

    if(player == 1){
        enemy = 2;
    }
    else if(player == 2){
        enemy = 1;
    }

    emptyArray(mPriority);  //Inizializza l'array delle priorita' a 0 in ogni sua cella

    //I cicli controllano tutte le righe e colonne chiamando le funzioni checkRow e checkColumn

    for(i = 0; i < BOARD_SIZE; i++){
        ifLesserReplace(&highestPriority, checkRow(gameBoard, i, player, enemy, mPriority));

        for(j = 0; j < BOARD_SIZE; j++){
            ifLesserReplace(&highestPriority, checkColumn(gameBoard, j, player, enemy, mPriority));
        }
    }

    //Analisi delle diagonali tramite checkDiagonal

    ifLesserReplace(&highestPriority, checkDiagonal(gameBoard, player, enemy, mPriority));

    if(highestPriority >= 5){
        //Termina la partita
        return 5;
    }
    printf("Highest status: %d\n", highestPriority);

    //Inserisce in un array le mosse con priorita' massima

    int highestPriorityMoves[9];
    int highestPriorityMovesNumber = 0;


    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            if(mPriority[i][j] == highestPriority){
                /*La mossa e' memorizzata come un numero a 2 cifre
                  La prima cifra e' la x, la seconda la y           */
                highestPriorityMoves[highestPriorityMovesNumber] = (i * 10) + j;

                highestPriorityMovesNumber++;
            }/*Fine analisi mosse con priorita massima*/
        }
    }

    //Estrae un numero pseudo-randomico compreso fra 0 e il numero di mosse con priorita' piu' alta (escluso)

    rnd = rand() % highestPriorityMovesNumber;

    //Inizializza l'array contenete le coord. della mossa scelta

    //Se la mossa scelta e' un numero < 10 (ovvero la x era 0) imposta la x a 0, altrimenti la imposta come la prima cifra del numero
    (highestPriorityMoves[rnd] < 10) ? (bestCell[0] = 0) : (bestCell[0] = highestPriorityMoves[rnd] / 10);

    //Per la y invece, nel caso sia < 10 la y equivale al numero stesso, altrimenti equivale all'ultima cifra del numero
    (highestPriorityMoves[rnd] < 10) ? (bestCell[1] = highestPriorityMoves[rnd]) : (bestCell[1] = highestPriorityMoves[rnd] % 10);

    /*Fine analisi mossa migliore*/

    return 1;
}