Exemplo n.º 1
0
/*This function alternates between user and the computer and handles
moves for both.*/
void playGame(int n, char board[21][21], bool humanPlayer) {
    int x = 0, y = 0;
    bool turn = !humanPlayer; //false is computer
    while (true) {
        if (turn) {
            humanTurn(n, board, humanPlayer);
            turn = !turn;
            if (checkWinner(n, board) != 'N')
                break;
            if (isBoardFull(n, board))
                break;
        }
        if (!turn) {
            computerTurn(n, board, humanPlayer);
            turn = !turn;
            if (checkWinner(n, board) != 'N')
                break;
            if (isBoardFull(n, board))

                break;
        }

    }
    return;
}
Exemplo n.º 2
0
    void Round::update(Float32 elapsedTime) {
        // Check if there's a winner
        if (!hasWinner()) {
            checkWinner();
        } else {
            gameOverWait = std::max(gameOverWait - elapsedTime, 0.0f);
            if (gameOverWait < (D6_GAME_OVER_WAIT - D6_ROUND_OVER_WAIT)) {
                return;
            }
        }

        for (Player &player : world.getPlayers()) {
            player.update(world, game.getSettings().getScreenMode(), elapsedTime);
            if (game.getSettings().isGhostEnabled() && !player.isInGame() && !player.isGhost()) {
                player.makeGhost();
            }
        }

        world.update(elapsedTime);

        if (suddenDeathMode) {
            waterFillWait += elapsedTime;
            if (waterFillWait > D6_RAISE_WATER_WAIT) {
                waterFillWait = 0;
                world.raiseWater();
            }
        }

        showYouAreHere = std::max(showYouAreHere - 3 * elapsedTime, 0.0f);
    }
Exemplo n.º 3
0
int Game::minMove(int depth, int alpha, int beta) {
	
	std::vector<int> allMoves = getAvailableMoves();
	int winner = checkWinner();

	if ((allMoves.size() <= 0 || winner > 0) || depth <= 0) {
		return evaluateState(2, winner);
	}
	
	int* bestMove = NULL;
	int bestScore = INT_MAX;
	
	for (std::vector<int>::const_iterator it = allMoves.begin(); it != allMoves.end(); it++) {
		setCell(*it, 1);
		int score = maxMove(depth - 1, alpha, beta);
		undoMove(*it);
		if (score < bestScore) {
			bestScore = score;
			beta = score;
		}
		if (beta < alpha) {
			return bestScore;
		}
	}
	return bestScore;
}
Exemplo n.º 4
0
void main() {
    while (checkWinner() == 3 && i < 8) {
        jugar(i);
        i++;
    }

    system("pause");
}
Exemplo n.º 5
0
int main() {

    bool sair;
    sair = false;
    while (!sair) {

        /* Variaveis! */

        int optEsc, numJog, tabP[9], player, aux;
        numJog = 0;
        tabP[1] = 0;
        tabP[2] = 0;
        tabP[3] = 0;
        tabP[4] = 0;
        tabP[5] = 0;
        tabP[6] = 0;
        tabP[7] = 0;
        tabP[8] = 0;
        tabP[9] = 0;

        /* Sessao de boas vindas! Eeeee! */

        cout << endl << endl << endl << endl;
        cout << "      Bem vindo ao TicTacToe!";
        cout << endl << endl;
        cout << "  1 - Jogar" << endl;
        cout << "  0 - Sair" << endl << endl;
        cout << "Sua escolha: ";
        cin >> optEsc;

        /* Lugar da jogatina */

        switch (optEsc) {
            case 1: //Caso o usuario escolha jogar
                while (checkWinner(tabP[1], tabP[2], tabP[3], tabP[4], tabP[5], tabP[6], tabP[7], tabP[8], tabP[9])) {
                    numJog++;
                    aux = numJog % 2;
                    if (aux = 1)
                        player = 1;
                    else if (aux = 0)
                        player = 2;
                    cout << endl << endl;
                    showTabIns();
                    cout << endl;
                    showTab(tabP[1], tabP[2], tabP[3], tabP[4], tabP[5], tabP[6], tabP[7], tabP[8], tabP[9]);
                    cout << endl << "Player " << player << ": ";
                    cin >> optEsc;
                }
                break;

            case 2: //Caso ele queira virar presunto
                sair = true;
                break;
        }
    }
    cout << endl << "Bye";
    return EXIT_SUCCESS;
}
int *
checkwinner_1_svc(void *argp, struct svc_req *rqstp)
{
  static int  result;

  result = checkWinner();

  return &result;
}
Exemplo n.º 7
0
void Game::onGameOver() {
	//Check if anyone wins
	int winner = checkWinner();

	if (winner == 0) {
		winnerText = "Draw, press s to start a new game";
	}
	roundOver = true;
	message = TTF_RenderText_Solid(font, winnerText.c_str(), textColor);
}
Exemplo n.º 8
0
bool Arbitre::checkMove(Vector<int> const &pos, char * const *map, int color)
{
	if (pos.x == -1 || pos.y == -1)
		return false;
	if (map[pos.y][pos.x])
		return false;
	map[pos.y][pos.x] = color;
	//checkWinner(pos, map, color);
	if (_ruleDoublethree == true /*&& !_isWinner*/ && checkDoubleThree(pos, map, color) > 1)
	{
		map[pos.y][pos.x] = 0;
		return (false);
	}
	std::deque<Vector<Vector<int>>> coord;
	std::deque<Vector<int>> toCheck;
	checkEat(pos, map, color, coord);
	if (coord.empty() == false)
	{
		while (coord.empty() == false)
		{
			color == BLACK ? _prisoner[0] += 2 : _prisoner[1] += 2;
			map[coord.front().x.y][coord.front().x.x] = 0;
			map[coord.front().y.y][coord.front().y.x] = 0;
			coord.pop_front();
		}
		toCheck.push_back(pos + Vector<int>(0,	3));
		toCheck.push_back(pos + Vector<int>(0,	-3));
		toCheck.push_back(pos + Vector<int>(3,	0));
		toCheck.push_back(pos + Vector<int>(-3,	0));
		toCheck.push_back(pos + Vector<int>(3, 3));
		toCheck.push_back(pos + Vector<int>(3, -3));
		toCheck.push_back(pos + Vector<int>(-3, 3));
		toCheck.push_back(pos + Vector<int>(-3, -3));
	}
	auto it = toCheck.begin();
	while (it != toCheck.end())
		if (it->x < 0 || it->y < 0 || it->x >= 19 || it->y >= 19)
			it = toCheck.erase(it);
		else
			++it;
	toCheck.push_back(pos);
	while (toCheck.size())
	{
		if (checkWinner(toCheck.front(), map, color) == true)
		{
			map[pos.y][pos.x] = 0;
			if (_winner == 0)
				_winner = color;
			return true;
		}
		toCheck.pop_front();
	}
	map[pos.y][pos.x] = 0;
	return (true);
}
Exemplo n.º 9
0
//Checks if someone has won and sets the winning message
void Game::checkForWin() {
	int winner = checkWinner();

	if (winner > 0) {
		if (gameType == 1) {
			if (winner == 1) {
				winnerText = "Computer wins, press s to start a new game";
			} else if (winner == 2) {
				winnerText = "You win, press s to start a new game";
			}
		} else if (gameType == 2) {
			if (winner == 1) {
				winnerText = "Player 1 wins, press s to start a new game";
			} else if (winner == 2) {
				winnerText = "Player 2 wins, press s to start a new game";
			}
		}
		roundOver = true;
		message = TTF_RenderText_Solid(font, winnerText.c_str(), textColor);
	}
}
Exemplo n.º 10
0
void Game::play(Position &_pos)
{
    if(_pos.x < m_gameTable.m_size && _pos.y < m_gameTable.m_size)
        m_gameTable.marcar(_pos);
    else
        return; //RETORNAR UNA EXCEPCIÓN PODRÍA SER MEJOR OPCIÓN

//****FUNCIONES ALTERNATIVAS, BORRARLAS CUANDO SE PUEDA****//
    draw();
    status winner = checkWinner();
    switch (winner) {
    case played_me:
        cout << "I won!" << endl;
        break;
    case played_op:
        cout << "You won!" << endl;
        break;
    default:
        break;
    }
    cout << endl;
//*********************************************************//
}
Exemplo n.º 11
0
/*
*	Where child processes will communicate with clients and run the game.
*/
void subserver(int client1_sock, int client2_sock) {

	//these will be used to control whose turn it is
	int current_sock;
	int waiting_sock;

	//game data
	char board[3][3];
	char turn = 1; //tracks whose turn it is
	char turn_count = 0;
	char winner = 0;
	
	//networking data
	int read_count = -1;
	int BUFFERSIZE = 256;
	char buffer[BUFFERSIZE+1];

	char x, y;
	
	//initialize the board array to all 0's
	dprintf("Preparing game board...\n");
	for (x = 0; x < 3; x = x + 1) {
		for(y = 0; y < 3; y = y + 1) {
			board[x][y] = 0;
		}
	}

	//let the game begin!
	while(turn_count < 9) {
		
		//set up "reply" sockets based on whose turn it is
		if(turn == 1) {
			current_sock = client1_sock;
			waiting_sock = client2_sock;
		} else {
			current_sock = client2_sock;
			waiting_sock = client1_sock;
		}

		//tell idle player to wait
		send_wait_msg(waiting_sock);
		//alert current player it's her turn
		send_turn_msg(current_sock, board);

		//get user input from client
		read_count = recv(current_sock, buffer, BUFFERSIZE, 0);
		buffer[read_count] = '\0';

		if(buffer[0] == P_MOVE) {
			x = buffer[1];
			y = buffer[2];
			
			dprintf("Player input: %d %d\n", x, y);
			
			//first, make sure the input is valid
			if(x < 0 || x > 2 || y < 0 || y > 2) {
				dprintf("Input error: out of range\n");
				send_inv_msg(current_sock, Q_OUT_OF_RANGE);
				continue;
			} else if(board[x][y] > 0) {
				dprintf("Input error: location taken\n");
				send_inv_msg(current_sock, Q_LOC_TAKEN);
				continue;
			}
			
			//update the board
			board[x][y] = get_player_symbol(turn);
			
			if(debug > 0) {
				print_board(board);
			}
			
			turn_count = turn_count + 1;
			
			winner = checkWinner(board);
			if(winner != 0) {
				if(winner == 'X') {
					dprintf("Game over. Player 1 wins!");
					send_game_over(client1_sock, Q_YOU_WON, board);
					send_game_over(client2_sock, Q_YOU_LOST, board);
					close(client1_sock);
					close(client2_sock);
					exit(0); 
				} else {
					dprintf("Game over. Player 2 wins!");
					send_game_over(client2_sock, Q_YOU_WON, board);
					send_game_over(client1_sock, Q_YOU_LOST, board);
					close(client1_sock);
					close(client2_sock);
					exit(0);
				}
			}
		}

		//prepare for next turn
		if(turn == 1) {
			turn = 2;
		} else {
			turn = 1;
		}
	}
	
	//if we make it this far, the game was a draw
	send_game_over(client1_sock, Q_GAME_DRAW, board);
	send_game_over(client2_sock, Q_GAME_DRAW, board);

	//goodbye
	close(client1_sock);
	close(client2_sock);

	exit(0);
}
Exemplo n.º 12
0
int main ()
{
    // Choose a player
    srand(time(0));
    int player = myRand(1,2);

    // Create and Initialize the grid
    gridInitializer();
    system("clear");

    printf("\n<---------START--------->\n");
    printf(" %sFirst player is : %d%s",ANSI_COLOR_GREEN , player, ANSI_COLOR_RESET);
    printf("\n<----------------------->\n\n");

    // Play game and find the winner
    int winner = 0;
    while(1) {
        if (player == 1) 
        {
            // Player one start            
            play(1);

            // Check the winner
            if (checkWinner() == 1)
            {
                winner  = checkWinner();
                break;                
            }

            // Change the player
            player = 2;
        }
        else
        {
            // Player two start
            play(2);

            // Check the winner
            if (checkWinner() == 2)
            {
                winner  = checkWinner();
                break;                
            }

            // Change the player
            player = 1;
        }
    }
    
    // Print the grid
    gridShow();

    // Print the winner
    printf("\n<---------WINNER-------->\n");
    if (winner == 1)
    {
        printf(" %sPLAYER %d%s",ANSI_COLOR_YELLOW , winner, ANSI_COLOR_RESET);
    }
    else if (winner == 2)
    {
        printf(" %sPLAYER %d%s",ANSI_COLOR_RED , winner, ANSI_COLOR_RESET);
    }
    printf("\n<----------------------->\n\n");
    
    return 0;
}
Exemplo n.º 13
0
int main()
{
	//prototypes for main
    void printTitle(), setGridSize(), clearGrid(int grid[column_size][row_size]), playerTurn(int player, int grid[column_size][row_size]);
    int checkWinner(int grid[column_size][row_size]), continueGame();
	
	//variable initilisation.
	int player, winner;

    printTitle();
		
		do
    	{
			//construction of new game
            player = 1; //resets player
			winner = 0; // resets winner
    		setGridSize();
    		int grid[column_size][row_size]; // constructs grid
            clearGrid(grid); // sets all grid values to 0
    		clearScreen();

			//game plays out inside until winner or draw.
    		while(winner == 0)
    		{

    			playerTurn(player, grid);
    			winner = checkWinner(grid);
    			if(winner == 0) 
				{
					//switches player
    				if(player == 1)
					{
    					player = 2;
					}
    				else
					{
    					player = 1;
					}
				}
    			else
    			{
						//clears screen and prints grid between turns.
    		        	clearScreen();
                        printGrid(grid);
                        spacer(); //spacer called repeatedly to move print to center of console
    					printf("\n\n");
    					spacer();
						
    				if(winner != 3) //checks if winner was a player or a draw. A winner of '3' is a draw
					{
                        printf("Winner is Player %d!!!", winner);
					}
                    else
					{
                        printf("Draw!!!");
					}
                }
    		}
		//starts a new game if true
    	}while(continueGame());

		return(0);
}
Exemplo n.º 14
0
bool Arbitre::checkWinner(Vector<int> const &pos, char const * const *map, int color, bool recusive)
{
	int	nb;
	Vector<int> next = pos;
	if (((color == BLACK ? _prisoner[0] : _prisoner[1])) >= 10)
	{
		_isWinner = true;
		_winner = color;
		return true;
	}
	nb = 1;
	++next.x;
	while (next.x < 19 && map[next.y][next.x] == color)
	{
		if (recusive) checkWinner(next, map, color, false);
		++next.x;
		++nb;
	}
	next.x = pos.x - 1;
	while (next.x >= 0 && map[next.y][next.x] == color)
	{
		if (recusive) checkWinner(next, map, color, false);
		--next.x;
		++nb;
	}
	if (nb >= 5)
	{
		++next.x;
		if (_ruleOptionalEnd == false)
		{
			_isWinner = true;
			return true;
		}
		else if (_ruleOptionalEnd == true && isBreakable(next, map, color, nb, Vector<int>(1, 0)) == false)
		{
			_isWinner = true;
			return true;
		}
	}
	nb = 1;
	next.x = pos.x;
	next.y = pos.y + 1;
	while (next.y < 19 && map[next.y][next.x] == color)
	{
		if (recusive) checkWinner(next, map, color, false);
		++next.y;
		++nb;
	}
	next.y = pos.y - 1;
	while (next.y >= 0 && map[next.y][next.x] == color)
	{
		if (recusive) checkWinner(next, map, color, false);
		--next.y;
		++nb;
	}
	if (nb >= 5)
	{
		++next.y;
		if (_ruleOptionalEnd == false)
		{
			_isWinner = true;
			return true;
		}
		else if (_ruleOptionalEnd == true && isBreakable(next, map, color, nb, Vector<int>(0, 1)) == false)
		{
			_isWinner = true;
			return true;
		}
	}
	nb = 1;
	next.x = pos.x + 1;
	next.y = pos.y + 1;
	while (next.x < 19 && next.y < 19 && map[next.y][next.x] == color)
	{
		if (recusive) checkWinner(next, map, color, false);
		++next.x;
		++next.y;
		++nb;
	}
	next.x = pos.x - 1;
	next.y = pos.y - 1;
	while (next.x >= 0 && next.y >= 0 && map[next.y][next.x] == color)
	{
		if (recusive) checkWinner(next, map, color, false);
		--next.x;
		--next.y;
		++nb;
	}
	if (nb >= 5)
	{
		++next.x;
		++next.y;
		if (_ruleOptionalEnd == false)
		{
			_isWinner = true;
			return true;
		}
		else if (_ruleOptionalEnd == true && isBreakable(next, map, color, nb, Vector<int>(1, 1)) == false)
		{
			_isWinner = true;
			return true;
		}
	}
	nb = 1;
	next.x = pos.x - 1;
	next.y = pos.y + 1;
	while (next.x >= 0 && next.y < 19 && map[next.y][next.x] == color)
	{
		if (recusive) checkWinner(next, map, color, false);
		--next.x;
		++next.y;
		++nb;
	}
	next.x = pos.x + 1;
	next.y = pos.y - 1;
	while (next.x < 19 && next.y >= 0 && map[next.y][next.x] == color)
	{
		if (recusive) checkWinner(next, map, color, false);
		++next.x;
		--next.y;
		++nb;
	}
	if (nb >= 5)
	{
		--next.x;
		++next.y;
		if (_ruleOptionalEnd == false)
		{
			_isWinner = true;
			return true;
		}
		else if (_ruleOptionalEnd == true && isBreakable(next, map, color, nb, Vector<int>(-1, 1)) == false)
		{
			_isWinner = true;
			return true;
		}
	}
	return (false);
}